Skip to content

Commit

Permalink
Add tests + fix semistandard issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Nassri committed Aug 8, 2016
1 parent 2726f37 commit fa148fe
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 53 deletions.
76 changes: 38 additions & 38 deletions bigquery/list_datasets_and_projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,22 @@
// 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
*/
/**
* 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';

var async = require('async');

// [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]
Expand All @@ -40,21 +37,23 @@ var gcloud = require('gcloud');
* @param {Function} callback Callback function.
*/
function listDatasets (projectId, callback) {
if (!projectId) {
throw new Error('projectId is required');
}

var bigquery = gcloud.bigquery({
projectId: projectId
});

// Grab paginated tables
bigquery.getDatasets(function (err, datasets, nextQuery, apiResponse) {

// Quit on error
if (err) {
return callback(err);
}

// Pagination
if (nextQuery) {
return bigquery.getDatasets(nextQuery, callback)
return bigquery.getDatasets(nextQuery, callback);
}

// Last page of datasets
Expand All @@ -71,49 +70,50 @@ function listDatasets (projectId, callback) {
*/
function listProjects (callback) {
var resource = gcloud.resource();

resource.getProjects({}, function(err, projects, nextQuery, apiResponse) {

resource.getProjects(function (err, projects, nextQuery) {
// Quit on error
if (err) {
return callback(err);
}

// Pagination
if (nextQuery) {
return resource.getProjects(nextQuery, callback)
return resource.getProjects(nextQuery, callback);
}

// Last page of projects
return callback(null, projects);

});
}
// [END list_projects]

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

// Exports
exports.listDatasets = listDatasets;
exports.listProjects = listProjects;
exports.printUsage = printUsage;

// Run the examples
exports.main = function (projectId, callback) {
var cbFunc =
function (err, response) {
if (err) {
return callback(err);
}
callback(null, response);
};


if (projectId != null) {
listDatasets(projectId, cbFunc);
exports.main = function (args, cb) {
printUsage();

if (args.length === 2 && args[0] === 'list-datasets') {
listDatasets(args[1], cb);
} else if (args.length === 1 && args[0] === 'list-projects') {
listProjects(cb);
} else {
listProjects(cbFunc);
exports.printUsage();
cb();
}

};

if (module === require.main) {
var args = process.argv.slice(1);
if (args.length > 2) {
throw new Error('Usage: node list_datasets_and_projects.js [<projectId>]');
}
exports.main(args[1], console.log);
}
exports.main(process.argv.slice(2), console.log);
}
40 changes: 25 additions & 15 deletions bigquery/system-test/list_datasets_and_projects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,33 @@

'use strict';

var listDatasetsAndProjectsExample = require("../list_datasets_and_projects")
var listDatasetsAndProjectsExample = require('../list_datasets_and_projects');

describe('bigquery:list_datasets_and_projects', function () {
it('should be tested');
describe('main', function () {
it('should list datasets via the command line', function () {
listDatasetsAndProjectsExample.main(
['list-datasets', 'googledata'],
function (err, datasets) {
assert.ifError(err);
assert.notNull(datasets);
assert(Array.isArray(datasets));
assert(datasets.length > 0);
assert(console.log.calledWith(datasets));
});
});

it('should list datasets', function () {
listDatasetsAndProjectsExample.main("googledata", function(datasets) {
assert.notNull(datasets)
assert.notEqual(len(datasets), 0)
})
it('should list projects via the command line', function () {
listDatasetsAndProjectsExample.main(
['list-projects'],
function (err, projects) {
assert.ifError(err);
assert.notNull(projects);
assert(Array.isArray(projects));
assert(projects.length > 0);
assert(console.log.calledWith(projects));
}
);
});
});

it('should list projects', function () {
listDatasetsAndProjectsExample.main(null, function(projects) {
assert.notNull(projects)
assert.notEqual(len(projects), 0)
})
});

});
84 changes: 84 additions & 0 deletions bigquery/test/list_datasets_and_projects.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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 listDatasetsAndProjectsExample = require('../list_datasets_and_projects');

describe('bigquery:list_datasets_and_projects', function () {
describe('main', function () {
sinon.spy(listDatasetsAndProjectsExample, 'printUsage');

it('should show usage if no arguments exist', function () {
listDatasetsAndProjectsExample.main([],
function (err) {
assert.ifError(err);
assert(listDatasetsAndProjectsExample.printUsage.called);
}
);
});

it('should show usage if first argument is -h', function () {
listDatasetsAndProjectsExample.main(['-h'],
function (err) {
assert.ifError(err);
assert(listDatasetsAndProjectsExample.printUsage.called);
}
);
});
});

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

describe('listProjects', function () {
it('should list projects', function () {
listDatasetsAndProjectsExample.listProjects(
function (err, projects) {
assert.ifError(err);
assert.notNull(projects);
assert(Array.isArray(projects));
assert(projects.length > 0);
});
});
});

describe('listDatasets', function () {
it('should list datasets', function () {
listDatasetsAndProjectsExample.listDatasets('googledata',
function (err, datasets) {
assert.ifError(err);
assert.notNull(datasets);
assert(Array.isArray(datasets));
assert(datasets.length > 0);
});
});

it('should require a Project ID', function () {
assert.throws(
function () {
listDatasetsAndProjectsExample.listDatasets(null, null);
},
Error,
'projectId is required'
);
});
});
});

0 comments on commit fa148fe

Please sign in to comment.