Browse Source

Backport testing improvements from ttrss branch

microsub
J. King 7 years ago
parent
commit
cc875be57e
  1. 4
      lib/Db/SQLite3/Driver.php
  2. 3
      tests/Db/SQLite3/TestDbDriverSQLite3.php
  3. 2
      tests/Db/SQLite3/TestDbUpdateSQLite3.php
  4. 2
      tests/lib/Database/DriverSQLite3.php
  5. 30
      tests/lib/Database/Setup.php

4
lib/Db/SQLite3/Driver.php

@ -16,13 +16,13 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver {
protected $db;
public function __construct() {
public function __construct(string $dbFile = null) {
// check to make sure required extension is loaded
if (!class_exists("SQLite3")) {
throw new Exception("extMissing", self::driverName()); // @codeCoverageIgnore
}
// if no database file is specified in the configuration, use a suitable default
$dbFile = Arsse::$conf->dbSQLite3File ?? \JKingWeb\Arsse\BASE."arsse.db";
$dbFile = $dbFile ?? Arsse::$conf->dbSQLite3File ?? \JKingWeb\Arsse\BASE."arsse.db";
$mode = \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE;
$timeout = Arsse::$conf->dbSQLite3Timeout * 1000;
try {

3
tests/Db/SQLite3/TestDbDriverSQLite3.php

@ -18,8 +18,9 @@ class TestDbDriverSQLite3 extends Test\AbstractTest {
$conf = new Conf();
Arsse::$conf = $conf;
$conf->dbDriver = Db\SQLite3\Driver::class;
$conf->dbSQLite3Timeout = 0;
$conf->dbSQLite3File = tempnam(sys_get_temp_dir(), 'ook');
$this->drv = new Db\SQLite3\Driver(true);
$this->drv = new Db\SQLite3\Driver();
$this->ch = new \SQLite3(Arsse::$conf->dbSQLite3File);
$this->ch->enableExceptions(true);
}

2
tests/Db/SQLite3/TestDbUpdateSQLite3.php

@ -30,7 +30,7 @@ class TestDbUpdateSQLite3 extends Test\AbstractTest {
Arsse::$conf = $conf;
$this->base = $this->vfs->url();
$this->path = $this->base."/SQLite3/";
$this->drv = new Db\SQLite3\Driver(true);
$this->drv = new Db\SQLite3\Driver();
}
public function tearDown() {

2
tests/lib/Database/DriverSQLite3.php

@ -11,7 +11,7 @@ trait DriverSQLite3 {
$this->markTestSkipped("SQLite extension not loaded");
}
Arsse::$conf->dbSQLite3File = ":memory:";
$this->drv = new Driver(true);
$this->drv = new Driver();
}
public function nextID(string $table): int {

30
tests/lib/Database/Setup.php

@ -48,15 +48,16 @@ trait Setup {
$this->clearData();
}
public function primeDatabase(array $data): bool {
$tr = $this->drv->begin();
public function primeDatabase(array $data, \JKingWeb\Arsse\Db\Driver $drv = null): bool {
$drv = $drv ?? $this->drv;
$tr = $drv->begin();
foreach ($data as $table => $info) {
$cols = implode(",", array_keys($info['columns']));
$bindings = array_values($info['columns']);
$params = implode(",", array_fill(0, sizeof($info['columns']), "?"));
$s = $this->drv->prepareArray("INSERT INTO $table($cols) values($params)", $bindings);
$s = $drv->prepareArray("INSERT INTO $table($cols) values($params)", $bindings);
foreach ($info['rows'] as $row) {
$this->assertEquals(1, $s->runArray($row)->changes());
$s->runArray($row);
}
}
$tr->commit();
@ -64,6 +65,16 @@ trait Setup {
return true;
}
public function primeFile(string $file, array $data = null): bool {
$data = $data ?? $this->data;
$primed = $this->primed;
$drv = new \JKingWeb\Arsse\Db\SQLite3\Driver($file);
$drv->schemaUpdate(\JKingWeb\Arsse\Database::SCHEMA_VERSION);
$this->primeDatabase($data, $drv);
$this->primed = $primed;
return true;
}
public function compareExpectations(array $expected): bool {
foreach ($expected as $table => $info) {
$cols = implode(",", array_keys($info['columns']));
@ -126,11 +137,12 @@ trait Setup {
$rows[] = array_intersect_key($row, $keys);
}
// compare the result set to the expectations
foreach ($expected as $index => $exp) {
$this->assertContains($exp, $rows, "Result set does not contain record at array index $index.");
$found = array_search($exp, $rows, true);
unset($rows[$found]);
foreach ($rows as $row) {
$this->assertContains($row, $expected, "Result set contains unexpected record.");
$found = array_search($row, $expected);
unset($expected[$found]);
}
$this->assertArraySubset($expected, [], "Expectations not in result set.");
}
}
}
}
Loading…
Cancel
Save