From eb7f40ac2b3ffc3825107d95f24534921266aaba Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Mon, 16 Nov 2015 16:24:37 -0800 Subject: [PATCH] Getting datastore tests running on CI. --- datastore/concepts.js | 48 +++++-- datastore/tasks.js | 33 +++-- test/datastore/entity.test.js | 136 +++++++++---------- test/datastore/indexes.test.js | 26 ++-- test/datastore/metadata.test.js | 40 +++--- test/datastore/query.test.js | 202 +++++++++++++++-------------- test/datastore/tasks.test.js | 108 ++++++++------- test/datastore/transaction.test.js | 40 +++--- 8 files changed, 339 insertions(+), 294 deletions(-) diff --git a/datastore/concepts.js b/datastore/concepts.js index 2ba3c25d28..773ba06d13 100644 --- a/datastore/concepts.js +++ b/datastore/concepts.js @@ -36,10 +36,18 @@ var datastore = { save: function() {} }; +var keyFile = process.env.DATASTORE_KEYFILE || + process.env.GOOGLE_APPLICATION_CREDENTIALS; + function Entity(projectId) { - this.datastore = gcloud.datastore({ + var options = { projectId: projectId - }); + }; + + if (keyFile) { + options.keyFilename = keyFile; + } + this.datastore = gcloud.datastore(options); // To create the keys, we have to use this instance of Datastore. datastore.key = this.datastore.key; @@ -437,9 +445,14 @@ Entity.prototype.testBatchDelete = function(callback) { }; function Index(projectId) { - this.datastore = gcloud.datastore({ + var options = { projectId: projectId - }); + }; + + if (keyFile) { + options.keyFilename = keyFile; + } + this.datastore = gcloud.datastore(options); } Index.prototype.testUnindexedPropertyQuery = function(callback) { @@ -481,9 +494,14 @@ Index.prototype.testExplodingProperties = function(callback) { }; function Metadata(projectId) { - this.datastore = gcloud.datastore({ + var options = { projectId: projectId - }); + }; + + if (keyFile) { + options.keyFilename = keyFile; + } + this.datastore = gcloud.datastore(options); } Metadata.prototype.testNamespaceRunQuery = function(callback) { @@ -614,9 +632,14 @@ Metadata.prototype.testPropertyByKindRunQuery = function(callback) { }; function Query(projectId) { - this.datastore = gcloud.datastore({ + var options = { projectId: projectId - }); + }; + + if (keyFile) { + options.keyFilename = keyFile; + } + this.datastore = gcloud.datastore(options); this.basicQuery = this.getBasicQuery(); this.projectionQuery = this.getProjectionQuery(); @@ -1039,9 +1062,14 @@ function transferFunds(fromKey, toKey, amount, callback) { // [END transactional_update] function Transaction(projectId) { - this.datastore = gcloud.datastore({ + var options = { projectId: projectId - }); + }; + + if (keyFile) { + options.keyFilename = keyFile; + } + this.datastore = gcloud.datastore(options); this.fromKey = this.datastore.key(['Bank', 1, 'Account', 1]); this.toKey = this.datastore.key(['Bank', 1, 'Account', 2]); diff --git a/datastore/tasks.js b/datastore/tasks.js index 9e0436fc44..fc583d5568 100755 --- a/datastore/tasks.js +++ b/datastore/tasks.js @@ -21,10 +21,18 @@ var projectId = process.env.DATASTORE_PROJECT_ID || process.env.TEST_PROJECT_ID; if (!projectId) { throw new Error('TEST_PROJECT_ID environment variable required.'); } +var keyFile = process.env.DATASTORE_KEYFILE || + process.env.GOOGLE_APPLICATION_CREDENTIALS; -var datastore = gcloud.datastore({ +var options = { projectId: projectId -}); +}; + +if (keyFile) { + options.keyFilename = keyFile; +} + +var datastore = gcloud.datastore(options); /* // [START build_service] @@ -216,14 +224,17 @@ switch (command) { } default: { - console.log([ - 'Usage:', - '', - ' new Adds a task with a description ', - ' done Marks a task as done', - ' list Lists all tasks by creation time', - ' delete Deletes a task' - ].join('\n')); + // Only print usage if this file is being executed directly + if (module === require.main) { + console.log([ + 'Usage:', + '', + ' new Adds a task with a description ', + ' done Marks a task as done', + ' list Lists all tasks by creation time', + ' delete Deletes a task' + ].join('\n')); + } } } @@ -231,4 +242,4 @@ module.exports.addEntity = addTask; module.exports.updateEntity = markDone; module.exports.retrieveEntities = listTasks; module.exports.deleteEntity = deleteTask; -module.exports.formatResults = formatTasks; +module.exports.formatTasks = formatTasks; diff --git a/test/datastore/entity.test.js b/test/datastore/entity.test.js index d0992836c8..7ae38a8440 100644 --- a/test/datastore/entity.test.js +++ b/test/datastore/entity.test.js @@ -16,103 +16,105 @@ var Entity = require('../../datastore/concepts').Entity; var entity; -before(function() { - var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples'; - entity = new Entity(projectId); -}); +describe('datastore/concepts/entity', function () { + before(function() { + var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples'; + entity = new Entity(projectId); + }); -describe('incomplete key', function() { - it('saves with an incomplete key', function(done) { - entity.testIncompleteKey(done); + describe('incomplete key', function() { + it('saves with an incomplete key', function(done) { + entity.testIncompleteKey(done); + }); }); -}); -describe('testNamedKey', function() { - it('saves with a named key', function(done) { - entity.testNamedKey(done); + describe('testNamedKey', function() { + it('saves with a named key', function(done) { + entity.testNamedKey(done); + }); }); -}); -describe('testKeyWithParent', function() { - it('saves a key with a parent', function(done) { - entity.testKeyWithParent(done); + describe('testKeyWithParent', function() { + it('saves a key with a parent', function(done) { + entity.testKeyWithParent(done); + }); }); -}); -describe('testKeyWithMultiLevelParent', function() { - it('saves a key with multiple parents', function(done) { - entity.testKeyWithMultiLevelParent(done); + describe('testKeyWithMultiLevelParent', function() { + it('saves a key with multiple parents', function(done) { + entity.testKeyWithMultiLevelParent(done); + }); }); -}); -describe('testEntityWithParent', function() { - it('saves an entity with a parent', function(done) { - entity.testEntityWithParent(done); + describe('testEntityWithParent', function() { + it('saves an entity with a parent', function(done) { + entity.testEntityWithParent(done); + }); }); -}); -describe('testProperties', function() { - it('saves an entity with properties', function(done) { - entity.testProperties(done); + describe('testProperties', function() { + it('saves an entity with properties', function(done) { + entity.testProperties(done); + }); }); -}); -describe('testArrayValue', function() { - it('saves an entity with arrays', function(done) { - entity.testArrayValue(done); + describe('testArrayValue', function() { + it('saves an entity with arrays', function(done) { + entity.testArrayValue(done); + }); }); -}); -describe('testBasicEntity', function() { - it('saves a basic entity', function(done) { - entity.testBasicEntity(done); + describe('testBasicEntity', function() { + it('saves a basic entity', function(done) { + entity.testBasicEntity(done); + }); }); -}); -describe('testUpsert', function() { - it('saves with an upsert', function(done) { - entity.testUpsert(done); + describe('testUpsert', function() { + it('saves with an upsert', function(done) { + entity.testUpsert(done); + }); }); -}); -describe('testInsert', function() { - it('saves with an insert', function(done) { - entity.testInsert(done); + describe('testInsert', function() { + it('saves with an insert', function(done) { + entity.testInsert(done); + }); }); -}); -describe('testLookup', function() { - it('performs a lookup', function(done) { - entity.testLookup(done); + describe('testLookup', function() { + it('performs a lookup', function(done) { + entity.testLookup(done); + }); }); -}); -describe('testUpdate', function() { - it('saves with an update', function(done) { - entity.testUpdate(done); + describe('testUpdate', function() { + it('saves with an update', function(done) { + entity.testUpdate(done); + }); }); -}); -describe('testDelete', function() { - it('deletes an entity', function(done) { - entity.testDelete(done); + describe('testDelete', function() { + it('deletes an entity', function(done) { + entity.testDelete(done); + }); }); -}); -describe('testBatchUpsert', function() { - it('performs a batch upsert', function(done) { - entity.testBatchUpsert(done); + describe('testBatchUpsert', function() { + it('performs a batch upsert', function(done) { + entity.testBatchUpsert(done); + }); }); -}); -describe('testBatchLookup', function() { - it('performs a batch lookup', function(done) { - entity.testBatchLookup(done); + describe('testBatchLookup', function() { + it('performs a batch lookup', function(done) { + entity.testBatchLookup(done); + }); }); -}); -describe('testBatchDelete', function() { - it('performs a batch delete', function(done) { - entity.testBatchDelete(done); + describe('testBatchDelete', function() { + it('performs a batch delete', function(done) { + entity.testBatchDelete(done); + }); }); }); diff --git a/test/datastore/indexes.test.js b/test/datastore/indexes.test.js index 193f4cb0aa..e208e1efbe 100644 --- a/test/datastore/indexes.test.js +++ b/test/datastore/indexes.test.js @@ -16,19 +16,23 @@ var Index = require('../../datastore/concepts').Index; var index; -before(function() { - var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples'; - index = new Index(projectId); -}); +describe('datastore/concepts/indexes', function () { + before(function() { + var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples'; + index = new Index(projectId); + }); -describe('unindexed properties', function() { - it('performs a query with a filter on an unindexed property', function(done) { - index.testUnindexedPropertyQuery(done); + describe('unindexed properties', function() { + it('performs a query with a filter on an unindexed property', + function(done) { + index.testUnindexedPropertyQuery(done); + } + ); }); -}); -describe('exploding properties', function() { - it('inserts arrays of data', function(done) { - index.testExplodingProperties(done); + describe('exploding properties', function() { + it('inserts arrays of data', function(done) { + index.testExplodingProperties(done); + }); }); }); diff --git a/test/datastore/metadata.test.js b/test/datastore/metadata.test.js index e64ed9d494..182993bd5e 100644 --- a/test/datastore/metadata.test.js +++ b/test/datastore/metadata.test.js @@ -16,31 +16,33 @@ var Metadata = require('../../datastore/concepts').Metadata; var metadata; -before(function() { - var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples'; - metadata = new Metadata(projectId); -}); +describe('datastore/concepts/metadata', function () { + before(function() { + var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples'; + metadata = new Metadata(projectId); + }); -describe('namespace query', function() { - it('performs a namespace query', function(done) { - metadata.testNamespaceRunQuery(done); + describe('namespace query', function() { + it('performs a namespace query', function(done) { + metadata.testNamespaceRunQuery(done); + }); }); -}); -describe('kinds query', function() { - it('performs a kind query', function(done) { - metadata.testKindRunQuery(done); + describe('kinds query', function() { + it('performs a kind query', function(done) { + metadata.testKindRunQuery(done); + }); }); -}); -describe('property query', function() { - it('performs a property query', function(done) { - metadata.testPropertyRunQuery(done); + describe('property query', function() { + it('performs a property query', function(done) { + metadata.testPropertyRunQuery(done); + }); }); -}); -describe('property by kind query', function() { - it('performs a property by kind query', function(done) { - metadata.testPropertyByKindRunQuery(done); + describe('property by kind query', function() { + it('performs a property by kind query', function(done) { + metadata.testPropertyByKindRunQuery(done); + }); }); }); diff --git a/test/datastore/query.test.js b/test/datastore/query.test.js index 0f981e902d..3fbd6a4d8b 100644 --- a/test/datastore/query.test.js +++ b/test/datastore/query.test.js @@ -18,152 +18,154 @@ var assert = require('assert'); var Query = require('../../datastore/concepts').Query; var query; -before(function() { - var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples'; - query = new Query(projectId); -}); - -describe('basic query', function() { - it('performs a basic query', function(done) { - query.testRunQuery(done); +describe('datastore/concepts/query', function () { + before(function() { + var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples'; + query = new Query(projectId); }); -}); -describe('property filter', function() { - it('performs a query with a property filter', function(done) { - query.testPropertyFilter(done); + describe('basic query', function() { + it('performs a basic query', function(done) { + query.testRunQuery(done); + }); }); -}); -describe('composite filter', function() { - it('performs a query with a composite filter', function(done) { - query.testCompositeFilter(done); + describe('property filter', function() { + it('performs a query with a property filter', function(done) { + query.testPropertyFilter(done); + }); }); -}); -describe('key filter', function() { - it('performs a query with a key filter', function(done) { - query.testKeyFilter(done); + describe('composite filter', function() { + it('performs a query with a composite filter', function(done) { + query.testCompositeFilter(done); + }); }); -}); -describe('ascending sort', function() { - it('performs a query with ascending sort', function(done) { - query.testAscendingSort(done); + describe('key filter', function() { + it('performs a query with a key filter', function(done) { + query.testKeyFilter(done); + }); }); -}); -describe('descending sort', function() { - it('performs a query with descending sort', function(done) { - query.testDescendingSort(done); + describe('ascending sort', function() { + it('performs a query with ascending sort', function(done) { + query.testAscendingSort(done); + }); }); -}); -describe('multi sort', function() { - it('performs a query with multi sort', function(done) { - query.testMultiSort(done); + describe('descending sort', function() { + it('performs a query with descending sort', function(done) { + query.testDescendingSort(done); + }); }); -}); -describe('kindless query', function() { - it('performs a kindless query', function(done) { - query.testKindlessQuery(done); + describe('multi sort', function() { + it('performs a query with multi sort', function(done) { + query.testMultiSort(done); + }); }); -}); -describe('projection query', function() { - it('performs a projection query', function(done) { - query.testRunQueryProjection(done); + describe('kindless query', function() { + it('performs a kindless query', function(done) { + query.testKindlessQuery(done); + }); }); -}); -describe('keys only query', function() { - it('performs a keys only query', function(done) { - query.testKeysOnlyQuery(done); + describe('projection query', function() { + it('performs a projection query', function(done) { + query.testRunQueryProjection(done); + }); }); -}); -describe('distinct query', function() { - it('performs a distinct query', function(done) { - query.testDistinctQuery(done); + describe('keys only query', function() { + it('performs a keys only query', function(done) { + query.testKeysOnlyQuery(done); + }); }); -}); -describe('distinct on query', function() { - it('performs a distinct on query', function(done) { - query.testDistinctOnQuery(done); + describe('distinct query', function() { + it('performs a distinct query', function(done) { + query.testDistinctQuery(done); + }); }); -}); -describe('array value inequality range', function() { - it('performs an array value inequality query', function(done) { - query.testArrayValueInequalityRange(done); + describe('distinct on query', function() { + it('performs a distinct on query', function(done) { + query.testDistinctOnQuery(done); + }); }); -}); -describe('array value equality', function() { - it('performs an array value equality query', function(done) { - query.testArrayValueEquality(done); + describe('array value inequality range', function() { + it('performs an array value inequality query', function(done) { + query.testArrayValueInequalityRange(done); + }); }); -}); -describe('inequality range', function() { - it('performs an inequality range query', function(done) { - query.testInequalityRange(done); + describe('array value equality', function() { + it('performs an array value equality query', function(done) { + query.testArrayValueEquality(done); + }); }); -}); -describe('inequality invalid', function() { - it('returns an error from an invalid query', function(done) { - query.testInequalityInvalid(function(err) { - assert.notStrictEqual(err, null); - done(); + describe('inequality range', function() { + it('performs an inequality range query', function(done) { + query.testInequalityRange(done); }); }); -}); -describe('equal and inequality range', function() { - it('performs an equal and inequality range query', function(done) { - query.testEqualAndInequalityRange(done); + describe('inequality invalid', function() { + it('returns an error from an invalid query', function(done) { + query.testInequalityInvalid(function(err) { + assert.notStrictEqual(err, null); + done(); + }); + }); }); -}); -describe('inequality sort', function() { - it('performs an equality sort query', function(done) { - query.testInequalitySort(done); + describe('equal and inequality range', function() { + it('performs an equal and inequality range query', function(done) { + query.testEqualAndInequalityRange(done); + }); }); -}); -describe('inequality sort invalid', function() { - it('returns an error when not sorted on filtered property', function(done) { - query.testInequalitySortInvalidNotSame(function(err) { - assert.notStrictEqual(err, null); - done(); + describe('inequality sort', function() { + it('performs an equality sort query', function(done) { + query.testInequalitySort(done); }); }); - it('returns an error when not sorted on first filter prop', function(done) { - query.testInequalitySortInvalidNotFirst(function(err) { - assert.notStrictEqual(err, null); - done(); + describe('inequality sort invalid', function() { + it('returns an error when not sorted on filtered property', function(done) { + query.testInequalitySortInvalidNotSame(function(err) { + assert.notStrictEqual(err, null); + done(); + }); + }); + + it('returns an error when not sorted on first filter prop', function(done) { + query.testInequalitySortInvalidNotFirst(function(err) { + assert.notStrictEqual(err, null); + done(); + }); }); }); -}); -describe('limit query', function() { - it('performs a query with a limit', function(done) { - query.testLimit(done); + describe('limit query', function() { + it('performs a query with a limit', function(done) { + query.testLimit(done); + }); }); -}); -describe('cursor paging', function() { - it('allows manual pagination through results', function(done) { - query.testCursorPaging(done); + describe('cursor paging', function() { + it('allows manual pagination through results', function(done) { + query.testCursorPaging(done); + }); }); -}); -describe.skip('eventually consistent query', function() { - it('performs an ancestor query', function(done) { - query.testEventualConsistentQuery(done); + describe.skip('eventually consistent query', function() { + it('performs an ancestor query', function(done) { + query.testEventualConsistentQuery(done); + }); }); }); diff --git a/test/datastore/tasks.test.js b/test/datastore/tasks.test.js index d011a8e2cb..9880dfe096 100644 --- a/test/datastore/tasks.test.js +++ b/test/datastore/tasks.test.js @@ -14,79 +14,72 @@ 'use strict'; var assert = require('assert'); +var async = require('async'); var tasks = require('../../datastore/tasks.js'); +var taskIds = []; -describe('adding an entity', function() { - it('should add a task', function(done) { - tasks.addEntity('description', done); - }); -}); - -describe('updating an entity', function() { - var taskId; - - before(function(done) { - getTaskId(function(err, _taskId) { - if (err) { - done(err); - return; - } +describe('datastore/tasks/', function() { - taskId = _taskId; - done(); - }); - }); - - it('should mark a task as done', function(done) { - tasks.updateEntity(taskId, done); + after(function (done) { + async.parallel(taskIds.map(function(taskId) { + return function (cb) { + tasks.deleteEntity(taskId, cb); + }; + }), done); }); -}); -describe('retrieving entities', function() { - it('should list tasks', function(done) { - tasks.retrieveEntities(done); + describe('adding an entity', function() { + it('should add a task', function(done) { + getTaskId(done); + }); }); -}); - -describe('deleting an entity', function() { - var taskId; - - before(function(done) { - getTaskId(function(err, _taskId) { - if (err) { - done(err); - return; - } - taskId = _taskId; - done(); + describe('updating an entity', function() { + it('should mark a task as done', function(done) { + getTaskId(function (err, taskId) { + if (err) { + return done(err); + } + tasks.updateEntity(taskId, done); + }); }); }); - it('should delete a task', function(done) { - tasks.deleteEntity(taskId, done); + describe('retrieving entities', function() { + it('should list tasks', function(done) { + tasks.retrieveEntities(done); + }); }); -}); - -describe('formatting results', function() { - var tasks; - - before(function(done) { - tasks.retrieveEntities(function(err, _tasks) { - if (err) { - done(err); - return; - } - tasks = _tasks; - done(); + describe('deleting an entity', function() { + it('should delete a task', function(done) { + getTaskId(function (err, taskId) { + if (err) { + return done(err); + } + tasks.deleteEntity(taskId, done); + }); }); }); - it('should format tasks', function() { - assert.doesNotThrow(function() { - tasks.formatResults(tasks); + describe('formatting results', function() { + it('should format tasks', function(done) { + tasks.retrieveEntities(function(err, _tasks) { + if (err) { + done(err); + return; + } + + try { + assert.doesNotThrow(function() { + tasks.formatTasks(_tasks); + }); + done(); + } catch (err) { + done(err); + } + }); }); }); }); @@ -99,6 +92,7 @@ function getTaskId(callback) { } var taskId = taskKey.path.pop(); + taskIds.push(taskId); callback(null, taskId); }); } diff --git a/test/datastore/transaction.test.js b/test/datastore/transaction.test.js index 5457e2f018..d5ed923619 100644 --- a/test/datastore/transaction.test.js +++ b/test/datastore/transaction.test.js @@ -16,31 +16,33 @@ var Transaction = require('../../datastore/concepts').Transaction; var transaction; -before(function() { - var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples'; - transaction = new Transaction(projectId); -}); +describe('datastore/concepts/transaction', function () { + before(function() { + var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples'; + transaction = new Transaction(projectId); + }); -describe('update', function() { - it('performs a transactional update', function(done) { - transaction.testTransactionalUpdate(done); + describe('update', function() { + it('performs a transactional update', function(done) { + transaction.testTransactionalUpdate(done); + }); }); -}); -describe('retry', function() { - it('performs retries if necessary', function(done) { - transaction.testTransactionalRetry(done); + describe('retry', function() { + it('performs retries if necessary', function(done) { + transaction.testTransactionalRetry(done); + }); }); -}); -describe('getOrCreate', function() { - it('performs a get or create', function(done) { - transaction.testTransactionalGetOrCreate(done); + describe('getOrCreate', function() { + it('performs a get or create', function(done) { + transaction.testTransactionalGetOrCreate(done); + }); }); -}); -describe('single entity group read only', function() { - it('gets a snapshot of task list entities', function(done) { - transaction.testSingleEntityGroupReadOnly(done); + describe('single entity group read only', function() { + it('gets a snapshot of task list entities', function(done) { + transaction.testSingleEntityGroupReadOnly(done); + }); }); });