Skip to content

Commit

Permalink
Tests: Improve Unit Test #69
Browse files Browse the repository at this point in the history
  • Loading branch information
georgmaisser authored and eynimeni committed Feb 18, 2025
1 parent 612304c commit 9661056
Showing 1 changed file with 52 additions and 10 deletions.
62 changes: 52 additions & 10 deletions tests/base_tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function setUp(): void {
* Test wb base functionality via webservice external class.
*
* @covers \wunderbyte_table::query_db_cached
* @runInSeparateProcess
* // @runInSeparateProcess
*
* @throws \coding_exception
* @throws \dml_exception
Expand All @@ -75,23 +75,23 @@ public function test_query_db_cached(): void {
$this->setUser($user);

$table = $this->create_demo2_table();
$nrofrows = $this->return_rows_for_table($table);
$nrofrows = $this->get_rowscount_for_table($table);

// Now we get back exactly 10.
$this->assertEquals($nrofrows, 10);

// Now we create another three courses.
$this->create_test_courses(3, ['fullname' => 'filtercourse']);

$nrofrows = $this->return_rows_for_table($table);
$nrofrows = $this->get_rowscount_for_table($table);

// Because of caching kicking in, we still get 10 items.
$this->assertEquals($nrofrows, 10);

// Now we purge the cache.
cache_helper::purge_by_event('changesinwunderbytetable');

$nrofrows = $this->return_rows_for_table($table);
$nrofrows = $this->get_rowscount_for_table($table);

// After purging, we expect 13.
$this->assertEquals($nrofrows, 13);
Expand All @@ -102,17 +102,17 @@ public function test_query_db_cached(): void {
// Now we purge the cache.
cache_helper::purge_by_event('changesinwunderbytetable');

$nrofrows = $this->return_rows_for_table($table);
$nrofrows = $this->get_rowscount_for_table($table);

$this->assertEquals($nrofrows, 20);

// Now we fetch the third page. With 43 coures, we expect only three rows now.
$nrofrows = $this->return_rows_for_table($table, 2);
$nrofrows = $this->get_rowscount_for_table($table, 2);

$this->assertEquals($nrofrows, 3);

// Now we fetch the third page. With 43 coures, we expect only three rows now.
$nrofrows = $this->return_rows_for_table(
$nrofrows = $this->get_rowscount_for_table(
$table,
0,
null,
Expand Down Expand Up @@ -205,7 +205,7 @@ public function create_test_courses(int $coursestocreate = 1, $options = []): ar
* @return int
*
*/
public function return_rows_for_table(
public function get_rowscount_for_table(
wunderbyte_table $table,
$page = null,
$tsort = null,
Expand All @@ -217,6 +217,49 @@ public function return_rows_for_table(
$searchtext = null
): int {

$rows = $this->get_rows_for_table(
$table,
$page,
$tsort,
$thide,
$tshow,
$tdir,
$treset,
$filterobjects,
$searchtext
);

return count($rows);
}

/**
* Returns the actual rows for a table. This only retrieves the rows for the current page.
*
* @param wunderbyte_table $table
* @param int $page
* @param string $tsort
* @param string $thide
* @param string $tshow
* @param int $tdir
* @param int $treset
* @param string $filterobjects
* @param string $searchtext
*
* @return array
*
*/
public function get_rows_for_table(
wunderbyte_table $table,
$page = null,
$tsort = null,
$thide = null,
$tshow = null,
$tdir = null,
$treset = null,
$filterobjects = null,
$searchtext = null
): array {

$encodedtable = $table->return_encoded_table();
$result = load_data::execute(
$encodedtable,
Expand All @@ -235,7 +278,6 @@ public function return_rows_for_table(
throw new moodle_exception('test', 'test', '', json_encode($jsonobject));
}
$rows = $jsonobject->table->rows ?? 0;

return count($rows);
return $rows;
}
}

0 comments on commit 9661056

Please sign in to comment.