Skip to content

Commit 0b98954

Browse files
alixhamibshaffer
authored andcommitted
BigQuery: Update snippets folder name and remove SELECT * tests (GoogleCloudPlatform#713)
1 parent fe390ea commit 0b98954

30 files changed

+33
-37
lines changed

bigquery/api/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ All code in the `snippets` directory demonstrate how to invoke Google BigQuery f
2020
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
2121
Run `php composer.phar install` (if composer is installed locally) or `composer install`
2222
(if composer is installed globally).
23-
5. Run `php snippets/SNIPPET_NAME.php`. The usage will print for each if no arguments
23+
5. Run `php src/SNIPPET_NAME.php`. The usage will print for each if no arguments
2424
are provided:
2525
```sh
26-
$ php snippets/create_dataset.php
27-
Usage: php snippets/create_dataset.php PROJECT_ID DATASET_ID
26+
$ php src/create_dataset.php
27+
Usage: php src/create_dataset.php PROJECT_ID DATASET_ID
2828
29-
$ php snippets/create_dataset.php your-project-id test_dataset_123
29+
$ php src/create_dataset.php your-project-id test_dataset_123
3030
Created dataset test_dataset_123
3131
```
3232

bigquery/api/snippets/bigquery_client.php renamed to bigquery/api/src/bigquery_client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* Usage:
3232
* ```
3333
* $projectId = 'Your Project ID';
34-
* $bigQuery = require 'snippets/bigquery_client.php';
34+
* $bigQuery = require 'src/bigquery_client.php';
3535
* ```
3636
*/
3737
# [START bigquery_client_default_credentials]
File renamed without changes.
File renamed without changes.

bigquery/api/test/bigqueryTest.php

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function setUpBeforeClass()
4848
public function testBigQueryClient()
4949
{
5050
$projectId = self::$projectId;
51-
$bigQuery = require __DIR__ . '/../snippets/bigquery_client.php';
51+
$bigQuery = require __DIR__ . '/../src/bigquery_client.php';
5252

5353
$this->assertInstanceOf(
5454
\Google\Cloud\BigQuery\BigQueryClient::class,
@@ -78,8 +78,9 @@ public function testCopyTable()
7878
$destinationTableId,
7979
]);
8080

81+
$destinationTable = self::$dataset->table($destinationTableId);
8182
$this->assertContains('Table copied successfully', $output);
82-
$this->verifyTempTable($destinationTableId);
83+
$this->verifyTable($destinationTable, 'Brent Shaffer', 3);
8384
}
8485

8586
public function testCreateAndDeleteDataset()
@@ -157,7 +158,7 @@ public function testGetTable()
157158
$projectId = self::$projectId;
158159
$datasetId = self::$datasetId;
159160
$tableId = $this->createTempEmptyTable();
160-
$table = require __DIR__ . '/../snippets/get_table.php';
161+
$table = require __DIR__ . '/../src/get_table.php';
161162

162163
$this->assertInstanceOf(
163164
\Google\Cloud\BigQuery\Table::class,
@@ -179,8 +180,9 @@ public function testImportFromFile()
179180
$source,
180181
]);
181182

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

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

204206
if ($runTruncateSnippet) {
205207
$truncateSnippet = sprintf('%s_truncate', $snippet);
@@ -208,7 +210,7 @@ public function testImportFromStorage($snippet, $runTruncateSnippet = false)
208210
$tableId,
209211
]);
210212
$this->assertContains('Data imported successfully', $output);
211-
$this->verifyStatesTable($table);
213+
$this->verifyTable($table, 'Washington', 50);
212214
}
213215
}
214216

@@ -244,8 +246,9 @@ public function testInsertSql()
244246
$tmpFile,
245247
]);
246248

249+
$tempTable = self::$dataset->table($tempTableId);
247250
$this->assertContains('Data imported successfully', $output);
248-
$this->verifyTempTable($tempTableId);
251+
$this->verifyTable($tempTable, 'Brent Shaffer', 3);
249252
}
250253

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

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

279283
public function testPaginateTable()
@@ -315,7 +319,7 @@ private function runSnippet($sampleName, $params = [])
315319
{
316320
$argv = array_merge([0, self::$projectId], $params);
317321
ob_start();
318-
require __DIR__ . "/../snippets/$sampleName.php";
322+
require __DIR__ . "/../src/$sampleName.php";
319323
return ob_get_clean();
320324
}
321325

@@ -345,31 +349,23 @@ private function createTempTable()
345349
return $tempTableId;
346350
}
347351

348-
private function verifyTempTable($tempTableId)
352+
private function verifyTable($table, $expectedValue, $expectedRowCount)
349353
{
350-
$query = sprintf('SELECT * FROM `%s.%s`', self::$datasetId, $tempTableId);
351-
$testFunction = function () use ($query) {
352-
$output = $this->runSnippet('run_query', [$query]);
353-
$this->assertContains('Brent Shaffer', $output);
354-
};
355-
356-
$this->runEventuallyConsistentTest($testFunction);
357-
}
358-
359-
private function verifyStatesTable($table)
360-
{
361-
$numRows = 0;
362-
$foundValue = false;
363-
foreach ($table->rows([]) as $row) {
364-
foreach ($row as $column => $value) {
365-
if ($value == 'Washington') {
366-
$foundValue = true;
354+
$testFunction = function () use ($table, $expectedValue, $expectedRowCount) {
355+
$numRows = 0;
356+
$foundValue = false;
357+
foreach ($table->rows([]) as $row) {
358+
foreach ($row as $column => $value) {
359+
if ($value == $expectedValue) {
360+
$foundValue = true;
361+
}
367362
}
363+
$numRows++;
368364
}
369-
$numRows++;
370-
}
371-
$this->assertTrue($foundValue);
372-
$this->assertEquals($numRows, 50);
365+
$this->assertTrue($foundValue);
366+
$this->assertEquals($numRows, $expectedRowCount);
367+
};
368+
$this->runEventuallyConsistentTest($testFunction);
373369
}
374370

375371
public function tearDown()

bigquery/api/test/data/test_data.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- This file is used to test ../../snippets/insert_sql.php
1+
-- This file is used to test ../../src/insert_sql.php
22
-- These are comments.
33
-- Each query to be executed should be on a single line.
44

0 commit comments

Comments
 (0)