<?php
namespace nur\sery\db\sqlite;

use PHPUnit\Framework\TestCase;

class _queryTest extends TestCase {
  function testParseConds(): void {
    $sql = $params = null;
    _query::parse_conds(null, $sql, $params);
    self::assertNull($sql);
    self::assertNull($params);

    $sql = $params = null;
    _query::parse_conds([], $sql, $params);
    self::assertNull($sql);
    self::assertNull($params);

    $sql = $params = null;
    _query::parse_conds(["col = 'value'"], $sql, $params);
    self::assertSame(["col = 'value'"], $sql);
    self::assertNull($params);

    $sql = $params = null;
    _query::parse_conds([["col = 'value'"]], $sql, $params);
    self::assertSame(["col = 'value'"], $sql);
    self::assertNull($params);

    $sql = $params = null;
    _query::parse_conds(["int" => 42, "string" => "value"], $sql, $params);
    self::assertSame(["(int = :int and string = :string)"], $sql);
    self::assertSame(["int" => 42, "string" => "value"], $params);

    $sql = $params = null;
    _query::parse_conds(["or", "int" => 42, "string" => "value"], $sql, $params);
    self::assertSame(["(int = :int or string = :string)"], $sql);
    self::assertSame(["int" => 42, "string" => "value"], $params);

    $sql = $params = null;
    _query::parse_conds([["int" => 42, "string" => "value"], ["int" => 24, "string" => "eulav"]], $sql, $params);
    self::assertSame(["((int = :int and string = :string) and (int = :int1 and string = :string1))"], $sql);
    self::assertSame(["int" => 42, "string" => "value", "int1" => 24, "string1" => "eulav"], $params);

    $sql = $params = null;
    _query::parse_conds(["int" => ["is null"], "string" => ["<>", "value"]], $sql, $params);
    self::assertSame(["(int is null and string <> :string)"], $sql);
    self::assertSame(["string" => "value"], $params);
  }

  function testParseValues(): void {
    $sql = $params = null;
    _query::parse_set_values(null, $sql, $params);
    self::assertNull($sql);
    self::assertNull($params);

    $sql = $params = null;
    _query::parse_set_values([], $sql, $params);
    self::assertNull($sql);
    self::assertNull($params);

    $sql = $params = null;
    _query::parse_set_values(["col = 'value'"], $sql, $params);
    self::assertSame(["col = 'value'"], $sql);
    self::assertNull($params);

    $sql = $params = null;
    _query::parse_set_values([["col = 'value'"]], $sql, $params);
    self::assertSame(["col = 'value'"], $sql);
    self::assertNull($params);

    $sql = $params = null;
    _query::parse_set_values(["int" => 42, "string" => "value"], $sql, $params);
    self::assertSame(["int = :int", "string = :string"], $sql);
    self::assertSame(["int" => 42, "string" => "value"], $params);

    $sql = $params = null;
    _query::parse_set_values(["int" => 42, "string" => "value"], $sql, $params);
    self::assertSame(["int = :int", "string = :string"], $sql);
    self::assertSame(["int" => 42, "string" => "value"], $params);

    $sql = $params = null;
    _query::parse_set_values([["int" => 42, "string" => "value"], ["int" => 24, "string" => "eulav"]], $sql, $params);
    self::assertSame(["int = :int", "string = :string", "int = :int1", "string = :string1"], $sql);
    self::assertSame(["int" => 42, "string" => "value", "int1" => 24, "string1" => "eulav"], $params);
  }

}
