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

118 lines
3.7 KiB
PHP

<?php
namespace nur\sery\db\sqlite;
use nulib\tests\TestCase;
use nur\sery\db\Capacitor;
use nur\sery\db\CapacitorChannel;
class SqliteCapacitorTest extends TestCase {
function _testChargeStrings(SqliteCapacitor $capacitor, ?string $channel) {
$capacitor->reset($channel);
$capacitor->charge($channel, "first");
$capacitor->charge($channel, "second");
$capacitor->charge($channel, "third");
$items = iterator_to_array($capacitor->discharge($channel, null, false));
self::assertSame(["first", "second", "third"], $items);
}
function _testChargeArrays(SqliteCapacitor $capacitor, ?string $channel) {
$capacitor->reset($channel);
$capacitor->charge($channel, ["id" => 10, "name" => "first"]);
$capacitor->charge($channel, ["name" => "second", "id" => 20]);
$capacitor->charge($channel, ["name" => "third", "id" => "30"]);
}
function testChargeStrings() {
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db');
$this->_testChargeStrings($capacitor, null);
$capacitor->close();
}
function testChargeArrays() {
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db');
$capacitor->addChannel(new class extends CapacitorChannel {
const NAME = "arrays";
function getKeyDefinitions(): ?array {
return ["id" => "integer"];
}
function getKeyValues($item): ?array {
return ["id" => $item["id"] ?? null];
}
});
$this->_testChargeStrings($capacitor, "strings");
$this->_testChargeArrays($capacitor, "arrays");
$capacitor->close();
}
function testEach() {
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db');
$capacitor = new Capacitor($capacitor, new class extends CapacitorChannel {
const NAME = "each";
function getKeyDefinitions(): ?array {
return [
"age" => "integer",
"done" => "integer default 0",
];
}
function getKeyValues($item): ?array {
return [
"age" => $item["age"],
];
}
});
$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]);
$setDone = function ($item, $row, $suffix=null) {
$updates = ["done" => 1];
if ($suffix !== null) {
$item["name"] .= $suffix;
$updates["_item"] = $item;
}
return $updates;
};
$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 [
"_id" => "varchar primary key",
"done" => "integer default 0",
];
}
function getKeyValues($item): ?array {
return [
"_id" => $item["numero"],
];
}
});
$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]);
$capacitor->close();
self::assertTrue(true);
}
}