mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-23 06:24:53 +00:00
Backport testing improvements from ttrss branch
This commit is contained in:
parent
1271a0c8c0
commit
cc875be57e
5 changed files with 27 additions and 14 deletions
|
@ -16,13 +16,13 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver {
|
||||||
|
|
||||||
protected $db;
|
protected $db;
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct(string $dbFile = null) {
|
||||||
// check to make sure required extension is loaded
|
// check to make sure required extension is loaded
|
||||||
if (!class_exists("SQLite3")) {
|
if (!class_exists("SQLite3")) {
|
||||||
throw new Exception("extMissing", self::driverName()); // @codeCoverageIgnore
|
throw new Exception("extMissing", self::driverName()); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
// if no database file is specified in the configuration, use a suitable default
|
// 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;
|
$mode = \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE;
|
||||||
$timeout = Arsse::$conf->dbSQLite3Timeout * 1000;
|
$timeout = Arsse::$conf->dbSQLite3Timeout * 1000;
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -18,8 +18,9 @@ class TestDbDriverSQLite3 extends Test\AbstractTest {
|
||||||
$conf = new Conf();
|
$conf = new Conf();
|
||||||
Arsse::$conf = $conf;
|
Arsse::$conf = $conf;
|
||||||
$conf->dbDriver = Db\SQLite3\Driver::class;
|
$conf->dbDriver = Db\SQLite3\Driver::class;
|
||||||
|
$conf->dbSQLite3Timeout = 0;
|
||||||
$conf->dbSQLite3File = tempnam(sys_get_temp_dir(), 'ook');
|
$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 = new \SQLite3(Arsse::$conf->dbSQLite3File);
|
||||||
$this->ch->enableExceptions(true);
|
$this->ch->enableExceptions(true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ class TestDbUpdateSQLite3 extends Test\AbstractTest {
|
||||||
Arsse::$conf = $conf;
|
Arsse::$conf = $conf;
|
||||||
$this->base = $this->vfs->url();
|
$this->base = $this->vfs->url();
|
||||||
$this->path = $this->base."/SQLite3/";
|
$this->path = $this->base."/SQLite3/";
|
||||||
$this->drv = new Db\SQLite3\Driver(true);
|
$this->drv = new Db\SQLite3\Driver();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tearDown() {
|
public function tearDown() {
|
||||||
|
|
|
@ -11,7 +11,7 @@ trait DriverSQLite3 {
|
||||||
$this->markTestSkipped("SQLite extension not loaded");
|
$this->markTestSkipped("SQLite extension not loaded");
|
||||||
}
|
}
|
||||||
Arsse::$conf->dbSQLite3File = ":memory:";
|
Arsse::$conf->dbSQLite3File = ":memory:";
|
||||||
$this->drv = new Driver(true);
|
$this->drv = new Driver();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function nextID(string $table): int {
|
public function nextID(string $table): int {
|
||||||
|
|
|
@ -48,15 +48,16 @@ trait Setup {
|
||||||
$this->clearData();
|
$this->clearData();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function primeDatabase(array $data): bool {
|
public function primeDatabase(array $data, \JKingWeb\Arsse\Db\Driver $drv = null): bool {
|
||||||
$tr = $this->drv->begin();
|
$drv = $drv ?? $this->drv;
|
||||||
|
$tr = $drv->begin();
|
||||||
foreach ($data as $table => $info) {
|
foreach ($data as $table => $info) {
|
||||||
$cols = implode(",", array_keys($info['columns']));
|
$cols = implode(",", array_keys($info['columns']));
|
||||||
$bindings = array_values($info['columns']);
|
$bindings = array_values($info['columns']);
|
||||||
$params = implode(",", array_fill(0, sizeof($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) {
|
foreach ($info['rows'] as $row) {
|
||||||
$this->assertEquals(1, $s->runArray($row)->changes());
|
$s->runArray($row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$tr->commit();
|
$tr->commit();
|
||||||
|
@ -64,6 +65,16 @@ trait Setup {
|
||||||
return true;
|
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 {
|
public function compareExpectations(array $expected): bool {
|
||||||
foreach ($expected as $table => $info) {
|
foreach ($expected as $table => $info) {
|
||||||
$cols = implode(",", array_keys($info['columns']));
|
$cols = implode(",", array_keys($info['columns']));
|
||||||
|
@ -126,11 +137,12 @@ trait Setup {
|
||||||
$rows[] = array_intersect_key($row, $keys);
|
$rows[] = array_intersect_key($row, $keys);
|
||||||
}
|
}
|
||||||
// compare the result set to the expectations
|
// compare the result set to the expectations
|
||||||
foreach ($expected as $index => $exp) {
|
foreach ($rows as $row) {
|
||||||
$this->assertContains($exp, $rows, "Result set does not contain record at array index $index.");
|
$this->assertContains($row, $expected, "Result set contains unexpected record.");
|
||||||
$found = array_search($exp, $rows, true);
|
$found = array_search($row, $expected);
|
||||||
unset($rows[$found]);
|
unset($expected[$found]);
|
||||||
}
|
}
|
||||||
|
$this->assertArraySubset($expected, [], "Expectations not in result set.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue