Skip to content

Commit

Permalink
bigtable: throw when required information is missing (#1452)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus authored Aug 7, 2016
1 parent 143f120 commit 0ea26b0
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 10 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"lint": "jshint scripts/ packages/ system-test/ test/ && jscs packages/ system-test/ test/",
"test": "npm run docs && mocha test/docs.js packages/*/test/*.js",
"system-test": "mocha packages/*/system-test/*.js --no-timeouts --bail",
"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"
"cover": "istanbul cover _mocha --report lcovonly -- --no-timeouts --bail packages/*/test/*.js -R spec",
"coveralls": "npm run cover && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"license": "Apache-2.0",
"engines": {
Expand Down
17 changes: 17 additions & 0 deletions packages/bigtable/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ var PKG = require('../package.json');
*
* @resource [Creating a Cloud Bigtable Cluster]{@link https://cloud.google.com/bigtable/docs/creating-compute-instance}
*
* @throws {error} If a cluster is not provided.
* @throws {error} If a zone is not provided.
*
* @param {object=} options - [Configuration object](#/docs).
* @param {string} options.cluster - The cluster name that hosts your tables.
* @param {string|module:compute/zone} options.zone - The zone in which your
Expand Down Expand Up @@ -278,6 +281,14 @@ function Bigtable(options) {
return new Bigtable(options);
}

if (!options.cluster) {
throw new Error('A cluster must be provided to interact with Bigtable.');
}

if (!options.zone) {
throw new Error('A zone must be provided to interact with Bigtable.');
}

options = extend({}, options, {
zone: options.zone.name || options.zone
});
Expand Down Expand Up @@ -337,6 +348,8 @@ Bigtable.formatTableName_ = function(name) {
* @resource [Designing Your Schema]{@link https://cloud.google.com/bigtable/docs/schema-design}
* @resource [Splitting Keys]{@link https://cloud.google.com/bigtable/docs/managing-tables#splits}
*
* @throws {error} If a name is not provided.
*
* @param {string} name - The name of the table.
* @param {object=} options - Table creation options.
* @param {object|string[]} options.families - Column families to be created
Expand Down Expand Up @@ -409,6 +422,10 @@ Bigtable.prototype.createTable = function(name, options, callback) {
options = {};
}

if (!name) {
throw new Error('A name is required to create a table.');
}

var protoOpts = {
service: 'BigtableTableService',
method: 'createTable'
Expand Down
15 changes: 13 additions & 2 deletions packages/bigtable/src/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ var RowError = createErrorClass('RowError', function(row) {
this.message = 'Unknown row: ' + row + '.';
});

/*! Developer Documentation
*
* @param {module:bigtable/table} table - The row's parent Table instance.
* @param {string} key - The key for this row.
*/
/**
* Create a Row object to interact with your table rows.
*
Expand All @@ -67,7 +72,7 @@ var RowError = createErrorClass('RowError', function(row) {
* var table = bigtable.table('prezzy');
* var row = table.row('gwashington');
*/
function Row(table, name) {
function Row(table, key) {
var methods = {

/**
Expand All @@ -87,7 +92,7 @@ function Row(table, name) {
var config = {
parent: table,
methods: methods,
id: name
id: key
};

common.GrpcServiceObject.call(this, config);
Expand Down Expand Up @@ -297,6 +302,8 @@ Row.prototype.create = function(entry, callback) {
* transformed into writes. Rules are applied in order, meaning that earlier
* rules will affect the results of later ones.
*
* @throws {error} If no rules are provided.
*
* @param {object|object[]} rules - The rules to apply to this row.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
Expand Down Expand Up @@ -334,6 +341,10 @@ Row.prototype.create = function(entry, callback) {
* ], callback);
*/
Row.prototype.createRules = function(rules, callback) {
if (!rules || rules.length === 0) {
throw new Error('At least one rule must be provided.');
}

rules = arrify(rules).map(function(rule) {
var column = Mutation.parseColumnName(rule.column);
var ruleData = {
Expand Down
18 changes: 18 additions & 0 deletions packages/bigtable/src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ Table.formatRowRange_ = function(range) {
*
* @resource [Garbage Collection Proto Docs]{@link https://github.com/googleapis/googleapis/blob/master/google/bigtable/admin/table/v1/bigtable_table_data.proto#L59}
*
* @throws {error} If a name is not provided.
*
* @param {string} name - The name of column family.
* @param {string|object=} rule - Garbage collection rule.
* @param {object=} rule.age - Delete cells in a column older than the given
Expand Down Expand Up @@ -294,6 +296,10 @@ Table.prototype.createFamily = function(name, rule, callback) {
rule = null;
}

if (!name) {
throw new Error('A name is required to create a family.');
}

var grpcOpts = {
service: 'BigtableTableService',
method: 'createColumnFamily'
Expand Down Expand Up @@ -383,13 +389,19 @@ Table.prototype.deleteRows = function(options, callback) {
/**
* Get a reference to a Table Family.
*
* @throws {error} If a name is not provided.
*
* @param {string} name - The family name.
* @return {module:bigtable/family}
*
* @example
* var family = table.family('my-family');
*/
Table.prototype.family = function(name) {
if (!name) {
throw new Error('A family name must be provided.');
}

return new Family(this, name);
};

Expand Down Expand Up @@ -769,13 +781,19 @@ Table.prototype.mutate = function(entries, callback) {
/**
* Get a reference to a table row.
*
* @throws {error} If a key is not provided.
*
* @param {string} key - The row key.
* @return {module:bigtable/row}
*
* @example
* var row = table.row('lincoln');
*/
Table.prototype.row = function(key) {
if (!key) {
throw new Error('A row key must be provided.');
}

return new Row(this, key);
};

Expand Down
4 changes: 1 addition & 3 deletions packages/bigtable/test/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,9 @@ describe('Bigtable/Filter', function() {
});

it('should throw an error for unknown types', function() {
var errorMessage = /Can\'t convert to RegExp String from unknown type\./;

assert.throws(function() {
Filter.convertToRegExpString(true);
}, errorMessage);
}, /Can\'t convert to RegExp String from unknown type\./);
});
});

Expand Down
23 changes: 22 additions & 1 deletion packages/bigtable/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,27 @@ describe('Bigtable', function() {
fakeUtil.normalizeArguments = normalizeArguments;
});

it('should throw if a cluster is not provided', function() {
assert.throws(function() {
new Bigtable({});
}, /A cluster must be provided to interact with Bigtable\./);
});

it('should throw if a zone is not provided', function() {
assert.throws(function() {
new Bigtable({
cluster: CLUSTER
});
}, /A zone must be provided to interact with Bigtable\./);
});

it('should leave the original options unaltered', function() {
var fakeOptions = {
a: 'a',
b: 'b',
c: 'c',
zone: 'zone'
cluster: CLUSTER,
zone: ZONE
};

var bigtable = new Bigtable(fakeOptions);
Expand Down Expand Up @@ -172,6 +187,12 @@ describe('Bigtable', function() {
describe('createTable', function() {
var TABLE_ID = 'my-table';

it('should throw if a name is not provided', function() {
assert.throws(function() {
bigtable.createTable();
}, /A name is required to create a table\./);
});

it('should provide the proper request options', function(done) {
bigtable.request = function(protoOpts, reqOpts) {
assert.deepEqual(protoOpts, {
Expand Down
6 changes: 6 additions & 0 deletions packages/bigtable/test/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ describe('Bigtable/Row', function() {
increment: 1
}];

it('should throw if a rule is not provided', function() {
assert.throws(function() {
row.createRules();
}, /At least one rule must be provided\./);
});

it('should read/modify/write rules', function(done) {
row.request = function(grpcOpts, reqOpts, callback) {
assert.deepEqual(grpcOpts, {
Expand Down
24 changes: 21 additions & 3 deletions packages/bigtable/test/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ describe('Bigtable/Table', function() {
describe('createFamily', function() {
var COLUMN_ID = 'my-column';

it('should throw if a name is not provided', function() {
assert.throws(function() {
table.createFamily();
}, /A name is required to create a family\./);
});

it('should provide the proper request options', function(done) {
table.request = function(grpcOpts, reqOpts) {
assert.deepEqual(grpcOpts, {
Expand Down Expand Up @@ -357,6 +363,12 @@ describe('Bigtable/Table', function() {
describe('family', function() {
var FAMILY_ID = 'test-family';

it('should throw if a name is not provided', function() {
assert.throws(function() {
table.family();
}, /A family name must be provided\./);
});

it('should create a family with the proper arguments', function() {
var family = table.family(FAMILY_ID);

Expand Down Expand Up @@ -806,14 +818,20 @@ describe('Bigtable/Table', function() {
});

describe('row', function() {
var ROW_ID = 'test-row';
var KEY = 'test-row';

it('should throw if a key is not provided', function() {
assert.throws(function() {
table.row();
}, /A row key must be provided\./);
});

it('should return a Row object', function() {
var row = table.row(ROW_ID);
var row = table.row(KEY);

assert(row instanceof FakeRow);
assert.strictEqual(row.calledWith_[0], table);
assert.strictEqual(row.calledWith_[1], ROW_ID);
assert.strictEqual(row.calledWith_[1], KEY);
});
});

Expand Down

0 comments on commit 0ea26b0

Please sign in to comment.