mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-23 05:34:55 +00:00
Improvements to database test harness
This commit is contained in:
parent
81419452b5
commit
78e669fbfc
1 changed files with 39 additions and 47 deletions
|
@ -98,13 +98,11 @@ trait Setup {
|
||||||
$cols = implode(",", array_keys($info['columns']));
|
$cols = implode(",", array_keys($info['columns']));
|
||||||
$data = $this->drv->prepare("SELECT $cols from $table")->run()->getAll();
|
$data = $this->drv->prepare("SELECT $cols from $table")->run()->getAll();
|
||||||
$cols = array_keys($info['columns']);
|
$cols = array_keys($info['columns']);
|
||||||
foreach($info['rows'] as $index => $values) {
|
foreach($info['rows'] as $index => $row) {
|
||||||
$row = [];
|
$this->assertCount(sizeof($cols), $row, "The number of values for array index $index does not match the number of fields");
|
||||||
foreach($values as $key => $value) {
|
$row = array_combine($cols, $row);
|
||||||
$row[$cols[$key]] = $value;
|
$this->assertContains($row, $data, "Table $table does not contain record at array index $index.");
|
||||||
}
|
|
||||||
$found = array_search($row, $data, true);
|
$found = array_search($row, $data, true);
|
||||||
$this->assertNotSame(false, $found, "Table $table does not contain record at array index $index.");
|
|
||||||
unset($data[$found]);
|
unset($data[$found]);
|
||||||
}
|
}
|
||||||
$this->assertSame([], $data);
|
$this->assertSame([], $data);
|
||||||
|
@ -115,58 +113,52 @@ trait Setup {
|
||||||
function primeExpectations(array $source, array $tableSpecs = null): array {
|
function primeExpectations(array $source, array $tableSpecs = null): array {
|
||||||
$out = [];
|
$out = [];
|
||||||
foreach($tableSpecs as $table => $columns) {
|
foreach($tableSpecs as $table => $columns) {
|
||||||
if(!isset($source[$table])) {
|
// make sure the source has the table we want
|
||||||
$this->assertTrue(false, "Source for expectations does not contain requested table $table.");
|
$this->assertArrayHasKey($table, $source, "Source for expectations does not contain requested table $table.");
|
||||||
return [];
|
|
||||||
}
|
|
||||||
$out[$table] = [
|
$out[$table] = [
|
||||||
'columns' => [],
|
'columns' => [],
|
||||||
'rows' => [],
|
'rows' => array_fill(0,sizeof($source[$table]['rows']), []),
|
||||||
];
|
];
|
||||||
$transformations = [];
|
// make sure the source has all the columns we want for the table
|
||||||
foreach($columns as $target => $col) {
|
$cols = array_flip($columns);
|
||||||
if(!isset($source[$table]['columns'][$col])) {
|
$cols = array_intersect_key($cols, $source[$table]['columns']);
|
||||||
$this->assertTrue(false, "Source for expectations does not contain requested column $col of table $table.");
|
$this->assertSame(array_keys($cols), $columns, "Source for table $table does not contain all requested columns");
|
||||||
return [];
|
// get a map of source value offsets and keys
|
||||||
|
$targets = array_flip(array_keys($source[$table]['columns']));
|
||||||
|
foreach($cols as $key => $order) {
|
||||||
|
// fill the column-spec
|
||||||
|
$out[$table]['columns'][$key] = $source[$table]['columns'][$key];
|
||||||
|
foreach($source[$table]['rows'] as $index => $row) {
|
||||||
|
// fill each row column-wise with re-ordered values
|
||||||
|
$out[$table]['rows'][$index][$order] = $row[$targets[$key]];
|
||||||
}
|
}
|
||||||
$found = array_search($col, array_keys($source[$table]['columns']));
|
|
||||||
$transformations[$found] = $target;
|
|
||||||
$out[$table]['columns'][$col] = $source[$table]['columns'][$col];
|
|
||||||
}
|
|
||||||
foreach($source[$table]['rows'] as $sourceRow) {
|
|
||||||
$newRow = [];
|
|
||||||
foreach($transformations as $from => $to) {
|
|
||||||
$newRow[$to] = $sourceRow[$from];
|
|
||||||
}
|
|
||||||
$out[$table]['rows'][] = $newRow;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
|
|
||||||
function assertResult(array $expected, Result $res) {
|
function assertResult(array $expected, Result $data) {
|
||||||
$exp = $expected;
|
$data = $data->getAll();
|
||||||
$res = $res->getAll();
|
$this->assertCount(sizeof($expected), $data, "Number of result rows (".sizeof($data).") differs from number of expected rows (".sizeof($expected).")");
|
||||||
$this->assertSame(sizeof($exp), sizeof($res), "Number of result rows (".sizeof($res).") differs from number of expected rows (".sizeof($exp).")");
|
if(sizeof($expected)) {
|
||||||
foreach($res as $r) {
|
// make sure the expectations are consistent
|
||||||
$found = false;
|
foreach($expected as $exp) {
|
||||||
foreach($exp as $index => $x) {
|
if(!isset($keys)) {
|
||||||
foreach($x as $field => $value) {
|
$keys = $exp;
|
||||||
$valid = true;
|
continue;
|
||||||
if(!array_key_exists($field, $r) || $r[$field] !== $value) {
|
|
||||||
$valid = false;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
$this->assertSame(array_keys($keys), array_keys($exp), "Result set expectations are irregular");
|
||||||
}
|
}
|
||||||
if($valid) {
|
// filter the result set to contain just the desired keys (we don't care if the result has extra keys)
|
||||||
$found = true;
|
$rows = [];
|
||||||
$this->assertArraySubset($x, $r);
|
foreach($data as $row) {
|
||||||
unset($exp[$index]);
|
$rows[] = array_intersect_key($row, $keys);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
// compare the result set to the expectations
|
||||||
if(!$found) {
|
foreach($expected as $index => $exp) {
|
||||||
$this->assertArraySubset($r, $expected);
|
$this->assertContains($exp, $rows, "Result set does not contain record at array index $index.");
|
||||||
|
$found = array_search($exp, $rows, true);
|
||||||
|
unset($rows[$found]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue