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

345 lines
12 KiB
PHP
Raw Normal View History

2024-05-18 13:09:01 +04:00
<?php
namespace nur\sery\db\sqlite;
2024-05-20 10:46:18 +04:00
use nulib\tests\TestCase;
2024-06-05 14:03:42 +04:00
use nur\sery\cl;
2024-05-20 10:46:18 +04:00
use nur\sery\db\Capacitor;
2024-05-20 09:24:47 +04:00
use nur\sery\db\CapacitorChannel;
2024-05-18 13:09:01 +04:00
class SqliteStorageTest extends TestCase {
function _testChargeStrings(SqliteStorage $storage, ?string $channel) {
$storage->reset($channel);
$storage->charge($channel, "first");
$storage->charge($channel, "second");
$storage->charge($channel, "third");
2024-06-05 14:03:42 +04:00
$items = cl::all($storage->discharge($channel, false));
2024-05-18 13:09:01 +04:00
self::assertSame(["first", "second", "third"], $items);
}
2024-05-20 10:46:18 +04:00
function _testChargeArrays(SqliteStorage $storage, ?string $channel) {
$storage->reset($channel);
$storage->charge($channel, ["id" => 10, "name" => "first"]);
$storage->charge($channel, ["name" => "second", "id" => 20]);
$storage->charge($channel, ["name" => "third", "id" => "30"]);
2024-05-18 13:09:01 +04:00
}
function testChargeStrings() {
$storage = new SqliteStorage(__DIR__.'/capacitor.db');
$this->_testChargeStrings($storage, null);
$storage->close();
2024-05-18 13:09:01 +04:00
}
function testChargeArrays() {
$storage = new SqliteStorage(__DIR__.'/capacitor.db');
$storage->addChannel(new class extends CapacitorChannel {
2024-05-20 09:24:47 +04:00
const NAME = "arrays";
2024-06-05 14:03:42 +04:00
const COLUMN_DEFINITIONS = ["id" => "integer"];
function getItemValues($item): ?array {
2024-05-20 09:24:47 +04:00
return ["id" => $item["id"] ?? null];
2024-05-18 13:09:01 +04:00
}
2024-05-20 09:24:47 +04:00
});
2024-05-18 13:09:01 +04:00
$this->_testChargeStrings($storage, "strings");
$this->_testChargeArrays($storage, "arrays");
$storage->close();
2024-05-18 13:09:01 +04:00
}
2024-05-19 11:19:00 +04:00
function testEach() {
$storage = new SqliteStorage(__DIR__.'/capacitor.db');
$capacitor = new Capacitor($storage, new class extends CapacitorChannel {
2024-05-20 09:24:47 +04:00
const NAME = "each";
2024-06-05 14:03:42 +04:00
const COLUMN_DEFINITIONS = [
"age" => "integer",
"done" => "integer default 0",
];
2024-05-20 10:46:18 +04:00
2024-06-05 14:03:42 +04:00
function getItemValues($item): ?array {
2024-05-19 11:19:00 +04:00
return [
"age" => $item["age"],
];
}
2024-05-20 09:24:47 +04:00
});
2024-05-19 11:19:00 +04:00
2024-05-20 10:46:18 +04:00
$capacitor->reset();
$capacitor->charge(["name" => "first", "age" => 5]);
$capacitor->charge(["name" => "second", "age" => 10]);
$capacitor->charge(["name" => "third", "age" => 15]);
$capacitor->charge(["name" => "fourth", "age" => 20]);
2024-05-19 11:19:00 +04:00
$setDone = function ($item, $row, $suffix=null) {
$updates = ["done" => 1];
if ($suffix !== null) {
$item["name"] .= $suffix;
2024-05-24 17:40:31 +04:00
$updates["item"] = $item;
2024-05-19 11:19:00 +04:00
}
return $updates;
};
2024-05-20 10:46:18 +04:00
$capacitor->each(["age" => [">", 10]], $setDone, ["++"]);
$capacitor->each(["done" => 0], $setDone, null);
2024-06-07 16:12:55 +04:00
Txx(cl::all($capacitor->discharge(false)));
2024-05-20 10:46:18 +04:00
$capacitor->close();
self::assertTrue(true);
}
function testPrimayKey() {
$storage = new SqliteStorage(__DIR__.'/capacitor.db');
$capacitor = new Capacitor($storage, new class extends CapacitorChannel {
2024-05-20 10:46:18 +04:00
const NAME = "pk";
2024-06-05 14:03:42 +04:00
const COLUMN_DEFINITIONS = [
"id_" => "varchar primary key",
"done" => "integer default 0",
];
2024-05-20 10:46:18 +04:00
2024-06-05 14:03:42 +04:00
function getItemValues($item): ?array {
2024-05-20 10:46:18 +04:00
return [
2024-05-24 17:40:31 +04:00
"id_" => $item["numero"],
2024-05-20 10:46:18 +04:00
];
}
});
$capacitor->charge(["numero" => "a", "name" => "first", "age" => 5]);
$capacitor->charge(["numero" => "b", "name" => "second", "age" => 10]);
$capacitor->charge(["numero" => "c", "name" => "third", "age" => 15]);
$capacitor->charge(["numero" => "d", "name" => "fourth", "age" => 20]);
sleep(2);
$capacitor->charge(["numero" => "b", "name" => "second", "age" => 100]);
$capacitor->charge(["numero" => "d", "name" => "fourth", "age" => 200]);
2024-05-19 11:19:00 +04:00
$capacitor->close();
self::assertTrue(true);
}
2024-06-07 16:12:55 +04:00
function testSum() {
$storage = new SqliteStorage(__DIR__.'/capacitor.db');
$capacitor = new Capacitor($storage, new class extends CapacitorChannel {
const NAME = "sum";
const COLUMN_DEFINITIONS = [
"a__" => "varchar",
"b__" => "varchar",
"b__sum_" => self::SUM_DEFINITION,
];
function getItemValues($item): ?array {
return [
"a" => $item["a"],
"b" => $item["b"],
];
}
});
$capacitor->reset();
$capacitor->charge(["a" => null, "b" => null]);
$capacitor->charge(["a" => "first", "b" => "second"]);
Txx("=== all");
/** @var Sqlite $sqlite */
$sqlite = $capacitor->getStorage()->db();
Txx(cl::all($sqlite->all([
"select",
"from" => $capacitor->getChannel()->getTableName(),
])));
Txx("=== each");
$capacitor->each(null, function ($item, $values) {
Txx($values);
});
$capacitor->close();
self::assertTrue(true);
}
2024-06-29 10:32:57 +04:00
function testEachValues() {
# tester que values contient bien toutes les valeurs de la ligne
$storage = new SqliteStorage(__DIR__.'/capacitor.db');
$capacitor = new Capacitor($storage, new class extends CapacitorChannel {
const NAME = "each_values";
const COLUMN_DEFINITIONS = [
"name" => "varchar primary key",
"age" => "integer",
"done" => "integer default 0",
"notes" => "text",
];
function getItemValues($item): ?array {
return [
"name" => $item["name"],
"age" => $item["age"],
];
}
});
$capacitor->reset();
$capacitor->charge(["name" => "first", "age" => 5], function($item, ?array $values, ?array $pvalues) {
self::assertSame("first", $item["name"]);
self::assertSame(5, $item["age"]);
self::assertnotnull($values);
self::assertSame(["name", "age", "item", "item__sum_", "created_", "modified_"], array_keys($values));
self::assertSame([
"name" => "first",
"age" => 5,
"item" => $item,
], cl::select($values, ["name", "age", "item"]));
self::assertNull($pvalues);
});
$capacitor->charge(["name" => "first", "age" => 10], function($item, ?array $values, ?array $pvalues) {
self::assertSame("first", $item["name"]);
self::assertSame(10, $item["age"]);
self::assertnotnull($values);
self::assertSame(["name", "age", "done", "notes", "item", "item__sum_", "created_", "modified_"], array_keys($values));
self::assertSame([
"name" => "first",
"age" => 10,
"done" => 0,
"notes" => null,
"item" => $item,
], cl::select($values, ["name", "age", "done", "notes", "item"]));
self::assertNotNull($pvalues);
self::assertSame([
"name" => "first",
"age" => 5,
"done" => 0,
"notes" => null,
"item" => ["name" => "first", "age" => 5],
], cl::select($pvalues, ["name", "age", "done", "notes", "item"]));
});
$capacitor->each(null, function($item, ?array $values) {
self::assertSame("first", $item["name"]);
self::assertSame(10, $item["age"]);
self::assertnotnull($values);
self::assertSame(["name", "age", "done", "notes", "item", "item__sum_", "created_", "modified_"], array_keys($values));
self::assertSame([
"name" => "first",
"age" => 10,
"done" => 0,
"notes" => null,
"item" => $item,
], cl::select($values, ["name", "age", "done", "notes", "item"]));
return [
"done" => 1,
"notes" => "modified",
];
});
$capacitor->charge(["name" => "first", "age" => 10], function($item, ?array $values, ?array $pvalues) {
self::assertSame("first", $item["name"]);
self::assertSame(10, $item["age"]);
self::assertnotnull($values);
self::assertSame(["name", "age", "done", "notes", "item", "item__sum_", "created_", "modified_"], array_keys($values));
self::assertSame([
"name" => "first",
"age" => 10,
"done" => 1,
"notes" => "modified",
"item" => $item,
], cl::select($values, ["name", "age", "done", "notes", "item"]));
self::assertNotNull($pvalues);
self::assertSame([
"name" => "first",
"age" => 10,
"done" => 1,
"notes" => "modified",
"item" => $item,
], cl::select($pvalues, ["name", "age", "done", "notes", "item"]));
});
$capacitor->charge(["name" => "first", "age" => 20], function($item, ?array $values, ?array $pvalues) {
self::assertSame("first", $item["name"]);
self::assertSame(20, $item["age"]);
self::assertnotnull($values);
self::assertSame(["name", "age", "done", "notes", "item", "item__sum_", "created_", "modified_"], array_keys($values));
self::assertSame([
"name" => "first",
"age" => 20,
"done" => 1,
"notes" => "modified",
"item" => $item,
], cl::select($values, ["name", "age", "done", "notes", "item"]));
self::assertNotNull($pvalues);
self::assertSame([
"name" => "first",
"age" => 10,
"done" => 1,
"notes" => "modified",
"item" => ["name" => "first", "age" => 10],
], cl::select($pvalues, ["name", "age", "done", "notes", "item"]));
});
}
function testSetItemNull() {
# tester le forçage de $îtem à null pour économiser la place
$storage = new SqliteStorage(__DIR__.'/capacitor.db');
$capacitor = new Capacitor($storage, new class extends CapacitorChannel {
const NAME = "set_item_null";
const COLUMN_DEFINITIONS = [
"name" => "varchar primary key",
"age" => "integer",
"done" => "integer default 0",
"notes" => "text",
];
function getItemValues($item): ?array {
return [
"name" => $item["name"],
"age" => $item["age"],
];
}
});
$capacitor->reset();
$nbModified = $capacitor->charge(["name" => "first", "age" => 5], function ($item, ?array $values, ?array $pvalues) {
self::assertSame([
"name" => "first", "age" => 5,
"item" => $item,
], cl::select($values, ["name", "age", "item"]));
return ["item" => null];
});
self::assertSame(1, $nbModified);
sleep(1);
# nb: on met des sleep() pour que la date de modification soit systématiquement différente
$nbModified = $capacitor->charge(["name" => "first", "age" => 10], function ($item, ?array $values, ?array $pvalues) {
self::assertSame([
"name" => "first", "age" => 10,
"item" => $item, "item__sum_" => "9181336dfca20c86313d6065d89aa2ad5070b0fc",
], cl::select($values, ["name", "age", "item", "item__sum_"]));
self::assertSame([
"name" => "first", "age" => 5,
"item" => null, "item__sum_" => null,
], cl::select($pvalues, ["name", "age", "item", "item__sum_"]));
return ["item" => null];
});
self::assertSame(1, $nbModified);
sleep(1);
# pas de modification ici
$nbModified = $capacitor->charge(["name" => "first", "age" => 10], function ($item, ?array $values, ?array $pvalues) {
self::assertSame([
"name" => "first", "age" => 10,
"item" => $item, "item__sum_" => "9181336dfca20c86313d6065d89aa2ad5070b0fc",
], cl::select($values, ["name", "age", "item", "item__sum_"]));
self::assertSame([
"name" => "first", "age" => 10,
"item" => null, "item__sum_" => null,
], cl::select($pvalues, ["name", "age", "item", "item__sum_"]));
return ["item" => null];
});
self::assertSame(0, $nbModified);
sleep(1);
$nbModified = $capacitor->charge(["name" => "first", "age" => 20], function ($item, ?array $values, ?array $pvalues) {
self::assertSame([
"name" => "first", "age" => 20,
"item" => $item, "item__sum_" => "001b91982b4e0883b75428c0eb28573a5dc5f7a5",
], cl::select($values, ["name", "age", "item", "item__sum_"]));
self::assertSame([
"name" => "first", "age" => 10,
"item" => null, "item__sum_" => null,
], cl::select($pvalues, ["name", "age", "item", "item__sum_"]));
return ["item" => null];
});
self::assertSame(1, $nbModified);
sleep(1);
}
2024-05-18 13:09:01 +04:00
}