Skip to content

Commit

Permalink
Fix comments; TODO add async queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Nassri committed Aug 11, 2016
1 parent 5c47b16 commit 21afb72
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 69 deletions.
32 changes: 12 additions & 20 deletions bigquery/sync_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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();
Expand Down
25 changes: 1 addition & 24 deletions bigquery/system-test/sync_query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
);
Expand Down
42 changes: 17 additions & 25 deletions bigquery/test/sync_query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

Expand All @@ -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));
Expand All @@ -92,34 +88,30 @@ 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);
});
});
});

describe('printUsage', 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;"'));
});
});
});

0 comments on commit 21afb72

Please sign in to comment.