Skip to content

Commit dc5e6a5

Browse files
bigtable: throw when required information is missing
1 parent 143f120 commit dc5e6a5

8 files changed

Lines changed: 100 additions & 10 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"lint": "jshint scripts/ packages/ system-test/ test/ && jscs packages/ system-test/ test/",
2828
"test": "npm run docs && mocha test/docs.js packages/*/test/*.js",
2929
"system-test": "mocha packages/*/system-test/*.js --no-timeouts --bail",
30-
"coveralls": "istanbul cover _mocha --report lcovonly -- --no-timeouts --bail packages/*/test/*.js -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
30+
"cover": "istanbul cover _mocha --report lcovonly -- --no-timeouts --bail packages/*/test/*.js -R spec",
31+
"coveralls": "npm run cover && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
3132
},
3233
"license": "Apache-2.0",
3334
"engines": {

packages/bigtable/src/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ var PKG = require('../package.json');
5656
*
5757
* @resource [Creating a Cloud Bigtable Cluster]{@link https://cloud.google.com/bigtable/docs/creating-compute-instance}
5858
*
59+
* @throws {error} If a cluster is not provided.
60+
* @throws {error} If a zone is not provided.
61+
*
5962
* @param {object=} options - [Configuration object](#/docs).
6063
* @param {string} options.cluster - The cluster name that hosts your tables.
6164
* @param {string|module:compute/zone} options.zone - The zone in which your
@@ -278,6 +281,14 @@ function Bigtable(options) {
278281
return new Bigtable(options);
279282
}
280283

284+
if (!options.cluster) {
285+
throw new Error('A cluster must be provided to interact with Bigtable.');
286+
}
287+
288+
if (!options.zone) {
289+
throw new Error('A zone must be provided to interact with Bigtable.');
290+
}
291+
281292
options = extend({}, options, {
282293
zone: options.zone.name || options.zone
283294
});
@@ -337,6 +348,8 @@ Bigtable.formatTableName_ = function(name) {
337348
* @resource [Designing Your Schema]{@link https://cloud.google.com/bigtable/docs/schema-design}
338349
* @resource [Splitting Keys]{@link https://cloud.google.com/bigtable/docs/managing-tables#splits}
339350
*
351+
* @throws {error} If a name is not provided.
352+
*
340353
* @param {string} name - The name of the table.
341354
* @param {object=} options - Table creation options.
342355
* @param {object|string[]} options.families - Column families to be created
@@ -409,6 +422,10 @@ Bigtable.prototype.createTable = function(name, options, callback) {
409422
options = {};
410423
}
411424

425+
if (!name) {
426+
throw new Error('A name is required to create a table.');
427+
}
428+
412429
var protoOpts = {
413430
service: 'BigtableTableService',
414431
method: 'createTable'

packages/bigtable/src/row.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ var RowError = createErrorClass('RowError', function(row) {
4848
this.message = 'Unknown row: ' + row + '.';
4949
});
5050

51+
/*! Developer Documentation
52+
*
53+
* @param {module:bigtable/table} table - The row's parent Table instance.
54+
* @param {string} key - The key for this row.
55+
*/
5156
/**
5257
* Create a Row object to interact with your table rows.
5358
*
@@ -67,7 +72,7 @@ var RowError = createErrorClass('RowError', function(row) {
6772
* var table = bigtable.table('prezzy');
6873
* var row = table.row('gwashington');
6974
*/
70-
function Row(table, name) {
75+
function Row(table, key) {
7176
var methods = {
7277

7378
/**
@@ -87,7 +92,7 @@ function Row(table, name) {
8792
var config = {
8893
parent: table,
8994
methods: methods,
90-
id: name
95+
id: key
9196
};
9297

9398
common.GrpcServiceObject.call(this, config);
@@ -297,6 +302,8 @@ Row.prototype.create = function(entry, callback) {
297302
* transformed into writes. Rules are applied in order, meaning that earlier
298303
* rules will affect the results of later ones.
299304
*
305+
* @throws {error} If no rules are provided.
306+
*
300307
* @param {object|object[]} rules - The rules to apply to this row.
301308
* @param {function} callback - The callback function.
302309
* @param {?error} callback.err - An error returned while making this
@@ -334,6 +341,10 @@ Row.prototype.create = function(entry, callback) {
334341
* ], callback);
335342
*/
336343
Row.prototype.createRules = function(rules, callback) {
344+
if (!rules || rules.length === 0) {
345+
throw new Error('At least one rule must be provided.');
346+
}
347+
337348
rules = arrify(rules).map(function(rule) {
338349
var column = Mutation.parseColumnName(rule.column);
339350
var ruleData = {

packages/bigtable/src/table.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ Table.formatRowRange_ = function(range) {
250250
*
251251
* @resource [Garbage Collection Proto Docs]{@link https://github.com/googleapis/googleapis/blob/master/google/bigtable/admin/table/v1/bigtable_table_data.proto#L59}
252252
*
253+
* @throws {error} If a name is not provided.
254+
*
253255
* @param {string} name - The name of column family.
254256
* @param {string|object=} rule - Garbage collection rule.
255257
* @param {object=} rule.age - Delete cells in a column older than the given
@@ -294,6 +296,10 @@ Table.prototype.createFamily = function(name, rule, callback) {
294296
rule = null;
295297
}
296298

299+
if (!name) {
300+
throw new Error('A name is required to create a family.');
301+
}
302+
297303
var grpcOpts = {
298304
service: 'BigtableTableService',
299305
method: 'createColumnFamily'
@@ -383,13 +389,19 @@ Table.prototype.deleteRows = function(options, callback) {
383389
/**
384390
* Get a reference to a Table Family.
385391
*
392+
* @throws {error} If a name is not provided.
393+
*
386394
* @param {string} name - The family name.
387395
* @return {module:bigtable/family}
388396
*
389397
* @example
390398
* var family = table.family('my-family');
391399
*/
392400
Table.prototype.family = function(name) {
401+
if (!name) {
402+
throw new Error('A family name must be provided.');
403+
}
404+
393405
return new Family(this, name);
394406
};
395407

@@ -769,13 +781,19 @@ Table.prototype.mutate = function(entries, callback) {
769781
/**
770782
* Get a reference to a table row.
771783
*
784+
* @throws {error} If a key is not provided.
785+
*
772786
* @param {string} key - The row key.
773787
* @return {module:bigtable/row}
774788
*
775789
* @example
776790
* var row = table.row('lincoln');
777791
*/
778792
Table.prototype.row = function(key) {
793+
if (!key) {
794+
throw new Error('A row key must be provided.');
795+
}
796+
779797
return new Row(this, key);
780798
};
781799

packages/bigtable/test/filter.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,9 @@ describe('Bigtable/Filter', function() {
8080
});
8181

8282
it('should throw an error for unknown types', function() {
83-
var errorMessage = /Can\'t convert to RegExp String from unknown type\./;
84-
8583
assert.throws(function() {
8684
Filter.convertToRegExpString(true);
87-
}, errorMessage);
85+
}, /Can\'t convert to RegExp String from unknown type\./);
8886
});
8987
});
9088

packages/bigtable/test/index.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,27 @@ describe('Bigtable', function() {
106106
fakeUtil.normalizeArguments = normalizeArguments;
107107
});
108108

109+
it('should throw if a cluster is not provided', function() {
110+
assert.throws(function() {
111+
new Bigtable({});
112+
}, /A cluster must be provided to interact with Bigtable\./);
113+
});
114+
115+
it('should throw if a zone is not provided', function() {
116+
assert.throws(function() {
117+
new Bigtable({
118+
cluster: CLUSTER
119+
});
120+
}, /A zone must be provided to interact with Bigtable\./);
121+
});
122+
109123
it('should leave the original options unaltered', function() {
110124
var fakeOptions = {
111125
a: 'a',
112126
b: 'b',
113127
c: 'c',
114-
zone: 'zone'
128+
cluster: CLUSTER,
129+
zone: ZONE
115130
};
116131

117132
var bigtable = new Bigtable(fakeOptions);
@@ -172,6 +187,12 @@ describe('Bigtable', function() {
172187
describe('createTable', function() {
173188
var TABLE_ID = 'my-table';
174189

190+
it('should throw if a name is not provided', function() {
191+
assert.throws(function() {
192+
bigtable.createTable();
193+
}, /A name is required to create a table\./);
194+
});
195+
175196
it('should provide the proper request options', function(done) {
176197
bigtable.request = function(protoOpts, reqOpts) {
177198
assert.deepEqual(protoOpts, {

packages/bigtable/test/row.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@ describe('Bigtable/Row', function() {
258258
increment: 1
259259
}];
260260

261+
it('should throw if a rule is not provided', function() {
262+
assert.throws(function() {
263+
row.createRules();
264+
}, /At least one rule must be provided\./);
265+
});
266+
261267
it('should read/modify/write rules', function(done) {
262268
row.request = function(grpcOpts, reqOpts, callback) {
263269
assert.deepEqual(grpcOpts, {

packages/bigtable/test/table.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ describe('Bigtable/Table', function() {
217217
describe('createFamily', function() {
218218
var COLUMN_ID = 'my-column';
219219

220+
it('should throw if a name is not provided', function() {
221+
assert.throws(function() {
222+
table.createFamily();
223+
}, /A name is required to create a family\./);
224+
});
225+
220226
it('should provide the proper request options', function(done) {
221227
table.request = function(grpcOpts, reqOpts) {
222228
assert.deepEqual(grpcOpts, {
@@ -357,6 +363,12 @@ describe('Bigtable/Table', function() {
357363
describe('family', function() {
358364
var FAMILY_ID = 'test-family';
359365

366+
it('should throw if a name is not provided', function() {
367+
assert.throws(function() {
368+
table.family();
369+
}, /A family name must be provided\./);
370+
});
371+
360372
it('should create a family with the proper arguments', function() {
361373
var family = table.family(FAMILY_ID);
362374

@@ -806,14 +818,20 @@ describe('Bigtable/Table', function() {
806818
});
807819

808820
describe('row', function() {
809-
var ROW_ID = 'test-row';
821+
var KEY = 'test-row';
822+
823+
it('should throw if a key is not provided', function() {
824+
assert.throws(function() {
825+
table.row();
826+
}, /A row key must be provided\./);
827+
});
810828

811829
it('should return a Row object', function() {
812-
var row = table.row(ROW_ID);
830+
var row = table.row(KEY);
813831

814832
assert(row instanceof FakeRow);
815833
assert.strictEqual(row.calledWith_[0], table);
816-
assert.strictEqual(row.calledWith_[1], ROW_ID);
834+
assert.strictEqual(row.calledWith_[1], KEY);
817835
});
818836
});
819837

0 commit comments

Comments
 (0)