Browse Source

Improved database tests

- Centralized initial state; this will be useful due to foreign key interdependence
- Added nextID() method to make insert tests less brittle
microsub
J. King 7 years ago
parent
commit
554beacfdb
  1. 4
      tests/lib/Database/DriverSQLite3.php
  2. 40
      tests/lib/Database/SeriesFolder.php
  3. 26
      tests/lib/Database/Setup.php

4
tests/lib/Database/DriverSQLite3.php

@ -9,4 +9,8 @@ trait DriverSQLite3 {
Data::$conf->dbSQLite3File = ":memory:";
$this->drv = new Driver(true);
}
function nextID(string $table): int {
return $this->drv->query("SELECT (case when max(id) then max(id) else 0 end)+1 from $table")->getValue();
}
}

40
tests/lib/Database/SeriesFolder.php

@ -7,45 +7,16 @@ use Phake;
trait SeriesFolder {
function setUpSeries() {
$data = [
'arsse_folders' => [
'columns' => [
'id' => "int",
'owner' => "str",
'parent' => "int",
'name' => "str",
],
/* Layout translates to:
Jane
Politics
John
Technology
Software
Politics
Rocketry
Politics
*/
'rows' => [
[1, "john.doe@example.com", null, "Technology"],
[2, "john.doe@example.com", 1, "Software"],
[3, "john.doe@example.com", 1, "Rocketry"],
[4, "jane.doe@example.com", null, "Politics"],
[5, "john.doe@example.com", null, "Politics"],
[6, "john.doe@example.com", 2, "Politics"],
]
]
];
// merge folder table with base user table
$this->data = array_merge($this->data, $data);
$this->primeDatabase($this->data);
}
function testAddARootFolder() {
$user = "john.doe@example.com";
$this->assertSame(7, Data::$db->folderAdd($user, ['name' => "Entertainment"]));
$folderID = $this->nextID("arsse_folders");
$this->assertSame($folderID, Data::$db->folderAdd($user, ['name' => "Entertainment"]));
Phake::verify(Data::$user)->authorize($user, "folderAdd");
$state = $this->primeExpectations($this->data, ['arsse_folders' => ['id','owner', 'parent', 'name']]);
$state['arsse_folders']['rows'][] = [7, $user, null, "Entertainment"];
$state['arsse_folders']['rows'][] = [$folderID, $user, null, "Entertainment"];
$this->compareExpectations($state);
}
@ -56,10 +27,11 @@ trait SeriesFolder {
function testAddANestedFolder() {
$user = "john.doe@example.com";
$this->assertSame(7, Data::$db->folderAdd($user, ['name' => "GNOME", 'parent' => 2]));
$folderID = $this->nextID("arsse_folders");
$this->assertSame($folderID, Data::$db->folderAdd($user, ['name' => "GNOME", 'parent' => 2]));
Phake::verify(Data::$user)->authorize($user, "folderAdd");
$state = $this->primeExpectations($this->data, ['arsse_folders' => ['id','owner', 'parent', 'name']]);
$state['arsse_folders']['rows'][] = [7, $user, 2, "GNOME"];
$state['arsse_folders']['rows'][] = [$folderID, $user, 2, "GNOME"];
$this->compareExpectations($state);
}

26
tests/lib/Database/Setup.php

@ -24,6 +24,32 @@ trait Setup {
["john.doe@example.com", "", "John Doe", UserDriver::RIGHTS_NONE],
],
],
'arsse_folders' => [
'columns' => [
'id' => "int",
'owner' => "str",
'parent' => "int",
'name' => "str",
],
/* Layout translates to:
Jane
Politics
John
Technology
Software
Politics
Rocketry
Politics
*/
'rows' => [
[1, "john.doe@example.com", null, "Technology"],
[2, "john.doe@example.com", 1, "Software"],
[3, "john.doe@example.com", 1, "Rocketry"],
[4, "jane.doe@example.com", null, "Politics"],
[5, "john.doe@example.com", null, "Politics"],
[6, "john.doe@example.com", 2, "Politics"],
]
],
];
function setUp() {

Loading…
Cancel
Save