/
has_many.spec.ts
101 lines (75 loc) 路 3.27 KB
/
has_many.spec.ts
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
import { test } from '@japa/runner'
import { DatabaseUtils } from '@julr/japa-database-plugin'
import { defineFactory } from '@julr/factorio'
import { UserFactory } from '../tests-helpers/setup.js'
import { setupDb } from '../tests-helpers/db.js'
test.group('HasMany', (group) => {
group.setup(async () => setupDb())
group.each.setup(() => DatabaseUtils.refreshDatabase())
test('Basic', async ({ database }) => {
const user = await UserFactory.with('posts').create()
await database.assertCount('user', 1)
await database.assertCount('post', 1)
await database.assertHas('post', { user_id: user.id }, 1)
})
test('With many', async ({ database }) => {
const user = await UserFactory.with('posts', 2).create()
await database.assertCount('user', 1)
await database.assertCount('post', 2)
await database.assertHas('post', { user_id: user.id }, 2)
})
test('Should returns as array', async ({ expect }) => {
const user = await UserFactory.with('posts', 2).create()
expect(user.posts).toBeInstanceOf(Array)
expect(user.posts.length).toStrictEqual(2)
})
test('Create many', async ({ expect, database }) => {
const users = await UserFactory.with('posts', 2).createMany(2)
await Promise.all([
database.assertCount('user', 2),
database.assertCount('post', 4),
database.assertHas('post', { user_id: users[0].id }, 2),
database.assertHas('post', { user_id: users[1].id }, 2),
])
expect(users[0].posts).toBeInstanceOf(Array)
expect(users[0].posts.length).toStrictEqual(2)
expect(users[1].posts).toBeInstanceOf(Array)
expect(users[1].posts.length).toStrictEqual(2)
})
test('Chaining', async ({ database }) => {
const user = await UserFactory.with('posts', 5).with('posts', 5).create()
await database.assertCount('user', 1)
await database.assertCount('post', 10)
await database.assertHas('post', { user_id: user.id }, 10)
})
test('With- state', async ({ database }) => {
const user = await UserFactory.with('posts', 10, (post) => post.apply('nodeArticle')).create()
await database.assertCount('user', 1)
await database.assertCount('post', 10)
await database.assertHas('post', { user_id: user.id, title: 'NodeJS' }, 10)
})
test('With - merge array', async ({ database }) => {
const user = await UserFactory.with('posts', 10, (post) =>
post.merge([{ title: 'Rust' }, { title: 'AdonisJS' }])
).create()
await database.assertCount('user', 1)
await database.assertCount('post', 10)
await database.assertHas('post', { user_id: user.id, title: 'Rust' }, 1)
await database.assertHas('post', { user_id: user.id, title: 'AdonisJS' }, 1)
})
test('auto detect foreign and primary keys', async ({ database }) => {
const postFactory = defineFactory<any>('post', ({ faker }) => ({
title: faker.company.bs(),
})).build()
const userFactory = defineFactory<any>('user', ({ faker }) => ({
email: faker.internet.email(),
password: faker.random.alphaNumeric(6),
}))
.hasMany('post', () => postFactory)
.build()
const user = await userFactory.with('post', 5).create()
await database.assertCount('user', 1)
await database.assertCount('post', 5)
await database.assertHas('post', { user_id: user.id }, 5)
})
})