forked from googleapis/google-cloud-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.js
More file actions
373 lines (329 loc) · 11.9 KB
/
Copy pathdataset.js
File metadata and controls
373 lines (329 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* Copyright 2014 Google Inc. All Rights Reserved.
*
* 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.
*/
/*global describe, it, beforeEach */
'use strict';
var assert = require('assert');
var ByteBuffer = require('bytebuffer');
var datastore = require('../../lib').datastore;
var entity = require('../../lib/datastore/entity.js');
var mockRespGet = require('../testdata/response_get.json');
var Transaction = require('../../lib/datastore/transaction.js');
describe('Dataset', function() {
describe('constructor', function() {
beforeEach(function() {
delete process.env.GAE_LONG_APP_ID;
delete process.env.GAE_VM;
});
it('should detect GAE prod', function(done) {
process.env.GAE_LONG_APP_ID = 'gae-app-id';
process.env.GAE_VM = true;
var ds = new datastore.Dataset();
assert.strictEqual(ds.id, 'gae-app-id');
ds.transaction.conn.createAuthorizedReq = function(req) {
assert.equal(req.host, 'www.googleapis.com');
done();
};
ds.get(ds.key('Kind', 123), function() {});
});
it('should detect GAE dev', function(done) {
process.env.GAE_LONG_APP_ID = 'gae-app-id';
var ds = new datastore.Dataset();
assert.strictEqual(ds.id, 'gae-app-id');
ds.transaction.conn.createAuthorizedReq = function(req) {
assert.equal(req.host, 'localhost');
done();
};
ds.get(ds.key('Kind', 123), function() {});
});
it ('should not set app id if GAE_LONG_APP_ID is not set', function(done) {
var ds = new datastore.Dataset();
assert.equal(!!ds.id, false);
ds.transaction.conn.createAuthorizedReq = function(req) {
assert.equal(req.host, 'www.googleapis.com');
done();
};
ds.get(ds.key('Kind', 123), function() {});
});
});
it('should return a key scoped by namespace', function() {
var ds = new datastore.Dataset({ projectId: 'test', namespace: 'my-ns' });
var key = ds.key('Company', 1);
assert.equal(key.namespace_, 'my-ns');
assert.deepEqual(key.path_, ['Company', 1]);
});
it('should allow namespace specification when creating a key', function() {
var ds = new datastore.Dataset({ projectId: 'test' });
var key = ds.key({
namespace: 'custom-ns',
path: ['Company', 1]
});
assert.equal(key.namespace_, 'custom-ns');
assert.deepEqual(key.path_, ['Company', 1]);
});
it('should get by key', function(done) {
var ds = new datastore.Dataset({ projectId: 'test' });
ds.transaction.makeReq = function(method, proto, typ, callback) {
assert.equal(method, 'lookup');
assert.equal(proto.key.length, 1);
callback(null, mockRespGet);
};
ds.get(ds.key('Kind', 123), function(err, entity) {
var data = entity.data;
assert.deepEqual(entity.key.path_, ['Kind', 5732568548769792]);
assert.strictEqual(data.author, 'Silvano');
assert.strictEqual(data.isDraft, false);
assert.deepEqual(data.publishedAt, new Date(978336000000));
done();
});
});
it('should multi get by keys', function(done) {
var ds = new datastore.Dataset({ projectId: 'test' });
ds.transaction.makeReq = function(method, proto, typ, callback) {
assert.equal(method, 'lookup');
assert.equal(proto.key.length, 1);
callback(null, mockRespGet);
};
var key = ds.key('Kind', 5732568548769792);
ds.get([key], function(err, entities) {
var entity = entities[0];
var data = entity.data;
assert.deepEqual(entity.key.path_, ['Kind', 5732568548769792]);
assert.strictEqual(data.author, 'Silvano');
assert.strictEqual(data.isDraft, false);
assert.deepEqual(data.publishedAt, new Date(978336000000));
done();
});
});
it('should continue looking for deferred results', function(done) {
var ds = new datastore.Dataset({ projectId: 'test' });
var key = ds.key('Kind', 5732568548769792);
var key2 = ds.key('Kind', 5732568548769792);
var lookupCount = 0;
ds.transaction.makeReq = function(method, proto, typ, callback) {
lookupCount++;
assert.equal(method, 'lookup');
if (mockRespGet.deferred.length) {
// Revert deferred to original state.
mockRespGet.deferred = [];
} else {
mockRespGet.deferred = [ entity.keyToKeyProto(key2) ];
}
callback(null, mockRespGet);
};
ds.get([key, key2], function(err, entities) {
assert.equal(entities.length, 2);
assert.equal(lookupCount, 2);
done();
});
});
it('should delete by key', function(done) {
var ds = new datastore.Dataset({ projectId: 'test' });
ds.transaction.makeReq = function(method, proto, typ, callback) {
assert.equal(method, 'commit');
assert.equal(!!proto.mutation.delete, true);
callback();
};
ds.delete(ds.key('Kind', 123), done);
});
it('should multi delete by keys', function(done) {
var ds = new datastore.Dataset({ projectId: 'test' });
ds.transaction.makeReq = function(method, proto, typ, callback) {
assert.equal(method, 'commit');
assert.equal(proto.mutation.delete.length, 2);
callback();
};
ds.delete([
ds.key('Kind', 123),
ds.key('Kind', 345)
], done);
});
it('should save with incomplete key', function(done) {
var ds = new datastore.Dataset({ projectId: 'test' });
ds.transaction.makeReq = function(method, proto, typ, callback) {
assert.equal(method, 'commit');
assert.equal(proto.mutation.insert_auto_id.length, 1);
callback();
};
var key = ds.key('Kind', null);
ds.save({ key: key, data: {} }, done);
});
it('should save with keys', function(done) {
var ds = new datastore.Dataset({ projectId: 'test' });
ds.transaction.makeReq = function(method, proto, typ, callback) {
assert.equal(method, 'commit');
assert.equal(proto.mutation.upsert.length, 2);
assert.equal(proto.mutation.upsert[0].property[0].name, 'k');
assert.equal(
proto.mutation.upsert[0].property[0].value.string_value, 'v');
callback();
};
ds.save([
{ key: ds.key('Kind', 123), data: { k: 'v' } },
{ key: ds.key('Kind', 456), data: { k: 'v' } }
], done);
});
it('should produce proper allocate IDs req protos', function(done) {
var ds = new datastore.Dataset({ projectId: 'test' });
ds.transaction.makeReq = function(method, proto, typ, callback) {
assert.equal(method, 'allocateIds');
assert.equal(proto.key.length, 1);
assert.deepEqual(proto.key[0], {
partition_id: null,
path_element :[{kind:'Kind', name: null, id: null}]
});
callback(null, {
key: [
{ path_element: [{ kind: 'Kind', id: 123 }] }
]
});
};
ds.allocateIds(ds.key('Kind', null), 1, function(err, ids) {
assert.deepEqual(ids[0], ds.key('Kind', 123));
done();
});
});
it('should throw if trying to allocate IDs with complete keys', function() {
var ds = new datastore.Dataset({ projectId: 'test' });
assert.throws(function() {
ds.allocateIds(ds.key('Kind', 123));
});
});
describe('runInTransaction', function() {
var ds;
var transaction;
beforeEach(function() {
ds = new datastore.Dataset({ projectId: 'test' });
ds.createTransaction_ = function() {
transaction = new Transaction();
transaction.makeReq = function(method, proto, typ, callback) {
assert.equal(method, 'beginTransaction');
callback(null, { transaction: '' });
};
return transaction;
};
});
it('should begin transaction', function() {
ds.runInTransaction(function() {}, function() {});
});
it('should return transaction object to the callback', function() {
ds.runInTransaction(function(transactionObject) {
assert.equal(transactionObject, transaction);
}, assert.ifError);
});
it('should commit the transaction when done', function() {
ds.runInTransaction(function(t, done) {
transaction.makeReq = function(method) {
assert.equal(method, 'commit');
};
done();
}, assert.ifError);
});
});
describe('createQuery', function() {
var ds;
var dsWithNs;
beforeEach(function() {
ds = new datastore.Dataset({ projectId: 'test' });
dsWithNs = new datastore.Dataset({
projectId: 'test',
namespace: 'my-ns'
});
});
it('should not include a namespace on a ns-less dataset', function() {
var query = ds.createQuery('Kind');
assert.equal(query.namespace, undefined);
});
it('should scope query to namespace', function() {
var query = dsWithNs.createQuery('Kind');
assert.equal(query.namespace, 'my-ns');
});
it('should allow control over namespace and kinds', function() {
var queryFromDs = ds.createQuery('my-ns', 'Kind');
assert.equal(queryFromDs.namespace, 'my-ns');
var queryFromDsWithNs = dsWithNs.createQuery('Kind');
assert.equal(queryFromDsWithNs.namespace, 'my-ns');
});
it('should allow removal of namespace', function() {
var query = dsWithNs.createQuery(null, 'Kind');
assert.strictEqual(query.namespace, null);
});
});
describe('runQuery', function() {
var ds;
var query;
var mockResponse = {
withResults: {
batch: { entity_result: mockRespGet.found }
},
withResultsAndEndCursor: {
batch: {
entity_result: mockRespGet.found,
end_cursor: new ByteBuffer().writeIString('cursor').flip()
}
},
withoutResults: mockRespGet
};
beforeEach(function() {
ds = new datastore.Dataset({ projectId: 'test' });
query = ds.createQuery('Kind');
});
describe('errors', function() {
it('should handle upstream errors', function() {
var upstreamError = new Error('upstream error.');
ds.transaction.makeReq = function(method, proto, typ, callback) {
assert.equal(method, 'runQuery');
callback(upstreamError);
};
ds.runQuery(query, function(err) {
assert.equal(err, upstreamError);
});
});
it('should handle missing results error', function() {
ds.transaction.makeReq = function(method, proto, typ, callback) {
assert.equal(method, 'runQuery');
callback('simulated-error', mockResponse.withoutResults);
};
ds.runQuery(query, function(err) {
assert.equal(err, 'simulated-error');
});
});
});
it('should execute callback with results', function() {
ds.transaction.makeReq = function(method, proto, typ, callback) {
assert.equal(method, 'runQuery');
callback(null, mockResponse.withResults);
};
ds.runQuery(query, function (err, entities) {
assert.ifError(err);
assert.deepEqual(entities[0].key.path_, ['Kind', 5732568548769792]);
var data = entities[0].data;
assert.strictEqual(data.author, 'Silvano');
assert.strictEqual(data.isDraft, false);
assert.deepEqual(data.publishedAt, new Date(978336000000));
});
});
it('should return a new query if results remain', function() {
ds.transaction.makeReq = function(method, proto, typ, callback) {
assert.equal(method, 'runQuery');
callback(null, mockResponse.withResultsAndEndCursor);
};
ds.runQuery(query, function(err, entities, nextQuery) {
assert.ifError(err);
assert.equal(nextQuery.constructor.name, 'Query');
});
});
});
});