Skip to content
Closed
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@
"entry": "ServiceBuilder.php"
}
}
}
}
4 changes: 2 additions & 2 deletions src/BigQuery/BigQueryClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Google\Cloud\BigQuery\Connection\ConnectionInterface;
use Google\Cloud\BigQuery\Connection\Rest;
use Google\Cloud\BigQuery\Job;
use Google\Cloud\Core\ArrayTrait;
use Google\Cloud\Core\ClientTrait;
use Google\Cloud\Core\ExponentialBackoff;
use Google\Cloud\Core\Int64;
Expand All @@ -43,7 +42,6 @@
*/
class BigQueryClient
{
use ArrayTrait;
use ClientTrait;
use JobConfigurationTrait;

Expand Down Expand Up @@ -291,6 +289,8 @@ public function runQuery($query, array $options = [])
* @type array $jobConfig Configuration settings for a query job are
* outlined in the [API Docs for `configuration.query`](https://goo.gl/PuRa3I).
* If not provided default settings will be used.
* @type string $jobIdPrefix [optional] If given, the returned job ID will
* be of format `{$jobIdPrefix-}{jobId}`. **Defaults to** `null`.
* }
* @return Job
*/
Expand Down
29 changes: 29 additions & 0 deletions src/BigQuery/JobConfigurationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@

namespace Google\Cloud\BigQuery;

use Google\Cloud\Core\ArrayTrait;
use Ramsey\Uuid\Uuid;

/**
* A trait used to build out configuration for jobs.
*/
trait JobConfigurationTrait
{
use ArrayTrait;

/**
* Builds a configuration for a job.
*
Expand All @@ -33,6 +38,8 @@ trait JobConfigurationTrait
*/
public function buildJobConfig($name, $projectId, array $config, array $userDefinedOptions)
{
$jobIdPrefix = $this->pluck('jobIdPrefix', $userDefinedOptions, false);

if (isset($userDefinedOptions['jobConfig'])) {
$config = $userDefinedOptions['jobConfig'] + $config;
}
Expand All @@ -41,9 +48,31 @@ public function buildJobConfig($name, $projectId, array $config, array $userDefi

return [
'projectId' => $projectId,
'jobReference' => [
'projectId' => $projectId,
'jobId' => $this->generateJobId($jobIdPrefix)
],
'configuration' => [
$name => $config
]
] + $userDefinedOptions;
}

/**
* Generate a Job ID with an optional user-defined prefix.
*
* @param string $jobIdPrefix [optional] If given, the returned job ID will
* be of format `{$jobIdPrefix-}{jobId}`. **Defaults to** `null`.
* @return string
*/
protected function generateJobId($jobIdPrefix = null)
{
$jobId = '';

if ($jobIdPrefix) {
$jobId = $jobIdPrefix . '-';
}

return $jobId . Uuid::uuid4();
}
}
8 changes: 6 additions & 2 deletions src/BigQuery/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
namespace Google\Cloud\BigQuery;

use Google\Cloud\BigQuery\Connection\ConnectionInterface;
use Google\Cloud\Core\ArrayTrait;
use Google\Cloud\Core\ConcurrencyControlTrait;
use Google\Cloud\Core\Exception\NotFoundException;
use Google\Cloud\Core\Iterator\ItemIterator;
Expand All @@ -33,7 +32,6 @@
*/
class Table
{
use ArrayTrait;
use ConcurrencyControlTrait;
use JobConfigurationTrait;

Expand Down Expand Up @@ -239,6 +237,8 @@ function (array $row) use ($schema) {
* @type array $jobConfig Configuration settings for a copy job are
* outlined in the [API Docs for `configuration.copy`](https://goo.gl/m8dro9).
* If not provided default settings will be used.
* @type string $jobIdPrefix [optional] If given, the returned job ID will
* be of format `{$jobIdPrefix-}{jobId}`. **Defaults to** `null`.
* }
* @return Job
*/
Expand Down Expand Up @@ -287,6 +287,8 @@ public function copy(Table $destination, array $options = [])
* @type array $jobConfig Configuration settings for an extract job are
* outlined in the [API Docs for `configuration.extract`](https://goo.gl/SQ9XAE).
* If not provided default settings will be used.
* @type string $jobIdPrefix [optional] If given, the returned job ID will
* be of format `{$jobIdPrefix-}{jobId}`. **Defaults to** `null`.
* }
* @return Job
*/
Expand Down Expand Up @@ -334,6 +336,8 @@ public function export($destination, array $options = [])
* @type array $jobConfig Configuration settings for a load job are
* outlined in the [API Docs for `configuration.load`](https://goo.gl/j6jyHv).
* If not provided default settings will be used.
* @type string $jobIdPrefix [optional] If given, the returned job ID will
* be of format `{$jobIdPrefix-}{jobId}`. **Defaults to** `null`.
* }
* @return Job
*/
Expand Down
3 changes: 2 additions & 1 deletion src/BigQuery/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"license": "Apache-2.0",
"minimum-stability": "stable",
"require": {
"google/cloud-core": "^1.0"
"google/cloud-core": "^1.0",
"ramsey/uuid": "~3"
},
"suggest": {
"google/cloud-storage": "Makes it easier to load data from Cloud Storage into BigQuery"
Expand Down
62 changes: 34 additions & 28 deletions tests/unit/BigQuery/BigQueryClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@
*/
class BigQueryClientTest extends \PHPUnit_Framework_TestCase
{
const JOBID = 'myJobId';

public $connection;
public $jobId = 'myJobId';
public $projectId = 'myProjectId';
public $datasetId = 'myDatasetId';
public $client;

public function setUp()
{
$this->connection = $this->prophesize(ConnectionInterface::class);
$this->client = new BigQueryTestClient(['projectId' => $this->projectId]);
$this->client = \Google\Cloud\Dev\stub(BigQueryTestClient::class, ['options' => ['projectId' => $this->projectId]]);
}

/**
Expand All @@ -54,16 +55,16 @@ public function testRunsQuery($query, $options, $expected)
$this->connection->query($expected)
->willReturn([
'jobReference' => [
'jobId' => $this->jobId
'jobId' => self::JOBID
],
'jobComplete' => true
])
->shouldBeCalledTimes(1);
$this->client->setConnection($this->connection->reveal());
$this->client->___setProperty('connection', $this->connection->reveal());
$queryResults = $this->client->runQuery($query, $options);

$this->assertInstanceOf(QueryResults::class, $queryResults);
$this->assertEquals($this->jobId, $queryResults->identity()['jobId']);
$this->assertEquals(self::JOBID, $queryResults->identity()['jobId']);
}

/**
Expand All @@ -74,7 +75,7 @@ public function testRunsQueryWithRetry($query, $options, $expected)
$this->connection->query($expected)
->willReturn([
'jobReference' => [
'jobId' => $this->jobId
'jobId' => self::JOBID
],
'jobComplete' => false
])
Expand All @@ -83,17 +84,17 @@ public function testRunsQueryWithRetry($query, $options, $expected)
$this->connection->getQueryResults(Argument::any())
->willReturn([
'jobReference' => [
'jobId' => $this->jobId
'jobId' => self::JOBID
],
'jobComplete' => true
])
->shouldBeCalledTimes(1);

$this->client->setConnection($this->connection->reveal());
$this->client->___setProperty('connection', $this->connection->reveal());
$queryResults = $this->client->runQuery($query, $options);

$this->assertInstanceOf(QueryResults::class, $queryResults);
$this->assertEquals($this->jobId, $queryResults->identity()['jobId']);
$this->assertEquals(self::JOBID, $queryResults->identity()['jobId']);
}

/**
Expand All @@ -107,17 +108,22 @@ public function testRunsQueryAsJob($query, $options, $expected)
'projectId' => $projectId,
'configuration' => [
'query' => $expected
],
'jobReference' => [
'projectId' => $projectId,
'jobId' => self::JOBID
]
])
->willReturn([
'jobReference' => ['jobId' => $this->jobId]
'jobReference' => ['jobId' => self::JOBID]
])
->shouldBeCalledTimes(1);
$this->client->setConnection($this->connection->reveal());

$this->client->___setProperty('connection', $this->connection->reveal());
$job = $this->client->runQueryAsJob($query, $options);

$this->assertInstanceOf(Job::class, $job);
$this->assertEquals($this->jobId, $job->id());
$this->assertEquals(self::JOBID, $job->id());
}

public function queryDataProvider()
Expand Down Expand Up @@ -193,8 +199,8 @@ public function queryDataProvider()

public function testGetsJob()
{
$this->client->setConnection($this->connection->reveal());
$this->assertInstanceOf(Job::class, $this->client->job($this->jobId));
$this->client->___setProperty('connection', $this->connection->reveal());
$this->assertInstanceOf(Job::class, $this->client->job(self::JOBID));
}

public function testGetsJobsWithNoResults()
Expand All @@ -203,7 +209,7 @@ public function testGetsJobsWithNoResults()
->willReturn([])
->shouldBeCalledTimes(1);

$this->client->setConnection($this->connection->reveal());
$this->client->___setProperty('connection', $this->connection->reveal());
$jobs = iterator_to_array($this->client->jobs());

$this->assertEmpty($jobs);
Expand All @@ -214,15 +220,15 @@ public function testGetsJobsWithoutToken()
$this->connection->listJobs(['projectId' => $this->projectId])
->willReturn([
'jobs' => [
['jobReference' => ['jobId' => $this->jobId]]
['jobReference' => ['jobId' => self::JOBID]]
]
])
->shouldBeCalledTimes(1);

$this->client->setConnection($this->connection->reveal());
$this->client->___setProperty('connection', $this->connection->reveal());
$jobs = iterator_to_array($this->client->jobs());

$this->assertEquals($this->jobId, $jobs[0]->id());
$this->assertEquals(self::JOBID, $jobs[0]->id());
}

public function testGetsJobsWithToken()
Expand All @@ -241,19 +247,19 @@ public function testGetsJobsWithToken()
])
->willReturn([
'jobs' => [
['jobReference' => ['jobId' => $this->jobId]]
['jobReference' => ['jobId' => self::JOBID]]
]
])->shouldBeCalledTimes(1);

$this->client->setConnection($this->connection->reveal());
$this->client->___setProperty('connection', $this->connection->reveal());
$job = iterator_to_array($this->client->jobs());

$this->assertEquals($this->jobId, $job[1]->id());
$this->assertEquals(self::JOBID, $job[1]->id());
}

public function testGetsDataset()
{
$this->client->setConnection($this->connection->reveal());
$this->client->___setProperty('connection', $this->connection->reveal());
$this->assertInstanceOf(Dataset::class, $this->client->dataset($this->datasetId));
}

Expand All @@ -263,7 +269,7 @@ public function testGetsDatasetsWithNoResults()
->willReturn([])
->shouldBeCalledTimes(1);

$this->client->setConnection($this->connection->reveal());
$this->client->___setProperty('connection', $this->connection->reveal());
$datasets = iterator_to_array($this->client->datasets());

$this->assertEmpty($datasets);
Expand All @@ -279,7 +285,7 @@ public function testGetsDatasetsWithoutToken()
])
->shouldBeCalledTimes(1);

$this->client->setConnection($this->connection->reveal());
$this->client->___setProperty('connection', $this->connection->reveal());
$datasets = iterator_to_array($this->client->datasets());

$this->assertEquals($this->datasetId, $datasets[0]->id());
Expand All @@ -303,7 +309,7 @@ public function testGetsDatasetsWithToken()
)
->shouldBeCalledTimes(2);

$this->client->setConnection($this->connection->reveal());
$this->client->___setProperty('connection', $this->connection->reveal());
$dataset = iterator_to_array($this->client->datasets());

$this->assertEquals($this->datasetId, $dataset[1]->id());
Expand All @@ -318,7 +324,7 @@ public function testCreatesDataset()
]
])
->shouldBeCalledTimes(1);
$this->client->setConnection($this->connection->reveal());
$this->client->___setProperty('connection', $this->connection->reveal());

$dataset = $this->client->createDataset($this->datasetId, [
'metadata' => [
Expand Down Expand Up @@ -360,8 +366,8 @@ public function testGetsTimestamp()

class BigQueryTestClient extends BigQueryClient
{
public function setConnection($connection)
protected function generateJobId($jobIdPrefix = null)
{
$this->connection = $connection;
return $jobIdPrefix ? $jobIdPrefix . '-' . BigQueryClientTest::JOBID : BigQueryClientTest::JOBID;
}
}
43 changes: 43 additions & 0 deletions tests/unit/BigQuery/JobConfigurationTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright 2017 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\Unit\BigQuery;

use Google\Cloud\BigQuery\JobConfigurationTrait;

/**
* @group bigquery
*/
class JobConfigurationTraitTest extends \PHPUnit_Framework_TestCase
{
private $trait;

public function setUp()
{
$this->trait = \Google\Cloud\Dev\impl(JobConfigurationTrait::class);
}

public function testGenerateJobId()
{
$this->assertTrue(is_string($this->trait->call('generateJobId')));
}

public function testGenerateJobIdWithPrefix()
{
$this->assertTrue(strpos($this->trait->call('generateJobId', ['foobar']), 'foobar-') === 0);
}
}
Loading