Issue #11 - Tests for outagedb get_all_active.

This commit is contained in:
Daniel Thee Roperto
2016-09-12 11:49:11 +10:00
parent 0d8ab19f83
commit e13bc05f5e

View File

@@ -30,6 +30,18 @@ defined('MOODLE_INTERNAL') || die();
class outagedb_test extends advanced_testcase {
/**
* Creates an array of ids in from the given outages array.
* @param $outages
*/
private static function createidarray(array $outages) {
$ids = [];
foreach ($outages as $outage) {
$ids[] = $outage->id;
}
return $ids;
}
/**
* Make sure we can save and update.
*/
@@ -170,6 +182,42 @@ class outagedb_test extends advanced_testcase {
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
}
public function test_getallactive() {
$this->resetAfterTest(true);
// Have a consistent time for now (no seconds variation), helps debugging.
$now = time();
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
self::assertEquals([], outagedb::get_all_active($now), 'There should be no active outages at this point.');
self::saveoutage($now, 1, 2, 3, 'An outage that starts in the future and is not in warning period.');
self::assertEquals([], outagedb::get_all_active($now), 'No active outages yet.');
self::saveoutage($now, -3, -2, -1, 'An outage that is already in the past.');
self::assertEquals([], outagedb::get_all_active($now), 'No active outages yet.');
$id1 = self::saveoutage($now, -2, 1, 2, 'An outage in warning period.');
self::assertEquals([$id1],
self::createidarray(outagedb::get_all_active($now)), 'Wrong actives data.');
$id2 = self::saveoutage($now, -1, 2, 3, 'Another outage in warning period.');
self::assertEquals([$id1, $id2],
self::createidarray(outagedb::get_all_active($now)), 'Wrong actives data.');
$id3 = self::saveoutage($now, -3, -2, 2, 'An ongoing outage.');
self::assertEquals([$id3, $id1, $id2],
self::createidarray(outagedb::get_all_active($now)), 'Wrong actives data.');
$id4 = self::saveoutage($now, -3, -1, 1, 'Another ongoing outage.');
self::assertEquals([$id3, $id4, $id1, $id2],
self::createidarray(outagedb::get_all_active($now)), 'Wrong actives data.');
$id5 = self::saveoutage($now, -3, -2, 1, 'Yet another ongoing outage.');
self::assertEquals([$id3, $id5, $id4, $id1, $id2],
self::createidarray(outagedb::get_all_active($now)), 'Wrong actives data.');
}
/**
* Helper function to create an outage then save it to the database.
*