-
Notifications
You must be signed in to change notification settings - Fork 362
/
loopback-dl.test.js
1971 lines (1648 loc) · 60.9 KB
/
loopback-dl.test.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
// Copyright IBM Corp. 2013,2018. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
// This test written in mocha+should.js
'use strict';
const should = require('./init.js');
const assert = require('assert');
const async = require('async');
const jdb = require('../');
const ModelBuilder = jdb.ModelBuilder;
const DataSource = jdb.DataSource;
describe('ModelBuilder', function() {
it('supports plain models', function(done) {
const modelBuilder = new ModelBuilder();
const User = modelBuilder.define('User', {
name: String,
bio: ModelBuilder.Text,
approved: Boolean,
joinedAt: Date,
age: Number,
});
// define any custom method
User.prototype.getNameAndAge = function() {
return this.name + ', ' + this.age;
};
modelBuilder.models.should.be.type('object').and.have.property('User').exactly(User);
modelBuilder.definitions.should.be.type('object').and.have.property('User');
const user = new User({name: 'Joe', age: 20, xyz: false});
User.modelName.should.equal('User');
user.should.be.type('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.have.property('xyz', false);
user.should.have.property('bio', undefined);
done(null, User);
});
it('ignores unknown properties in strict mode', function(done) {
const modelBuilder = new ModelBuilder();
const User = modelBuilder.define('User', {name: String, bio: String}, {strict: true});
const user = new User({name: 'Joe', age: 20});
User.modelName.should.equal('User');
user.should.be.type('object');
user.should.have.property('name', 'Joe');
user.should.not.have.property('age');
user.toObject().should.not.have.property('age');
user.toObject(true).should.not.have.property('age');
user.should.have.property('bio', undefined);
done(null, User);
});
it('ignores non-predefined properties in strict mode', function(done) {
const modelBuilder = new ModelBuilder();
const User = modelBuilder.define('User', {name: String, bio: String}, {strict: true});
const user = new User({name: 'Joe'});
user.age = 10;
user.bio = 'me';
user.should.have.property('name', 'Joe');
user.should.have.property('bio', 'me');
// Non predefined property age should be ignored in strict mode if schemaOnly parameter is not false
user.toObject().should.not.have.property('age');
user.toObject(true).should.not.have.property('age');
user.toObject(false).should.have.property('age', 10);
// Predefined property bio should be kept in strict mode
user.toObject().should.have.property('bio', 'me');
user.toObject(true).should.have.property('bio', 'me');
user.toObject(false).should.have.property('bio', 'me');
done(null, User);
});
it('throws an error when unknown properties are used if strict=throw', function(done) {
const modelBuilder = new ModelBuilder();
const User = modelBuilder.define('User', {name: String, bio: String}, {strict: 'throw'});
try {
const user = new User({name: 'Joe', age: 20});
assert(false, 'The code should have thrown an error');
} catch (e) {
assert(true, 'The code is expected to throw an error');
}
done(null, User);
});
it('supports open models', function(done) {
const modelBuilder = new ModelBuilder();
const User = modelBuilder.define('User', {}, {strict: false});
const user = new User({name: 'Joe', age: 20});
User.modelName.should.equal('User');
user.should.be.type('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
done(null, User);
});
it('accepts non-predefined properties in non-strict mode', function(done) {
const modelBuilder = new ModelBuilder();
const User = modelBuilder.define('User', {name: String, bio: String}, {strict: false});
const user = new User({name: 'Joe'});
user.age = 10;
user.bio = 'me';
user.should.have.property('name', 'Joe');
user.should.have.property('bio', 'me');
// Non predefined property age should be kept in non-strict mode
user.toObject().should.have.property('age', 10);
user.toObject(true).should.have.property('age', 10);
user.toObject(false).should.have.property('age', 10);
// Predefined property bio should be kept
user.toObject().should.have.property('bio', 'me');
user.toObject({onlySchema: true}).should.have.property('bio', 'me');
user.toObject({onlySchema: false}).should.have.property('bio', 'me');
done(null, User);
});
it('uses non-strict mode by default', function(done) {
const modelBuilder = new ModelBuilder();
const User = modelBuilder.define('User', {});
const user = new User({name: 'Joe', age: 20});
User.modelName.should.equal('User');
user.should.be.type('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
done(null, User);
});
it('supports nested model definitions', function(done) {
const modelBuilder = new ModelBuilder();
// simplier way to describe model
const User = modelBuilder.define('User', {
name: String,
bio: ModelBuilder.Text,
approved: Boolean,
joinedAt: Date,
age: Number,
address: {
street: String,
city: String,
state: String,
zipCode: String,
country: String,
},
emails: [
{
label: String,
email: String,
},
],
friends: [String],
});
// define any custom method
User.prototype.getNameAndAge = function() {
return this.name + ', ' + this.age;
};
modelBuilder.models.should.be.type('object').and.have.property('User', User);
modelBuilder.definitions.should.be.type('object').and.have.property('User');
let user = new User({
name: 'Joe', age: 20,
address: {street: '123 Main St', 'city': 'San Jose', state: 'CA'},
emails: [
{label: 'work', email: '[email protected]'},
],
friends: ['Mary', 'John'],
});
User.modelName.should.equal('User');
user.should.be.type('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.have.property('bio', undefined);
user.should.have.property('address');
user.address.should.have.property('city', 'San Jose');
user.address.should.have.property('state', 'CA');
user = user.toObject();
user.emails.should.have.property('length', 1);
user.emails[0].should.have.property('label', 'work');
user.emails[0].should.have.property('email', '[email protected]');
user.friends.should.have.property('length', 2);
assert.equal(user.friends[0], 'Mary');
assert.equal(user.friends[1], 'John');
done(null, User);
});
it('allows models to be referenced by name before they are defined', function(done) {
const modelBuilder = new ModelBuilder();
const User = modelBuilder.define('User', {name: String, address: 'Address'});
let user;
try {
user = new User({name: 'Joe', address: {street: '123 Main St', 'city': 'San Jose', state: 'CA'}});
assert(false, 'An exception should have been thrown');
} catch (e) {
// Ignore
}
const Address = modelBuilder.define('Address', {
street: String,
city: String,
state: String,
zipCode: String,
country: String,
});
user = new User({name: 'Joe', address: {street: '123 Main St', 'city': 'San Jose', state: 'CA'}});
User.modelName.should.equal('User');
User.definition.properties.address.should.have.property('type', Address);
user.should.be.type('object');
assert(user.name === 'Joe');
user.address.should.have.property('city', 'San Jose');
user.address.should.have.property('state', 'CA');
done(null, User);
});
it('defines an id property for composite ids', function() {
const modelBuilder = new ModelBuilder();
const Follow = modelBuilder.define('Follow', {
followerId: {type: String, id: 1},
followeeId: {type: String, id: 2},
followAt: Date,
});
const follow = new Follow({followerId: 1, followeeId: 2});
follow.should.have.property('id');
assert.deepEqual(follow.id, {followerId: 1, followeeId: 2});
});
it('instantiates model from data with no constructor', function(done) {
const modelBuilder = new ModelBuilder();
const User = modelBuilder.define('User', {name: String, age: Number});
try {
const data = Object.create(null);
data.name = 'Joe';
data.age = 20;
const user = new User(data);
assert(true, 'The code is expected to pass');
} catch (e) {
assert(false, 'The code should have not thrown an error');
}
done();
});
it('instantiates model from data with non function constructor', function(done) {
const modelBuilder = new ModelBuilder();
const User = modelBuilder.define('User', {name: String, age: Number});
try {
const Person = function(name, age) {
this.name = name;
this.age = age;
};
Person.prototype.constructor = 'constructor';
const data = new Person('Joe', 20);
const user = new User(data);
assert(false, 'The code should have thrown an error');
} catch (e) {
e.message.should.equal('Property name "constructor" is not allowed in User data');
assert(true, 'The code is expected to throw an error');
}
done();
});
});
describe('DataSource ping', function() {
const ds = new DataSource('memory');
ds.settings.connectionTimeout = 50; // ms
ds.connector.connect = function(cb) {
// Mock up the long delay
setTimeout(cb, 100);
};
ds.connector.ping = function(cb) {
cb(new Error('bad connection 2'));
};
it('reports connection errors during ping', function(done) {
ds.ping(function(err) {
(!!err).should.be.true;
err.message.should.be.eql('bad connection 2');
done();
});
});
it('cancels invocation after timeout', function(done) {
ds.connected = false; // Force connect
const Post = ds.define('Post', {
title: {type: String, length: 255},
});
Post.create(function(err) {
(!!err).should.be.true;
err.message.should.be.eql('Timeout in connecting after 50 ms');
done();
});
});
});
describe('DataSource define model', function() {
it('supports plain model definitions', function() {
const ds = new DataSource('memory');
// define models
const Post = ds.define('Post', {
title: {type: String, length: 255},
content: {type: ModelBuilder.Text},
date: {type: Date, default: function() {
return new Date();
}},
timestamp: {type: Number, default: Date.now},
published: {type: Boolean, default: false, index: true},
});
// simpler way to describe model
const User = ds.define('User', {
name: String,
bio: ModelBuilder.Text,
approved: Boolean,
joinedAt: {type: Date, default: Date},
age: Number,
});
const Group = ds.define('Group', {group: String});
User.mixin(Group);
// define any custom method
User.prototype.getNameAndAge = function() {
return this.name + ', ' + this.age;
};
const user = new User({name: 'Joe', group: 'G1'});
assert.equal(user.name, 'Joe');
assert.equal(user.group, 'G1');
assert(user.joinedAt instanceof Date);
// setup relationships
User.hasMany(Post, {as: 'posts', foreignKey: 'userId'});
Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
User.hasAndBelongsToMany('groups');
const user2 = new User({name: 'Smith'});
user2.save(function(err) {
const post = user2.posts.build({title: 'Hello world'});
post.save(function(err, data) {
// console.log(err ? err : data);
});
});
Post.findOne({where: {published: false}, order: 'date DESC'}, function(err, data) {
// console.log(data);
});
User.create({name: 'Jeff'}, function(err, data) {
if (err) {
return;
}
const post = data.posts.build({title: 'My Post'});
});
User.create({name: 'Ray'}, function(err, data) {
// console.log(data);
});
const Article = ds.define('Article', {title: String});
const Tag = ds.define('Tag', {name: String});
Article.hasAndBelongsToMany('tags');
Article.create(function(e, article) {
article.tags.create({name: 'popular'}, function(err, data) {
Article.findOne(function(e, article) {
article.tags(function(e, tags) {
// console.log(tags);
});
});
});
});
// should be able to attach a data source to an existing model
const modelBuilder = new ModelBuilder();
const Color = modelBuilder.define('Color', {
name: String,
});
Color.should.not.have.property('create');
// attach
ds.attach(Color);
Color.should.have.property('create');
Color.create({name: 'red'});
Color.create({name: 'green'});
Color.create({name: 'blue'});
Color.all(function(err, colors) {
colors.should.have.lengthOf(3);
});
});
it('emits events during attach', function() {
const ds = new DataSource('memory');
const modelBuilder = new ModelBuilder();
const User = modelBuilder.define('User', {
name: String,
});
let seq = 0;
let dataAccessConfigured = -1;
let dataSourceAttached = -1;
User.on('dataAccessConfigured', function(model) {
dataAccessConfigured = seq++;
assert(User.create);
assert(User.hasMany);
});
User.on('dataSourceAttached', function(model) {
assert(User.dataSource instanceof DataSource);
dataSourceAttached = seq++;
});
ds.attach(User);
assert.equal(dataAccessConfigured, 0);
assert.equal(dataSourceAttached, 1);
});
it('ignores unknown properties in strict mode', function(done) {
const ds = new DataSource('memory');
const User = ds.define('User', {name: String, bio: String}, {strict: true});
User.create({name: 'Joe', age: 20}, function(err, user) {
User.modelName.should.equal('User');
user.should.be.type('object');
assert(user.name === 'Joe');
assert(user.age === undefined);
assert(user.toObject().age === undefined);
assert(user.toObject(true).age === undefined);
assert(user.bio === undefined);
done(null, User);
});
});
it('throws an error when unknown properties are used if strict=throw', function(done) {
const ds = new DataSource('memory');
const User = ds.define('User', {name: String, bio: String}, {strict: 'throw'});
try {
const user = new User({name: 'Joe', age: 20});
assert(false, 'The code should have thrown an error');
} catch (e) {
assert(true, 'The code is expected to throw an error');
}
done(null, User);
});
describe('strict mode "validate"', function() {
it('reports validation errors for unknown properties', function() {
const ds = new DataSource('memory');
const User = ds.define('User', {name: String}, {strict: 'validate'});
const user = new User({name: 'Joe', age: 20});
user.isValid().should.be.false;
const codes = user.errors && user.errors.codes || {};
codes.should.have.property('age').eql(['unknown-property']);
});
});
it('supports open model definitions', function(done) {
const ds = new DataSource('memory');
const User = ds.define('User', {}, {strict: false});
User.modelName.should.equal('User');
User.create({name: 'Joe', age: 20}, function(err, user) {
user.should.be.type('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
User.findById(user.id, function(err, user) {
user.should.be.type('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
done(null, User);
});
});
});
it('uses non-strict mode by default', function(done) {
const ds = new DataSource('memory');
const User = ds.define('User', {});
User.create({name: 'Joe', age: 20}, function(err, user) {
User.modelName.should.equal('User');
user.should.be.type('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
done(null, User);
});
});
it('uses strict mode by default for relational DBs', function(done) {
const ds = new DataSource('memory');
ds.connector.relational = true; // HACK
const User = ds.define('User', {name: String, bio: String}, {strict: true});
const user = new User({name: 'Joe', age: 20});
User.modelName.should.equal('User');
user.should.be.type('object');
assert(user.name === 'Joe');
assert(user.age === undefined);
assert(user.toObject().age === undefined);
assert(user.toObject(true).age === undefined);
assert(user.bio === undefined);
done(null, User);
});
it('throws an error with unknown properties in non-strict mode for relational DBs', function(done) {
const ds = new DataSource('memory');
ds.connector.relational = true; // HACK
const User = ds.define('User', {name: String, bio: String}, {strict: 'throw'});
try {
const user = new User({name: 'Joe', age: 20});
assert(false, 'The code should have thrown an error');
} catch (e) {
assert(true, 'The code is expected to throw an error');
}
done(null, User);
});
it('changes the property value for save in non-strict mode', function(done) {
const ds = new DataSource('memory');// define models
const Post = ds.define('Post');
Post.create({price: 900}, function(err, post) {
assert.equal(post.price, 900);
post.price = 1000;
post.save(function(err, result) {
assert.equal(1000, result.price);
done(err, result);
});
});
});
it('supports instance level strict mode', function() {
const ds = new DataSource('memory');
const User = ds.define('User', {name: String, bio: String}, {strict: true});
const user = new User({name: 'Joe', age: 20}, {strict: false});
user.should.have.property('__strict', false);
user.should.be.type('object');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.toObject().should.have.property('age', 20);
user.toObject(true).should.have.property('age', 20);
user.setStrict(true);
user.toObject().should.not.have.property('age');
user.toObject(true).should.not.have.property('age');
user.toObject(false).should.have.property('age', 20);
});
it('updates instances with unknown properties in non-strict mode', function(done) {
const ds = new DataSource('memory');// define models
const Post = ds.define('Post', {
title: {type: String, length: 255, index: true},
content: {type: String},
});
Post.create({title: 'a', content: 'AAA'}, function(err, post) {
post.updateAttributes({title: 'b', xyz: 'xyz'}, function(err, p) {
should.not.exist(err);
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
p.xyz.should.be.equal('xyz');
Post.findById(post.id, function(err, p) {
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
p.xyz.should.be.equal('xyz');
p.title.should.be.equal('b');
done();
});
});
});
});
it('injects id by default', function(done) {
const ds = new ModelBuilder();
const User = ds.define('User', {});
assert.deepEqual(User.definition.properties.id,
{type: Number, id: 1, generated: true, updateOnly: true});
done();
});
it('disables idInjection if the value is false', function(done) {
const ds = new ModelBuilder();
const User1 = ds.define('User', {}, {idInjection: false});
assert(!User1.definition.properties.id);
done();
});
it('updates generated id type by the connector', function(done) {
const builder = new ModelBuilder();
const User = builder.define('User', {id: {type: String, generated: true, id: true}});
assert.deepEqual(User.definition.properties.id,
{type: String, id: 1, generated: true, updateOnly: true});
const ds = new DataSource('memory');// define models
User.attachTo(ds);
assert.deepEqual(User.definition.properties.id,
{type: Number, id: 1, generated: true, updateOnly: true});
done();
});
it('allows an explicit remoting path', function() {
const ds = new DataSource('memory');
const User = ds.define('User', {name: String, bio: String}, {
http: {path: 'accounts'},
});
User.http.path.should.equal('/accounts');
});
it('allows an explicit remoting path with leading /', function() {
const ds = new DataSource('memory');
const User = ds.define('User', {name: String, bio: String}, {
http: {path: '/accounts'},
});
User.http.path.should.equal('/accounts');
});
});
describe('Model loaded with a base', function() {
it('has a base class according to the base option', function() {
const ds = new ModelBuilder();
const User = ds.define('User', {name: String});
User.staticMethod = function staticMethod() {
};
User.prototype.instanceMethod = function instanceMethod() {
};
const Customer = ds.define('Customer', {vip: Boolean}, {base: 'User'});
assert(Customer.prototype instanceof User);
assert(Customer.staticMethod === User.staticMethod);
assert(Customer.prototype.instanceMethod === User.prototype.instanceMethod);
assert.equal(Customer.base, User);
assert.equal(Customer.base, Customer.super_);
try {
const Customer1 = ds.define('Customer1', {vip: Boolean}, {base: 'User1'});
} catch (e) {
assert(e);
}
});
it('inherits properties from base model', function() {
const ds = new ModelBuilder();
const User = ds.define('User', {name: String});
const Customer = ds.define('Customer', {vip: Boolean}, {base: 'User'});
Customer.definition.properties.should.have.property('name');
Customer.definition.properties.name.should.have.property('type', String);
});
it('inherits properties by clone from base model', function() {
const ds = new ModelBuilder();
const User = ds.define('User', {name: String});
const Customer1 = ds.define('Customer1', {vip: Boolean}, {base: 'User'});
const Customer2 = ds.define('Customer2', {vip: Boolean}, {base: 'User'});
Customer1.definition.properties.should.have.property('name');
Customer2.definition.properties.should.have.property('name');
Customer1.definition.properties.name.should.not.be.equal(
Customer2.definition.properties.name
);
Customer1.definition.properties.name.should.eql(
Customer2.definition.properties.name
);
});
it('can remove properties from base model', function() {
const ds = new ModelBuilder();
const User = ds.define('User', {username: String, email: String});
const Customer = ds.define('Customer',
{name: String, username: null, email: false},
{base: 'User'});
Customer.definition.properties.should.have.property('name');
// username/email are now shielded
Customer.definition.properties.should.not.have.property('username');
Customer.definition.properties.should.not.have.property('email');
const c = new Customer({name: 'John'});
c.should.have.property('username', undefined);
c.should.have.property('email', undefined);
c.should.have.property('name', 'John');
const u = new User({username: 'X', email: '[email protected]'});
u.should.not.have.property('name');
u.should.have.property('username', 'X');
u.should.have.property('email', '[email protected]');
});
it('can configure base class via parent argument', function() {
const ds = new ModelBuilder();
const User = ds.define('User', {name: String});
User.staticMethod = function staticMethod() {
};
User.prototype.instanceMethod = function instanceMethod() {
};
const Customer = ds.define('Customer', {vip: Boolean}, {}, User);
Customer.definition.properties.should.have.property('name');
Customer.definition.properties.name.should.have.property('type', String);
assert(Customer.prototype instanceof User);
assert(Customer.staticMethod === User.staticMethod);
assert(Customer.prototype.instanceMethod === User.prototype.instanceMethod);
assert.equal(Customer.base, User);
assert.equal(Customer.base, Customer.super_);
});
});
describe('Models attached to a dataSource', function() {
let Post;
before(function() {
const ds = new DataSource('memory');// define models
Post = ds.define('Post', {
title: {type: String, length: 255, index: true},
content: {type: String},
comments: [String],
}, {forceId: false});
});
beforeEach(function(done) {
Post.destroyAll(done);
});
describe('updateOrCreate', function() {
it('updates instances', function(done) {
Post.create({title: 'a', content: 'AAA'}, function(err, post) {
post.title = 'b';
Post.updateOrCreate(post, function(err, p) {
should.not.exist(err);
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
should.not.exist(p._id);
Post.findById(post.id, function(err, p) {
p.id.should.be.equal(post.id);
should.not.exist(p._id);
p.content.should.be.equal(post.content);
p.title.should.be.equal('b');
done();
});
});
});
});
it('updates instances without removing existing properties', function(done) {
Post.create({title: 'a', content: 'AAA', comments: ['Comment1']}, function(err, post) {
post = post.toObject();
delete post.title;
delete post.comments;
Post.updateOrCreate(post, function(err, p) {
should.not.exist(err);
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
should.not.exist(p._id);
Post.findById(post.id, function(err, p) {
p.id.should.be.equal(post.id);
should.not.exist(p._id);
p.content.should.be.equal(post.content);
p.title.should.be.equal('a');
p.comments.length.should.be.equal(1);
p.comments[0].should.be.equal('Comment1');
done();
});
});
});
});
it('creates a new instance if it does not exist', function(done) {
const post = {id: 123, title: 'a', content: 'AAA'};
Post.updateOrCreate(post, function(err, p) {
should.not.exist(err);
p.title.should.be.equal(post.title);
p.content.should.be.equal(post.content);
p.id.should.be.equal(post.id);
Post.findById(p.id, function(err, p) {
p.id.should.be.equal(post.id);
should.not.exist(p._id);
p.content.should.be.equal(post.content);
p.title.should.be.equal(post.title);
p.id.should.be.equal(post.id);
done();
});
});
});
});
describe('save', function() {
it('updates instance with the same id', function(done) {
Post.create({title: 'a', content: 'AAA'}, function(err, post) {
post.title = 'b';
post.save(function(err, p) {
should.not.exist(err);
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
should.not.exist(p._id);
Post.findById(post.id, function(err, p) {
p.id.should.be.equal(post.id);
should.not.exist(p._id);
p.content.should.be.equal(post.content);
p.title.should.be.equal('b');
done();
});
});
});
});
it('updates the instance without removing existing properties', function(done) {
Post.create({title: 'a', content: 'AAA'}, function(err, post) {
delete post.title;
post.save(function(err, p) {
should.not.exist(err);
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
should.not.exist(p._id);
Post.findById(post.id, function(err, p) {
p.id.should.be.equal(post.id);
should.not.exist(p._id);
p.content.should.be.equal(post.content);
p.title.should.be.equal('a');
done();
});
});
});
});
it('creates a new instance if it does not exist', function(done) {
const post = new Post({id: '123', title: 'a', content: 'AAA'});
post.save(post, function(err, p) {
should.not.exist(err);
p.title.should.be.equal(post.title);
p.content.should.be.equal(post.content);
p.id.should.be.equal(post.id);
Post.findById(p.id, function(err, p) {
p.id.should.be.equal(post.id);
should.not.exist(p._id);
p.content.should.be.equal(post.content);
p.title.should.be.equal(post.title);
p.id.should.be.equal(post.id);
done();
});
});
});
});
});
describe('DataSource connector types', function() {
it('returns an array of types using getTypes', function() {
const ds = new DataSource('memory');
const types = ds.getTypes();
assert.deepEqual(types, ['db', 'nosql', 'memory']);
});
describe('supportTypes', function() {
it('tests supported types by string', function() {
const ds = new DataSource('memory');
const result = ds.supportTypes('db');
assert(result);
});
it('tests supported types by array', function() {
const ds = new DataSource('memory');
const result = ds.supportTypes(['db', 'memory']);
assert(result);
});
it('tests unsupported types by string', function() {
const ds = new DataSource('memory');
const result = ds.supportTypes('rdbms');
assert(!result);
});
it('tests unsupported types by array', function() {
const ds = new DataSource('memory');
let result = ds.supportTypes(['rdbms', 'memory']);
assert(!result);
result = ds.supportTypes(['rdbms']);
assert(!result);
});
});
});
describe('DataSource._resolveConnector', function() {
// Mocked require
const loader = function(name) {
if (name.indexOf('./connectors/') !== -1) {
// ./connectors/<name> doesn't exist
return null;
}
if (name === 'loopback-connector-abc') {
// Assume loopback-connector-abc doesn't exist
return null;
}
return {
name: name,
};
};
it('resolves connector by path', function() {
const connector = DataSource._resolveConnector(__dirname + '/../lib/connectors/memory');
assert(connector.connector);
});
it('resolves connector by internal name', function() {
const connector = DataSource._resolveConnector('memory');
assert(connector.connector);
});
it('resolves connector by module name starting with loopback-connector-', function() {
const connector = DataSource._resolveConnector('loopback-connector-xyz', loader);
assert(connector.connector);