From b805ba63c84d76eb57267ce81502993387eb64eb Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Thu, 19 Nov 2015 14:26:09 -0500 Subject: [PATCH] Delete all created Task entities after tests run --- datastore/concepts.js | 9 +++------ test/datastore/entity.test.js | 9 +++++++++ test/datastore/indexes.test.js | 9 +++++++++ test/datastore/metadata.test.js | 9 +++++++++ test/datastore/query.test.js | 23 ++++++++++++++++++++-- test/datastore/transaction.test.js | 9 +++++++++ test/datastore/util.js | 31 ++++++++++++++++++++++++++++++ 7 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 test/datastore/util.js diff --git a/datastore/concepts.js b/datastore/concepts.js index 3d0223625a..eaf4bd913d 100644 --- a/datastore/concepts.js +++ b/datastore/concepts.js @@ -343,10 +343,8 @@ Entity.prototype.testDelete = function(callback) { }; Entity.prototype.testBatchUpsert = function(callback) { - datastore.key = this.datastore.key; - - var taskKey1 = datastore.key(['Task', 1]); - var taskKey2 = datastore.key(['Task', 2]); + var taskKey1 = this.datastore.key(['Task', 1]); + var taskKey2 = this.datastore.key(['Task', 2]); var task1 = { type: 'Personal', @@ -654,8 +652,7 @@ Query.prototype.getProjectionQuery = function() { // [START projection_query] var query = datastore.createQuery('Task') - .select('priority') - .select('percent_complete'); + .select(['priority', 'percent_complete']); // [END projection_query] return query; diff --git a/test/datastore/entity.test.js b/test/datastore/entity.test.js index 7ae38a8440..bc6d907ed9 100644 --- a/test/datastore/entity.test.js +++ b/test/datastore/entity.test.js @@ -13,6 +13,8 @@ 'use strict'; +var testUtil = require('./util.js'); + var Entity = require('../../datastore/concepts').Entity; var entity; @@ -22,6 +24,13 @@ describe('datastore/concepts/entity', function () { entity = new Entity(projectId); }); + after(function(done) { + var datastore = entity.datastore; + var query = datastore.createQuery('Task'); + + testUtil.deleteEntities(datastore, query, done); + }); + describe('incomplete key', function() { it('saves with an incomplete key', function(done) { entity.testIncompleteKey(done); diff --git a/test/datastore/indexes.test.js b/test/datastore/indexes.test.js index e208e1efbe..c84c1fd54e 100644 --- a/test/datastore/indexes.test.js +++ b/test/datastore/indexes.test.js @@ -13,6 +13,8 @@ 'use strict'; +var testUtil = require('./util.js'); + var Index = require('../../datastore/concepts').Index; var index; @@ -22,6 +24,13 @@ describe('datastore/concepts/indexes', function () { index = new Index(projectId); }); + after(function(done) { + var datastore = index.datastore; + var query = datastore.createQuery('Task'); + + testUtil.deleteEntities(datastore, query, done); + }); + describe('unindexed properties', function() { it('performs a query with a filter on an unindexed property', function(done) { diff --git a/test/datastore/metadata.test.js b/test/datastore/metadata.test.js index 182993bd5e..89dbe81226 100644 --- a/test/datastore/metadata.test.js +++ b/test/datastore/metadata.test.js @@ -13,6 +13,8 @@ 'use strict'; +var testUtil = require('./util.js'); + var Metadata = require('../../datastore/concepts').Metadata; var metadata; @@ -22,6 +24,13 @@ describe('datastore/concepts/metadata', function () { metadata = new Metadata(projectId); }); + after(function(done) { + var datastore = metadata.datastore; + var query = datastore.createQuery('Task'); + + testUtil.deleteEntities(datastore, query, done); + }); + describe('namespace query', function() { it('performs a namespace query', function(done) { metadata.testNamespaceRunQuery(done); diff --git a/test/datastore/query.test.js b/test/datastore/query.test.js index 3fbd6a4d8b..98a32a1c7f 100644 --- a/test/datastore/query.test.js +++ b/test/datastore/query.test.js @@ -15,13 +15,24 @@ var assert = require('assert'); -var Query = require('../../datastore/concepts').Query; +var concepts = require('../../datastore/concepts.js'); +var testUtil = require('./util.js'); + var query; +var entity; describe('datastore/concepts/query', function () { before(function() { var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples'; - query = new Query(projectId); + entity = new concepts.Entity(projectId); + query = new concepts.Query(projectId); + }); + + after(function(done) { + var datastore = query.datastore; + var q = datastore.createQuery('Task'); + + testUtil.deleteEntities(datastore, q, done); }); describe('basic query', function() { @@ -73,6 +84,10 @@ describe('datastore/concepts/query', function () { }); describe('projection query', function() { + before(function(done) { + entity.testProperties(done); + }); + it('performs a projection query', function(done) { query.testRunQueryProjection(done); }); @@ -158,6 +173,10 @@ describe('datastore/concepts/query', function () { }); describe('cursor paging', function() { + before(function(done) { + entity.testBatchUpsert(done); + }); + it('allows manual pagination through results', function(done) { query.testCursorPaging(done); }); diff --git a/test/datastore/transaction.test.js b/test/datastore/transaction.test.js index d5ed923619..64f482fa57 100644 --- a/test/datastore/transaction.test.js +++ b/test/datastore/transaction.test.js @@ -13,6 +13,8 @@ 'use strict'; +var testUtil = require('./util.js'); + var Transaction = require('../../datastore/concepts').Transaction; var transaction; @@ -22,6 +24,13 @@ describe('datastore/concepts/transaction', function () { transaction = new Transaction(projectId); }); + after(function(done) { + var datastore = transaction.datastore; + var query = datastore.createQuery('Task'); + + testUtil.deleteEntities(datastore, query, done); + }); + describe('update', function() { it('performs a transactional update', function(done) { transaction.testTransactionalUpdate(done); diff --git a/test/datastore/util.js b/test/datastore/util.js new file mode 100644 index 0000000000..0b01e787af --- /dev/null +++ b/test/datastore/util.js @@ -0,0 +1,31 @@ +// Copyright 2015, Google, Inc. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +module.exports = { + deleteEntities: function(datastore, query, callback) { + datastore.runQuery(query, function(err, entities) { + if (err) { + callback(err); + return; + } + + var keys = entities.map(function(entity) { + return entity.key; + }); + + datastore.delete(keys, callback); + }); + } +}; \ No newline at end of file