Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bigquery/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ All code in the `snippets` directory demonstrate how to invoke Google BigQuery f
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
Run `php composer.phar install` (if composer is installed locally) or `composer install`
(if composer is installed globally).
5. Run `php snippets/SNIPPET_NAME.php`. The usage will print for each if no arguments
5. Run `php src/SNIPPET_NAME.php`. The usage will print for each if no arguments
are provided:
```sh
$ php snippets/create_dataset.php
Usage: php snippets/create_dataset.php PROJECT_ID DATASET_ID
$ php src/create_dataset.php
Usage: php src/create_dataset.php PROJECT_ID DATASET_ID

$ php snippets/create_dataset.php your-project-id test_dataset_123
$ php src/create_dataset.php your-project-id test_dataset_123
Created dataset test_dataset_123
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* Usage:
* ```
* $projectId = 'Your Project ID';
* $bigQuery = require 'snippets/bigquery_client.php';
* $bigQuery = require 'src/bigquery_client.php';
* ```
*/
# [START bigquery_client_default_credentials]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
58 changes: 27 additions & 31 deletions bigquery/api/test/bigqueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static function setUpBeforeClass()
public function testBigQueryClient()
{
$projectId = self::$projectId;
$bigQuery = require __DIR__ . '/../snippets/bigquery_client.php';
$bigQuery = require __DIR__ . '/../src/bigquery_client.php';

$this->assertInstanceOf(
\Google\Cloud\BigQuery\BigQueryClient::class,
Expand Down Expand Up @@ -78,8 +78,9 @@ public function testCopyTable()
$destinationTableId,
]);

$destinationTable = self::$dataset->table($destinationTableId);
$this->assertContains('Table copied successfully', $output);
$this->verifyTempTable($destinationTableId);
$this->verifyTable($destinationTable, 'Brent Shaffer', 3);
}

public function testCreateAndDeleteDataset()
Expand Down Expand Up @@ -157,7 +158,7 @@ public function testGetTable()
$projectId = self::$projectId;
$datasetId = self::$datasetId;
$tableId = $this->createTempEmptyTable();
$table = require __DIR__ . '/../snippets/get_table.php';
$table = require __DIR__ . '/../src/get_table.php';

$this->assertInstanceOf(
\Google\Cloud\BigQuery\Table::class,
Expand All @@ -179,8 +180,9 @@ public function testImportFromFile()
$source,
]);

$tempTable = self::$dataset->table($tempTableId);
$this->assertContains('Data imported successfully', $output);
$this->verifyTempTable($tempTableId);
$this->verifyTable($tempTable, 'Brent Shaffer', 3);
}

/**
Expand All @@ -199,7 +201,7 @@ public function testImportFromStorage($snippet, $runTruncateSnippet = false)
// verify table contents
$table = self::$dataset->table($tableId);
self::$tempTables[] = $table;
$this->verifyStatesTable($table);
$this->verifyTable($table, 'Washington', 50);

if ($runTruncateSnippet) {
$truncateSnippet = sprintf('%s_truncate', $snippet);
Expand All @@ -208,7 +210,7 @@ public function testImportFromStorage($snippet, $runTruncateSnippet = false)
$tableId,
]);
$this->assertContains('Data imported successfully', $output);
$this->verifyStatesTable($table);
$this->verifyTable($table, 'Washington', 50);
}
}

Expand Down Expand Up @@ -244,8 +246,9 @@ public function testInsertSql()
$tmpFile,
]);

$tempTable = self::$dataset->table($tempTableId);
$this->assertContains('Data imported successfully', $output);
$this->verifyTempTable($tempTableId);
$this->verifyTable($tempTable, 'Brent Shaffer', 3);
}

public function testListDatasets()
Expand All @@ -272,8 +275,9 @@ public function testStreamRow()
json_encode(['name' => 'Brent Shaffer', 'title' => 'Developer'])
]);

$tempTable = self::$dataset->table($tempTableId);
$this->assertcontains('Data streamed into BigQuery successfully', $output);
$this->verifyTempTable($tempTableId);
$this->verifyTable($tempTable, 'Brent Shaffer', 1);
}

public function testPaginateTable()
Expand Down Expand Up @@ -315,7 +319,7 @@ private function runSnippet($sampleName, $params = [])
{
$argv = array_merge([0, self::$projectId], $params);
ob_start();
require __DIR__ . "/../snippets/$sampleName.php";
require __DIR__ . "/../src/$sampleName.php";
return ob_get_clean();
}

Expand Down Expand Up @@ -345,31 +349,23 @@ private function createTempTable()
return $tempTableId;
}

private function verifyTempTable($tempTableId)
private function verifyTable($table, $expectedValue, $expectedRowCount)
{
$query = sprintf('SELECT * FROM `%s.%s`', self::$datasetId, $tempTableId);
$testFunction = function () use ($query) {
$output = $this->runSnippet('run_query', [$query]);
$this->assertContains('Brent Shaffer', $output);
};

$this->runEventuallyConsistentTest($testFunction);
}

private function verifyStatesTable($table)
{
$numRows = 0;
$foundValue = false;
foreach ($table->rows([]) as $row) {
foreach ($row as $column => $value) {
if ($value == 'Washington') {
$foundValue = true;
$testFunction = function () use ($table, $expectedValue, $expectedRowCount) {
$numRows = 0;
$foundValue = false;
foreach ($table->rows([]) as $row) {
foreach ($row as $column => $value) {
if ($value == $expectedValue) {
$foundValue = true;
}
}
$numRows++;
}
$numRows++;
}
$this->assertTrue($foundValue);
$this->assertEquals($numRows, 50);
$this->assertTrue($foundValue);
$this->assertEquals($numRows, $expectedRowCount);
};
$this->runEventuallyConsistentTest($testFunction);
}

public function tearDown()
Expand Down
2 changes: 1 addition & 1 deletion bigquery/api/test/data/test_data.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- This file is used to test ../../snippets/insert_sql.php
-- This file is used to test ../../src/insert_sql.php
-- These are comments.
-- Each query to be executed should be on a single line.

Expand Down