Skip to content

Commit

Permalink
datastore: normalize arguments in Dataset constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Jan 27, 2016
1 parent fb2cf47 commit 5dcb369
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,17 @@ var SCOPES = [
*/
function Dataset(options) {
if (!(this instanceof Dataset)) {
options = util.normalizeArguments(this, options);
return new Dataset(options);
}

options = options || {};

this.datasetId = options.projectId || process.env.DATASTORE_DATASET;
this.datasetId = options.projectId;

if (process.env.DATASTORE_DATASET) {
this.datasetId = process.env.DATASTORE_DATASET;
}

if (!this.datasetId) {
throw new Error('A project or dataset ID is required to use a Dataset.');
Expand Down
11 changes: 11 additions & 0 deletions test/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,17 @@ describe('common/util', function() {
});

describe('projectIdRequired', function() {
var defaultProjectId;

before(function() {
defaultProjectId = process.env.GCLOUD_PROJECT;
delete process.env.GCLOUD_PROJECT;
});

after(function() {
process.env.GCLOUD_PROJECT = defaultProjectId;
});

var fakeContextWithoutProjectId = {
config_: {}
};
Expand Down
37 changes: 36 additions & 1 deletion test/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ var extend = require('extend');
var mockery = require('mockery');
var util = require('../../lib/common/util.js');

var normalizeArgumentsCache = util.normalizeArguments;
var normalizeArgumentsOverride;
util.normalizeArguments = function() {
return (normalizeArgumentsOverride || normalizeArgumentsCache)
.apply(this, arguments);
};

var makeAuthenticatedRequestFactoryCache = util.makeAuthenticatedRequestFactory;
var makeAuthenticatedRequestFactoryOverride;
util.makeAuthenticatedRequestFactory = function() {
Expand Down Expand Up @@ -68,10 +75,22 @@ describe('Dataset', function() {
beforeEach(function() {
delete process.env.DATASTORE_DATASET;
makeAuthenticatedRequestFactoryOverride = null;
normalizeArgumentsOverride = null;
dataset = new Dataset(OPTIONS);
});

describe('instantiation', function() {
var defaultProjectId;

before(function() {
defaultProjectId = process.env.GCLOUD_PROJECT;
delete process.env.GCLOUD_PROJECT;
});

after(function() {
process.env.GCLOUD_PROJECT = defaultProjectId;
});

it('should localize the dataset id', function() {
assert.strictEqual(dataset.datasetId, OPTIONS.projectId);
});
Expand All @@ -86,7 +105,7 @@ describe('Dataset', function() {
delete process.env.DATASTORE_DATASET;
});

it('should throw if a projectId is not specified', function() {
it('should throw if a datasetId can not be found', function() {
assert.throws(function() {
new Dataset();
}, 'A project or dataset ID is required to use a Dataset.');
Expand All @@ -109,6 +128,22 @@ describe('Dataset', function() {
assert.strictEqual(dataset.namespace, OPTIONS.namespace);
});

it('should normalize the arguments', function() {
var normalizeArgumentsCalled = false;
var fakeOptions = { projectId: 'project-id' };
var fakeContext = {};

normalizeArgumentsOverride = function(context, options) {
normalizeArgumentsCalled = true;
assert.strictEqual(context, fakeContext);
assert.strictEqual(options, fakeOptions);
return options;
};

Dataset.call(fakeContext, fakeOptions);
assert(normalizeArgumentsCalled);
});

it('should create an authenticated request factory', function() {
var authenticatedRequest = {};
var customEndpoint = 'custom-endpoint';
Expand Down

0 comments on commit 5dcb369

Please sign in to comment.