Skip to content

Commit

Permalink
Move from gcloud to google-cloud/bigquery + tweak cmdline args
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Nassri committed Aug 15, 2016
1 parent f7065fd commit e216b35
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 48 deletions.
1 change: 1 addition & 0 deletions bigquery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
},
"dependencies": {
"@google-cloud/bigquery": "^0.1.1",
"async": "^1.5.2",
"gcloud": "^0.37.0",
"request": "^2.72.0"
Expand Down
51 changes: 31 additions & 20 deletions bigquery/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,30 @@
// by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use the
// project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication
var gcloud = require('gcloud');
var BigQuery = require('@google-cloud/bigquery');

// Instantiate the bigquery client
var bigquery = gcloud.bigquery();
var bigquery = BigQuery();
// [END auth]

// [START sync_query]
/**
* Run a synchronous query.
* @param {object} queryObj The BigQuery query to run, plus any additional options
* listed at https://cloud.google.com/bigquery/docs/reference/v2/jobs/query
* @param {string} query The BigQuery query to run, as a string
* @param {function} callback Callback function to receive query results.
*/
function syncQuery (queryObj, callback) {
if (!queryObj || !queryObj.query) {
return callback(Error('queryObj must be an object with a "query" parameter'));
function syncQuery (query, callback) {
if (!query || typeof query !== 'string') {
return callback(Error('"query" is required, and must be a string!'));
}

// Construct query object
// Query options list: https://cloud.google.com/bigquery/docs/reference/v2/jobs/query
var queryObj = {
query: query,
timeoutMs: 10000 // Time out after 10 seconds
};

bigquery.query(queryObj, function (err, rows) {
if (err) {
return callback(err);
Expand All @@ -59,15 +65,20 @@ function syncQuery (queryObj, callback) {
// [START async_query]
/**
* Run an asynchronous query.
* @param {object} queryObj The BigQuery query to run, plus any additional options
* listed at https://cloud.google.com/bigquery/docs/reference/v2/jobs/query
* @param {string} query The BigQuery query to run, as a string
* @param {function} callback Callback function to receive job data.
*/
function asyncQuery (queryObj, callback) {
if (!queryObj || !queryObj.query) {
return callback(Error('queryObj must be an object with a "query" parameter'));
function asyncQuery (query, callback) {
if (!query || typeof query !== 'string') {
return callback(Error('"query" is required, and must be a string!'));
}

// Construct query object
// Query options list: https://cloud.google.com/bigquery/docs/reference/v2/jobs/query
var queryObj = {
query: query
};

bigquery.startQuery(queryObj, function (err, job) {
if (err) {
return callback(err);
Expand Down Expand Up @@ -117,12 +128,12 @@ function asyncPoll (jobId, callback) {
function printUsage () {
console.log('Usage:');
console.log('\nCommands:\n');
console.log('\tnode query sync-query QUERY');
console.log('\tnode query async-query QUERY');
console.log('\tnode query sync QUERY');
console.log('\tnode query async QUERY');
console.log('\tnode query poll JOB_ID');
console.log('\nExamples:\n');
console.log('\tnode query sync-query "SELECT * FROM publicdata:samples.natality LIMIT 5;"');
console.log('\tnode query async-query "SELECT * FROM publicdata:samples.natality LIMIT 5;"');
console.log('\tnode query sync "SELECT * FROM publicdata:samples.natality LIMIT 5;"');
console.log('\tnode query async "SELECT * FROM publicdata:samples.natality LIMIT 5;"');
console.log('\tnode query poll 12345');
}
// [END usage]
Expand All @@ -142,10 +153,10 @@ var program = {
main: function (args, cb) {
var command = args.shift();
var arg = args.shift();
if (command === 'sync-query') {
this.syncQuery({ query: arg, timeoutMs: 10000 }, cb);
} else if (command === 'async-query') {
this.asyncQuery({ query: arg }, cb);
if (command === 'sync') {
this.syncQuery(arg, cb);
} else if (command === 'async') {
this.asyncQuery(arg, cb);
} else if (command === 'poll') {
this.asyncPoll(arg, cb);
} else {
Expand Down
6 changes: 2 additions & 4 deletions bigquery/system-test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ var example = require('../query');
describe('bigquery:query', function () {
describe('sync_query', function () {
it('should fetch data given a query', function (done) {
example.syncQuery(
{ query: 'SELECT * FROM publicdata:samples.natality LIMIT 5;' },
example.syncQuery('SELECT * FROM publicdata:samples.natality LIMIT 5;',
function (err, data) {
assert.ifError(err);
assert.notEqual(data, null);
Expand All @@ -33,8 +32,7 @@ describe('bigquery:query', function () {

describe('async_query', function () {
it('should submit a job and fetch its results', function (done) {
example.asyncQuery(
{ query: 'SELECT * FROM publicdata:samples.natality LIMIT 5;' },
example.asyncQuery('SELECT * FROM publicdata:samples.natality LIMIT 5;',
function (err, job) {
assert.ifError(err);
assert.notEqual(job.id, null);
Expand Down
49 changes: 25 additions & 24 deletions bigquery/test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ function getSample () {
startQuery: sinon.stub().callsArgWith(1, null, jobMock),
query: sinon.stub().callsArgWith(1, null, natalityMock)
};
var gcloudMock = {
bigquery: sinon.stub().returns(bigqueryMock)
};

var BigQueryMock = sinon.stub().returns(bigqueryMock);

return {
program: proxyquire('../query', {
gcloud: gcloudMock
'@google-cloud/bigquery': BigQueryMock
}),
mocks: {
gcloud: gcloudMock,
BigQuery: BigQueryMock,
bigquery: bigqueryMock,
natality: natalityMock,
metadata: metadataMock,
Expand Down Expand Up @@ -79,10 +79,10 @@ describe('bigquery:query', function () {
sinon.stub(program, 'asyncQuery');
sinon.stub(program, 'asyncPoll');

program.main(['sync-query']);
program.main(['sync']);
assert(program.syncQuery.calledOnce);

program.main(['async-query']);
program.main(['async']);
assert(program.asyncQuery.calledOnce);

program.main(['poll']);
Expand All @@ -102,14 +102,14 @@ describe('bigquery:query', function () {
});

describe('syncQuery', function () {
var queryObj = { query: 'foo' };
var query = 'foo';

it('should return results', function () {
var example = getSample();
example.program.syncQuery(queryObj,
example.program.syncQuery(query,
function (err, data) {
assert.ifError(err);
assert(example.mocks.bigquery.query.calledWith(queryObj));
assert(example.mocks.bigquery.query.called);
assert.deepEqual(data, example.mocks.natality);
assert(console.log.calledWith(
'SyncQuery: found %d rows!',
Expand All @@ -119,12 +119,12 @@ describe('bigquery:query', function () {
);
});

it('should require a query', function () {
it('should require a query as a string', function () {
var example = getSample();
example.program.syncQuery({}, function (err, data) {
assert.deepEqual(
err,
Error('queryObj must be an object with a "query" parameter')
Error('"query" is required, and must be a string!')
);
assert.equal(data, undefined);
});
Expand All @@ -134,22 +134,22 @@ describe('bigquery:query', function () {
var error = Error('syncQueryError');
var example = getSample();
example.mocks.bigquery.query = sinon.stub().callsArgWith(1, error);
example.program.syncQuery(queryObj, function (err, data) {
example.program.syncQuery(query, function (err, data) {
assert.deepEqual(err, error);
assert.equal(data, undefined);
});
});
});

describe('asyncQuery', function () {
var queryObj = { query: 'foo' };
var query = 'foo';

it('should submit a job', function () {
var example = getSample();
example.program.asyncQuery(queryObj,
example.program.asyncQuery(query,
function (err, job) {
assert.ifError(err);
assert(example.mocks.bigquery.startQuery.calledWith(queryObj));
assert(example.mocks.bigquery.startQuery.called);
assert.deepEqual(example.mocks.job, job);
assert(console.log.calledWith(
'AsyncQuery: submitted job %s!', example.jobId
Expand All @@ -158,11 +158,11 @@ describe('bigquery:query', function () {
);
});

it('should require a query', function () {
it('should require a query as a string', function () {
var example = getSample();
example.program.asyncQuery({}, function (err, job) {
assert.deepEqual(err, Error(
'queryObj must be an object with a "query" parameter'
'"query" is required, and must be a string!'
));
assert.equal(job, undefined);
});
Expand All @@ -172,7 +172,7 @@ describe('bigquery:query', function () {
var error = Error('asyncQueryError');
var example = getSample();
example.mocks.bigquery.startQuery = sinon.stub().callsArgWith(1, error);
example.program.asyncQuery(queryObj, function (err, job) {
example.program.asyncQuery(query, function (err, job) {
assert.deepEqual(err, error);
assert.equal(job, undefined);
});
Expand Down Expand Up @@ -215,8 +215,8 @@ describe('bigquery:query', function () {
example.mocks.job.getMetadata = sinon.stub().callsArgWith(0, null, pendingState);
example.program.asyncPoll(example.jobId, function (err, rows) {
assert.deepEqual(err, Error('Job %s is not done', example.jobId));
assert(example.mocks.job.getMetadata.called);
assert(console.log.calledWith('Job status: %s', pendingState.status.state));
assert(example.mocks.job.getMetadata.called);
assert.equal(example.mocks.job.getQueryResults.called, false);
assert.equal(rows, undefined);
});
Expand All @@ -228,6 +228,7 @@ describe('bigquery:query', function () {
assert(console.log.calledWith('Job status: %s', doneState.status.state));
assert(example.mocks.job.getMetadata.called);
assert(example.mocks.job.getQueryResults.called);
assert.equal(rows, example.mocks.natality);
});
});

Expand Down Expand Up @@ -256,12 +257,12 @@ describe('bigquery:query', function () {
program.printUsage();
assert(console.log.calledWith('Usage:'));
assert(console.log.calledWith('\nCommands:\n'));
assert(console.log.calledWith('\tnode query sync-query QUERY'));
assert(console.log.calledWith('\tnode query async-query QUERY'));
assert(console.log.calledWith('\tnode query sync QUERY'));
assert(console.log.calledWith('\tnode query async QUERY'));
assert(console.log.calledWith('\tnode query poll JOB_ID'));
assert(console.log.calledWith('\nExamples:\n'));
assert(console.log.calledWith('\tnode query sync-query "SELECT * FROM publicdata:samples.natality LIMIT 5;"'));
assert(console.log.calledWith('\tnode query async-query "SELECT * FROM publicdata:samples.natality LIMIT 5;"'));
assert(console.log.calledWith('\tnode query sync "SELECT * FROM publicdata:samples.natality LIMIT 5;"'));
assert(console.log.calledWith('\tnode query async "SELECT * FROM publicdata:samples.natality LIMIT 5;"'));
assert(console.log.calledWith('\tnode query poll 12345'));
});
});
Expand Down

0 comments on commit e216b35

Please sign in to comment.