forked from googleapis/google-cloud-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.js
More file actions
1045 lines (882 loc) · 29.1 KB
/
request.js
File metadata and controls
1045 lines (882 loc) · 29.1 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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.
*/
'use strict';
var arrify = require('arrify');
var assert = require('assert');
var extend = require('extend');
var is = require('is');
var mockery = require('mockery-next');
var stream = require('stream');
var util = require('../../lib/common/util.js');
var entity = require('../../lib/datastore/entity.js');
var Query = require('../../lib/datastore/query.js');
var entityOverrides = {};
var fakeEntity;
fakeEntity = Object.keys(entity).reduce(function(fakeEntity, methodName) {
fakeEntity[methodName] = function() {
var method = entityOverrides[methodName] || entity[methodName];
return method.apply(this, arguments);
};
return fakeEntity;
}, {});
var utilOverrides = {};
var fakeUtil;
fakeUtil = Object.keys(util).reduce(function(fakeUtil, methodName) {
fakeUtil[methodName] = function() {
var method = utilOverrides[methodName] || util[methodName];
return method.apply(this, arguments);
};
return fakeUtil;
}, {});
function FakeQuery() {
this.calledWith_ = arguments;
}
var extended = false;
var fakeStreamRouter = {
extend: function(Class, methods) {
if (Class.name !== 'DatastoreRequest') {
return;
}
methods = arrify(methods);
assert.equal(Class.name, 'DatastoreRequest');
assert.deepEqual(methods, ['runQuery']);
extended = true;
}
};
describe('Request', function() {
var Request;
var request;
var key;
before(function() {
mockery.registerMock('../../lib/common/stream-router.js', fakeStreamRouter);
mockery.registerMock('../../lib/common/util.js', fakeUtil);
mockery.registerMock('../../lib/datastore/entity.js', fakeEntity);
mockery.registerMock('../../lib/datastore/query.js', FakeQuery);
mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
});
Request = require('../../lib/datastore/request.js');
});
after(function() {
mockery.deregisterAll();
mockery.disable();
});
beforeEach(function() {
key = new entity.Key({
namespace: 'namespace',
path: ['Company', 123]
});
FakeQuery.prototype = new Query();
entityOverrides = {};
utilOverrides = {};
request = new Request();
});
describe('instantiation', function() {
it('should extend the correct methods', function() {
assert(extended); // See `fakeStreamRouter.extend`
});
});
describe('allocateIds', function() {
var incompleteKey;
var apiResponse = {
keys: [
{ path: [{ kind: 'Kind', id: 123 }] }
]
};
beforeEach(function() {
incompleteKey = new entity.Key({ namespace: null, path: ['Kind'] });
});
it('should produce proper allocate IDs req protos', function(done) {
request.request_ = function(protoOpts, reqOpts, callback) {
assert.strictEqual(protoOpts.service, 'Datastore');
assert.strictEqual(protoOpts.method, 'allocateIds');
assert.equal(reqOpts.keys.length, 1);
callback(null, apiResponse);
};
request.allocateIds(incompleteKey, 1, function(err, keys) {
assert.ifError(err);
var generatedKey = keys[0];
assert.strictEqual(generatedKey.path.pop(), 123);
done();
});
});
it('should exec callback with error & API response', function(done) {
var error = new Error('Error.');
request.request_ = function(protoOpts, reqOpts, callback) {
callback(error, apiResponse);
};
request.allocateIds(incompleteKey, 1, function(err, keys, apiResponse_) {
assert.strictEqual(err, error);
assert.strictEqual(keys, null);
assert.strictEqual(apiResponse_, apiResponse);
done();
});
});
it('should return apiResponse in callback', function(done) {
request.request_ = function(protoOpts, reqOpts, callback) {
callback(null, apiResponse);
};
request.allocateIds(incompleteKey, 1, function(err, keys, apiResponse_) {
assert.ifError(err);
assert.strictEqual(apiResponse_, apiResponse);
done();
});
});
it('should throw if trying to allocate IDs with complete keys', function() {
assert.throws(function() {
request.allocateIds(key);
});
});
});
describe('delete', function() {
it('should delete by key', function(done) {
request.request_ = function(protoOpts, reqOpts, callback) {
assert.strictEqual(protoOpts.service, 'Datastore');
assert.strictEqual(protoOpts.method, 'commit');
assert(is.object(reqOpts.mutations[0].delete));
callback();
};
request.delete(key, done);
});
it('should return apiResponse in callback', function(done) {
var resp = { success: true };
request.request_ = function(protoOpts, reqOpts, callback) {
callback(null, resp);
};
request.delete(key, function(err, apiResponse) {
assert.ifError(err);
assert.deepEqual(resp, apiResponse);
done();
});
});
it('should multi delete by keys', function(done) {
request.request_ = function(protoOpts, reqOpts, callback) {
assert.equal(reqOpts.mutations.length, 2);
callback();
};
request.delete([ key, key ], done);
});
describe('transactions', function() {
beforeEach(function() {
// Trigger transaction mode.
request.id = 'transaction-id';
request.requests_ = [];
});
it('should queue request', function() {
request.delete(key);
assert(is.object(request.requests_[0].mutations[0].delete));
});
});
});
describe('get', function() {
beforeEach(function() {
request.request_ = function() {};
});
it('should throw if no keys are provided', function() {
assert.throws(function() {
request.get();
}, /At least one Key object is required/);
});
it('should return a stream if no callback is provided', function() {
assert(request.get(key) instanceof stream);
});
it('should convert key to key proto', function(done) {
entityOverrides.keyToKeyProto = function(key_) {
assert.strictEqual(key_, key);
done();
};
request.get(key, assert.ifError);
});
it('should make correct request', function(done) {
request.request_ = function(protoOpts, reqOpts) {
assert.strictEqual(protoOpts.service, 'Datastore');
assert.strictEqual(protoOpts.method, 'lookup');
assert.deepEqual(reqOpts.keys[0], entity.keyToKeyProto(key));
done();
};
request.get(key, assert.ifError);
});
describe('error', function() {
var error = new Error('Error.');
var apiResponse = { a: 'b', c: 'd' };
beforeEach(function() {
request.request_ = function(protoOpts, reqOpts, callback) {
setImmediate(function() {
callback(error, apiResponse);
});
};
});
describe('callback mode', function() {
it('should execute callback with error', function(done) {
request.get(key, function(err) {
assert.strictEqual(err, error);
done();
});
});
});
describe('stream mode', function() {
it('should emit error', function(done) {
request.get(key)
.on('error', function(err) {
assert.strictEqual(err, error);
done();
});
});
it('should end stream', function(done) {
var stream = request.get(key);
stream.on('error', function() {
setImmediate(function() {
assert.strictEqual(stream._destroyed, true);
done();
});
});
});
});
});
describe('success', function() {
var apiResponse = {
found: [
{
entity: {
key: {
partitionId: {
projectId: 'grape-spaceship-123'
},
path: [
{
kind: 'Post',
name: 'post1'
}
]
},
properties: {
title: {
stringValue: 'How to make the perfect pizza in your grill'
},
tags: {
arrayValue: {
values: [
{
stringValue: 'pizza'
},
{
stringValue: 'grill'
}
]
}
},
rating: {
integerValue: '5'
},
author: {
stringValue: 'Silvano'
},
wordCount: {
integerValue: '400'
},
isDraft: {
booleanValue: false
}
}
}
}
]
};
var expectedResult = entity.formatArray(apiResponse.found)[0];
var apiResponseWithMultiEntities = extend(true, {}, apiResponse);
var entities = apiResponseWithMultiEntities.found;
entities.push(entities[0]);
var expectedResults = entity.formatArray(entities);
var apiResponseWithDeferred = extend(true, {}, apiResponse);
apiResponseWithDeferred.deferred = [
apiResponseWithDeferred.found[0].entity.key
];
beforeEach(function() {
request.request_ = function(protoOpts, reqOpts, callback) {
callback(null, apiResponse);
};
});
it('should format the results', function(done) {
entityOverrides.formatArray = function(arr) {
assert.strictEqual(arr, apiResponse.found);
setImmediate(done);
return arr;
};
request.get(key, assert.ifError);
});
it('should continue looking for deferred results', function(done) {
request.request_ = function(protoOpts, reqOpts, callback) {
callback(null, apiResponseWithDeferred);
};
request.get(key, assert.ifError);
request.get = function(keys) {
var expectedKeys = apiResponseWithDeferred.deferred
.map(entity.keyFromKeyProto);
assert.deepEqual(keys, expectedKeys);
done();
};
});
describe('callback mode', function() {
it('should exec callback with results', function(done) {
request.get(key, function(err, entity) {
assert.ifError(err);
assert.deepEqual(entity, expectedResult);
done();
});
});
it('should exec callback w/ array from multiple keys', function(done) {
request.request_ = function(protoOpts, reqOpts, callback) {
callback(null, apiResponseWithMultiEntities);
};
request.get([key, key], function(err, entities) {
assert.ifError(err);
assert.strictEqual(is.array(entities), true);
assert.deepEqual(entities, expectedResults);
done();
});
});
});
describe('stream mode', function() {
it('should push results to the stream', function(done) {
request.get(key)
.on('error', done)
.on('data', function(entity) {
assert.deepEqual(entity, expectedResult);
})
.on('end', done);
});
it('should not push more results if stream was ended', function(done) {
var entitiesEmitted = 0;
request.request_ = function(protoOpts, reqOpts, callback) {
setImmediate(function() {
callback(null, apiResponseWithMultiEntities);
});
};
request.get([key, key])
.on('data', function() {
entitiesEmitted++;
this.end();
})
.on('end', function() {
assert.strictEqual(entitiesEmitted, 1);
done();
});
});
it('should not get more results if stream was ended', function(done) {
var lookupCount = 0;
request.request_ = function(protoOpts, reqOpts, callback) {
lookupCount++;
setImmediate(function() {
callback(null, apiResponseWithDeferred);
});
};
request.get(key)
.on('error', done)
.on('data', function() {
this.end();
})
.on('end', function() {
assert.strictEqual(lookupCount, 1);
done();
});
});
});
});
});
describe('insert', function() {
it('should pass the correct arguments to save', function(done) {
request.save = function(entities, callback) {
assert.deepEqual(entities, [{
key: {
namespace: 'ns',
kind: 'Company',
path: ['Company', undefined],
},
data: {},
method: 'insert'
}]);
callback();
};
var key = new entity.Key({ namespace: 'ns', path: ['Company'] });
request.insert({ key: key, data: {} }, done);
});
});
describe('runQuery', function() {
it('should make correct request', function(done) {
var query = { namespace: 'namespace' };
var queryProto = {};
entityOverrides.queryToQueryProto = function(query_) {
assert.strictEqual(query_, query);
return queryProto;
};
request.request_ = function(protoOpts, reqOpts) {
assert.strictEqual(protoOpts.service, 'Datastore');
assert.strictEqual(protoOpts.method, 'runQuery');
assert(is.empty(reqOpts.readOptions));
assert.strictEqual(reqOpts.query, queryProto);
assert.strictEqual(reqOpts.partitionId.namespaceId, query.namespace);
done();
};
request.runQuery(query, assert.ifError);
});
describe('error', function() {
var error = new Error('Error.');
var apiResponse = {};
beforeEach(function() {
entity.queryToQueryProto = util.noop;
request.request_ = function(protoOpts, reqOpts, callback) {
callback(error, apiResponse);
};
});
it('should execute callback with error & API response', function(done) {
request.runQuery({}, function(err, results, nextQuery, apiResponse_) {
assert.strictEqual(err, error);
assert.strictEqual(results, null);
assert.strictEqual(nextQuery, null);
assert.strictEqual(apiResponse_, apiResponse);
done();
});
});
});
describe('success', function() {
var entityResults = ['a', 'b', 'c'];
var endCursor = 'endCursor';
var apiResponse = {
batch: {
entityResults: entityResults,
endCursor: endCursor,
moreResults: 'MORE_RESULTS_AFTER_LIMIT',
skippedResults: 0
}
};
beforeEach(function() {
request.request_ = function(protoOpts, reqOpts, callback) {
callback(null, apiResponse);
};
});
it('should format results', function(done) {
entityOverrides.formatArray = function(array) {
assert.strictEqual(array, entityResults);
return entityResults;
};
request.runQuery({}, function(err, entities) {
assert.ifError(err);
assert.deepEqual(entities, entityResults);
done();
});
});
it('should re-run query if not finished', function(done) {
var entityResults = {
1: ['a'],
2: ['b', 'c']
};
var nextQuery;
var query = {
limitVal: 1,
offsetVal: 8
};
var queryProto = {
limit: {
value: query.limitVal
}
};
var timesRequestCalled = 0;
var startCalled = false;
var offsetCalled = false;
entityOverrides.formatArray = function(array) {
assert.strictEqual(array, entityResults[timesRequestCalled]);
return entityResults[timesRequestCalled];
};
request.request_ = function(protoOpts, reqOpts, callback) {
timesRequestCalled++;
var resp = extend(true, {}, apiResponse);
resp.batch.entityResults = entityResults[timesRequestCalled];
if (timesRequestCalled === 1) {
assert.strictEqual(protoOpts.service, 'Datastore');
assert.strictEqual(protoOpts.method, 'runQuery');
resp.batch.moreResults = 'NOT_FINISHED';
callback(null, resp);
} else {
assert.strictEqual(startCalled, true);
assert.strictEqual(offsetCalled, true);
assert.strictEqual(reqOpts.query, queryProto);
resp.batch.moreResults = 'MORE_RESULTS_AFTER_LIMIT';
callback(null, resp);
}
};
FakeQuery.prototype.start = function(endCursor_) {
nextQuery = this;
assert.strictEqual(endCursor_, endCursor);
startCalled = true;
return this;
};
FakeQuery.prototype.offset = function(offset_) {
var offset = query.offsetVal - apiResponse.batch.skippedResults;
assert.strictEqual(offset_, offset);
offsetCalled = true;
return this;
};
FakeQuery.prototype.limit = function(limit_) {
if (timesRequestCalled === 1) {
assert.strictEqual(
limit_,
entityResults[1].length - query.limitVal
);
} else {
// Should restore the original limit.
assert.strictEqual(limit_, query.limitVal);
}
return this;
};
entityOverrides.queryToQueryProto = function(query_) {
if (timesRequestCalled > 1) {
assert.strictEqual(query_, nextQuery);
}
return queryProto;
};
request.runQuery(query, function(err, entities, nextQuery_) {
assert.ifError(err);
assert.deepEqual(
entities,
[].slice.call(entityResults[1]).concat(entityResults[2])
);
assert.strictEqual(nextQuery_, nextQuery);
done();
});
});
it('should return nextQuery', function(done) {
entityOverrides.formatArray = util.noop;
var query = {
offsetVal: 8
};
var startCalled = false;
var offsetCalled = false;
FakeQuery.prototype.start = function(endCursor_) {
assert.strictEqual(endCursor_, endCursor);
startCalled = true;
return this;
};
FakeQuery.prototype.offset = function(offset_) {
var offset = query.offsetVal - apiResponse.batch.skippedResults;
assert.strictEqual(offset_, offset);
offsetCalled = true;
return this;
};
request.runQuery(query, function(err) {
assert.ifError(err);
assert.strictEqual(startCalled, true);
assert.strictEqual(offsetCalled, true);
done();
});
});
});
});
describe('save', function() {
it('should save with keys', function(done) {
var expectedReq = {
mutations: [
{
upsert: {
key: {
partitionId: {
namespaceId: 'namespace'
},
path: [
{
kind: 'Company',
id: 123
}
]
},
properties: {
k: {
stringValue: 'v'
}
}
}
},
{
upsert: {
key: {
partitionId: {
namespaceId: 'namespace'
},
path: [
{
kind: 'Company',
id: 123
}
]
},
properties: {
k: {
stringValue: 'v'
}
}
}
}
]
};
request.request_ = function(protoOpts, reqOpts, callback) {
assert.strictEqual(protoOpts.service, 'Datastore');
assert.strictEqual(protoOpts.method, 'commit');
assert.deepEqual(reqOpts, expectedReq);
callback();
};
request.save([
{ key: key, data: { k: 'v' } },
{ key: key, data: { k: 'v' } }
], done);
});
it('should save with specific method', function(done) {
request.request_ = function(protoOpts, reqOpts, callback) {
assert.equal(reqOpts.mutations.length, 3);
assert(is.object(reqOpts.mutations[0].insert));
assert(is.object(reqOpts.mutations[1].update));
assert(is.object(reqOpts.mutations[2].upsert));
var insert = reqOpts.mutations[0].insert;
assert.deepEqual(insert.properties.k, { stringValue: 'v' });
var update = reqOpts.mutations[1].update;
assert.deepEqual(update.properties.k2, { stringValue: 'v2' });
var upsert = reqOpts.mutations[2].upsert;
assert.deepEqual(upsert.properties.k3, { stringValue: 'v3' });
callback();
};
request.save([
{ key: key, method: 'insert', data: { k: 'v' } },
{ key: key, method: 'update', data: { k2: 'v2' } },
{ key: key, method: 'upsert', data: { k3: 'v3' } }
], done);
});
it('should throw if a given method is not recognized', function() {
assert.throws(function() {
request.save({
key: key,
method: 'auto_insert_id',
data: {
k: 'v'
}
}, assert.ifError);
}, /Method auto_insert_id not recognized/);
});
it('should not alter the provided data object', function(done) {
var entities = [
{
key: key,
method: 'insert',
indexed: false,
data: {
value: {
a: 'b',
c: [1, 2, 3]
}
}
}
];
var expectedEntities = extend(true, {}, entities);
request.request_ = function() {
// By the time the request is made, the original object has already been
// transformed into a raw request.
assert.deepEqual(entities, expectedEntities);
done();
};
request.save(entities, assert.ifError);
});
it('should return apiResponse in callback', function(done) {
var key = new entity.Key({ namespace: 'ns', path: ['Company'] });
var mockCommitResponse = {};
request.request_ = function(protoOpts, reqOpts, callback) {
callback(null, mockCommitResponse);
};
request.save({ key: key, data: {} }, function(err, apiResponse) {
assert.ifError(err);
assert.strictEqual(mockCommitResponse, apiResponse);
done();
});
});
it('should allow setting the indexed value of a property', function(done) {
request.request_ = function(protoOpts, reqOpts) {
var property = reqOpts.mutations[0].upsert.properties.name;
assert.strictEqual(property.stringValue, 'value');
assert.strictEqual(property.excludeFromIndexes, true);
done();
};
request.save({
key: key,
data: [
{
name: 'name',
value: 'value',
excludeFromIndexes: true
}
]
}, assert.ifError);
});
it('should allow setting the indexed value on arrays', function(done) {
request.request_ = function(protoOpts, reqOpts) {
var property = reqOpts.mutations[0].upsert.properties.name;
property.arrayValue.values.forEach(function(value) {
assert.strictEqual(value.excludeFromIndexes, true);
});
done();
};
request.save({
key: key,
data: [
{
name: 'name',
value: ['one', 'two', 'three'],
excludeFromIndexes: true
}
]
}, assert.ifError);
});
describe('transactions', function() {
beforeEach(function() {
// Trigger transaction mode.
request.id = 'transaction-id';
request.requestCallbacks_ = [];
request.requests_ = [];
});
it('should queue request & callback', function() {
request.save({
key: key,
data: [{ name: 'name', value: 'value' }]
});
assert.equal(typeof request.requestCallbacks_[0], 'function');
assert.equal(typeof request.requests_[0], 'object');
});
});
});
describe('update', function() {
it('should pass the correct arguments to save', function(done) {
request.save = function(entities, callback) {
assert.deepEqual(entities, [{
key: {
namespace: 'ns',
kind: 'Company',
path: ['Company', undefined],
},
data: {},
method: 'update'
}]);
callback();
};
var key = new entity.Key({ namespace: 'ns', path: ['Company'] });
request.update({ key: key, data: {} }, done);
});
});
describe('upsert', function() {
it('should pass the correct arguments to save', function(done) {
request.save = function(entities, callback) {
assert.deepEqual(entities, [{
key: {
namespace: 'ns',
kind: 'Company',
path: ['Company', undefined],
},
data: {},
method: 'upsert'
}]);
callback();
};
var key = new entity.Key({ namespace: 'ns', path: ['Company'] });
request.upsert({ key: key, data: {} }, done);
});
});
describe('request_', function() {
var PROJECT_ID = 'project-id';
var PROTO_OPTS = {};
beforeEach(function() {
request.projectId = PROJECT_ID;
});
it('should make the correct request', function(done) {
var reqOpts = {};
request.request = function(protoOpts, reqOpts_) {
assert.strictEqual(protoOpts, PROTO_OPTS);
assert.strictEqual(reqOpts_, reqOpts);
assert.strictEqual(reqOpts_.projectId, PROJECT_ID);
done();
};
request.request_(PROTO_OPTS, reqOpts, assert.ifError);
});
describe('commit', function() {
it('should set the mode', function(done) {
var reqOpts = {};
request.request = function(protoOpts, reqOpts_) {
assert.strictEqual(reqOpts_, reqOpts);
assert.strictEqual(reqOpts_.mode, 'NON_TRANSACTIONAL');
done();
};
request.request_({ method: 'commit' }, reqOpts, assert.ifError);
});
});
describe('transaction', function() {
var TRANSACTION_ID = 'transaction';
beforeEach(function() {
request.id = TRANSACTION_ID;
});
it('should set the commit transaction info', function(done) {
var reqOpts = {};
request.request = function(protoOpts, reqOpts_) {
assert.strictEqual(reqOpts_, reqOpts);
assert.strictEqual(reqOpts_.mode, 'TRANSACTIONAL');
assert.strictEqual(reqOpts_.transaction, request.id);
done();
};
request.id = 'transaction-id';
request.request_({ method: 'commit' }, reqOpts, assert.ifError);
});
it('should set the rollback transaction info', function(done) {
var reqOpts = {};