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

use Exception;
use nulib\tests\TestCase;

class SqliteTest extends TestCase {
  const CREATE_PERSON = "create table person(nom varchar, prenom varchar, age integer)";
  const INSERT_JEPHTE = "insert into person(nom, prenom, age) values ('clain', 'jephte', 50)";
  const INSERT_JEAN = "insert into person(nom, prenom, age) values ('payet', 'jean', 32)";

  function testMigration() {
    $sqlite = new Sqlite(":memory:", [
      "migrate" => [
        self::CREATE_PERSON,
        self::INSERT_JEPHTE,
      ],
    ]);
    self::assertSame("clain", $sqlite->get("select nom, age from person"));
    self::assertSame([
      "nom" => "clain",
      "age" => 50,
    ], $sqlite->get("select nom, age from person", null, true));

    $sqlite->exec(self::INSERT_JEAN);
    self::assertSame("payet", $sqlite->get("select nom, age from person where nom = 'payet'"));
    self::assertSame([
      "nom" => "payet",
      "age" => 32,
    ], $sqlite->get("select nom, age from person where nom = 'payet'", null, true));

    self::assertSame([
      ["key" => "0", "value" => self::CREATE_PERSON, "done" => 1],
      ["key" => "1", "value" => self::INSERT_JEPHTE, "done" => 1],
    ], iterator_to_array($sqlite->all("select key, value, done from _migration")));
  }

  function testException() {
    $sqlite = new Sqlite(":memory:");
    self::assertException(Exception::class, [$sqlite, "exec"], "prout");
    self::assertException(SqliteException::class, [$sqlite, "exec"], ["prout"]);
  }

  protected function assertInserted(Sqlite $sqlite, array $row, array $query): void {
    $sqlite->exec($query);
    self::assertSame($row, $sqlite->one("select * from mapping where i = :i", [
      "i" => $query["values"]["i"],
    ]));
  }
  function testInsert() {
    $sqlite = new Sqlite(":memory:", [
      "migrate" => "create table mapping (i integer, s varchar)",
    ]);
    $sqlite->exec(["insert into mapping", "values" => ["i" => 1, "s" => "un"]]);
    $sqlite->exec(["insert mapping", "values" => ["i" => 2, "s" => "deux"]]);
    $sqlite->exec(["insert into", "into" => "mapping", "values" => ["i" => 3, "s" => "trois"]]);
    $sqlite->exec(["insert", "into" => "mapping", "values" => ["i" => 4, "s" => "quatre"]]);
    $sqlite->exec(["insert into mapping(i)", "values" => ["i" => 5, "s" => "cinq"]]);
    $sqlite->exec(["insert into (i)", "into" => "mapping", "values" => ["i" => 6, "s" => "six"]]);
    $sqlite->exec(["insert into mapping(i) values ()", "values" => ["i" => 7, "s" => "sept"]]);
    $sqlite->exec(["insert into mapping(i) values (8)", "values" => ["i" => 42, "s" => "whatever"]]);
    $sqlite->exec(["insert into mapping(i, s) values (9, 'neuf')", "values" => ["i" => 43, "s" => "garbage"]]);
    $sqlite->exec(["insert into mapping", "cols" => ["i"], "values" => ["i" => 10, "s" => "dix"]]);

    self::assertSame([
      ["i" => 1, "s" => "un"],
      ["i" => 2, "s" => "deux"],
      ["i" => 3, "s" => "trois"],
      ["i" => 4, "s" => "quatre"],
      ["i" => 5, "s" => null/*"cinq"*/],
      ["i" => 6, "s" => null/*"six"*/],
      ["i" => 7, "s" => null/*"sept"*/],
      ["i" => 8, "s" => null/*"huit"*/],
      ["i" => 9, "s" => "neuf"],
      ["i" => 10, "s" => null/*"dix"*/],
    ], iterator_to_array($sqlite->all("select * from mapping")));
  }

  function testSelect() {
    $sqlite = new Sqlite(":memory:", [
      "migrate" => "create table user (name varchar, amount integer)",
    ]);
    $sqlite->exec(["insert into user", "values" => ["name" => "jclain1", "amount" => 1]]);
    $sqlite->exec(["insert into user", "values" => ["name" => "jclain2", "amount" => 2]]);
    $sqlite->exec(["insert into user", "values" => ["name" => "jclain5", "amount" => 5]]);
    $sqlite->exec(["insert into user", "values" => ["name" => "fclain7", "amount" => 7]]);
    $sqlite->exec(["insert into user", "values" => ["name" => "fclain9", "amount" => 9]]);
    $sqlite->exec(["insert into user", "values" => ["name" => "fclain10", "amount" => 10]]);
    self::assertSame([
      "name" => "jclain1",
      "amount" => 1,
    ], $sqlite->one("select * from user where name = 'jclain1'"));
    self::assertSame([
      "name" => "jclain1",
      "amount" => 1,
    ], $sqlite->one(["select * from user where name = 'jclain1'"]));
    self::assertSame([
      "name" => "jclain1",
      "amount" => 1,
    ], $sqlite->one(["select from user where name = 'jclain1'"]));
    self::assertSame([
      "name" => "jclain1",
      "amount" => 1,
    ], $sqlite->one(["select from user where", "where" => ["name = 'jclain1'"]]));
    self::assertSame([
      "name" => "jclain1",
      "amount" => 1,
    ], $sqlite->one(["select from user", "where" => ["name = 'jclain1'"]]));
    self::assertSame([
      "name" => "jclain1",
      "amount" => 1,
    ], $sqlite->one(["select", "from" => "user", "where" => ["name = 'jclain1'"]]));
    self::assertSame([
      "name" => "jclain1",
      "amount" => 1,
    ], $sqlite->one(["select", "from" => "user", "where" => ["name" => "jclain1"]]));
    self::assertSame([
      "name" => "jclain1",
    ], $sqlite->one(["select name", "from" => "user", "where" => ["name" => "jclain1"]]));
    self::assertSame([
      "name" => "jclain1",
    ], $sqlite->one(["select", "cols" => "name", "from" => "user", "where" => ["name" => "jclain1"]]));
    self::assertSame([
      "name" => "jclain1",
    ], $sqlite->one(["select", "cols" => ["name"], "from" => "user", "where" => ["name" => "jclain1"]]));
    self::assertSame([
      "plouf" => "jclain1",
    ], $sqlite->one(["select", "cols" => ["plouf" => "name"], "from" => "user", "where" => ["name" => "jclain1"]]));
  }

  function testSelectGroupBy() {
    $sqlite = new Sqlite(":memory:", [
      "migrate" => "create table user (name varchar, amount integer)",
    ]);
    $sqlite->exec(["insert into user", "values" => ["name" => "jclain1", "amount" => 1]]);
    $sqlite->exec(["insert into user", "values" => ["name" => "jclain2", "amount" => 1]]);
    $sqlite->exec(["insert into user", "values" => ["name" => "jclain5", "amount" => 2]]);
    $sqlite->exec(["insert into user", "values" => ["name" => "fclain7", "amount" => 2]]);
    $sqlite->exec(["insert into user", "values" => ["name" => "fclain9", "amount" => 2]]);
    $sqlite->exec(["insert into user", "values" => ["name" => "fclain10", "amount" => 3]]);

    self::assertSame([
      ["count" => 2],
    ], iterator_to_array($sqlite->all(["select count(name) as count from user", "group by" => ["amount"], "having" => ["count(name) = 2"]])));
  }
}
