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;
|
|
|
|
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 SqliteCapacitorTest extends TestCase {
|
|
|
|
function _testChargeStrings(SqliteCapacitor $capacitor, ?string $channel) {
|
|
|
|
$capacitor->reset($channel);
|
2024-05-20 10:46:18 +04:00
|
|
|
$capacitor->charge($channel, "first");
|
|
|
|
$capacitor->charge($channel, "second");
|
|
|
|
$capacitor->charge($channel, "third");
|
|
|
|
$items = iterator_to_array($capacitor->discharge($channel, null, false));
|
2024-05-18 13:09:01 +04:00
|
|
|
self::assertSame(["first", "second", "third"], $items);
|
|
|
|
}
|
2024-05-20 10:46:18 +04:00
|
|
|
|
2024-05-18 13:09:01 +04:00
|
|
|
function _testChargeArrays(SqliteCapacitor $capacitor, ?string $channel) {
|
|
|
|
$capacitor->reset($channel);
|
2024-05-20 10:46:18 +04:00
|
|
|
$capacitor->charge($channel, ["id" => 10, "name" => "first"]);
|
|
|
|
$capacitor->charge($channel, ["name" => "second", "id" => 20]);
|
|
|
|
$capacitor->charge($channel, ["name" => "third", "id" => "30"]);
|
2024-05-18 13:09:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function testChargeStrings() {
|
|
|
|
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db');
|
|
|
|
$this->_testChargeStrings($capacitor, null);
|
|
|
|
$capacitor->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
function testChargeArrays() {
|
2024-05-20 09:24:47 +04:00
|
|
|
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db');
|
|
|
|
$capacitor->addChannel(new class extends CapacitorChannel {
|
|
|
|
const NAME = "arrays";
|
|
|
|
function getKeyDefinitions(): ?array {
|
|
|
|
return ["id" => "integer"];
|
2024-05-18 13:09:01 +04:00
|
|
|
}
|
2024-05-20 09:24:47 +04:00
|
|
|
function getKeyValues($item): ?array {
|
|
|
|
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($capacitor, "strings");
|
|
|
|
$this->_testChargeArrays($capacitor, "arrays");
|
|
|
|
$capacitor->close();
|
|
|
|
}
|
2024-05-19 11:19:00 +04:00
|
|
|
|
|
|
|
function testEach() {
|
2024-05-20 09:24:47 +04:00
|
|
|
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db');
|
2024-05-20 10:46:18 +04:00
|
|
|
$capacitor = new Capacitor($capacitor, new class extends CapacitorChannel {
|
2024-05-20 09:24:47 +04:00
|
|
|
const NAME = "each";
|
2024-05-20 10:46:18 +04:00
|
|
|
|
2024-05-20 09:24:47 +04:00
|
|
|
function getKeyDefinitions(): ?array {
|
2024-05-19 11:19:00 +04:00
|
|
|
return [
|
|
|
|
"age" => "integer",
|
|
|
|
"done" => "integer default 0",
|
|
|
|
];
|
|
|
|
}
|
2024-05-20 09:24:47 +04:00
|
|
|
function getKeyValues($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);
|
|
|
|
Txx(iterator_to_array($capacitor->discharge(null, false)));
|
|
|
|
|
|
|
|
$capacitor->close();
|
|
|
|
self::assertTrue(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testPrimayKey() {
|
|
|
|
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db');
|
|
|
|
$capacitor = new Capacitor($capacitor, new class extends CapacitorChannel {
|
|
|
|
const NAME = "pk";
|
|
|
|
|
|
|
|
function getKeyDefinitions(): ?array {
|
|
|
|
return [
|
2024-05-24 17:40:31 +04:00
|
|
|
"id_" => "varchar primary key",
|
2024-05-20 10:46:18 +04:00
|
|
|
"done" => "integer default 0",
|
|
|
|
];
|
|
|
|
}
|
|
|
|
function getKeyValues($item): ?array {
|
|
|
|
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-05-18 13:09:01 +04:00
|
|
|
}
|