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

74 lines
3.0 KiB
PHP
Raw Normal View History

2024-04-24 23:16:55 +04:00
<?php
namespace nur\sery\db\sqlite;
2024-04-25 08:41:10 +04:00
use Exception;
use nulib\tests\TestCase;
use nur\sery\ValueException;
2024-04-24 23:16:55 +04:00
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")));
}
2024-04-25 08:20:33 +04:00
2024-04-25 08:41:10 +04:00
function testException() {
$sqlite = new Sqlite(":memory:");
self::assertException(Exception::class, [$sqlite, "exec"], "prout");
self::assertException(SqliteException::class, [$sqlite, "exec"], ["prout"]);
}
2024-04-25 08:20:33 +04:00
function testInsert() {
$sqlite = new Sqlite(":memory:", [
2024-04-25 08:41:10 +04:00
"migrate" => "create table mapping (i integer, s varchar)",
2024-04-25 08:20:33 +04:00
]);
$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")));
}
2024-04-24 23:16:55 +04:00
}