nur-sery/tests/db/sqlite/_queryTest.php

126 lines
5.0 KiB
PHP
Raw Normal View History

2024-05-09 11:33:53 +04:00
<?php
namespace nur\sery\db\sqlite;
use PHPUnit\Framework\TestCase;
2024-05-18 13:09:01 +04:00
class _queryTest extends TestCase {
2024-05-09 11:33:53 +04:00
function testParseConds(): void {
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_conds(null, $sql, $params);
2024-05-09 11:33:53 +04:00
self::assertNull($sql);
self::assertNull($params);
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_conds([], $sql, $params);
2024-05-09 11:33:53 +04:00
self::assertNull($sql);
self::assertNull($params);
2024-05-24 17:40:31 +04:00
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_conds(["col" => null], $sql, $params);
2024-05-24 17:40:31 +04:00
self::assertSame(["col is null"], $sql);
self::assertNull($params);
2024-05-09 11:33:53 +04:00
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_conds(["col = 'value'"], $sql, $params);
2024-05-09 11:33:53 +04:00
self::assertSame(["col = 'value'"], $sql);
self::assertNull($params);
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_conds([["col = 'value'"]], $sql, $params);
2024-05-09 11:33:53 +04:00
self::assertSame(["col = 'value'"], $sql);
self::assertNull($params);
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_conds(["int" => 42, "string" => "value"], $sql, $params);
2024-05-09 11:33:53 +04:00
self::assertSame(["(int = :int and string = :string)"], $sql);
self::assertSame(["int" => 42, "string" => "value"], $params);
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_conds(["or", "int" => 42, "string" => "value"], $sql, $params);
2024-05-09 11:33:53 +04:00
self::assertSame(["(int = :int or string = :string)"], $sql);
self::assertSame(["int" => 42, "string" => "value"], $params);
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_conds([["int" => 42, "string" => "value"], ["int" => 24, "string" => "eulav"]], $sql, $params);
2024-05-28 17:56:42 +04:00
self::assertSame(["((int = :int and string = :string) and (int = :int2 and string = :string2))"], $sql);
self::assertSame(["int" => 42, "string" => "value", "int2" => 24, "string2" => "eulav"], $params);
2024-05-09 11:33:53 +04:00
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_conds(["int" => ["is null"], "string" => ["<>", "value"]], $sql, $params);
2024-05-09 11:33:53 +04:00
self::assertSame(["(int is null and string <> :string)"], $sql);
self::assertSame(["string" => "value"], $params);
2024-05-28 17:56:42 +04:00
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_conds(["col" => ["between", "lower", "upper"]], $sql, $params);
2024-05-28 17:56:42 +04:00
self::assertSame(["col between :col and :col2"], $sql);
self::assertSame(["col" => "lower", "col2" => "upper"], $params);
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_conds(["col" => ["in", "one"]], $sql, $params);
2024-05-28 17:56:42 +04:00
self::assertSame(["col in (:col)"], $sql);
self::assertSame(["col" => "one"], $params);
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_conds(["col" => ["in", ["one", "two"]]], $sql, $params);
2024-05-28 17:56:42 +04:00
self::assertSame(["col in (:col, :col2)"], $sql);
self::assertSame(["col" => "one", "col2" => "two"], $params);
2024-06-07 11:57:15 +04:00
$sql = $params = null;
_query_base::parse_conds(["col" => ["=", ["one", "two"]]], $sql, $params);
self::assertSame(["col = :col and col = :col2"], $sql);
self::assertSame(["col" => "one", "col2" => "two"], $params);
$sql = $params = null;
_query_base::parse_conds(["or", "col" => ["=", ["one", "two"]]], $sql, $params);
self::assertSame(["col = :col or col = :col2"], $sql);
self::assertSame(["col" => "one", "col2" => "two"], $params);
$sql = $params = null;
_query_base::parse_conds(["col" => ["<>", ["one", "two"]]], $sql, $params);
self::assertSame(["col <> :col and col <> :col2"], $sql);
self::assertSame(["col" => "one", "col2" => "two"], $params);
$sql = $params = null;
_query_base::parse_conds(["or", "col" => ["<>", ["one", "two"]]], $sql, $params);
self::assertSame(["col <> :col or col <> :col2"], $sql);
self::assertSame(["col" => "one", "col2" => "two"], $params);
2024-05-09 11:33:53 +04:00
}
function testParseValues(): void {
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_set_values(null, $sql, $params);
2024-05-09 11:33:53 +04:00
self::assertNull($sql);
self::assertNull($params);
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_set_values([], $sql, $params);
2024-05-09 11:33:53 +04:00
self::assertNull($sql);
self::assertNull($params);
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_set_values(["col = 'value'"], $sql, $params);
2024-05-09 11:33:53 +04:00
self::assertSame(["col = 'value'"], $sql);
self::assertNull($params);
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_set_values([["col = 'value'"]], $sql, $params);
2024-05-09 11:33:53 +04:00
self::assertSame(["col = 'value'"], $sql);
self::assertNull($params);
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_set_values(["int" => 42, "string" => "value"], $sql, $params);
2024-05-09 11:33:53 +04:00
self::assertSame(["int = :int", "string = :string"], $sql);
self::assertSame(["int" => 42, "string" => "value"], $params);
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_set_values(["int" => 42, "string" => "value"], $sql, $params);
2024-05-09 11:33:53 +04:00
self::assertSame(["int = :int", "string = :string"], $sql);
self::assertSame(["int" => 42, "string" => "value"], $params);
$sql = $params = null;
2024-05-29 18:47:44 +04:00
_query_base::parse_set_values([["int" => 42, "string" => "value"], ["int" => 24, "string" => "eulav"]], $sql, $params);
2024-06-07 11:57:15 +04:00
self::assertSame(["int = :int", "string = :string", "int = :int2", "string = :string2"], $sql);
self::assertSame(["int" => 42, "string" => "value", "int2" => 24, "string2" => "eulav"], $params);
2024-05-09 11:33:53 +04:00
}
}