diff --git a/bigquery/system-test/tables.test.js b/bigquery/system-test/tables.test.js index 71e65ab052..b42f2267f7 100644 --- a/bigquery/system-test/tables.test.js +++ b/bigquery/system-test/tables.test.js @@ -191,6 +191,19 @@ describe('bigquery:tables', function () { }); }); + describe('browseRows', function () { + it('should display rows in a table', function (done) { + program.browseRows(options.dataset, options.table, function (err, rows) { + assert.equal(err, null); + assert.equal(Array.isArray(rows), true); + assert.equal(rows.length > 0, true); + assert.equal(console.log.calledOnce, true); + assert.deepEqual(console.log.firstCall.args, ['Found %d row(s)!', rows.length]); + done(); + }); + }); + }); + describe('deleteTable', function () { it('should delete table', function (done) { program.deleteTable(options, function (err) { diff --git a/bigquery/tables.js b/bigquery/tables.js index 008497bc01..694c0fc640 100644 --- a/bigquery/tables.js +++ b/bigquery/tables.js @@ -80,6 +80,21 @@ function listTables (options, callback) { } // [END list_tables] +function browseRows (dataset, table, callback) { + var bigquery = BigQuery(); + var tableObj = bigquery.dataset(dataset).table(table); + + // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/bigquery/latest/bigquery/table?method=getRows + tableObj.getRows(function (err, rows) { + if (err) { + return callback(err); + } + + console.log('Found %d row(s)!', rows.length); + return callback(null, rows); + }); +} + // [START delete_table] /** * Deletes a table with the specified name from the specified dataset. @@ -110,6 +125,7 @@ function copyTable (srcDataset, srcTable, destDataset, destTable, callback) { var srcTableObj = bigquery.dataset(srcDataset).table(srcTable); var destTableObj = bigquery.dataset(destDataset).table(destTable); + // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/bigquery/latest/bigquery/table?method=copy srcTableObj.copy(destTableObj, function (err, job) { if (err) { return callback(err); @@ -236,6 +252,7 @@ var fs = require('fs'); var program = module.exports = { createTable: createTable, listTables: listTables, + browseRows: browseRows, deleteTable: deleteTable, importFile: importFile, exportTableToGCS: exportTableToGCS, @@ -270,6 +287,9 @@ cli ); } ) + .command('browse ', 'List the rows in a BigQuery table.', {}, function (options) { + program.browseRows(options.dataset, options.table, utils.makeHandler()); + }) .command('import
', 'Import data from a local file or a Google Cloud Storage file into BigQuery.', { bucket: { alias: 'b', @@ -331,6 +351,10 @@ cli 'node $0 list my_dataset', 'List tables in "my_dataset".' ) + .example( + 'node $0 browse my_dataset my_table', + 'Display rows from "my_table" in "my_dataset".' + ) .example( 'node $0 delete my_dataset my_table', 'Delete "my_table" from "my_dataset".' @@ -363,7 +387,7 @@ cli 'node $0 copy src_dataset src_table dest_dataset dest_table', 'Copy src_dataset:src_table to dest_dataset:dest_table.' ) - .wrap(100) + .wrap(120) .recommendCommands() .epilogue('For more information, see https://cloud.google.com/bigquery/docs'); diff --git a/bigquery/test/tables.test.js b/bigquery/test/tables.test.js index 267cc27886..51ec1abbed 100644 --- a/bigquery/test/tables.test.js +++ b/bigquery/test/tables.test.js @@ -58,9 +58,10 @@ function getSample () { var tableMock = { export: sinon.stub().yields(null, jobMock), copy: sinon.stub().yields(null, jobMock), - delete: sinon.stub().yields(null), import: sinon.stub().yields(null, jobMock), - insert: sinon.stub().yields(null, errorList) + insert: sinon.stub().yields(null, errorList), + getRows: sinon.stub().yields(null, jsonArray), + delete: sinon.stub().yields(null) }; var datasetMock = { table: sinon.stub().returns(tableMock), @@ -185,6 +186,34 @@ describe('bigquery:tables', function () { }); }); + describe('browseRows', function () { + it('should display rows', function () { + var sample = getSample(); + var callback = sinon.stub(); + + sample.program.browseRows(dataset, table, callback); + + assert.equal(sample.mocks.table.getRows.calledOnce, true); + assert.deepEqual(sample.mocks.table.getRows.firstCall.args.slice(0, -1), []); + assert.equal(callback.calledOnce, true); + assert.deepEqual(callback.firstCall.args, [null, jsonArray]); + assert.equal(console.log.calledOnce, true); + assert.deepEqual(console.log.firstCall.args, ['Found %d row(s)!', jsonArray.length]); + }); + + it('should handle error', function () { + var error = new Error('error'); + var sample = getSample(); + var callback = sinon.stub(); + sample.mocks.table.getRows.yields(error); + + sample.program.browseRows(dataset, table, callback); + + assert.equal(callback.calledOnce, true); + assert.deepEqual(callback.firstCall.args, [error]); + }); + }); + describe('deleteTable', function () { it('should delete a table', function () { var sample = getSample(); @@ -400,6 +429,15 @@ describe('bigquery:tables', function () { assert.deepEqual(program.listTables.firstCall.args.slice(0, -1), [{ dataset: dataset }]); }); + it('should call browseRows', function () { + var program = getSample().program; + program.browseRows = sinon.stub(); + + program.main(['browse', dataset, table]); + assert.equal(program.browseRows.calledOnce, true); + assert.deepEqual(program.browseRows.firstCall.args.slice(0, -1), [dataset, table]); + }); + it('should call deleteTable', function () { var program = getSample().program; program.deleteTable = sinon.stub();