diff --git a/bigquery/api/README.md b/bigquery/api/README.md index 85bcf2c6a7..d5aabba682 100644 --- a/bigquery/api/README.md +++ b/bigquery/api/README.md @@ -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 ``` diff --git a/bigquery/api/snippets/bigquery_client.php b/bigquery/api/src/bigquery_client.php similarity index 95% rename from bigquery/api/snippets/bigquery_client.php rename to bigquery/api/src/bigquery_client.php index beb0a74bb4..9d63dec148 100644 --- a/bigquery/api/snippets/bigquery_client.php +++ b/bigquery/api/src/bigquery_client.php @@ -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] diff --git a/bigquery/api/snippets/browse_table.php b/bigquery/api/src/browse_table.php similarity index 100% rename from bigquery/api/snippets/browse_table.php rename to bigquery/api/src/browse_table.php diff --git a/bigquery/api/snippets/copy_table.php b/bigquery/api/src/copy_table.php similarity index 100% rename from bigquery/api/snippets/copy_table.php rename to bigquery/api/src/copy_table.php diff --git a/bigquery/api/snippets/create_dataset.php b/bigquery/api/src/create_dataset.php similarity index 100% rename from bigquery/api/snippets/create_dataset.php rename to bigquery/api/src/create_dataset.php diff --git a/bigquery/api/snippets/create_table.php b/bigquery/api/src/create_table.php similarity index 100% rename from bigquery/api/snippets/create_table.php rename to bigquery/api/src/create_table.php diff --git a/bigquery/api/snippets/delete_dataset.php b/bigquery/api/src/delete_dataset.php similarity index 100% rename from bigquery/api/snippets/delete_dataset.php rename to bigquery/api/src/delete_dataset.php diff --git a/bigquery/api/snippets/delete_table.php b/bigquery/api/src/delete_table.php similarity index 100% rename from bigquery/api/snippets/delete_table.php rename to bigquery/api/src/delete_table.php diff --git a/bigquery/api/snippets/extract_table.php b/bigquery/api/src/extract_table.php similarity index 100% rename from bigquery/api/snippets/extract_table.php rename to bigquery/api/src/extract_table.php diff --git a/bigquery/api/snippets/get_table.php b/bigquery/api/src/get_table.php similarity index 100% rename from bigquery/api/snippets/get_table.php rename to bigquery/api/src/get_table.php diff --git a/bigquery/api/snippets/import_from_local_csv.php b/bigquery/api/src/import_from_local_csv.php similarity index 100% rename from bigquery/api/snippets/import_from_local_csv.php rename to bigquery/api/src/import_from_local_csv.php diff --git a/bigquery/api/snippets/import_from_storage_csv.php b/bigquery/api/src/import_from_storage_csv.php similarity index 100% rename from bigquery/api/snippets/import_from_storage_csv.php rename to bigquery/api/src/import_from_storage_csv.php diff --git a/bigquery/api/snippets/import_from_storage_csv_autodetect.php b/bigquery/api/src/import_from_storage_csv_autodetect.php similarity index 100% rename from bigquery/api/snippets/import_from_storage_csv_autodetect.php rename to bigquery/api/src/import_from_storage_csv_autodetect.php diff --git a/bigquery/api/snippets/import_from_storage_csv_truncate.php b/bigquery/api/src/import_from_storage_csv_truncate.php similarity index 100% rename from bigquery/api/snippets/import_from_storage_csv_truncate.php rename to bigquery/api/src/import_from_storage_csv_truncate.php diff --git a/bigquery/api/snippets/import_from_storage_json.php b/bigquery/api/src/import_from_storage_json.php similarity index 100% rename from bigquery/api/snippets/import_from_storage_json.php rename to bigquery/api/src/import_from_storage_json.php diff --git a/bigquery/api/snippets/import_from_storage_json_autodetect.php b/bigquery/api/src/import_from_storage_json_autodetect.php similarity index 100% rename from bigquery/api/snippets/import_from_storage_json_autodetect.php rename to bigquery/api/src/import_from_storage_json_autodetect.php diff --git a/bigquery/api/snippets/import_from_storage_json_truncate.php b/bigquery/api/src/import_from_storage_json_truncate.php similarity index 100% rename from bigquery/api/snippets/import_from_storage_json_truncate.php rename to bigquery/api/src/import_from_storage_json_truncate.php diff --git a/bigquery/api/snippets/import_from_storage_orc.php b/bigquery/api/src/import_from_storage_orc.php similarity index 100% rename from bigquery/api/snippets/import_from_storage_orc.php rename to bigquery/api/src/import_from_storage_orc.php diff --git a/bigquery/api/snippets/import_from_storage_orc_truncate.php b/bigquery/api/src/import_from_storage_orc_truncate.php similarity index 100% rename from bigquery/api/snippets/import_from_storage_orc_truncate.php rename to bigquery/api/src/import_from_storage_orc_truncate.php diff --git a/bigquery/api/snippets/import_from_storage_parquet.php b/bigquery/api/src/import_from_storage_parquet.php similarity index 100% rename from bigquery/api/snippets/import_from_storage_parquet.php rename to bigquery/api/src/import_from_storage_parquet.php diff --git a/bigquery/api/snippets/import_from_storage_parquet_truncate.php b/bigquery/api/src/import_from_storage_parquet_truncate.php similarity index 100% rename from bigquery/api/snippets/import_from_storage_parquet_truncate.php rename to bigquery/api/src/import_from_storage_parquet_truncate.php diff --git a/bigquery/api/snippets/insert_sql.php b/bigquery/api/src/insert_sql.php similarity index 100% rename from bigquery/api/snippets/insert_sql.php rename to bigquery/api/src/insert_sql.php diff --git a/bigquery/api/snippets/list_datasets.php b/bigquery/api/src/list_datasets.php similarity index 100% rename from bigquery/api/snippets/list_datasets.php rename to bigquery/api/src/list_datasets.php diff --git a/bigquery/api/snippets/list_tables.php b/bigquery/api/src/list_tables.php similarity index 100% rename from bigquery/api/snippets/list_tables.php rename to bigquery/api/src/list_tables.php diff --git a/bigquery/api/snippets/paginate_table.php b/bigquery/api/src/paginate_table.php similarity index 100% rename from bigquery/api/snippets/paginate_table.php rename to bigquery/api/src/paginate_table.php diff --git a/bigquery/api/snippets/run_query.php b/bigquery/api/src/run_query.php similarity index 100% rename from bigquery/api/snippets/run_query.php rename to bigquery/api/src/run_query.php diff --git a/bigquery/api/snippets/run_query_as_job.php b/bigquery/api/src/run_query_as_job.php similarity index 100% rename from bigquery/api/snippets/run_query_as_job.php rename to bigquery/api/src/run_query_as_job.php diff --git a/bigquery/api/snippets/stream_row.php b/bigquery/api/src/stream_row.php similarity index 100% rename from bigquery/api/snippets/stream_row.php rename to bigquery/api/src/stream_row.php diff --git a/bigquery/api/test/bigqueryTest.php b/bigquery/api/test/bigqueryTest.php index 9b2aa5eac5..edaaba4b9b 100644 --- a/bigquery/api/test/bigqueryTest.php +++ b/bigquery/api/test/bigqueryTest.php @@ -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, @@ -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() @@ -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, @@ -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); } /** @@ -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); @@ -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); } } @@ -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() @@ -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() @@ -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(); } @@ -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() diff --git a/bigquery/api/test/data/test_data.sql b/bigquery/api/test/data/test_data.sql index cc03b155e0..8611ddc070 100644 --- a/bigquery/api/test/data/test_data.sql +++ b/bigquery/api/test/data/test_data.sql @@ -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.