Skip to content

Commit 58edff8

Browse files
committed
Test
- listViews - test if call list table on nonexisting dataset - test table exist and throw exception
1 parent 729f6aa commit 58edff8

4 files changed

Lines changed: 76 additions & 7 deletions

File tree

src/Schema/Bigquery/BigquerySchemaReflection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function getTablesNames(): array
3030
{
3131
$isDatasetExist = $this->bqClient->dataset($this->datasetName)->exists();
3232
if ($isDatasetExist === false) {
33-
throw new ReflectionException(sprintf('Dataset "%s" not found', $this->datasetName));
33+
throw new ReflectionException(sprintf('Dataset "%s" not found.', $this->datasetName));
3434
}
3535
$query = $this->bqClient->query(
3636
sprintf(

src/Table/Bigquery/BigqueryTableReflection.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(BigQueryClient $bqClient, string $datasetName, strin
3636
public function getColumnsNames(): array
3737
{
3838
if ($this->exists() === false) {
39-
throw new TableNotExistsReflectionException(sprintf('Table "%s" not found', $this->tableName));
39+
throw new TableNotExistsReflectionException(sprintf('Table "%s" not found.', $this->tableName));
4040
}
4141
$query = $this->bqClient->query(
4242
sprintf(
@@ -75,7 +75,7 @@ public function getColumnsNames(): array
7575
public function getColumnsDefinitions(): ColumnCollection
7676
{
7777
if ($this->exists() === false) {
78-
throw new TableNotExistsReflectionException(sprintf('Table "%s" not found', $this->tableName));
78+
throw new TableNotExistsReflectionException(sprintf('Table "%s" not found.', $this->tableName));
7979
}
8080
$query = $this->bqClient->query(
8181
sprintf(
@@ -117,7 +117,7 @@ public function getColumnsDefinitions(): ColumnCollection
117117
public function getRowsCount(): int
118118
{
119119
if ($this->exists() === false) {
120-
throw new TableNotExistsReflectionException(sprintf('Table "%s" not found', $this->tableName));
120+
throw new TableNotExistsReflectionException(sprintf('Table "%s" not found.', $this->tableName));
121121
}
122122
$query = $this->bqClient->query(sprintf(
123123
'SELECT COUNT(*) AS NumberOfRows FROM %s.%s',
@@ -140,9 +140,6 @@ public function getPrimaryKeysNames(): array
140140

141141
public function getTableStats(): TableStatsInterface
142142
{
143-
if ($this->exists() === false) {
144-
throw new TableNotExistsReflectionException(sprintf('Table "%s" not found', $this->tableName));
145-
}
146143
$sql = sprintf(
147144
'SELECT size_bytes FROM %s.__TABLES__ WHERE table_id=%s',
148145
BigqueryQuote::quoteSingleIdentifier($this->datasetName),

tests/Functional/Bigquery/Schema/BigquerySchemaReflectionTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Generator;
88
use Keboola\TableBackendUtils\Column\Bigquery\BigqueryColumn;
99
use Keboola\TableBackendUtils\Column\ColumnCollection;
10+
use Keboola\TableBackendUtils\ReflectionException;
1011
use Keboola\TableBackendUtils\Schema\Bigquery\BigquerySchemaReflection;
1112
use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableDefinition;
1213
use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableQueryBuilder;
@@ -35,6 +36,10 @@ public function testListTables(): void
3536
$dbName = $this->getDatasetName();
3637
$sql = <<<EOT
3738
CREATE TABLE $dbName.`nopitable` (`amount` STRING (32000));
39+
EOT;
40+
$this->bqClient->runQuery($this->bqClient->query($sql));
41+
$sql = <<<EOT
42+
CREATE VIEW $dbName.`nopiview` AS SELECT * FROM $dbName.`nopitable`;
3843
EOT;
3944
$this->bqClient->runQuery($this->bqClient->query($sql));
4045
$expectedTables = [self::TABLE_GENERIC, 'nopitable'];
@@ -43,6 +48,35 @@ public function testListTables(): void
4348
$this->assertCount(0, array_diff($actualTables, $expectedTables));
4449
}
4550

51+
public function testListViews(): void
52+
{
53+
$this->initTable($this->getDatasetName(), self::TABLE_GENERIC, false);
54+
$dbName = $this->getDatasetName();
55+
$sql = <<<EOT
56+
CREATE TABLE $dbName.`nopitable` (`amount` STRING (32000));
57+
EOT;
58+
$this->bqClient->runQuery($this->bqClient->query($sql));
59+
$sql = <<<EOT
60+
CREATE VIEW $dbName.`nopiview` AS SELECT * FROM $dbName.`nopitable`;
61+
EOT;
62+
$this->bqClient->runQuery($this->bqClient->query($sql));
63+
$expectedTables = ['nopiview'];
64+
$actualTables = $this->schemaRef->getViewsNames();
65+
$this->assertCount(0, array_diff($expectedTables, $actualTables));
66+
$this->assertCount(0, array_diff($actualTables, $expectedTables));
67+
}
68+
69+
70+
public function testListTablesOnNonExistingDatasetThrowException(): void
71+
{
72+
$this->initTable($this->getDatasetName(), self::TABLE_GENERIC, false);
73+
74+
$this->expectException(ReflectionException::class);
75+
$this->expectExceptionMessage('Dataset "notExisting" not found.');
76+
$schemaRef = new BigquerySchemaReflection($this->bqClient, 'notExisting');
77+
$schemaRef->getTablesNames();
78+
}
79+
4680
/**
4781
* @dataProvider createTableTestFromDefinitionSqlProvider
4882
*/

tests/Functional/Bigquery/Table/BigqueryTableReflectionTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Keboola\TableBackendUtils\Escaping\Bigquery\BigqueryQuote;
1111
use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableReflection;
1212
use Keboola\TableBackendUtils\Table\TableStats;
13+
use Keboola\TableBackendUtils\TableNotExistsReflectionException;
1314
use Tests\Keboola\TableBackendUtils\Functional\Bigquery\BigqueryBaseCase;
1415

1516
class BigqueryTableReflectionTest extends BigqueryBaseCase
@@ -287,4 +288,41 @@ public function testGetRowsCount(): void
287288
}
288289
self::assertEquals(2, $ref->getRowsCount());
289290
}
291+
292+
public function testIfTableExists(): void
293+
{
294+
$this->initTable();
295+
296+
$ref = new BigqueryTableReflection($this->bqClient, self::TEST_SCHEMA, self::TABLE_GENERIC);
297+
self::assertTrue($ref->exists());
298+
}
299+
300+
public function testIfTableDoesNotExists(): void
301+
{
302+
$this->initTable();
303+
304+
$ref = new BigqueryTableReflection($this->bqClient, self::TEST_SCHEMA, 'notExisting');
305+
self::assertFalse($ref->exists());
306+
307+
try {
308+
$ref->getColumnsNames();
309+
$this->fail('Should failed!');
310+
} catch (TableNotExistsReflectionException $e) {
311+
$this->assertSame('Table "notExisting" not found.', $e->getMessage());
312+
}
313+
314+
try {
315+
$ref->getColumnsDefinitions();
316+
$this->fail('Should failed!');
317+
} catch (TableNotExistsReflectionException $e) {
318+
$this->assertSame('Table "notExisting" not found.', $e->getMessage());
319+
}
320+
321+
try {
322+
$ref->getRowsCount();
323+
$this->fail('Should failed!');
324+
} catch (TableNotExistsReflectionException $e) {
325+
$this->assertSame('Table "notExisting" not found.', $e->getMessage());
326+
}
327+
}
290328
}

0 commit comments

Comments
 (0)