118 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			118 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
namespace nur\sery\db\sqlite;
 | 
						|
 | 
						|
use nulib\tests\TestCase;
 | 
						|
use nur\sery\db\Capacitor;
 | 
						|
use nur\sery\db\CapacitorChannel;
 | 
						|
 | 
						|
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");
 | 
						|
    $items = iterator_to_array($storage->discharge($channel, false));
 | 
						|
    self::assertSame(["first", "second", "third"], $items);
 | 
						|
  }
 | 
						|
 | 
						|
  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"]);
 | 
						|
  }
 | 
						|
 | 
						|
  function testChargeStrings() {
 | 
						|
    $storage = new SqliteStorage(__DIR__.'/capacitor.db');
 | 
						|
    $this->_testChargeStrings($storage, null);
 | 
						|
    $storage->close();
 | 
						|
  }
 | 
						|
 | 
						|
  function testChargeArrays() {
 | 
						|
    $storage = new SqliteStorage(__DIR__.'/capacitor.db');
 | 
						|
    $storage->addChannel(new class extends CapacitorChannel {
 | 
						|
      const NAME = "arrays";
 | 
						|
      function getColumnDefinitions(): ?array {
 | 
						|
        return ["id" => "integer"];
 | 
						|
      }
 | 
						|
      function getKeyValues($item): ?array {
 | 
						|
        return ["id" => $item["id"] ?? null];
 | 
						|
      }
 | 
						|
    });
 | 
						|
 | 
						|
    $this->_testChargeStrings($storage, "strings");
 | 
						|
    $this->_testChargeArrays($storage, "arrays");
 | 
						|
    $storage->close();
 | 
						|
  }
 | 
						|
 | 
						|
  function testEach() {
 | 
						|
    $storage = new SqliteStorage(__DIR__.'/capacitor.db');
 | 
						|
    $capacitor = new Capacitor($storage, new class extends CapacitorChannel {
 | 
						|
      const NAME = "each";
 | 
						|
 | 
						|
      function getColumnDefinitions(): ?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() {
 | 
						|
    $storage = new SqliteStorage(__DIR__.'/capacitor.db');
 | 
						|
    $capacitor = new Capacitor($storage, new class extends CapacitorChannel {
 | 
						|
      const NAME = "pk";
 | 
						|
 | 
						|
      function getColumnDefinitions(): ?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);
 | 
						|
  }
 | 
						|
}
 |