Skip to content

Commit d701672

Browse files
author
Ace Nassri
committed
Fix comments; TODO add async queries
1 parent 9882d91 commit d701672

File tree

3 files changed

+30
-69
lines changed

3 files changed

+30
-69
lines changed

bigquery/sync_query.js

+12-20
Original file line numberDiff line numberDiff line change
@@ -29,43 +29,37 @@
2929
// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication
3030
var gcloud = require('gcloud');
3131

32-
// Get a reference to the bigquery component
32+
// Instantiate the bigquery client
3333
var bigquery = gcloud.bigquery();
3434
// [END auth]
3535

3636
// [START query]
3737
/**
3838
* Run an example synchronous query.
39-
* @param {Object} queryObj The BigQuery query to run, plus any additional options
39+
* @param {object} queryObj The BigQuery query to run, plus any additional options
4040
* listed at https://cloud.google.com/bigquery/docs/reference/v2/jobs/query
41-
* @param {Function} callback Callback function.
41+
* @param {function} callback Callback function.
4242
*/
4343
function syncQuery (queryObj, callback) {
4444
if (!queryObj || !queryObj.query) {
45-
return callback(Error('queryObj must be an object with a \'query\' parameter'));
45+
return callback(Error('queryObj must be an object with a "query" parameter'));
4646
}
4747

48-
// Paginate through the results
49-
var allRows = [];
50-
var paginator = function (err, rows, nextQueryObj) {
48+
bigquery.query(queryObj, function (err, rows) {
5149
if (err) {
5250
return callback(err);
5351
}
54-
allRows = allRows.concat(rows);
55-
if (nextQueryObj) {
56-
bigquery.query(nextQueryObj, paginator);
57-
} else {
58-
console.log('Found %d rows!', allRows.length);
59-
return callback(null, allRows);
60-
}
61-
};
6252

63-
return bigquery.query(queryObj, paginator);
53+
console.log('Found %d rows!', rows.length);
54+
return callback(null, rows);
55+
});
6456
}
6557
// [END query]
6658
// [START usage]
6759
function printUsage () {
68-
console.log('Usage: node sync_query.js QUERY');
60+
console.log('Usage: node sync_query QUERY');
61+
console.log('\nExamples:\n');
62+
console.log('\tnode sync_query "SELECT * FROM publicdata:samples.natality LIMIT 5;"');
6963
}
7064
// [END usage]
7165

@@ -81,9 +75,7 @@ var program = {
8175
// Run the sample
8276
main: function (args, cb) {
8377
if (args.length === 1 && !(args[0] === '-h' || args[0] === '--help')) {
84-
var queryObj = {
85-
query: args[0]
86-
};
78+
var queryObj = { query: args[0], timeoutMs: 10000 };
8779
this.syncQuery(queryObj, cb);
8880
} else {
8981
this.printUsage();

bigquery/system-test/sync_query.test.js

+1-24
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,17 @@
1414
'use strict';
1515

1616
var example = require('../sync_query');
17-
var querySpy = sinon.spy(example.bigquery, 'query');
1817

1918
describe('bigquery:sync_query', function () {
2019
describe('sync_query', function () {
2120
it('should fetch data given a query', function (done) {
22-
querySpy.reset();
2321
example.syncQuery(
24-
{
25-
query: 'SELECT * FROM publicdata:samples.natality LIMIT 5;'
26-
},
22+
{ query: 'SELECT * FROM publicdata:samples.natality LIMIT 5;' },
2723
function (err, data) {
2824
assert.ifError(err);
2925
assert.notEqual(data, null);
3026
assert(Array.isArray(data));
3127
assert(data.length === 5);
32-
assert(example.bigquery.query.calledOnce);
33-
done();
34-
}
35-
);
36-
});
37-
38-
it('should paginate and re-call bigquery.query', function (done) {
39-
querySpy.reset();
40-
example.syncQuery(
41-
{
42-
query: 'SELECT * FROM publicdata:samples.natality LIMIT 15;',
43-
maxResults: 5
44-
},
45-
function (err, data) {
46-
assert.ifError(err);
47-
assert.notEqual(data, null);
48-
assert(Array.isArray(data));
49-
assert(data.length === 15);
50-
assert(example.bigquery.query.calledThrice);
5128
done();
5229
}
5330
);

bigquery/test/sync_query.test.js

+17-25
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,11 @@ describe('bigquery:sync_query', function () {
6262
var example = getSample();
6363
sinon.stub(example.program, 'syncQuery');
6464

65-
example.program.main(
66-
['foo'],
67-
function (err, data) {
68-
assert.ifError(err);
69-
assert(example.program.syncQuery.calledWith({ query: 'foo' }));
70-
assert.deepEqual(data, example.mocks.natality);
71-
}
72-
);
65+
example.program.main(['foo'], function (err, data) {
66+
assert.ifError(err);
67+
assert(example.program.syncQuery.calledWith({ query: 'foo' }));
68+
assert.deepEqual(data, example.mocks.natality);
69+
});
7370
});
7471
});
7572

@@ -79,8 +76,7 @@ describe('bigquery:sync_query', function () {
7976

8077
it('should return results', function () {
8178
var example = getSample();
82-
example.program.syncQuery(
83-
queryObj,
79+
example.program.syncQuery(queryObj,
8480
function (err, data) {
8581
assert.ifError(err);
8682
assert(example.mocks.bigquery.query.calledWith(queryObj));
@@ -92,34 +88,30 @@ describe('bigquery:sync_query', function () {
9288

9389
it('should require a query', function () {
9490
var example = getSample();
95-
example.program.syncQuery(
96-
{},
97-
function (err, data) {
98-
assert.deepEqual(err, Error('queryObj must be an object with a \'query\' parameter'));
99-
assert.equal(data, undefined);
100-
}
101-
);
91+
example.program.syncQuery({}, function (err, data) {
92+
assert.deepEqual(err, Error('queryObj must be an object with a "query" parameter'));
93+
assert.equal(data, undefined);
94+
});
10295
});
10396

10497
it('should handle error', function () {
10598
var error = Error('syncQueryError');
10699
var example = getSample();
107100
example.mocks.bigquery.query = sinon.stub().callsArgWith(1, error);
108-
example.program.syncQuery(
109-
queryObj,
110-
function (err, data) {
111-
assert.deepEqual(err, error);
112-
assert.equal(data, undefined);
113-
}
114-
);
101+
example.program.syncQuery(queryObj, function (err, data) {
102+
assert.deepEqual(err, error);
103+
assert.equal(data, undefined);
104+
});
115105
});
116106
});
117107

118108
describe('printUsage', function () {
119109
it('should print usage', function () {
120110
var program = getSample().program;
121111
program.printUsage();
122-
assert(console.log.calledWith('Usage: node sync_query.js QUERY'));
112+
assert(console.log.calledWith('Usage: node sync_query QUERY'));
113+
assert(console.log.calledWith('\nExamples:\n'));
114+
assert(console.log.calledWith('\tnode sync_query "SELECT * FROM publicdata:samples.natality LIMIT 5;"'));
123115
});
124116
});
125117
});

0 commit comments

Comments
 (0)