-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
factory.spec.js
389 lines (364 loc) · 12.1 KB
/
factory.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
'use strict'
/**
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/* global describe, it, after, before */
const chai = require('chai')
const expect = chai.expect
const Model = require('../../src/Lucid/Model')
const Database = require('../../src/Database')
const Factory = require('../../src/Factory')
const filesFixtures = require('./fixtures/files')
const modelFixtures = require('./fixtures/model')
const ModelFactory = require('../../src/Factory/ModelFactory')
const config = require('./helpers/config')
const _ = require('lodash')
const Ioc = require('adonis-fold').Ioc
require('co-mocha')
describe('Factory', function () {
before(function * () {
Database._setConfigProvider(config)
yield filesFixtures.createDir()
yield modelFixtures.up(Database)
Ioc.bind('Adonis/Src/Database', function () {
return Database
})
Ioc.bind('Adonis/Src/Helpers', function () {
return {
makeNameSpace: function (hook) {
return `App/${hook}`
}
}
})
Factory.clear()
})
after(function * () {
yield modelFixtures.down(Database)
Database.close()
})
it('should throw an error when blueprint callback is not a function', function () {
const fn = function () {
Factory.blueprint('App/Model/User', 'foo')
}
expect(fn).to.throw('InvalidArgumentException: E_INVALID_PARAMETER: Factory blueprint expects callback to be a function')
})
it('should throw an error when unable to resolve model factory', function () {
const fn = function () {
Factory.model('App/Model/User')
}
expect(fn).to.throw('RuntimeException: E_MISSING_MODEL_FACTORY: Cannot find model factory for App/Model/User')
})
it('should throw an error when unable to resolve database factory', function () {
const fn = function () {
Factory.get('users')
}
expect(fn).to.throw('RuntimeException: E_MISSING_DATABASE_FACTORY: Cannot find database factory for users')
})
it('should be able to define a factory blueprint', function () {
Factory.blueprint('App/Model/User', function (fake) {
return {
username: fake.username(),
email: fake.email()
}
})
const blueprints = Factory.blueprints()
expect(blueprints).to.be.an('object')
expect(blueprints['App/Model/User']).to.be.a('function')
})
it('should return instance of model factory when using model method', function () {
Ioc.bind('App/Model/User', function () {
return {}
})
Factory.blueprint('App/Model/User', function (fake) {
return {
username: fake.username(),
email: fake.email()
}
})
const userModelFactory = Factory.model('App/Model/User')
expect(userModelFactory instanceof ModelFactory).to.equal(true)
})
it('should return the model instance from ModelFactory make method', function () {
class User {
constructor (values) {
this.attributes = values
}
}
Ioc.bind('App/Model/User', function () {
return User
})
Factory.blueprint('App/Model/User', function (fake) {
return {
username: fake.username(),
email: fake.email()
}
})
const user = Factory.model('App/Model/User').make()
expect(user instanceof User).to.equal(true)
expect(user.attributes.username).to.be.a('string')
expect(user.attributes.email).to.be.a('string')
})
it('should return an array of model instances from ModelFactory make method is called with a count', function () {
class User {
constructor (values) {
this.attributes = values
}
}
Ioc.bind('App/Model/User', function () {
return User
})
Factory.blueprint('App/Model/User', function (fake) {
return {
username: fake.username(),
email: fake.email()
}
})
const users = Factory.model('App/Model/User').make(4)
expect(users).is.an('array')
expect(users.length).to.equal(4)
users.forEach(function (user) {
expect(user instanceof User).to.equal(true)
expect(user.attributes.username).to.be.a('string')
expect(user.attributes.email).to.be.a('string')
})
})
it('should return the model instance from ModelFactory make method is asked to return 1 instance', function () {
class User {
constructor (values) {
this.attributes = values
}
}
Ioc.bind('App/Model/User', function () {
return User
})
Factory.blueprint('App/Model/User', function (fake) {
return {
username: fake.username(),
email: fake.email()
}
})
const user = Factory.model('App/Model/User').make(1)
expect(user instanceof User).to.equal(true)
expect(user.attributes.username).to.be.a('string')
expect(user.attributes.email).to.be.a('string')
})
it('should call user model create method to create given rows inside the database', function * () {
class User {
constructor (values) {
this.attributes = values
}
static * create (values) {
const instance = new this(values)
return instance
}
}
Ioc.bind('App/Model/User', function () {
return User
})
Factory.blueprint('App/Model/User', function (fake) {
return {
username: fake.username(),
email: fake.email()
}
})
const userModelFactory = yield Factory.model('App/Model/User').create(10)
expect(userModelFactory.instances.length).to.equal(10)
userModelFactory.instances.forEach(function (user) {
expect(user instanceof User).to.equal(true)
})
})
it('should be able to loop through each created instance and call generator methods inside it', function * () {
class User {
constructor (values) {
this.attributes = values
}
static * create (values) {
const instance = new this(values)
return instance
}
}
Ioc.bind('App/Model/User', function () {
return User
})
Factory.blueprint('App/Model/User', function (fake) {
return {
username: fake.username(),
email: fake.email()
}
})
const userModelFactory = yield Factory.model('App/Model/User').create(10)
userModelFactory.each(function * (user) {
user.attributes.touched = true
})
userModelFactory.instances.forEach(function (user) {
expect(user.attributes.touched).to.equal(true)
})
})
it('should create rows inside associated table for a given model', function * () {
class User extends Model {}
Ioc.bind('App/Model/User', function () {
return User
})
Factory.blueprint('App/Model/User', function (fake) {
return {
username: fake.username(),
firstname: fake.first()
}
})
yield Factory.model('App/Model/User').create(10)
const users = yield User.all()
expect(users.size()).to.equal(10)
yield modelFixtures.truncate(Database)
})
it('should truncate rows inside associated table for a given model', function * () {
class User extends Model {}
Ioc.bind('App/Model/User', function () {
return User
})
Factory.blueprint('App/Model/User', function (fake) {
return {
username: fake.username(),
firstname: fake.first()
}
})
const userModelFactory = yield Factory.model('App/Model/User').create(10)
const users = yield User.all()
expect(users.size()).to.equal(10)
yield userModelFactory.reset()
const afterReset = yield User.all()
expect(afterReset.size()).to.equal(0)
})
it('should be able to create records using the database factory', function * () {
Factory.blueprint('users', function (fake) {
return {
username: fake.username(),
firstname: fake.first()
}
})
const ids = yield Factory.get('users').create(10)
expect(ids).to.be.an('array')
expect(ids.length).to.equal(10)
yield modelFixtures.truncate(Database)
})
it('should be able to define different table name when using database factory', function * () {
Factory.blueprint('forUsers', function (fake) {
return {
username: fake.username(),
firstname: fake.first()
}
})
const ids = yield Factory.get('forUsers').table('users').create(10)
expect(ids).to.be.an('array')
expect(ids.length).to.equal(10)
yield modelFixtures.truncate(Database)
})
it('should be able to define different returning field when using database factory', function * () {
Factory.blueprint('forUsers', function (fake) {
return {
username: fake.username(),
firstname: fake.first()
}
})
const dbFactory = Factory.get('forUsers').table('users').returning('username')
yield dbFactory.create(10)
expect(dbFactory.returningField).to.equal('username')
yield modelFixtures.truncate(Database)
})
it('should be able to truncate the database table using the reset method', function * () {
Factory.blueprint('users', function (fake) {
return {
username: fake.username(),
firstname: fake.first()
}
})
yield Factory.get('users').create(10)
yield Factory.get('users').reset()
const ids = yield Database.table('users').pluck('id')
expect(ids).to.be.an('array')
expect(ids.length).to.equal(0)
})
it('should get the iteration count when making multiple instances', function * () {
class User extends Model {}
Ioc.bind('App/Model/User', function () {
return User
})
Factory.blueprint('App/Model/User', function (fake, i) {
return {
username: fake.username(),
firstname: fake.first(),
custom_id: i
}
})
const users = Factory.model('App/Model/User').make(5)
expect(_.map(users, 'custom_id')).deep.equal([1, 2, 3, 4, 5])
})
it('should get the iteration count when creating multiple instances', function * () {
class User extends Model {}
Ioc.bind('App/Model/User', function () {
return User
})
Factory.blueprint('App/Model/User', function (fake, i) {
return {
username: i,
firstname: fake.first()
}
})
yield Factory.model('App/Model/User').create(5)
const users = yield User.all()
expect(users.size()).to.equal(5)
expect(users.map('username').value()).deep.equal(['1', '2', '3', '4', '5'])
yield modelFixtures.truncate(Database)
})
it('should able to pass custom data to the factory blueprint via make method', function * () {
class User extends Model {}
Ioc.bind('App/Model/User', function () {
return User
})
Factory.blueprint('App/Model/User', function (fake, i, user) {
return {
username: user.username,
firstname: user.firstname
}
})
const user = Factory.model('App/Model/User').make(1, {username: 'foo', firstname: 'bar'})
expect(user.username).to.equal('foo')
expect(user.firstname).to.equal('bar')
})
it('should able to pass custom data to the factory blueprint via create method', function * () {
class User extends Model {}
Ioc.bind('App/Model/User', function () {
return User
})
Factory.blueprint('App/Model/User', function (fake, i, user) {
return {
username: user.username,
firstname: user.firstname
}
})
yield Factory.model('App/Model/User').create(1, {username: 'foo', firstname: 'bar'})
const users = yield User.all()
expect(users.size()).to.equal(1)
expect(users.first().username).to.equal('foo')
expect(users.first().firstname).to.equal('bar')
yield modelFixtures.truncate(Database)
})
it('should be able to pass custom values when using database factory', function * () {
Factory.blueprint('forUsers', function (fake, i, user) {
return {
username: user.username,
firstname: user.firstname
}
})
const dbFactory = Factory.get('forUsers').table('users')
yield dbFactory.create(1, {username: 'foo', firstname: 'bar'})
const users = yield Database.table('users')
expect(users[0].username).to.equal('foo')
expect(users[0].firstname).to.equal('bar')
yield modelFixtures.truncate(Database)
})
})