1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00

Backport testing improvements from ttrss branch

This commit is contained in:
J. King 2017-10-19 15:32:18 -04:00
parent 1271a0c8c0
commit cc875be57e
5 changed files with 27 additions and 14 deletions

View file

@ -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 {

View file

@ -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);
}

View file

@ -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() {

View file

@ -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 {

View file

@ -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.");
}
}
}
}