1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2025-01-03 14:32:40 +00:00

Improved database tests

- Centralized initial state; this will be useful due to foreign key interdependence
- Added nextID() method to make insert tests less brittle
This commit is contained in:
J. King 2017-05-10 21:21:23 -04:00
parent 8ebcb01cb5
commit 554beacfdb
3 changed files with 36 additions and 34 deletions

View file

@ -9,4 +9,8 @@ trait DriverSQLite3 {
Data::$conf->dbSQLite3File = ":memory:"; Data::$conf->dbSQLite3File = ":memory:";
$this->drv = new Driver(true); $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();
}
} }

View file

@ -7,45 +7,16 @@ use Phake;
trait SeriesFolder { trait SeriesFolder {
function setUpSeries() { 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); $this->primeDatabase($this->data);
} }
function testAddARootFolder() { function testAddARootFolder() {
$user = "john.doe@example.com"; $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"); Phake::verify(Data::$user)->authorize($user, "folderAdd");
$state = $this->primeExpectations($this->data, ['arsse_folders' => ['id','owner', 'parent', 'name']]); $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); $this->compareExpectations($state);
} }
@ -56,10 +27,11 @@ trait SeriesFolder {
function testAddANestedFolder() { function testAddANestedFolder() {
$user = "john.doe@example.com"; $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"); Phake::verify(Data::$user)->authorize($user, "folderAdd");
$state = $this->primeExpectations($this->data, ['arsse_folders' => ['id','owner', 'parent', 'name']]); $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); $this->compareExpectations($state);
} }

View file

@ -24,6 +24,32 @@ trait Setup {
["john.doe@example.com", "", "John Doe", UserDriver::RIGHTS_NONE], ["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() { function setUp() {