Skip to content

Commit b922176

Browse files
committed
Merge pull request #30 from GoogleCloudPlatform/datastore-tests
Getting datastore tests running on CI.
2 parents aac4192 + ec17af5 commit b922176

File tree

8 files changed

+339
-295
lines changed

8 files changed

+339
-295
lines changed

datastore/concepts.js

+38-10
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,18 @@ var datastore = {
3636
save: function() {}
3737
};
3838

39+
var keyFile = process.env.DATASTORE_KEYFILE ||
40+
process.env.GOOGLE_APPLICATION_CREDENTIALS;
41+
3942
function Entity(projectId) {
40-
this.datastore = gcloud.datastore({
43+
var options = {
4144
projectId: projectId
42-
});
45+
};
46+
47+
if (keyFile) {
48+
options.keyFilename = keyFile;
49+
}
50+
this.datastore = gcloud.datastore(options);
4351

4452
// To create the keys, we have to use this instance of Datastore.
4553
datastore.key = this.datastore.key;
@@ -427,9 +435,14 @@ Entity.prototype.testBatchDelete = function(callback) {
427435
};
428436

429437
function Index(projectId) {
430-
this.datastore = gcloud.datastore({
438+
var options = {
431439
projectId: projectId
432-
});
440+
};
441+
442+
if (keyFile) {
443+
options.keyFilename = keyFile;
444+
}
445+
this.datastore = gcloud.datastore(options);
433446
}
434447

435448
Index.prototype.testUnindexedPropertyQuery = function(callback) {
@@ -471,9 +484,14 @@ Index.prototype.testExplodingProperties = function(callback) {
471484
};
472485

473486
function Metadata(projectId) {
474-
this.datastore = gcloud.datastore({
487+
var options = {
475488
projectId: projectId
476-
});
489+
};
490+
491+
if (keyFile) {
492+
options.keyFilename = keyFile;
493+
}
494+
this.datastore = gcloud.datastore(options);
477495
}
478496

479497
Metadata.prototype.testNamespaceRunQuery = function(callback) {
@@ -604,9 +622,14 @@ Metadata.prototype.testPropertyByKindRunQuery = function(callback) {
604622
};
605623

606624
function Query(projectId) {
607-
this.datastore = gcloud.datastore({
625+
var options = {
608626
projectId: projectId
609-
});
627+
};
628+
629+
if (keyFile) {
630+
options.keyFilename = keyFile;
631+
}
632+
this.datastore = gcloud.datastore(options);
610633

611634
this.basicQuery = this.getBasicQuery();
612635
this.projectionQuery = this.getProjectionQuery();
@@ -1029,9 +1052,14 @@ function transferFunds(fromKey, toKey, amount, callback) {
10291052
// [END transactional_update]
10301053

10311054
function Transaction(projectId) {
1032-
this.datastore = gcloud.datastore({
1055+
var options = {
10331056
projectId: projectId
1034-
});
1057+
};
1058+
1059+
if (keyFile) {
1060+
options.keyFilename = keyFile;
1061+
}
1062+
this.datastore = gcloud.datastore(options);
10351063

10361064
this.fromKey = this.datastore.key(['Bank', 1, 'Account', 1]);
10371065
this.toKey = this.datastore.key(['Bank', 1, 'Account', 2]);

datastore/tasks.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,20 @@ var projectId = process.env.DATASTORE_PROJECT_ID || process.env.TEST_PROJECT_ID;
2020
if (!projectId) {
2121
throw new Error('TEST_PROJECT_ID environment variable required.');
2222
}
23+
var keyFile = process.env.DATASTORE_KEYFILE ||
24+
process.env.GOOGLE_APPLICATION_CREDENTIALS;
2325

2426
// [START build_service]
2527
var gcloud = require('gcloud');
26-
27-
var datastore = gcloud.datastore({
28+
var options = {
2829
projectId: projectId
29-
});
30+
};
31+
32+
if (keyFile) {
33+
options.keyFilename = keyFile;
34+
}
35+
36+
var datastore = gcloud.datastore(options);
3037
// [END build_service]
3138

3239
/*
@@ -218,19 +225,22 @@ switch (command) {
218225
}
219226

220227
default: {
221-
console.log([
222-
'Usage:',
223-
'',
224-
' new <description> Adds a task with a description <description>',
225-
' done <task-id> Marks a task as done',
226-
' list Lists all tasks by creation time',
227-
' delete <task-id> Deletes a task'
228-
].join('\n'));
228+
// Only print usage if this file is being executed directly
229+
if (module === require.main) {
230+
console.log([
231+
'Usage:',
232+
'',
233+
' new <description> Adds a task with a description <description>',
234+
' done <task-id> Marks a task as done',
235+
' list Lists all tasks by creation time',
236+
' delete <task-id> Deletes a task'
237+
].join('\n'));
238+
}
229239
}
230240
}
231241

232242
module.exports.addEntity = addTask;
233243
module.exports.updateEntity = markDone;
234244
module.exports.retrieveEntities = listTasks;
235245
module.exports.deleteEntity = deleteTask;
236-
module.exports.formatResults = formatTasks;
246+
module.exports.formatTasks = formatTasks;

test/datastore/entity.test.js

+69-67
Original file line numberDiff line numberDiff line change
@@ -16,103 +16,105 @@
1616
var Entity = require('../../datastore/concepts').Entity;
1717
var entity;
1818

19-
before(function() {
20-
var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples';
21-
entity = new Entity(projectId);
22-
});
19+
describe('datastore/concepts/entity', function () {
20+
before(function() {
21+
var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples';
22+
entity = new Entity(projectId);
23+
});
2324

24-
describe('incomplete key', function() {
25-
it('saves with an incomplete key', function(done) {
26-
entity.testIncompleteKey(done);
25+
describe('incomplete key', function() {
26+
it('saves with an incomplete key', function(done) {
27+
entity.testIncompleteKey(done);
28+
});
2729
});
28-
});
2930

30-
describe('testNamedKey', function() {
31-
it('saves with a named key', function(done) {
32-
entity.testNamedKey(done);
31+
describe('testNamedKey', function() {
32+
it('saves with a named key', function(done) {
33+
entity.testNamedKey(done);
34+
});
3335
});
34-
});
3536

36-
describe('testKeyWithParent', function() {
37-
it('saves a key with a parent', function(done) {
38-
entity.testKeyWithParent(done);
37+
describe('testKeyWithParent', function() {
38+
it('saves a key with a parent', function(done) {
39+
entity.testKeyWithParent(done);
40+
});
3941
});
40-
});
4142

42-
describe('testKeyWithMultiLevelParent', function() {
43-
it('saves a key with multiple parents', function(done) {
44-
entity.testKeyWithMultiLevelParent(done);
43+
describe('testKeyWithMultiLevelParent', function() {
44+
it('saves a key with multiple parents', function(done) {
45+
entity.testKeyWithMultiLevelParent(done);
46+
});
4547
});
46-
});
4748

48-
describe('testEntityWithParent', function() {
49-
it('saves an entity with a parent', function(done) {
50-
entity.testEntityWithParent(done);
49+
describe('testEntityWithParent', function() {
50+
it('saves an entity with a parent', function(done) {
51+
entity.testEntityWithParent(done);
52+
});
5153
});
52-
});
5354

54-
describe('testProperties', function() {
55-
it('saves an entity with properties', function(done) {
56-
entity.testProperties(done);
55+
describe('testProperties', function() {
56+
it('saves an entity with properties', function(done) {
57+
entity.testProperties(done);
58+
});
5759
});
58-
});
5960

60-
describe('testArrayValue', function() {
61-
it('saves an entity with arrays', function(done) {
62-
entity.testArrayValue(done);
61+
describe('testArrayValue', function() {
62+
it('saves an entity with arrays', function(done) {
63+
entity.testArrayValue(done);
64+
});
6365
});
64-
});
6566

66-
describe('testBasicEntity', function() {
67-
it('saves a basic entity', function(done) {
68-
entity.testBasicEntity(done);
67+
describe('testBasicEntity', function() {
68+
it('saves a basic entity', function(done) {
69+
entity.testBasicEntity(done);
70+
});
6971
});
70-
});
7172

72-
describe('testUpsert', function() {
73-
it('saves with an upsert', function(done) {
74-
entity.testUpsert(done);
73+
describe('testUpsert', function() {
74+
it('saves with an upsert', function(done) {
75+
entity.testUpsert(done);
76+
});
7577
});
76-
});
7778

78-
describe('testInsert', function() {
79-
it('saves with an insert', function(done) {
80-
entity.testInsert(done);
79+
describe('testInsert', function() {
80+
it('saves with an insert', function(done) {
81+
entity.testInsert(done);
82+
});
8183
});
82-
});
8384

84-
describe('testLookup', function() {
85-
it('performs a lookup', function(done) {
86-
entity.testLookup(done);
85+
describe('testLookup', function() {
86+
it('performs a lookup', function(done) {
87+
entity.testLookup(done);
88+
});
8789
});
88-
});
8990

90-
describe('testUpdate', function() {
91-
it('saves with an update', function(done) {
92-
entity.testUpdate(done);
91+
describe('testUpdate', function() {
92+
it('saves with an update', function(done) {
93+
entity.testUpdate(done);
94+
});
9395
});
94-
});
9596

96-
describe('testDelete', function() {
97-
it('deletes an entity', function(done) {
98-
entity.testDelete(done);
97+
describe('testDelete', function() {
98+
it('deletes an entity', function(done) {
99+
entity.testDelete(done);
100+
});
99101
});
100-
});
101102

102-
describe('testBatchUpsert', function() {
103-
it('performs a batch upsert', function(done) {
104-
entity.testBatchUpsert(done);
103+
describe('testBatchUpsert', function() {
104+
it('performs a batch upsert', function(done) {
105+
entity.testBatchUpsert(done);
106+
});
105107
});
106-
});
107108

108-
describe('testBatchLookup', function() {
109-
it('performs a batch lookup', function(done) {
110-
entity.testBatchLookup(done);
109+
describe('testBatchLookup', function() {
110+
it('performs a batch lookup', function(done) {
111+
entity.testBatchLookup(done);
112+
});
111113
});
112-
});
113114

114-
describe('testBatchDelete', function() {
115-
it('performs a batch delete', function(done) {
116-
entity.testBatchDelete(done);
115+
describe('testBatchDelete', function() {
116+
it('performs a batch delete', function(done) {
117+
entity.testBatchDelete(done);
118+
});
117119
});
118120
});

test/datastore/indexes.test.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@
1616
var Index = require('../../datastore/concepts').Index;
1717
var index;
1818

19-
before(function() {
20-
var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples';
21-
index = new Index(projectId);
22-
});
19+
describe('datastore/concepts/indexes', function () {
20+
before(function() {
21+
var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples';
22+
index = new Index(projectId);
23+
});
2324

24-
describe('unindexed properties', function() {
25-
it('performs a query with a filter on an unindexed property', function(done) {
26-
index.testUnindexedPropertyQuery(done);
25+
describe('unindexed properties', function() {
26+
it('performs a query with a filter on an unindexed property',
27+
function(done) {
28+
index.testUnindexedPropertyQuery(done);
29+
}
30+
);
2731
});
28-
});
2932

30-
describe('exploding properties', function() {
31-
it('inserts arrays of data', function(done) {
32-
index.testExplodingProperties(done);
33+
describe('exploding properties', function() {
34+
it('inserts arrays of data', function(done) {
35+
index.testExplodingProperties(done);
36+
});
3337
});
3438
});

0 commit comments

Comments
 (0)