From 21afb7243b8352182f43dd9740c370864a176159 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Thu, 11 Aug 2016 12:36:01 -0700 Subject: [PATCH] Fix comments; TODO add async queries --- bigquery/sync_query.js | 32 +++++++------------ bigquery/system-test/sync_query.test.js | 25 +-------------- bigquery/test/sync_query.test.js | 42 ++++++++++--------------- 3 files changed, 30 insertions(+), 69 deletions(-) diff --git a/bigquery/sync_query.js b/bigquery/sync_query.js index 421c7cf2ebd..a71d146082c 100644 --- a/bigquery/sync_query.js +++ b/bigquery/sync_query.js @@ -29,43 +29,37 @@ // https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication var gcloud = require('gcloud'); -// Get a reference to the bigquery component +// Instantiate the bigquery client var bigquery = gcloud.bigquery(); // [END auth] // [START query] /** * Run an example synchronous query. - * @param {Object} queryObj The BigQuery query to run, plus any additional options + * @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 {Function} callback Callback function. + * @param {function} callback Callback function. */ function syncQuery (queryObj, callback) { if (!queryObj || !queryObj.query) { - return callback(Error('queryObj must be an object with a \'query\' parameter')); + return callback(Error('queryObj must be an object with a "query" parameter')); } - // Paginate through the results - var allRows = []; - var paginator = function (err, rows, nextQueryObj) { + bigquery.query(queryObj, function (err, rows) { if (err) { return callback(err); } - allRows = allRows.concat(rows); - if (nextQueryObj) { - bigquery.query(nextQueryObj, paginator); - } else { - console.log('Found %d rows!', allRows.length); - return callback(null, allRows); - } - }; - return bigquery.query(queryObj, paginator); + console.log('Found %d rows!', rows.length); + return callback(null, rows); + }); } // [END query] // [START usage] function printUsage () { - console.log('Usage: node sync_query.js QUERY'); + console.log('Usage: node sync_query QUERY'); + console.log('\nExamples:\n'); + console.log('\tnode sync_query "SELECT * FROM publicdata:samples.natality LIMIT 5;"'); } // [END usage] @@ -81,9 +75,7 @@ var program = { // Run the sample main: function (args, cb) { if (args.length === 1 && !(args[0] === '-h' || args[0] === '--help')) { - var queryObj = { - query: args[0] - }; + var queryObj = { query: args[0], timeoutMs: 10000 }; this.syncQuery(queryObj, cb); } else { this.printUsage(); diff --git a/bigquery/system-test/sync_query.test.js b/bigquery/system-test/sync_query.test.js index 178f27be55b..48c115b0b0f 100644 --- a/bigquery/system-test/sync_query.test.js +++ b/bigquery/system-test/sync_query.test.js @@ -14,40 +14,17 @@ 'use strict'; var example = require('../sync_query'); -var querySpy = sinon.spy(example.bigquery, 'query'); describe('bigquery:sync_query', function () { describe('sync_query', function () { it('should fetch data given a query', function (done) { - querySpy.reset(); example.syncQuery( - { - query: 'SELECT * FROM publicdata:samples.natality LIMIT 5;' - }, + { query: 'SELECT * FROM publicdata:samples.natality LIMIT 5;' }, function (err, data) { assert.ifError(err); assert.notEqual(data, null); assert(Array.isArray(data)); assert(data.length === 5); - assert(example.bigquery.query.calledOnce); - done(); - } - ); - }); - - it('should paginate and re-call bigquery.query', function (done) { - querySpy.reset(); - example.syncQuery( - { - query: 'SELECT * FROM publicdata:samples.natality LIMIT 15;', - maxResults: 5 - }, - function (err, data) { - assert.ifError(err); - assert.notEqual(data, null); - assert(Array.isArray(data)); - assert(data.length === 15); - assert(example.bigquery.query.calledThrice); done(); } ); diff --git a/bigquery/test/sync_query.test.js b/bigquery/test/sync_query.test.js index 957b60f3972..9012df80255 100644 --- a/bigquery/test/sync_query.test.js +++ b/bigquery/test/sync_query.test.js @@ -62,14 +62,11 @@ describe('bigquery:sync_query', function () { var example = getSample(); sinon.stub(example.program, 'syncQuery'); - example.program.main( - ['foo'], - function (err, data) { - assert.ifError(err); - assert(example.program.syncQuery.calledWith({ query: 'foo' })); - assert.deepEqual(data, example.mocks.natality); - } - ); + example.program.main(['foo'], function (err, data) { + assert.ifError(err); + assert(example.program.syncQuery.calledWith({ query: 'foo' })); + assert.deepEqual(data, example.mocks.natality); + }); }); }); @@ -79,8 +76,7 @@ describe('bigquery:sync_query', function () { it('should return results', function () { var example = getSample(); - example.program.syncQuery( - queryObj, + example.program.syncQuery(queryObj, function (err, data) { assert.ifError(err); assert(example.mocks.bigquery.query.calledWith(queryObj)); @@ -92,26 +88,20 @@ describe('bigquery:sync_query', function () { it('should require a query', function () { var example = getSample(); - example.program.syncQuery( - {}, - function (err, data) { - assert.deepEqual(err, Error('queryObj must be an object with a \'query\' parameter')); - assert.equal(data, undefined); - } - ); + example.program.syncQuery({}, function (err, data) { + assert.deepEqual(err, Error('queryObj must be an object with a "query" parameter')); + assert.equal(data, undefined); + }); }); it('should handle error', function () { var error = Error('syncQueryError'); var example = getSample(); example.mocks.bigquery.query = sinon.stub().callsArgWith(1, error); - example.program.syncQuery( - queryObj, - function (err, data) { - assert.deepEqual(err, error); - assert.equal(data, undefined); - } - ); + example.program.syncQuery(queryObj, function (err, data) { + assert.deepEqual(err, error); + assert.equal(data, undefined); + }); }); }); @@ -119,7 +109,9 @@ describe('bigquery:sync_query', function () { it('should print usage', function () { var program = getSample().program; program.printUsage(); - assert(console.log.calledWith('Usage: node sync_query.js QUERY')); + assert(console.log.calledWith('Usage: node sync_query QUERY')); + assert(console.log.calledWith('\nExamples:\n')); + assert(console.log.calledWith('\tnode sync_query "SELECT * FROM publicdata:samples.natality LIMIT 5;"')); }); }); });