Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkasemmler committed Jan 28, 2025
1 parent d070936 commit b786a1c
Showing 1 changed file with 121 additions and 53 deletions.
174 changes: 121 additions & 53 deletions tests/Backend/CommonPart1/DeleteRowsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,13 @@ public function tableDeleteRowsByFiltersData(): array
],
],
],
'where filter: valuesByTableInWorkspace' => [
'where filter: valuesByTableInStorage' => [
[
'whereFilters' => [
[
'column' => 'city',
'valuesByTableInWorkspace' => [
'workspaceId' => 123,
'table' => 'table',
'valuesByTableInStorage' => [
'tableId' => 'table',
'column' => 'city',
],
],
Expand Down Expand Up @@ -368,56 +367,99 @@ public function tableDeleteRowsByFiltersData(): array
],
],
],
'where filter: valuesByTableInStorage' => [
[
'whereFilters' => [
[
'column' => 'city',
'valuesByTableInStorage' => [
'tableId' => 'table',
'column' => 'city',
],
];
}

public function testDeleteByValuesInWorkspaceWithInvalidData(): void
{
$importFile = __DIR__ . '/../../_data/users.csv';
$tableId = $this->_client->createTableAsync($this->getTestBucketId(self::STAGE_IN), 'users', new CsvFile($importFile));

$workspaces = new Workspaces($this->_client);

foreach ($workspaces->listWorkspaces() as $workspace) {
$workspaces->deleteWorkspace($workspace['id'], [], true);
}

$runId = $this->_client->generateRunId();
$this->_client->setRunId($runId);
$this->_client->setRunId($runId);

$workspace = $workspaces->createWorkspace();
$backend = WorkspaceBackendFactory::createWorkspaceBackend($workspace);
$backend->createTable('USERS', ['ID' => 'INT', 'name' => 'VARCHAR(255)']);

$backend->executeQuery("INSERT INTO USERS VALUES (1, 'martin');");
$backend->executeQuery("INSERT INTO USERS VALUES (3, 'ondra');");

// test invalid table
try {
$this->_client->deleteTableRows($tableId, [
'whereFilters' => [
[
'column' => 'id',
'valuesByTableInWorkspace' => [
'workspaceId' => $workspace['id'],
'table' => 'NOTEXISTING',
'column' => 'ID',
],
],
],
// no rows should be deleted because valuesByTableInStorage doesn't do anything yet
[
[
'1',
'martin',
'PRG',
'male',
],
[
'2',
'klara',
'PRG',
'female',
],
[
'3',
'ondra',
'VAN',
'male',
],
]);
$this->fail('Should fail because table does not exist');
} catch (ClientException $e) {
$this->assertSame(
sprintf(
'Table "NOTEXISTING" not found in schema "%s"',
$workspace['connection']['schema'],
),
$e->getMessage(),
);
$this->assertSame('storage.tableNotFound', $e->getStringCode());
}

// test type of column (storage is type of VARCHAR)
try {
$this->_client->deleteTableRows($tableId, [
'whereFilters' => [
[
'4',
'miro',
'BRA',
'male',
'column' => 'id',
'valuesByTableInWorkspace' => [
'workspaceId' => $workspace['id'],
'table' => 'USERS',
'column' => 'ID',
],
],
],
]);
$this->fail('Should fail because of invalid column type');
} catch (ClientException $e) {
$this->assertSame('Cannot use column "ID" to delete by. Column types do not match. Type is "NUMBER" but expected type is "VARCHAR".', $e->getMessage());
$this->assertSame('storage.tables.validation.invalidColumnToDeleteBy', $e->getStringCode());
}

// test non-existing column
try {
$this->_client->deleteTableRows($tableId, [
'whereFilters' => [
[
'5',
'hidden',
'',
'male',
'column' => 'id',
'valuesByTableInWorkspace' => [
'workspaceId' => $workspace['id'],
'table' => 'USERS',
'column' => 'NOTEXISTING',
],
],
],
],
];
]);
$this->fail('Should fail because of column does not exist');
} catch (ClientException $e) {
$this->assertSame('Cannot use column "NOTEXISTING" to delete by. Column does not exist.', $e->getMessage());
$this->assertSame('storage.tables.validation.invalidColumnToDeleteBy', $e->getStringCode());
}
}

public function testDeleteByValuesInWorkspace()
public function testDeleteByValuesInWorkspaceWithValidData(): void
{
$importFile = __DIR__ . '/../../_data/users.csv';
$tableId = $this->_client->createTableAsync($this->getTestBucketId(self::STAGE_IN), 'users', new CsvFile($importFile));
Expand All @@ -434,12 +476,28 @@ public function testDeleteByValuesInWorkspace()

$workspace = $workspaces->createWorkspace();
$backend = WorkspaceBackendFactory::createWorkspaceBackend($workspace);
$backend->createTable('USERS', ['ID' => 'INT', 'name' => 'VARCHAR(255)']);
$backend->createTable('USERS', ['ID' => 'VARCHAR', 'name' => 'VARCHAR(255)']);

$backend->executeQuery("INSERT INTO USERS VALUES (1, 'martin');");
$backend->executeQuery("INSERT INTO USERS VALUES (3, 'ondra');");
// WS table is empty - should pass and not delete anything
$result = $this->_client->deleteTableRows($tableId, [
'whereFilters' => [
[
'column' => 'id',
'valuesByTableInWorkspace' => [
'workspaceId' => $workspace['id'],
'table' => 'USERS',
'column' => 'ID',
],
],
],
]);
assert(is_array($result));
assert(array_key_exists('deletedRows', $result));
$this->assertEquals(0, $result['deletedRows']);

$this->_client->deleteTableRows($tableId, [
// there is one row in WS table - should delete one row
$backend->executeQuery("INSERT INTO USERS VALUES ('3', 'ondra');");
$result = $this->_client->deleteTableRows($tableId, [
'whereFilters' => [
[
'column' => 'id',
Expand All @@ -451,8 +509,22 @@ public function testDeleteByValuesInWorkspace()
],
],
]);
// 1 and 3 should be deleted
assert(is_array($result));
assert(array_key_exists('deletedRows', $result));
$this->assertEquals(1, $result['deletedRows']);

$data = $this->_client->getTableDataPreview($tableId);

$parsedData = Client::parseCsv($data, false);
array_shift($parsedData); // remove header

$expectedTableContent = [
[
'1',
'martin',
'PRG',
'male',
],
[
'2',
'klara',
Expand All @@ -472,10 +544,6 @@ public function testDeleteByValuesInWorkspace()
'male',
],
];
$dataPreview = $this->_client->getTableDataPreview($tableId);
$parsedData = Client::parseCsv($dataPreview, false);
array_shift($parsedData); // remove header

$this->assertArrayEqualsSorted($expectedTableContent, $parsedData, 0);
}
}

0 comments on commit b786a1c

Please sign in to comment.