Skip to content

Commit 6543ff9

Browse files
Ace Nassrijmdobry
authored andcommitted
Add browsing data sample (GoogleCloudPlatform#201)
* First draft of browseTable sample * A few minor tweaks * Fix bad rebase * Address comments * Address comments
1 parent 92f033d commit 6543ff9

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

bigquery/system-test/tables.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ describe('bigquery:tables', function () {
191191
});
192192
});
193193

194+
describe('browseRows', function () {
195+
it('should display rows in a table', function (done) {
196+
program.browseRows(options.dataset, options.table, function (err, rows) {
197+
assert.equal(err, null);
198+
assert.equal(Array.isArray(rows), true);
199+
assert.equal(rows.length > 0, true);
200+
assert.equal(console.log.calledOnce, true);
201+
assert.deepEqual(console.log.firstCall.args, ['Found %d row(s)!', rows.length]);
202+
done();
203+
});
204+
});
205+
});
206+
194207
describe('deleteTable', function () {
195208
it('should delete table', function (done) {
196209
program.deleteTable(options, function (err) {

bigquery/tables.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ function listTables (options, callback) {
8080
}
8181
// [END list_tables]
8282

83+
function browseRows (dataset, table, callback) {
84+
var bigquery = BigQuery();
85+
var tableObj = bigquery.dataset(dataset).table(table);
86+
87+
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/bigquery/latest/bigquery/table?method=getRows
88+
tableObj.getRows(function (err, rows) {
89+
if (err) {
90+
return callback(err);
91+
}
92+
93+
console.log('Found %d row(s)!', rows.length);
94+
return callback(null, rows);
95+
});
96+
}
97+
8398
// [START delete_table]
8499
/**
85100
* Deletes a table with the specified name from the specified dataset.
@@ -110,6 +125,7 @@ function copyTable (srcDataset, srcTable, destDataset, destTable, callback) {
110125
var srcTableObj = bigquery.dataset(srcDataset).table(srcTable);
111126
var destTableObj = bigquery.dataset(destDataset).table(destTable);
112127

128+
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/bigquery/latest/bigquery/table?method=copy
113129
srcTableObj.copy(destTableObj, function (err, job) {
114130
if (err) {
115131
return callback(err);
@@ -236,6 +252,7 @@ var fs = require('fs');
236252
var program = module.exports = {
237253
createTable: createTable,
238254
listTables: listTables,
255+
browseRows: browseRows,
239256
deleteTable: deleteTable,
240257
importFile: importFile,
241258
exportTableToGCS: exportTableToGCS,
@@ -270,6 +287,9 @@ cli
270287
);
271288
}
272289
)
290+
.command('browse <dataset> <table>', 'List the rows in a BigQuery table.', {}, function (options) {
291+
program.browseRows(options.dataset, options.table, utils.makeHandler());
292+
})
273293
.command('import <dataset> <table> <file>', 'Import data from a local file or a Google Cloud Storage file into BigQuery.', {
274294
bucket: {
275295
alias: 'b',
@@ -331,6 +351,10 @@ cli
331351
'node $0 list my_dataset',
332352
'List tables in "my_dataset".'
333353
)
354+
.example(
355+
'node $0 browse my_dataset my_table',
356+
'Display rows from "my_table" in "my_dataset".'
357+
)
334358
.example(
335359
'node $0 delete my_dataset my_table',
336360
'Delete "my_table" from "my_dataset".'
@@ -363,7 +387,7 @@ cli
363387
'node $0 copy src_dataset src_table dest_dataset dest_table',
364388
'Copy src_dataset:src_table to dest_dataset:dest_table.'
365389
)
366-
.wrap(100)
390+
.wrap(120)
367391
.recommendCommands()
368392
.epilogue('For more information, see https://cloud.google.com/bigquery/docs');
369393

bigquery/test/tables.test.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ function getSample () {
5858
var tableMock = {
5959
export: sinon.stub().yields(null, jobMock),
6060
copy: sinon.stub().yields(null, jobMock),
61-
delete: sinon.stub().yields(null),
6261
import: sinon.stub().yields(null, jobMock),
63-
insert: sinon.stub().yields(null, errorList)
62+
insert: sinon.stub().yields(null, errorList),
63+
getRows: sinon.stub().yields(null, jsonArray),
64+
delete: sinon.stub().yields(null)
6465
};
6566
var datasetMock = {
6667
table: sinon.stub().returns(tableMock),
@@ -185,6 +186,34 @@ describe('bigquery:tables', function () {
185186
});
186187
});
187188

189+
describe('browseRows', function () {
190+
it('should display rows', function () {
191+
var sample = getSample();
192+
var callback = sinon.stub();
193+
194+
sample.program.browseRows(dataset, table, callback);
195+
196+
assert.equal(sample.mocks.table.getRows.calledOnce, true);
197+
assert.deepEqual(sample.mocks.table.getRows.firstCall.args.slice(0, -1), []);
198+
assert.equal(callback.calledOnce, true);
199+
assert.deepEqual(callback.firstCall.args, [null, jsonArray]);
200+
assert.equal(console.log.calledOnce, true);
201+
assert.deepEqual(console.log.firstCall.args, ['Found %d row(s)!', jsonArray.length]);
202+
});
203+
204+
it('should handle error', function () {
205+
var error = new Error('error');
206+
var sample = getSample();
207+
var callback = sinon.stub();
208+
sample.mocks.table.getRows.yields(error);
209+
210+
sample.program.browseRows(dataset, table, callback);
211+
212+
assert.equal(callback.calledOnce, true);
213+
assert.deepEqual(callback.firstCall.args, [error]);
214+
});
215+
});
216+
188217
describe('deleteTable', function () {
189218
it('should delete a table', function () {
190219
var sample = getSample();
@@ -400,6 +429,15 @@ describe('bigquery:tables', function () {
400429
assert.deepEqual(program.listTables.firstCall.args.slice(0, -1), [{ dataset: dataset }]);
401430
});
402431

432+
it('should call browseRows', function () {
433+
var program = getSample().program;
434+
program.browseRows = sinon.stub();
435+
436+
program.main(['browse', dataset, table]);
437+
assert.equal(program.browseRows.calledOnce, true);
438+
assert.deepEqual(program.browseRows.firstCall.args.slice(0, -1), [dataset, table]);
439+
});
440+
403441
it('should call deleteTable', function () {
404442
var program = getSample().program;
405443
program.deleteTable = sinon.stub();

0 commit comments

Comments
 (0)