Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datastore: recognize DATASTORE_DATASET env var #1043

Merged
merged 1 commit into from
Jan 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 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 Expand Up @@ -306,7 +310,7 @@ Dataset.prototype.runInTransaction = function(fn, callback) {
* @private
*/
Dataset.prototype.createTransaction_ = function() {
return new Transaction(this, this.projectId);
return new Transaction(this, this.datasetId);
};

module.exports = Dataset;
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
7 changes: 3 additions & 4 deletions lib/datastore/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ var extend = require('extend');
*
* @param {module:common/connection#Connection} connection - An authenticated
* connection to Google Cloud Datastore.
* @param {string} projectId - Dataset ID. This is your project ID from the
* Google Developers Console.
* @param {string} datasetId - Dataset ID.
*
* @example
* // This is how to create a transaction object directly using this Transaction
Expand All @@ -69,11 +68,11 @@ var extend = require('extend');
* // `transaction` is a Transaction object.
* }, function(err) {});
*/
function Transaction(dataset, projectId) {
function Transaction(dataset, datasetId) {
this.id = null;
this.apiEndpoint = dataset.apiEndpoint;
this.makeAuthenticatedRequest_ = dataset.makeAuthenticatedRequest_;
this.projectId = projectId;
this.datasetId = datasetId;

// A queue for entity modifications made during the transaction.
this.modifiedEntities_ = [];
Expand Down
43 changes: 34 additions & 9 deletions test/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ util.makeAuthenticatedRequestFactory = function() {
return makeAuthenticatedRequestFactoryCache.apply(this, arguments);
};

function FakeTransaction() {
this.calledWith_ = arguments;
}

describe('Dataset', function() {
var Dataset;
var dataset;
Expand All @@ -46,6 +50,7 @@ describe('Dataset', function() {

before(function() {
mockery.registerMock('../common/util.js', util);
mockery.registerMock('./transaction.js', FakeTransaction);

mockery.enable({
useCleanCache: true,
Expand All @@ -61,15 +66,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 +105,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 +136,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 Expand Up @@ -337,4 +353,13 @@ describe('Dataset', function() {
});
});
});

describe('createTransaction_', function() {
it('should create and return a Transaction', function() {
var transaction = dataset.createTransaction_();
assert(transaction instanceof FakeTransaction);
assert.strictEqual(transaction.calledWith_[0], dataset);
assert.strictEqual(transaction.calledWith_[1], dataset.datasetId);
});
});
});
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
6 changes: 3 additions & 3 deletions test/datastore/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,21 @@ describe('Transaction', function() {

describe('instantiation', function() {
it('should assign default properties', function() {
var projectId = 'abc';
var datasetId = 'abc';
var fakeDataset = {
apiEndpoint: 'http://localhost:8080',
makeAuthenticatedRequest_: function fakeMakeAuthenticatedRequest_() {}
};

var transaction = new Transaction(fakeDataset, projectId);
var transaction = new Transaction(fakeDataset, datasetId);

assert.strictEqual(transaction.id, null);
assert.deepEqual(transaction.apiEndpoint, fakeDataset.apiEndpoint);
assert.equal(
transaction.makeAuthenticatedRequest_,
fakeDataset.makeAuthenticatedRequest_
);
assert.equal(transaction.projectId, projectId);
assert.equal(transaction.datasetId, datasetId);
});
});

Expand Down