-
Notifications
You must be signed in to change notification settings - Fork 45
/
FieldDBObject-spec.js
1627 lines (1412 loc) · 61.6 KB
/
FieldDBObject-spec.js
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
"use strict";
/* globals localStorage, FieldDB*/
var FieldDBObject;
var Q;
try {
if (FieldDB) {
FieldDBObject = FieldDB.FieldDBObject;
Q = FieldDB.Q;
}
} catch (e) {}
FieldDBObject = FieldDBObject || require("./../api/FieldDBObject").FieldDBObject;
Q = Q || require("q");
var specIsRunningTooLong = 5000;
var mockDatabase = require("./corpus/DatabaseMock").mockDatabase;
var CORS_ERROR = "CORS not supported, your device will be unable to contact";
describe("FieldDBObject", function() {
afterEach(function() {
if (FieldDBObject.application) {
// console.log("Cleaning up.");
FieldDBObject.application = null;
}
mockDatabase = {
get: mockDatabase.get,
set: mockDatabase.set,
fetchRevisions: mockDatabase.fetchRevisions
};
try {
localStorage.clear();
} catch (e) {}
});
describe("construction", function() {
it("should accept a json object", function() {
var u = new FieldDBObject();
u.aproperty = "hasavalue";
expect(u.aproperty).toEqual("hasavalue");
u = new FieldDBObject({
aproperty: "adifferentvalue"
});
expect(u.aproperty).toEqual("adifferentvalue");
});
it("should accept a json object on extended classes", function() {
var Child = function Child(options) {
this.debug("In Child ", options);
FieldDBObject.apply(this, arguments);
this._fieldDBtype = "Child";
};
Child.prototype = Object.create(FieldDBObject.prototype, /** @lends Child.prototype */ {
constructor: {
value: Child
}
});
// var u = new Child();
// u.aproperty = "hasavalue";
// expect(u.aproperty).toEqual("hasavalue");
var u2 = new Child({
aproperty: "adifferentvalue"
});
expect(u2.aproperty).toEqual("adifferentvalue");
expect(u2.fieldDBtype).toEqual("Child");
});
it("should add dateCreated if it was missing", function() {
var u = new FieldDBObject();
expect(u.dateCreated).toBeDefined();
});
it("should pass a reference to the application if it was specified", function() {
delete FieldDBObject.application;
var applicationLess = new FieldDBObject();
expect(applicationLess.application).toBeUndefined();
FieldDBObject.application = new FieldDBObject({
context: "Offline",
fieldDBtype: "PsycholinguisticsApp"
});
var u = new FieldDBObject();
expect(u.application).toBeDefined();
expect(u.application.context).toEqual("Offline");
expect(applicationLess.application).toBe(u.application);
var t = new FieldDBObject();
expect(u.application).toBe(t.application);
FieldDBObject.application = new FieldDBObject({
context: "Online",
fieldDBtype: "PsycholinguisticsApp"
});
expect(u.application.context).toEqual("Online");
expect(u.application).toBe(t.application);
expect(u.toJSON().application).toBeUndefined();
});
});
describe("deserialization", function() {
it("should be able to guess its type", function() {
var mysteryObject = {
_id: "2389jr9rj490",
collection: "somethingnotinthesystem"
};
mysteryObject = FieldDBObject.convertDocIntoItsType(mysteryObject);
expect(mysteryObject.fieldDBtype).toEqual("FieldDBObject");
});
it("should be mark the previous type if it cant guess its type", function() {
var wasACommentButFieldDBIsUndefinedInNPMRequireContexts = {
"text": "How to do something",
"username": "lingllama",
"timestamp": "2012-09-26T14:42:05.349Z",
"gravatar": "weoaeoriaew"
};
wasACommentButFieldDBIsUndefinedInNPMRequireContexts = FieldDBObject.convertDocIntoItsType(wasACommentButFieldDBIsUndefinedInNPMRequireContexts);
if (wasACommentButFieldDBIsUndefinedInNPMRequireContexts.previousFieldDBtype) {
expect(wasACommentButFieldDBIsUndefinedInNPMRequireContexts).toEqual({
// _fieldDBtype: "FieldDBObject",
text: "How to do something",
username: "lingllama",
_timestamp: 1348670525349,
gravatar: "weoaeoriaew",
previousFieldDBtype: "Comment",
_dateCreated: wasACommentButFieldDBIsUndefinedInNPMRequireContexts.timestamp
});
} else {
expect(wasACommentButFieldDBIsUndefinedInNPMRequireContexts.fieldDBtype).toEqual("Comment");
expect(wasACommentButFieldDBIsUndefinedInNPMRequireContexts.previousFieldDBtype).toBeUndefined();
}
});
it("should be use the previous type when guessing type", function() {
var hadAPreviousType = {
"text": "How to do something",
"username": "lingllama",
"timestamp": "2012-09-26T14:42:05.349Z",
"gravatar": "weoaeoriaew",
"previousFieldDBtype": "SomeSpecializedCommentYouCantGuess"
};
hadAPreviousType = FieldDBObject.convertDocIntoItsType(hadAPreviousType);
expect(hadAPreviousType.fieldDBtype).toEqual("FieldDBObject");
expect(hadAPreviousType.previousFieldDBtype).toEqual("SomeSpecializedCommentYouCantGuess");
});
});
describe("serialization", function() {
var penguin;
var body = [{
wings: "flightless"
}, {
feet: "good-for-walking"
}];
beforeEach(function() {
penguin = new FieldDBObject({
body: body,
_id: "firstPenguin",
_rev: "2-123"
});
});
it("should not loose attributes that were in the original JSON", function() {
var resultingJSON = penguin.toJSON();
expect(resultingJSON.body).toBe(body);
expect(resultingJSON._id).toEqual("firstPenguin");
expect(resultingJSON.id).toBeUndefined();
expect(resultingJSON._rev).toEqual("2-123");
expect(resultingJSON.rev).toBeUndefined();
});
it("should not add an id if there was none", function() {
var resultingJSON = new FieldDBObject({
some: "attrib"
}).toJSON();
expect(resultingJSON.some).toEqual("attrib");
expect(resultingJSON._id).toBeUndefined();
expect(resultingJSON.id).toBeUndefined();
expect(resultingJSON._rev).toBeUndefined();
expect(resultingJSON.rev).toBeUndefined();
});
it("should not introduce attributes that weren't in the original JSON", function() {
var resultingJSON = penguin.toJSON();
expect(resultingJSON).toEqual({
body: [{
wings: "flightless"
}, {
feet: "good-for-walking"
}],
_id: "firstPenguin",
_rev: "2-123",
version: FieldDBObject.DEFAULT_VERSION,
fieldDBtype: "FieldDBObject"
});
var accessingAttributeShouldNotCauseItToExist = penguin.dbname;
expect(accessingAttributeShouldNotCauseItToExist).toEqual(FieldDBObject.DEFAULT_STRING);
accessingAttributeShouldNotCauseItToExist = penguin.dateModified;
expect(accessingAttributeShouldNotCauseItToExist).toEqual(FieldDBObject.DEFAULT_DATE);
expect(penguin.toJSON()).toEqual(resultingJSON);
});
it("should be possible to request a smaller object with empty attributes removed if caller requests", function() {
var resultingJSON = new FieldDBObject({
body: [],
_id: "firstPenguin",
_rev: ""
}).toJSON(null, "removeEmptyAttributes");
expect(resultingJSON).toEqual({
_id: "firstPenguin",
version: FieldDBObject.DEFAULT_VERSION,
fieldDBtype: "FieldDBObject"
});
});
it("should be possible to request a complete object if caller requests", function() {
var resultingJSON = new FieldDBObject({
dateCreated: 1,
_id: "123"
}).toJSON("complete");
expect(resultingJSON).toEqual({
fieldDBtype: "FieldDBObject",
dateCreated: 1,
_id: "123",
version: FieldDBObject.DEFAULT_VERSION,
dbname: "",
dateModified: 0,
comments: []
});
});
});
describe("cloning and minimal pairs", function() {
var penguin;
var body = [{
wings: "flightless"
}, {
feet: "good-for-walking"
}];
beforeEach(function() {
penguin = new FieldDBObject({
body: body,
_id: "firstPenguin",
_rev: "2-123"
});
});
it("should not clone id and rev", function() {
expect(penguin.rev).toEqual("2-123");
var babypenguin = penguin.clone();
expect(babypenguin instanceof FieldDBObject).toBeTruthy();
expect(penguin.rev).toEqual("2-123");
expect(babypenguin.rev).toEqual("");
});
it("should clone objects deeply", function() {
expect(penguin.body).toBe(body);
var babypenguin = penguin.clone();
expect(babypenguin.body).not.toBe(body);
expect(babypenguin.body).toEqual(body);
penguin.body.beak = "yellow";
expect(penguin.body.beak).toEqual("yellow");
expect(babypenguin.body.beak).toBeUndefined();
});
it("should clone objects recursively", function() {
var datumTypeThing = new FieldDBObject({
_id: "82u398jaeoiajwo3a",
_rev: "8-ojqa3ja0eios09k3aw",
utterance: "noqata tusunaywanmi",
translation: "I feel like dancing",
dateCreated: 1469294300622,
version: "0.2.28ss",
sessionTypeThing: new FieldDBObject({
_id: "9a0j0ejoi32jo",
_rev: "29-903jaoijoiw3ajow",
page: "34",
publisher: "MITWPL",
dateCreated: 1469294600622,
version: "0.1.1",
speakerTypeThing: new FieldDBObject({
_id: "yuioiuni98y932",
_rev: "3-i3orj0jw203j",
version: "3.110.1",
fields: []
})
})
});
var clonedParentForMinimalPairs = datumTypeThing.clone();
expect(clonedParentForMinimalPairs).toEqual(new FieldDBObject({
utterance: "noqata tusunaywanmi",
translation: "I feel like dancing",
_dateCreated: 1469294300622,
_version: "0.2.28ss",
sessionTypeThing: {
fieldDBtype: "FieldDBObject",
page: "34",
publisher: "MITWPL",
dateCreated: 1469294600622,
version: "0.1.1",
speakerTypeThing: {
fieldDBtype: "FieldDBObject",
fields: [],
dateCreated: clonedParentForMinimalPairs.sessionTypeThing.speakerTypeThing.dateCreated,
version: "3.110.1",
relatedData: [{
URI: "yuioiuni98y932?rev=3-i3orj0jw203j",
relation: "clonedFrom"
}]
},
relatedData: [{
URI: "9a0j0ejoi32jo?rev=29-903jaoijoiw3ajow",
relation: "clonedFrom"
}]
},
relatedData: [{
URI: "82u398jaeoiajwo3a?rev=8-ojqa3ja0eios09k3aw",
relation: "clonedFrom"
}]
}));
});
it("should not effect clone if original object is changed", function() {
var adatum = new FieldDBObject({
"tags": "apositive",
fields: [new FieldDBObject({
id: "judgement",
value: "#"
}), new FieldDBObject({
id: "utterance",
value: "noqata tusunayawanmi"
})]
});
var aminimalPair = adatum.clone();
aminimalPair.fields[1].value = "noqata tusunayami";
aminimalPair.fields[0].value = "*";
expect(adatum.fields[0].value).toEqual("#");
expect(aminimalPair.fields[0].value).toEqual("*");
adatum.fields[1].value = "noqata tusunayawaanmi";
expect(adatum.fields[1].value).toEqual("noqata tusunayawaanmi");
expect(aminimalPair.fields[1].value).toEqual("noqata tusunayami");
});
it("should add a new linked data to the original", function() {
var babypenguin = penguin.clone();
expect(babypenguin.relatedData).toEqual([{
URI: "firstPenguin?rev=2-123",
relation: "clonedFrom"
}]);
});
it("should be an instanceof the same type", function() {
var Child = function Child(options) {
this.debug("In Child ", options);
FieldDBObject.apply(this, arguments);
this._fieldDBtype = "Child";
};
Child.prototype = Object.create(FieldDBObject.prototype, /** @lends Child.prototype */ {
constructor: {
value: Child
}
});
var one = new Child({
aproperty: "adifferentvalue"
});
var two = one.clone();
expect(two).toEqual(new Child({
aproperty: "adifferentvalue",
_dateCreated: two.dateCreated,
_version: two.version,
_fieldDBtype: "Child"
}));
expect(two instanceof Child).toBeTruthy();
expect(two instanceof FieldDBObject).toBeTruthy();
});
});
describe("persisance", function() {
var penguin;
var body = [{
wings: "flightless"
}, {
feet: "good-for-walking"
}];
beforeEach(function() {
penguin = new FieldDBObject({
body: body,
_id: "firstPenguin",
_rev: "2-123"
});
});
it("should be able to return a promise for an item from the database", function(done) {
var object = new FieldDBObject({
dbname: "lingallama-communitycorpus",
id: "D093j2ae-akmoi3m-2a3wkjen",
corpus: {
get: mockDatabase.get,
set: mockDatabase.set
}
});
expect(object.fetching).toEqual(undefined);
expect(object.loading).toEqual(undefined);
object.fetch().then(function(resultingdocument) {
expect(resultingdocument).toEqual(object);
expect(object.warnMessage).toContain("calling merge with overwrite from server");
expect(object.fetching).toEqual(false);
expect(object.loading).toEqual(false);
expect(object.title).toEqual("Community corpus");
// return object;
}, function(error) {
object.debug(error);
expect(false).toBeTruthy();
// return object;
}).done(done);
expect(object.fetching).toEqual(true);
expect(object.loading).toEqual(true);
}, specIsRunningTooLong);
it("should refuse to save an item which belongs in another database", function(done) {
var object = new FieldDBObject({
dbname: "lingallama-communitycorpus",
});
// add a mock database
object._corpus = {
get: mockDatabase.get,
set: mockDatabase.set
};
object._corpus.dbname = "jenkins-doesntmatchdb";
expect(object._corpus).toBeDefined();
expect(object.corpus.set).toBeDefined();
object.fossil = {};
object.save().then(function() {
expect(false).toBeTruthy();
}, function(error) {
expect(error.userFriendlyErrors).toEqual(["This item belongs in the lingallama-communitycorpusdatabase, not in the jenkins-doesntmatchdb database."]);
}).done(done);
}, specIsRunningTooLong);
it("should refuse to fetch an item which has no id", function(done) {
// FieldDBObject.application = null;
var object = new FieldDBObject({
dbname: "lingallama-communitycorpus",
});
object.fetch().then(function() {
expect(false).toBeTruthy();
}, function(error) {
expect(error.userFriendlyErrors).toEqual(["This application has errored. Please notify its developers: Cannot fetch data which has no id, or the if database is not currently opened."]);
}).done(done);
}, specIsRunningTooLong);
it("should refuse to fetch if no database can be deduced", function(done) {
FieldDBObject.application = null;
var object = new FieldDBObject({
dbname: "lingallama-communitycorpus",
id: "D093j2ae-akmoi3m-2a3wkjen",
corpus: {}
});
object.fetch().then(function() {
expect(false).toBeTruthy();
}, function(error) {
expect(error.userFriendlyErrors).toEqual(["This application has errored. Please notify its developers: Cannot fetch data which has no id, or the if database is not currently opened."]);
}).done(done);
}, specIsRunningTooLong);
it("should add the user who saved and other housekeeping before a save of a new item", function() {
var object = new FieldDBObject({
dbname: "lingallama-communitycorpus",
something: "else",
// debugMode: true
});
var snapshot = object.createSaveSnapshot();
expect(snapshot.fieldDBtype).toEqual("FieldDBObject");
expect((snapshot.dateCreated + "").length).toEqual(13);
expect(snapshot.version).toEqual(object.version);
expect(snapshot.enteredByUser).toBeDefined();
expect(snapshot.enteredByUser.value).toEqual("unknown");
expect(snapshot.enteredByUser.json).toBeDefined();
expect(snapshot.enteredByUser.json.user).toEqual({
username: "unknown",
name: "",
lastname: undefined,
firstname: undefined,
gravatar: undefined
});
expect(snapshot.enteredByUser.json.software).toBeDefined();
expect(snapshot.enteredByUser.json.hardware).toBeDefined();
// {
// value: 'unknown',
// json: {
// user: {
// username: 'unknown',
// name: "",
// lastname: undefined,
// firstname: undefined,
// gravatar: undefined
// },
// software: {
// version: 'v0.10.29',
// appVersion: 'PhantomJS unknown'
// },
// hardware: {
// endianness: 'LE',
// platform: 'darwin',
// hostname: 'nariakira.local',
// type: 'Darwin',
// arch: 'x64',
// release: '13.4.0',
// totalmem: 8589934592,
// cpus: 8
// }
// }
// }
expect(snapshot.enteredByUser.value).toEqual("unknown");
expect(snapshot.enteredByUser.json.user.name).toEqual("");
expect(snapshot.enteredByUser.json.user.username).toEqual("unknown");
object.debug("hardware", snapshot.enteredByUser.json.hardware);
if (snapshot.enteredByUser.json.software.appVersion === "PhantomJS unknown") {
expect(snapshot.enteredByUser.json.software.appVersion).toEqual("PhantomJS unknown");
expect(snapshot.enteredByUser.json.hardware.cpus).toBeGreaterThan(1);
} else {
expect(snapshot.enteredByUser.json.software.appVersion).toBeDefined();
expect(snapshot.enteredByUser.json.software.appVersion).toContain("Safari");
expect(snapshot.enteredByUser.json.hardware.cpus).toBeUndefined();
}
expect(object.fieldDBtype).toEqual("FieldDBObject");
expect((object.dateCreated + "").length).toEqual(13);
expect(object.version).toEqual(object.version);
expect(object.enteredByUser).toBeDefined();
expect(object.enteredByUser.value).toEqual("unknown");
expect(object.enteredByUser.json).toBeDefined();
expect(object.enteredByUser.json.user).toEqual({
username: "unknown",
name: "",
lastname: undefined,
firstname: undefined,
gravatar: undefined
});
expect(object.enteredByUser.json.software).toBeDefined();
expect(object.enteredByUser.json.hardware).toBeDefined();
});
it("should add the user who saved and other housekeeping before a save of an existing item", function() {
var originalObject = {
dbname: "lingallama-communitycorpus",
something: "else",
_id: "67gwes98rdjo",
_rev: "8-aeifnaoao"
// debugMode: true
};
var object = new FieldDBObject(originalObject);
var snapshot = object.createSaveSnapshot();
expect(snapshot.fieldDBtype).toEqual("FieldDBObject");
expect(snapshot.dateCreated).toEqual(originalObject.dateCreated);
expect(snapshot.version).toEqual(object.version);
expect(snapshot.enteredByUser).toEqual(originalObject.enteredByUser);
expect(snapshot.modifiedByUser).toBeDefined();
expect(snapshot.modifiedByUser).toBeDefined();
expect(snapshot.modifiedByUser.value).toEqual("unknown");
expect(snapshot.modifiedByUser.json).toBeDefined();
expect(snapshot.modifiedByUser.json.users).toBeDefined();
expect(snapshot.modifiedByUser.json.users[0]).toBeDefined();
expect(snapshot.modifiedByUser.json.users[0].username).toEqual("unknown");
expect(snapshot.modifiedByUser.json.users[0].software).toBeDefined();
expect(snapshot.modifiedByUser.json.users[0].hardware).toBeDefined();
// {
// users: [{
// username: 'unknown',
// name: "",
// lastname: undefined,
// firstname: undefined,
// gravatar: undefined,
// software: {
// version: 'v0.10.29',
// appVersion: 'PhantomJS unknown'
// },
// hardware: {
// endianness: 'LE',
// platform: 'darwin',
// hostname: 'nariakira.local',
// type: 'Darwin',
// arch: 'x64',
// release: '13.4.0',
// totalmem: 8589934592,
// cpus: 8
// }
// }]
// }
expect(object.fieldDBtype).toEqual("FieldDBObject");
expect(object.modifiedByUser).toBeDefined();
expect(object.modifiedByUser).toBeDefined();
expect(object.modifiedByUser.value).toEqual("unknown");
expect(object.modifiedByUser.json).toBeDefined();
expect(object.modifiedByUser.json.users).toBeDefined();
expect(object.modifiedByUser.json.users[0]).toBeDefined();
expect(object.modifiedByUser.json.users[0].username).toEqual("unknown");
expect(object.modifiedByUser.json.users[0].software).toBeDefined();
expect(object.modifiedByUser.json.users[0].hardware).toBeDefined();
});
it("should be able to set the revision number and other housekeeping after a save of a new item", function(done) {
var object = new FieldDBObject({
dbname: "lingallama-communitycorpus",
something: "else",
corpus: {
get: mockDatabase.get,
set: mockDatabase.set,
dbname: "lingallama-communitycorpus"
}
// debugMode: true
});
object.fossil = object.toJSON();
object.something = "must change something in order to do a true save.";
object.save().then(function(resultingdocument) {
expect(resultingdocument).toBe(object);
expect(object.id).toBeDefined();
expect(object.rev).toBeDefined();
expect(object.unsaved).toEqual(false);
expect(object.fossil).toBeDefined();
}, function(error) {
object.debug(error);
expect(true).toBeFalsy();
}).done(done);
}, specIsRunningTooLong);
it("should be able to set the revision number and other housekeeping after a save of an existing item", function(done) {
var object = new FieldDBObject({
corpus: {
get: mockDatabase.get,
set: mockDatabase.set,
dbname: "lingallama-communitycorpus"
},
dbname: "lingallama-communitycorpus",
something: "else",
_rev: "5-ioewmraoimwa",
_id: "weomaoi23o",
modifiedByUser: {
"label": "modifiedByUser",
"value": "inuktitutcleaningbot",
"mask": "inuktitutcleaningbot",
"encrypted": "",
"shouldBeEncrypted": "",
"help": "An array of users who modified the datum",
"showToUserTypes": "all",
"readonly": true,
"users": [{
"gravatar": "968b8e7fb72b5ffe2915256c28a9414c",
"username": "inuktitutcleaningbot",
"collection": "users",
"firstname": "Cleaner",
"lastname": "Bot"
}],
"userchooseable": "disabled"
},
// debugMode: true
});
expect(object.modifiedByUser.users).toBeDefined();
object.fossil = object.toJSON();
object.something = "causing a real save";
object.save().then(function(resultingdocument) {
expect(resultingdocument).toBe(object);
expect(object.id).toBeDefined();
expect(object.rev).toBeDefined();
expect(object.unsaved).toEqual(false);
expect(object.fossil).toBeDefined();
expect(object.modifiedByUser.value).toEqual("inuktitutcleaningbot, unknown");
expect(object.modifiedByUser.users).toBeUndefined();
expect(object.modifiedByUser.json.users[0].username).toEqual("inuktitutcleaningbot");
expect(object.modifiedByUser.json.users[1].username).toEqual("unknown");
expect(object.modifiedByUser.json.users[1].software.appVersion.length).toBeGreaterThan(10);
object.debug("hardware", object.modifiedByUser.json.hardware);
expect(object.modifiedByUser.json.users[1].hardware).toBeDefined();
}, function(userFriendlyErrors) {
expect(userFriendlyErrors[0]).toContain(CORS_ERROR);
}).done(done);
expect(object.saving).toEqual(true);
expect(object.modifiedByUser.value).toEqual("inuktitutcleaningbot, unknown");
expect(object.modifiedByUser.users).toBeUndefined();
expect(object.modifiedByUser.json.users[0].username).toEqual("inuktitutcleaningbot");
expect(object.modifiedByUser.json.users[1].username).toEqual("unknown");
expect(object.modifiedByUser.json.users[1].software.appVersion.length).toBeGreaterThan(10);
object.debug("hardware", object.modifiedByUser.json.hardware);
expect(object.modifiedByUser.json.users[1].hardware).toBeDefined();
}, specIsRunningTooLong);
it("should avoid unnecesary saving", function(done) {
var object = new FieldDBObject({
id: "2839aj983aja",
corpus: {
get: mockDatabase.get,
set: mockDatabase.set
}
});
expect(object.fossil).toBeUndefined();
expect(object.unsaved).toEqual(undefined);
expect(object.calculateUnsaved()).toEqual(undefined);
object.fetch().then(function(result) {
// fetch is working
expect(result).toBe(object);
expect(object.id).toEqual("2839aj983aja");
expect(object.rev).toBeDefined();
expect(object.modifiedByUser.equals({
_fieldDBtype: "FieldDBObject",
label: "modifiedByUser",
value: "inuktitutcleaningbot",
mask: "inuktitutcleaningbot",
encrypted: "",
shouldBeEncrypted: "",
help: "An array of users who modified the datum",
showToUserTypes: "all",
readonly: true,
users: [{
gravatar: "968b8e7fb72b5ffe2915256c28a9414c",
username: "inuktitutcleaningbot",
collection: "users",
firstname: "Cleaner",
lastname: "Bot"
}],
userchooseable: "disabled",
_dateCreated: object.modifiedByUser.dateCreated,
_version: object.version
})).toBeTruthy();
// this was a placeholder because it had no rev, so we should now have a fossil
expect(object.fossil).toBeDefined();
expect(object.unsaved).toEqual(false);
expect(object.calculateUnsaved()).toEqual(false);
object.warnMessage = "";
var oldRev = object.rev + "";
object.save().then(function(result) {
expect(result).toBe(object);
expect(object.warnMessage).toContain("Item hasn't really changed, no need to save...");
expect(object.rev).toEqual(oldRev);
return object;
}, function(error) {
object.debug(error);
expect(false).toBeTruthy();
return object;
});
}).done(done);
}, specIsRunningTooLong);
it("should detect if item was actually changed", function(done) {
var object = new FieldDBObject({
corpus: {
get: mockDatabase.get,
set: mockDatabase.set,
dbname: "jenkins-firstcorpus"
},
dbname: "jenkins-firstcorpus",
something: "else",
_rev: "2-28q9ja9q0ka",
_id: "weomaoi23o",
modifiedByUser: {
"label": "modifiedByUser",
"value": "quotecleaningbot",
"mask": "quotecleaningbot",
"encrypted": "",
"shouldBeEncrypted": "",
"help": "An array of users who modified the datum",
"showToUserTypes": "all",
"readonly": true,
"users": [{
"gravatar": "968b8e7fb72b5ffe2915256c28a9414c",
"username": "quotecleaningbot",
"collection": "users",
"firstname": "Cleaner",
"lastname": "Bot"
}],
"userchooseable": "disabled"
},
// debugMode: true
});
// When we load an object, we dont know where it comes from we wont set the fossil, since it might not match any database version
expect(object.fossil).toBeUndefined();
// It will have an undefined (falsy) status which means to most UIs that it should be saved before they leave the page.
expect(object.calculateUnsaved()).toEqual(undefined);
var oldRev = object.rev;
expect(oldRev).toEqual("2-28q9ja9q0ka");
object.save().then(function(result) {
expect(result).toEqual(object);
// It really saved
expect(object.rev.length).toBeGreaterThan(13);
expect(oldRev).not.toEqual(object.rev);
// It now has an updated fossil
expect(object.fossil).toBeDefined();
expect(object.fossil.rev).not.toEqual(oldRev);
expect(object.unsaved).toEqual(false);
expect(object.calculateUnsaved()).toEqual(false);
// Make modifications and it should detect them
object.fields = [{
value: "something new"
}];
expect(object.unsaved).toEqual(false);
expect(object.calculateUnsaved()).toEqual(true);
expect(object.unsaved).toEqual(true);
}, function(error) {
object.debug(error);
expect(true).toBeFalsy();
return object;
}).done(done);
}, specIsRunningTooLong);
it("should be able set entered by user using database connection info", function(done) {
var object = new FieldDBObject({
dbname: "lingallama-communitycorpus",
something: "else",
corpus: {
get: mockDatabase.get,
set: mockDatabase.set,
dbname: "lingallama-communitycorpus"
}
});
object.corpus.connectionInfo = {
"ok": true,
"userCtx": {
"name": "teammatetiger",
"roles": ["computationalfieldworkshop-group_data_entry_tutorial_reader", "fielddbuser", "jessepollak-spring_2013_field_methods_reader", "lingllama-cherokee_admin", "lingllama-cherokee_commenter", "lingllama-cherokee_reader", "lingllama-cherokee_writer", "lingllama-communitycorpus_admin", "lingllama-firstcorpus_admin", "lingllama-firstcorpus_commenter",
"lingllama-firstcorpus_reader", "lingllama-firstcorpus_writer", "lingllama-test_corpus_admin", "lingllama-test_corpus_commenter", "lingllama-test_corpus_reader", "lingllama-test_corpus_writer", "teammatetiger-firstcorpus_commenter", "teammatetiger-firstcorpus_reader", "teammatetiger-firstcorpus_writer", "lingllama-communitycorpus_commenter",
"lingllama-communitycorpus_reader", "lingllama-communitycorpus_writer"
]
},
"info": {
"authentication_db": "_users",
"authentication_handlers": ["oauth", "cookie", "default"],
"authenticated": "cookie"
}
};
expect(object.corpus.connectionInfo.userCtx.name).toEqual("teammatetiger");
object.fossil = object.toJSON();
object.something = "modified after fossil was created";
object.save().then(function(resultingdocument) {
expect(resultingdocument).toEqual(object);
expect(object.rev.length).toBeGreaterThan(13);
expect(object.enteredByUser).toBeDefined();
expect(object.enteredByUser.value).toEqual("teammatetiger");
}, function(error) {
object.debug(error);
expect(true).toBeFalsy();
return object;
}).done(done);
}, specIsRunningTooLong);
it("should be able set location of the data", function(done) {
FieldDBObject.software = {
location: {
"speed": null,
"heading": null,
"altitudeAccuracy": null,
"accuracy": 57,
"altitude": null,
"longitude": -73.5537868,
"latitude": 45.5169767
}
};
var object = new FieldDBObject({
dbname: "lingallama-communitycorpus",
corpus: {
get: mockDatabase.get,
set: mockDatabase.set,
dbname: "lingallama-communitycorpus"
},
something: "else",
location: {
"id": "location",
"type": "location",
"labelFieldLinguists": "Location",
"labelNonLinguists": "Location",
"labelTranslators": "Location",
"shouldBeEncrypted": true,
"encrypted": true,
"defaultfield": true,
"value": "41,21",
"json": {
"location": {
"latitude": 21,
"longitude": 41,
"accuracy": 20
}
},
"help": "This is the GPS location of where the document exists/was created (if available)",
"helpLinguists": "This is the GPS location of where the document exists/was created (if available)"
}
});
expect(object.location.value).toEqual("41,21");
object.fossil = object.toJSON();
object.something = "i changed this after the fossil was created";
object.save().then(function(resultingdocument) {
expect(resultingdocument).toEqual(object);
expect(object.rev.length).toBeGreaterThan(13);
}, function(userFriendlyErrors) {
expect(userFriendlyErrors[0]).toContain(CORS_ERROR);
}).done(done);
expect(object.location.value).toEqual("45.5169767,-73.5537868");
expect(object.location.json.previousLocations).toEqual([{
"latitude": 21,
"longitude": 41,
"accuracy": 20
}]);
expect(object.location.json.location.latitude).toEqual(45.5169767);
}, specIsRunningTooLong);
var putInTrash = function(done) {
var object = new FieldDBObject({
dbname: "lingallama-communitycorpus",
something: "else",
corpus: {
get: mockDatabase.get,