/
objectid.test.js
196 lines (169 loc) · 5.89 KB
/
objectid.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
// Copyright IBM Corp. 2013,2020. All Rights Reserved.
// Node module: loopback-connector-mongodb
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
require('./init.js');
let Book, Chapter;
const ds = global.getDataSource();
const objectIDLikeString = '7cd2ad46ffc580ba45d3cb1f';
const objectIDLikeString2 = '7cd2ad46ffc580ba45d3cb1e';
describe('ObjectID', function() {
before(function() {
Book = ds.define('Book');
Chapter = ds.define('Chapter');
Book.hasMany('chapters');
Chapter.belongsTo('book');
});
it('should cast foreign keys as ObjectID', function(done) {
Chapter.beforeCreate = function(next, data) {
data.bookId.should.be.an.instanceOf(ds.ObjectID);
this.bookId.should.be.an.instanceOf(ds.ObjectID);
next();
};
Book.create(function(err, book) {
if (err) return done(err);
Chapter.create({bookId: book.id.toString()}, done);
});
});
it('should convert 24 byte hex string as ObjectID', function() {
const ObjectID = ds.connector.getDefaultIdType();
const str = objectIDLikeString;
ObjectID(str).should.be.an.instanceOf(ds.ObjectID);
});
it('should not convert 12 byte string as ObjectID', function() {
const ObjectID = ds.connector.getDefaultIdType();
const str = 'line-by-line';
ObjectID(str).should.be.equal(str);
});
it('should keep mongodb ObjectID as is', function() {
const ObjectID = ds.connector.getDefaultIdType();
const id = new ds.ObjectID();
ObjectID(id).should.be.an.instanceOf(ds.ObjectID);
});
it('should keep non-string id as it', function() {
const ObjectID = ds.connector.getDefaultIdType();
const id = 123;
ObjectID(id).should.be.equal(123);
});
context('strictObjectIDCoercion', function() {
context('when set to false (default)', function() {
const Article = ds.createModel(
'ArticleA',
{
xid: String,
title: String,
},
);
beforeEach(function(done) {
Article.deleteAll(done);
});
it('should save as ObjectID', async function() {
await Article.create({xid: objectIDLikeString, title: 'abc'});
const found = await Article.findOne({where: {title: 'abc'}});
found.xid.should.be.an.instanceOf(ds.ObjectID);
});
});
context('when set to true', function() {
const Article = ds.createModel(
'ArticleB',
{
xid: String,
title: String,
},
{strictObjectIDCoercion: true},
);
beforeEach(function(done) {
Article.deleteAll(done);
});
it('should not save as ObjectID', async function() {
await Article.create({xid: objectIDLikeString, title: 'abc'});
const found = await Article.findOne({where: {title: 'abc'}});
found.xid.should.not.be.an.instanceOf(ds.ObjectID);
});
});
});
context("mongodb: {dataType: 'objectid'}", function() {
const Article = ds.createModel(
'ArticleC',
{
xid: {type: String, mongodb: {dataType: 'objectid'}},
xidArr: {type: [String], mongodb: {dataType: 'objectid'}},
title: String,
},
{strictObjectIDCoercion: true},
);
beforeEach(function(done) {
Article.deleteAll(done);
});
it('should throw if value is not an ObjectID-like string', async function() {
try {
await Article.create({xid: '', title: 'abc'});
} catch (e) {
e.message.should.match(/not an ObjectID string/);
}
});
it('should save as ObjectID regardless of strictObjectIDCoercion: true', async function() {
await Article.create({xid: objectIDLikeString, title: 'abc'});
const found = await Article.findOne({where: {title: 'abc'}});
found.xid.should.be.an.instanceOf(ds.ObjectID);
});
it('should properly save an array of ObjectIDs', async () => {
await Article.create({
xid: objectIDLikeString,
xidArr: [objectIDLikeString, objectIDLikeString2],
title: 'arrayOfObjectID',
});
const found = await Article.findOne({where: {title: 'arrayOfObjectID'}});
// the type of the returned array is actually string even it's stored as ObjectIds in the db as expected
found.xidArr.should.be.an.Array().which.containDeep([
new ds.ObjectID(objectIDLikeString),
new ds.ObjectID(objectIDLikeString2),
]);
});
it('handles auto-generated PK properties defined in LB4 style', async () => {
const Note = ds.createModel('NoteLB4', {
id: {
type: 'string',
id: true,
generated: true,
mongodb: {dataType: 'ObjectID'},
},
title: {
type: 'string',
required: true,
},
});
const result = await Note.create({title: 'hello'});
// the test passes when this call does not throw
});
});
context('ObjectID as a constructor', function() {
const Article = ds.createModel(
'ArticleC2',
{
xid: {type: ds.ObjectID},
xidArr: {type: [ds.ObjectID]},
title: String,
},
{strictObjectIDCoercion: true},
);
beforeEach(function(done) {
Article.deleteAll(done);
});
it('should save as ObjectID regardless of strictObjectIDCoercion: true', async function() {
await Article.create({xid: objectIDLikeString, title: 'abc'});
const found = await Article.findOne({where: {title: 'abc'}});
found.xid.should.be.an.instanceOf(ds.ObjectID);
});
it('should properly save an array of ObjectIDs', async () => {
await Article.create({
xid: objectIDLikeString,
xidArr: [objectIDLikeString],
title: 'arrayOfObjectID',
});
const found = await Article.findOne({where: {title: 'arrayOfObjectID'}});
found.xidArr.should.be.an.Array().which.containDeep([new ds.ObjectID(objectIDLikeString)]);
});
});
});