Skip to content

Commit

Permalink
initial bigquery system tests (#227)
Browse files Browse the repository at this point in the history
* initial bigquery system tests

* Fix capitalization of namespace (#233)

* initial pubsub system tests (#230)

* initial datastore system tests (#228)

* initial datastore system tests

* Fix capitalization of namespace (#233)

* initial pubsub system tests (#230)

* initial storage system tests (#231)

* initial storage system tests

* Fix capitalization of namespace (#233)

* initial pubsub system tests (#230)

* initial datastore system tests (#228)

* initial datastore system tests

* Fix capitalization of namespace (#233)

* initial pubsub system tests (#230)

* initial logging system tests (#229)

* initial logging system tests

* Fix capitalization of namespace (#233)

* initial pubsub system tests (#230)

* initial datastore system tests (#228)

* initial datastore system tests

* Fix capitalization of namespace (#233)

* initial pubsub system tests (#230)

* initial storage system tests (#231)

* initial storage system tests

* Fix capitalization of namespace (#233)

* initial pubsub system tests (#230)

* initial datastore system tests (#228)

* initial datastore system tests

* Fix capitalization of namespace (#233)

* initial pubsub system tests (#230)
  • Loading branch information
dwsupplee authored and jdpedrie committed Nov 9, 2016
1 parent ae69516 commit 638443d
Show file tree
Hide file tree
Showing 7 changed files with 580 additions and 0 deletions.
87 changes: 87 additions & 0 deletions tests/system/BigQuery/BigQueryTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Tests\System\BigQuery;

use Google\Cloud\BigQuery\BigQueryClient;
use Google\Cloud\ExponentialBackoff;
use Google\Cloud\Storage\StorageClient;

class BigQueryTestCase extends \PHPUnit_Framework_TestCase
{
const TESTING_PREFIX = 'gcloud_testing_';

protected static $bucket;
protected static $client;
protected static $dataset;
protected static $deletionQueue = [];
protected static $table;
private static $hasSetUp = false;

public static function setUpBeforeClass()
{
if (self::$hasSetUp) {
return;
}

$keyFilePath = getenv('GOOGLE_CLOUD_PHP_TESTS_KEY_PATH');
$schema = [
'fields' => [
[
'name' => 'city',
'type' => 'STRING'
],
[
'name' => 'state',
'type' => 'STRING'
]
]
];
self::$bucket = (new StorageClient([
'keyFilePath' => $keyFilePath
]))->createBucket(uniqid(self::TESTING_PREFIX));
self::$client = new BigQueryClient([
'keyFilePath' => $keyFilePath
]);
self::$dataset = self::$client->createDataset(uniqid(self::TESTING_PREFIX));
self::$table = self::$dataset->createTable(uniqid(self::TESTING_PREFIX), [
'schema' => $schema
]);
self::$hasSetUp = true;
}

public static function tearDownFixtures()
{
if (!self::$hasSetUp) {
return;
}

self::$deletionQueue[] = self::$bucket;
self::$deletionQueue[] = self::$table;
self::$deletionQueue[] = self::$dataset;

$backoff = new ExponentialBackoff(8);

foreach (self::$deletionQueue as $item) {
$backoff->execute(function () use ($item) {
$item->delete();
});
}
}
}


199 changes: 199 additions & 0 deletions tests/system/BigQuery/LoadDataAndQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Tests\System\BigQuery;

use Google\Cloud\ExponentialBackoff;
use GuzzleHttp\Psr7;

class LoadDataAndQueryTest extends BigQueryTestCase
{
private static $expectedRows = 0;

/**
* @dataProvider rowProvider
*/
public function testLoadsDataToTable($data)
{
$job = self::$table->load($data, [
'jobConfig' => [
'sourceFormat' => 'CSV'
]
]);
$backoff = new ExponentialBackoff(8);
$backoff->execute(function () use ($job) {
$job->reload();

if (!$job->isComplete()) {
throw new \Exception();
}
});

if (!$job->isComplete()) {
$this->fail('Job failed to complete within the allotted time.');
}

self::$expectedRows += count(file(__DIR__ . '/../data/table-data.csv'));
$actualRows = count(iterator_to_array(self::$table->rows()));

$this->assertEquals(self::$expectedRows, $actualRows);
}

public function rowProvider()
{
$data = file_get_contents(__DIR__ . '/../data/table-data.csv');

return [
[$data],
[fopen(__DIR__ . '/../data/table-data.csv', 'r')],
[Psr7\stream_for($data)]
];
}

/**
* @depends testLoadsDataToTable
*/
public function testLoadsDataFromStorageToTable()
{
$object = self::$bucket->upload(
fopen(__DIR__ . '/../data/table-data.csv', 'r')
);
self::$deletionQueue[] = $object;

$job = self::$table->loadFromStorage($object, [
'jobConfig' => [
'sourceFormat' => 'CSV'
]
]);
$backoff = new ExponentialBackoff(8);
$backoff->execute(function () use ($job) {
$job->reload();

if (!$job->isComplete()) {
throw new \Exception();
}
});
if (!$job->isComplete()) {
$this->fail('Job failed to complete within the allotted time.');
}

self::$expectedRows += count(file(__DIR__ . '/../data/table-data.csv'));
$actualRows = count(iterator_to_array(self::$table->rows()));

$this->assertEquals(self::$expectedRows, $actualRows);
}

/**
* @depends testLoadsDataFromStorageToTable
*/
public function testInsertRowToTable()
{
self::$expectedRows++;
$insertResponse = self::$table->insertRow([
'city' => 'Detroit',
'state' => 'MI'
]);

$this->assertTrue($insertResponse->isSuccessful());
}

/**
* @depends testInsertRowToTable
*/
public function testInsertRowsToTable()
{
$rows = [
[
'data' => [
'city' => 'Detroit',
'state' => 'MI'
]
],
[
'data' => [
'city' => 'Ann Arbor',
'state' => 'MI'
]
]
];
self::$expectedRows += count($rows);
$insertResponse = self::$table->insertRows($rows);

$this->assertTrue($insertResponse->isSuccessful());
}

/**
* @depends testInsertRowsToTable
*/
public function testRunQuery()
{
$results = self::$client->runQuery(
sprintf(
'SELECT * FROM [%s.%s]',
self::$dataset->id(),
self::$table->id()
)
);
$backoff = new ExponentialBackoff(8);
$backoff->execute(function () use ($results) {
$results->reload();

if (!$results->isComplete()) {
throw new \Exception();
}
});

if (!$results->isComplete()) {
$this->fail('Query did not complete within the allotted time.');
}

$actualRows = count(iterator_to_array($results->rows()));

$this->assertEquals(self::$expectedRows, $actualRows);
}

/**
* @depends testInsertRowsToTable
*/
public function testRunQueryAsJob()
{
$job = self::$client->runQueryAsJob(
sprintf(
'SELECT * FROM [%s.%s]',
self::$dataset->id(),
self::$table->id()
)
);
$results = $job->queryResults();
$backoff = new ExponentialBackoff(8);
$backoff->execute(function () use ($results) {
$results->reload();

if (!$results->isComplete()) {
throw new \Exception();
}
});

if (!$results->isComplete()) {
$this->fail('Query did not complete within the allotted time.');
}

$actualRows = count(iterator_to_array($results->rows()));

$this->assertEquals(self::$expectedRows, $actualRows);
}
}
79 changes: 79 additions & 0 deletions tests/system/BigQuery/ManageDatasetsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Tests\System\BigQuery;

class ManageDatasetsTest extends BigQueryTestCase
{
public function testListDatasets()
{
$foundDatasets = [];
$datasetsToCreate = [
uniqid(self::TESTING_PREFIX),
uniqid(self::TESTING_PREFIX)
];

foreach ($datasetsToCreate as $datasetToCreate) {
self::$deletionQueue[] = self::$client->createDataset($datasetToCreate);
}

$datasets = self::$client->datasets();

foreach ($datasets as $dataset) {
foreach ($datasetsToCreate as $key => $datasetToCreate) {
if ($dataset->id() === $datasetToCreate) {
$foundDatasets[$key] = $dataset->id();
}
}
}

$this->assertEquals($datasetsToCreate, $foundDatasets);
}

public function testCreatesDataset()
{
$id = uniqid(self::TESTING_PREFIX);
$options = [
'friendlyName' => 'Test',
'description' => 'Test'
];
$this->assertFalse(self::$client->dataset($id)->exists());

$dataset = self::$client->createDataset($id, $options);
self::$deletionQueue[] = $dataset;

$this->assertTrue(self::$client->dataset($id)->exists());
$this->assertEquals($id, $dataset->id());
$this->assertEquals($options['friendlyName'], $dataset->info()['friendlyName']);
$this->assertEquals($options['description'], $dataset->info()['description']);
}

public function testUpdateDataset()
{
$metadata = [
'friendlyName' => 'Test'
];
$info = self::$dataset->update($metadata);

$this->assertEquals($metadata['friendlyName'], $info['friendlyName']);
}

public function testReloadsDataset()
{
$this->assertEquals('bigquery#dataset', self::$dataset->reload()['kind']);
}
}
Loading

0 comments on commit 638443d

Please sign in to comment.