Skip to content

Commit

Permalink
datastore: recognize DATASTORE_DATASET env var
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Jan 4, 2016
1 parent fbf1ecc commit bed1c99
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
10 changes: 7 additions & 3 deletions lib/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ var SCOPES = [
* Interact with a dataset from the
* [Google Cloud Datastore](https://developers.google.com/datastore/).
*
* If a project ID is not specified, the `DATASTORE_DATASET` environment
* variable from the gcloud SDK is used.
*
* @constructor
* @alias module:datastore/dataset
* @mixes module:datastore/request
Expand Down Expand Up @@ -106,13 +109,14 @@ function Dataset(options) {

options = options || {};

if (!options.projectId) {
throw util.missingProjectIdError;
this.datasetId = options.projectId || process.env.DATASTORE_DATASET;

if (!this.datasetId) {
throw new Error('A project or dataset ID is required to use a Dataset.');
}

this.determineApiEndpoint_(options.apiEndpoint);
this.namespace = options.namespace;
this.projectId = options.projectId;

this.makeAuthenticatedRequest_ = util.makeAuthenticatedRequestFactory({
customEndpoint: this.customEndpoint,
Expand Down
4 changes: 2 additions & 2 deletions lib/datastore/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,10 +784,10 @@ DatastoreRequest.prototype.makeReq_ = function(method, body, callback) {

var reqOpts = {
method: 'POST',
uri: format('{apiEndpoint}/{path}/{projectId}/{method}', {
uri: format('{apiEndpoint}/{path}/{datasetId}/{method}', {
apiEndpoint: this.apiEndpoint,
path: 'datastore/v1beta2/datasets',
projectId: this.projectId,
datasetId: this.datasetId,
method: method
}),
body: is.empty(body) ? '' : pbRequest,
Expand Down
29 changes: 20 additions & 9 deletions test/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,30 @@ describe('Dataset', function() {
});

beforeEach(function() {
delete process.env.DATASTORE_DATASET;
makeAuthenticatedRequestFactoryOverride = null;
dataset = new Dataset(OPTIONS);
});

describe('instantiation', function() {
it('should localize the dataset id', function() {
assert.strictEqual(dataset.datasetId, OPTIONS.projectId);
});

it('should detect the dataset ID', function() {
var datasetId = 'dataset-id';
process.env.DATASTORE_DATASET = datasetId;

var ds = new Dataset();
assert.strictEqual(ds.datasetId, datasetId);

delete process.env.DATASTORE_DATASET;
});

it('should throw if a projectId is not specified', function() {
assert.throws(function() {
new Dataset();
}, /Sorry, we cannot connect/);
}, 'A project or dataset ID is required to use a Dataset.');
});

it('should set default API connection details', function(done) {
Expand All @@ -85,6 +100,10 @@ describe('Dataset', function() {
new Dataset(OPTIONS);
});

it('should localize the namespace', function() {
assert.strictEqual(dataset.namespace, OPTIONS.namespace);
});

it('should create an authenticated request factory', function() {
var authenticatedRequest = {};
var customEndpoint = 'custom-endpoint';
Expand Down Expand Up @@ -112,14 +131,6 @@ describe('Dataset', function() {
var ds = new Dataset(OPTIONS);
assert.strictEqual(ds.makeAuthenticatedRequest_, authenticatedRequest);
});

it('should localize the project id', function() {
assert.strictEqual(dataset.projectId, OPTIONS.projectId);
});

it('should localize the namespace', function() {
assert.strictEqual(dataset.namespace, OPTIONS.namespace);
});
});

describe('key', function() {
Expand Down
8 changes: 4 additions & 4 deletions test/datastore/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -864,15 +864,15 @@ describe('Request', function() {

it('should assemble correct request', function(done) {
var method = 'commit';
var projectId = 'project-id';
var datasetId = 'dataset-id';
var expectedUri =
format('{apiEndpoint}/datastore/v1beta2/datasets/{pId}/{method}', {
format('{apiEndpoint}/datastore/v1beta2/datasets/{dId}/{method}', {
apiEndpoint: CUSTOM_ENDPOINT,
pId: projectId,
dId: datasetId,
method: method
});

request.projectId = projectId;
request.datasetId = datasetId;
request.makeAuthenticatedRequest_ = function(opts) {
assert.equal(opts.method, 'POST');
assert.equal(opts.uri, expectedUri);
Expand Down

0 comments on commit bed1c99

Please sign in to comment.