Skip to content

Commit

Permalink
Merge pull request #35 from stephenplusplus/datastore-v1beta3
Browse files Browse the repository at this point in the history
Delete all created Task entities after tests run
  • Loading branch information
jmdobry committed Nov 20, 2015
2 parents 6c4a6ef + b805ba6 commit 73251e5
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 8 deletions.
9 changes: 3 additions & 6 deletions datastore/concepts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions test/datastore/entity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

'use strict';

var testUtil = require('./util.js');

var Entity = require('../../datastore/concepts').Entity;
var entity;

Expand All @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions test/datastore/indexes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

'use strict';

var testUtil = require('./util.js');

var Index = require('../../datastore/concepts').Index;
var index;

Expand All @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions test/datastore/metadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

'use strict';

var testUtil = require('./util.js');

var Metadata = require('../../datastore/concepts').Metadata;
var metadata;

Expand All @@ -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);
Expand Down
23 changes: 21 additions & 2 deletions test/datastore/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
});
Expand Down
9 changes: 9 additions & 0 deletions test/datastore/transaction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

'use strict';

var testUtil = require('./util.js');

var Transaction = require('../../datastore/concepts').Transaction;
var transaction;

Expand All @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions test/datastore/util.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
};

0 comments on commit 73251e5

Please sign in to comment.