Skip to content

Commit fa148fe

Browse files
author
Ace Nassri
committed
Add tests + fix semistandard issues
1 parent 2726f37 commit fa148fe

File tree

3 files changed

+147
-53
lines changed

3 files changed

+147
-53
lines changed

bigquery/list_datasets_and_projects.js

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,22 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
/*
15-
Command-line application to list all projects and datasets in BigQuery.
16-
17-
This sample is used on this page:
18-
19-
https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects
20-
*/
14+
/**
15+
* Command-line application to list all projects and datasets in BigQuery.
16+
*
17+
* This sample is used on this page:
18+
*
19+
* https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects
20+
*/
2121

2222
'use strict';
2323

24-
var async = require('async');
25-
2624
// [START auth]
2725
// By default, gcloud will authenticate using the service account file specified
2826
// by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use the
2927
// project specified by the GCLOUD_PROJECT environment variable. See
3028
// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication
3129
var gcloud = require('gcloud');
32-
3330
// [END auth]
3431

3532
// [START list_tables]
@@ -40,21 +37,23 @@ var gcloud = require('gcloud');
4037
* @param {Function} callback Callback function.
4138
*/
4239
function listDatasets (projectId, callback) {
40+
if (!projectId) {
41+
throw new Error('projectId is required');
42+
}
43+
4344
var bigquery = gcloud.bigquery({
4445
projectId: projectId
4546
});
4647

47-
// Grab paginated tables
4848
bigquery.getDatasets(function (err, datasets, nextQuery, apiResponse) {
49-
5049
// Quit on error
5150
if (err) {
5251
return callback(err);
5352
}
5453

5554
// Pagination
5655
if (nextQuery) {
57-
return bigquery.getDatasets(nextQuery, callback)
56+
return bigquery.getDatasets(nextQuery, callback);
5857
}
5958

6059
// Last page of datasets
@@ -71,49 +70,50 @@ function listDatasets (projectId, callback) {
7170
*/
7271
function listProjects (callback) {
7372
var resource = gcloud.resource();
74-
75-
resource.getProjects({}, function(err, projects, nextQuery, apiResponse) {
76-
73+
resource.getProjects(function (err, projects, nextQuery) {
7774
// Quit on error
7875
if (err) {
7976
return callback(err);
8077
}
8178

8279
// Pagination
8380
if (nextQuery) {
84-
return resource.getProjects(nextQuery, callback)
81+
return resource.getProjects(nextQuery, callback);
8582
}
8683

8784
// Last page of projects
8885
return callback(null, projects);
89-
9086
});
9187
}
9288
// [END list_projects]
9389

90+
// Print usage instructions
91+
function printUsage () {
92+
console.log('Usage: node list_datasets_and_projects.js COMMAND [ARGS]');
93+
console.log('\nCommands:');
94+
console.log('\tlist-datasets PROJECT_ID');
95+
console.log('\tlist-projects');
96+
}
97+
98+
// Exports
99+
exports.listDatasets = listDatasets;
100+
exports.listProjects = listProjects;
101+
exports.printUsage = printUsage;
102+
94103
// Run the examples
95-
exports.main = function (projectId, callback) {
96-
var cbFunc =
97-
function (err, response) {
98-
if (err) {
99-
return callback(err);
100-
}
101-
callback(null, response);
102-
};
103-
104-
105-
if (projectId != null) {
106-
listDatasets(projectId, cbFunc);
104+
exports.main = function (args, cb) {
105+
printUsage();
106+
107+
if (args.length === 2 && args[0] === 'list-datasets') {
108+
listDatasets(args[1], cb);
109+
} else if (args.length === 1 && args[0] === 'list-projects') {
110+
listProjects(cb);
107111
} else {
108-
listProjects(cbFunc);
112+
exports.printUsage();
113+
cb();
109114
}
110-
111115
};
112116

113117
if (module === require.main) {
114-
var args = process.argv.slice(1);
115-
if (args.length > 2) {
116-
throw new Error('Usage: node list_datasets_and_projects.js [<projectId>]');
117-
}
118-
exports.main(args[1], console.log);
119-
}
118+
exports.main(process.argv.slice(2), console.log);
119+
}

bigquery/system-test/list_datasets_and_projects.test.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,33 @@
1313

1414
'use strict';
1515

16-
var listDatasetsAndProjectsExample = require("../list_datasets_and_projects")
16+
var listDatasetsAndProjectsExample = require('../list_datasets_and_projects');
1717

1818
describe('bigquery:list_datasets_and_projects', function () {
19-
it('should be tested');
19+
describe('main', function () {
20+
it('should list datasets via the command line', function () {
21+
listDatasetsAndProjectsExample.main(
22+
['list-datasets', 'googledata'],
23+
function (err, datasets) {
24+
assert.ifError(err);
25+
assert.notNull(datasets);
26+
assert(Array.isArray(datasets));
27+
assert(datasets.length > 0);
28+
assert(console.log.calledWith(datasets));
29+
});
30+
});
2031

21-
it('should list datasets', function () {
22-
listDatasetsAndProjectsExample.main("googledata", function(datasets) {
23-
assert.notNull(datasets)
24-
assert.notEqual(len(datasets), 0)
25-
})
32+
it('should list projects via the command line', function () {
33+
listDatasetsAndProjectsExample.main(
34+
['list-projects'],
35+
function (err, projects) {
36+
assert.ifError(err);
37+
assert.notNull(projects);
38+
assert(Array.isArray(projects));
39+
assert(projects.length > 0);
40+
assert(console.log.calledWith(projects));
41+
}
42+
);
43+
});
2644
});
27-
28-
it('should list projects', function () {
29-
listDatasetsAndProjectsExample.main(null, function(projects) {
30-
assert.notNull(projects)
31-
assert.notEqual(len(projects), 0)
32-
})
33-
});
34-
3545
});
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var listDatasetsAndProjectsExample = require('../list_datasets_and_projects');
17+
18+
describe('bigquery:list_datasets_and_projects', function () {
19+
describe('main', function () {
20+
sinon.spy(listDatasetsAndProjectsExample, 'printUsage');
21+
22+
it('should show usage if no arguments exist', function () {
23+
listDatasetsAndProjectsExample.main([],
24+
function (err) {
25+
assert.ifError(err);
26+
assert(listDatasetsAndProjectsExample.printUsage.called);
27+
}
28+
);
29+
});
30+
31+
it('should show usage if first argument is -h', function () {
32+
listDatasetsAndProjectsExample.main(['-h'],
33+
function (err) {
34+
assert.ifError(err);
35+
assert(listDatasetsAndProjectsExample.printUsage.called);
36+
}
37+
);
38+
});
39+
});
40+
41+
describe('printUsage', function () {
42+
it('should print usage', function () {
43+
listDatasetsAndProjectsExample.printUsage();
44+
assert(console.log.calledWith('Usage: node list_datasets_and_projects.js COMMAND [ARGS]'));
45+
assert(console.log.calledWith('\nCommands:'));
46+
assert(console.log.calledWith('\tlist-datasets PROJECT_ID'));
47+
assert(console.log.calledWith('\tlist-projects'));
48+
});
49+
});
50+
51+
describe('listProjects', function () {
52+
it('should list projects', function () {
53+
listDatasetsAndProjectsExample.listProjects(
54+
function (err, projects) {
55+
assert.ifError(err);
56+
assert.notNull(projects);
57+
assert(Array.isArray(projects));
58+
assert(projects.length > 0);
59+
});
60+
});
61+
});
62+
63+
describe('listDatasets', function () {
64+
it('should list datasets', function () {
65+
listDatasetsAndProjectsExample.listDatasets('googledata',
66+
function (err, datasets) {
67+
assert.ifError(err);
68+
assert.notNull(datasets);
69+
assert(Array.isArray(datasets));
70+
assert(datasets.length > 0);
71+
});
72+
});
73+
74+
it('should require a Project ID', function () {
75+
assert.throws(
76+
function () {
77+
listDatasetsAndProjectsExample.listDatasets(null, null);
78+
},
79+
Error,
80+
'projectId is required'
81+
);
82+
});
83+
});
84+
});

0 commit comments

Comments
 (0)