This repository has been archived by the owner on Nov 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.js
277 lines (238 loc) · 7.25 KB
/
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
import test from 'tape';
import mongoose from './index';
mongoose.connect('mongodb://localhost/mongoose_fill_test');
const userSchema = new mongoose.Schema({
_id: 'number',
name: 'string',
age: 'number',
dude: { type: 'number', ref: 'User' }
})
const accountSchema = new mongoose.Schema({
_id: 'number',
name: 'string'
})
const petSchema = new mongoose.Schema({
_id: 'number',
name: 'string',
master: { type: 'number', ref: 'User' }
})
accountSchema.fill('upper', function (add, callback) {
this.db.model('User')
callback(null, this.name.toUpperCase() + add)
}).options('_Y')
const surnames = ['Galt', 'Dow']
const Account = mongoose.model('Account', accountSchema)
const makeAccounts = (names) => {
return names.map(name => new Account({ name: name }))
}
userSchema.fill('surname', function (callback) {
this.db.model('User')
const val = surnames[this._id - 1]
callback(null, val)
}).multi(function (users, ids, callback) {
const result = ids.map(function (id) {
return { _id: id, surname: surnames[id - 1] }
})
callback(null, result)
})
userSchema.fill('accounts').value(function (callback) {
const val = [
makeAccounts(['fb', 'twitter']),
makeAccounts(['google'])
][this._id - 1]
callback(null, val)
}).default([])
userSchema.fill('actions mood', function (upperCase, prefix, callback) {
const mood = prefix + 'good'
callback(null, {
actions: ['eat', 'pray', 'kill'],
mood: (upperCase ? mood.toUpperCase() : mood)
})
}).options(false, 'super')
userSchema.fill('purchases', function (callback) {
callback(null, [{ amount: 5 }, { amount: 10 }])
})
userSchema.fill('friend', function (callback) {
const val = [
new User({ _id: 2 }),
new User({ _id: 1 })
][this._id - 1]
callback(null, val)
})
userSchema.fill('nested.dude', function (callback) {
this.db.model('User')
.findOne({_id: this.dude})
.exec(callback)
})
const User = mongoose.model('User', userSchema)
const Pet = mongoose.model('Pet', petSchema)
test('setup', async t => {
const usersData = [
{ _id: 1, name: 'Alex', age: 10 },
{ _id: 2, name: 'Jane', age: 12, dude: 1 }
];
try {
await User.remove({});
await User.create(usersData);
t.end();
} catch (err) {
t.error(err);
}
})
test('fill one property: purchases', async t => {
try {
const user = await User.findById(1).fill('purchases');
t.ok(user.name == 'Alex', 'user name is ok');
t.ok(!!user.purchases, 'user purchases present');
t.ok(user.purchases[0].amount == 5, 'first purchase amount is ok');
t.end();
} catch (err) {
t.error(err);
}
})
test('fill one property: purchases (exec callback)', async t => {
User.findById(1).fill('purchases').exec((err, user) => {
t.ok(user.name == 'Alex', 'user name is ok');
t.ok(!!user.purchases, 'user purchases present');
t.ok(user.purchases[0].amount == 5, 'first purchase amount is ok');
t.end();
});
});
test('fill multiple properties with select: purchases, actions', async t => {
try {
const user = await User.findById(1).select('name purchases actions')
t.ok(user.name == 'Alex', 'user name is ok')
//t.ok(user.accounts && user.accounts.length == 0, 'user accounts filled with default value')
t.ok(!user.age, 'user age not selected')
t.ok(user.purchases && user.purchases.length > 0, 'purchases here')
t.ok(user.actions && user.actions.length > 0, 'actions here')
t.ok(!user.mood, 'mood is not here')
t.end();
} catch (err) {
t.error(err);
}
})
test('fill on instance only one (exclusive) "mood" prop with lacking options', async t => {
t.plan(2);
try {
let user = await User.findById(1)
user = await user.fill('mood', true);
t.is(user.mood, 'SUPERGOOD', 'user mood here')
t.ok(!user.actions, 'user actions not here')
//t.ok(user.actions && user.actions.length > 0, 'user actions here')
} catch (err) {
t.error(err);
};
})
test('fill on instance: mood, actions, surname', async t => {
try {
let user = await User.findById(1);
user = await user.fill('mood actions surname');
t.is(user.mood, 'supergood', 'user mood here')
t.ok(user.actions && user.actions.length > 0, 'user actions here')
t.ok(user.surname == 'Galt', 'surname is here')
t.end()
} catch (err) {
t.error(err);
}
});
test('fill property using multi: surnames', async t => {
try {
const users = await User
.find({ name: { $exists: true } })
//.sort('accounts')
.limit(5)
.fill('accounts surname')
.fill('accounts.upper')
t.ok(users.length == 2, 'user count ok')
t.is(users[0].surname, surnames[users[0].id - 1], 'user1 surname ok ok')
t.ok(users[0].accounts, 'user1 accounts filled')
t.is(users[0].accounts.length, 2, 'user1 accounts count ok')
t.is(users[0].accounts[0].upper, 'FB_Y', 'user1 accounts count ok')
t.is(users[1].surname, surnames[users[1].id - 1], 'user2 surname ok ok')
t.end();
} catch (err) {
t.error(err);
}
});
test('fill friend nested', async t => {
try {
const users = await User
.find({ name: { $exists: true } })
.fill('friend.accounts.upper', '_X');
t.ok(users[0].friend, 'user1 friend filled')
t.ok(users[0].friend.accounts, 'user1 friend.accounts filled')
t.is(users[0].friend.accounts[0].upper, 'GOOGLE_X', 'user1 friend filled')
t.ok(users[1].friend, 'user2 friend filled')
t.end()
} catch (err) {
t.error(err);
}
})
test('fill nested dude', async t => {
try {
const users = await User
.find({ name: 'Jane' })
.fill('nested.dude');
t.ok(users[0].nested.dude, 'user2 nested dude filled')
t.is(users[0].nested.dude.name, 'Alex', 'user2 nested dude filled')
t.end()
} catch (err) {
t.error(err);
}
})
test('should not fill absent property', async t => {
try {
const users = await User
.find({ name: { $exists: true } })
.fill('absent.accounts.upper', '_X')
t.ok(!users[0].enemy, 'user1 enemy is empty')
t.end()
} catch (err) {
t.error(err);
}
})
test('should not fill absent property', async t => {
try {
const users = await User
.find({ name: { $exists: true } })
.fill('enemy.accounts.upper', '_X')
t.ok(!users[0].enemy, 'user1 enemy is empty')
t.end()
} catch (err) {
t.error(err);
}
})
test('should fill nested not filled property', async t => {
try {
const user = await User
.findOne({ name: 'Jane' })
.populate('dude')
.fill('dude.accounts.upper', '_X')
t.is(user.dude.name, 'Alex', 'user.dude name is correct')
t.is(user.dude.accounts.length, 2, 'user.dude accounts filled')
t.end()
} catch (err) {
t.error(err);
}
})
test('should fill nested on model that has no fill property', async t => {
try {
const pet = new Pet({ master: new User({ name: 'Alex', _id: 1 }) })
await pet.fill('master.accounts')
t.is(pet.master.accounts.length, 2, 'pet.master.accounts filled')
t.end()
} catch (err) {
t.error(err);
}
})
test('should fill nested on model that has no fill property (promise)', t => {
const pet = new Pet({ master: new User({ name: 'Alex', _id: 1 }) })
pet.fill('master.accounts').then((pet) => {
t.is(pet.master.accounts.length, 2, 'pet.master.accounts filled')
t.end()
}, t.error)
})
test.onFinish(() => {
process.exit()
})