Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list_datasets_and_projects sample #161

Merged
merged 11 commits into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions bigquery/list_datasets_and_projects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2016, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* Command-line application to list all projects and datasets in BigQuery.
*
* This sample is used on this page:
*
* https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects
*/

'use strict';

// [START all]
// [START auth]
// By default, gcloud will authenticate using the service account file specified
// by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use the
// project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication
var gcloud = require('gcloud');
// [END auth]

// [START list_tables]
/**
* Retrieve all datasets for the specified project.
*
* @param {string} projectId The project to get datasets from.
* @param {Function} callback Callback function.
*/
function listDatasets (projectId, callback) {
if (!projectId) {
return callback(new Error('projectId is required!'));
}
var bigquery = gcloud.bigquery({
projectId: projectId
});

bigquery.getDatasets(function (err, datasets) {
if (err) {
return callback(err);
}

console.log('Found %d datasets!', datasets.length);
return callback(null, datasets);
});
}
// [END list_tables]

// [START list_projects]
/**
* Retrieve all projects a user has access to.
*
* @param {Function} callback Callback function.
*/
function listProjects (callback) {
var resource = gcloud.resource();

resource.getProjects(function (err, projects) {
if (err) {
return callback(err);
}

console.log('Found %d projects!', projects.length);
return callback(null, projects);
});
}
// [END list_projects]

// [START usage]
function printUsage () {
console.log('Usage: node list_datasets_and_projects [COMMAND] [ARGS...]');
console.log('\nCommands:\n');
console.log('\tlist-datasets PROJECT_ID');
console.log('\tlist-projects');
}
// [END usage]

// The command-line program
var program = {
// Print usage instructions
printUsage: printUsage,

// Exports
listDatasets: listDatasets,
listProjects: listProjects,

// Run the examples
main: function (args, cb) {
var command = args.shift();
if (command === 'list-datasets') {
this.listDatasets(args[0], cb);
} else if (command === 'list-projects') {
this.listProjects(cb);
} else {
this.printUsage();
}
}
};

if (module === require.main) {
program.main(process.argv.slice(2), console.log);
}
// [END all]

module.exports = program;
44 changes: 44 additions & 0 deletions bigquery/system-test/list_datasets_and_projects.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2016, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

var example = require('../list_datasets_and_projects');
var projectId = process.env.GCLOUD_PROJECT || 'nodejs-docs-samples';

describe('bigquery:list_datasets_and_projects', function () {
describe('listDatasets', function () {
it('should list datasets', function (done) {
example.listDatasets(projectId, function (err, datasets) {
assert.ifError(err);
assert(Array.isArray(datasets));
assert(datasets.length > 0);
assert(datasets[0].id);
assert(console.log.calledWith('Found %d datasets!', datasets.length));
done();
});
});
});
describe('listProjects', function () {
it('should list projects', function (done) {
example.listProjects(function (err, projects) {
assert.ifError(err);
assert(Array.isArray(projects));
assert(projects.length > 0);
assert(projects[0].id);
assert(console.log.calledWith('Found %d projects!', projects.length));
done();
});
});
});
});
142 changes: 142 additions & 0 deletions bigquery/test/list_datasets_and_projects.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2016, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

var proxyquire = require('proxyquire').noCallThru();

function getSample () {
var datasetsMock = [
{
id: 'foo'
}
];
var projectsMock = [
{
id: 'bar'
}
];
var bigqueryMock = {
getDatasets: sinon.stub().callsArgWith(0, null, datasetsMock)
};
var resourceMock = {
getProjects: sinon.stub().callsArgWith(0, null, projectsMock)
};
var gcloudMock = {
bigquery: sinon.stub().returns(bigqueryMock),
resource: sinon.stub().returns(resourceMock)
};
return {
program: proxyquire('../list_datasets_and_projects', {
gcloud: gcloudMock
}),
mocks: {
gcloud: gcloudMock,
bigquery: bigqueryMock,
resource: resourceMock,
datasets: datasetsMock,
projects: projectsMock
}
};
}

describe('bigquery:list_datasets_and_projects', function () {
describe('main', function () {
it('should show usage if no arguments exist', function () {
var program = getSample().program;

sinon.stub(program, 'printUsage');
program.main([]);
assert(program.printUsage.calledOnce);
});
it('should show usage if first argument is -h', function () {
var program = getSample().program;

sinon.stub(program, 'printUsage');
program.main(['-h']);
assert(program.printUsage.calledOnce);

program.main(['--help']);
assert(program.printUsage.calledTwice);
});
it('should call correct commands', function () {
var program = getSample().program;

sinon.stub(program, 'listDatasets');
program.main(['list-datasets']);
assert(program.listDatasets.calledOnce);

sinon.stub(program, 'listProjects');
program.main(['list-projects']);
assert(program.listProjects.calledOnce);
});
});

describe('printUsage', function () {
it('should print usage', function () {
var example = getSample();
example.program.printUsage();
assert(console.log.calledWith('Usage: node list_datasets_and_projects [COMMAND] [ARGS...]'));
assert(console.log.calledWith('\nCommands:\n'));
assert(console.log.calledWith('\tlist-datasets PROJECT_ID'));
assert(console.log.calledWith('\tlist-projects'));
});
});

describe('listProjects', function () {
it('should list projects', function () {
var example = getSample();
example.program.listProjects(function (err, projects) {
assert.ifError(err);
assert.strictEqual(projects, example.mocks.projects);
assert(console.log.calledWith('Found %d projects!', projects.length));
});
});
it('should handle error', function () {
var error = 'listProjectsError';
var example = getSample();
example.mocks.resource.getProjects = sinon.stub().callsArgWith(0, error);
example.program.listProjects(function (err, projects) {
assert.equal(err, error);
assert.equal(projects, undefined);
});
});
});

describe('listDatasets', function () {
it('should list datasets', function () {
var example = getSample();
example.program.listDatasets('googledata', function (err, datasets) {
assert.ifError(err);
assert.strictEqual(datasets, example.mocks.datasets);
assert(console.log.calledWith('Found %d datasets!', datasets.length));
});
});
it('should require a Project ID', function () {
var example = getSample();
example.program.listDatasets(undefined, function (err) {
assert(err);
assert.equal(err.message, 'projectId is required!');
});
});
it('should handle error', function () {
var error = 'listDatasetsError';
var example = getSample();
example.mocks.bigquery.getDatasets = sinon.stub().callsArgWith(0, error);
example.program.listDatasets('googledata', function (err, datasets) {
assert.equal(err, error);
assert.equal(datasets, undefined);
});
});
});
});
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@
"scripts": {
"lint": "semistandard",
"pretest": "npm run lint && ./scripts/geddy && ./scripts/clean",
"mocha": "mocha -R spec -S -t 120000 --require intelli-espower-loader ./test/_setup.js '{*,appengine/*,functions/*}/test/*.test.js'",
"mocha": "mocha -R spec -t 120000 --require intelli-espower-loader ./test/_setup.js '{*,appengine/*,functions/*}/test/*.test.js'",
"test": "npm run mocha",
"cover": "nyc --cache npm test && nyc report --reporter=html && nyc report --reporter=lcov",
"system-test": "mocha -R spec -S -t 120000 --require intelli-espower-loader ./system-test/_setup.js '{*,appengine/*}/system-test/*.test.js'",
"system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ./system-test/_setup.js '{*,appengine/*}/system-test/*.test.js'",
"system-cover": "npm run pretest && nyc --cache npm run system-test && nyc report --reporter=html && nyc report --reporter=lcov",
"all-test": "mocha -R spec -S -t 120000 --require intelli-espower-loader ./system-test/_setup.js '{*,appengine/*,functions/*}/test/*.test.js' '{*,appengine/*}/system-test/*.test.js'",
"all-test": "mocha -R spec -t 120000 --require intelli-espower-loader ./system-test/_setup.js '{*,appengine/*,functions/*}/test/*.test.js' '{*,appengine/*}/system-test/*.test.js'",
"all-cover": "npm run pretest && nyc --cache npm run all-test && nyc report --reporter=html && nyc report --reporter=lcov"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion pubsub/system-test/iam.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var proxyquire = require('proxyquire');
describe('pubsub:iam', function () {
it('should run the sample', function (done) {
proxyquire('../iam', {}).main(function (err, results) {
assert(!err);
assert.ifError(err);
assert(results.length === 8);
// Got topic and apiResponse
assert(results[0].length === 2);
Expand Down
2 changes: 1 addition & 1 deletion pubsub/system-test/subscription.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var proxyquire = require('proxyquire');
describe('pubsub:subscription', function () {
it('should run the sample', function (done) {
proxyquire('../subscription', {}).main(function (err, results) {
assert(!err);
assert.ifError(err);
assert(results.length === 8);
// Got topic and apiResponse
assert(results[0].length === 2);
Expand Down
4 changes: 2 additions & 2 deletions system-test/_setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ global.sinon = sinon;
var log = console.log;

beforeEach(function () {
assert(process.env.GCLOUD_PROJECT, 'Must set GCLOUD_PROJECT environment variable!');
assert(process.env.GOOGLE_APPLICATION_CREDENTIALS, 'Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!');
if (process.env.DEBUG) {
sinon.spy(console, 'error');
sinon.spy(console, 'log');
Expand All @@ -35,6 +33,8 @@ beforeEach(function () {
}
});
}
assert(process.env.GCLOUD_PROJECT, 'Must set GCLOUD_PROJECT environment variable!');
assert(process.env.GOOGLE_APPLICATION_CREDENTIALS, 'Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!');
});

afterEach(function () {
Expand Down
6 changes: 2 additions & 4 deletions test/_setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@

var assert = require('power-assert');
var sinon = require('sinon');
var uuid = require('node-uuid');

global.assert = assert;
global.sinon = sinon;
global.uuid = uuid;

var log = console.log;

beforeEach(function () {
assert(process.env.GCLOUD_PROJECT, 'Must set GCLOUD_PROJECT environment variable!');
assert(process.env.GOOGLE_APPLICATION_CREDENTIALS, 'Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!');
if (process.env.DEBUG) {
sinon.spy(console, 'error');
sinon.spy(console, 'log');
Expand All @@ -37,6 +33,8 @@ beforeEach(function () {
}
});
}
assert(process.env.GCLOUD_PROJECT, 'Must set GCLOUD_PROJECT environment variable!');
assert(process.env.GOOGLE_APPLICATION_CREDENTIALS, 'Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!');
});

afterEach(function () {
Expand Down