-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
tests.ts
151 lines (137 loc) · 3.96 KB
/
tests.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
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
import { checkIfEmpty } from '../_utils/relationMode/checkIfEmpty'
import testMatrix from './_matrix'
/* eslint-disable @typescript-eslint/no-unused-vars, jest/no-identical-title */
// @ts-ignore this is just for type checks
declare let prisma: import('@prisma/client').PrismaClient
// 1:1 relation
async function createXItems({ count }) {
const prismaPromises: any = []
for (let i = 0; i < count; i++) {
// We want to start at 1
const id = (i + 1).toString()
const prismaPromise = prisma.main.create({
data: {
id,
bob: {
create: { id },
},
alice: {
create: { id },
},
},
include: {
bob: true,
alice: true,
},
})
prismaPromises.push(prismaPromise)
}
return await prisma.$transaction(prismaPromises)
}
testMatrix.setupTestSuite(
(suiteConfig, suiteMeta) => {
describe('original', () => {
beforeEach(async () => {
// The order is important here
const queries = [prisma.bob.deleteMany(), prisma.main.deleteMany(), prisma.alice.deleteMany()]
await prisma.$transaction(queries)
await checkIfEmpty('alice', 'bob', 'main')
await createXItems({
count: 2,
})
})
test('[update] main with nested delete alice should succeed', async () => {
const bobCountBefore = await prisma.bob.count()
// now, update the main instance and delete alice
await prisma.main.update({
where: { id: '1' },
data: { alice: { delete: true } },
})
const bobCountAfter = await prisma.bob.count()
// Deletion DOES NOT happen
expect(bobCountAfter).toEqual(bobCountBefore)
expect(
await prisma.main.findMany({
orderBy: { id: 'asc' },
}),
).toEqual([
{
id: '1',
// We expect SetNull to happen
aliceId: null,
},
{ id: '2', aliceId: '2' },
])
expect(
await prisma.bob.findMany({
orderBy: { id: 'asc' },
}),
).toEqual([
// We expect "1" to be here
{ id: '1', mainId: '1' },
{ id: '2', mainId: '2' },
])
expect(
await prisma.alice.findMany({
orderBy: { id: 'asc' },
}),
).toEqual([
// We expect the deletion of "1" to happen
{ id: '2' },
])
})
test('[update] main with nested disconnect alice should succeed', async () => {
const bobCountBefore = await prisma.bob.count()
// now, update the main instance and delete alice
await prisma.main.update({
where: { id: '1' },
data: { alice: { disconnect: true } },
})
const bobCountAfter = await prisma.bob.count()
// No deletion should happen
expect(bobCountAfter).toEqual(bobCountBefore)
expect(
await prisma.main.findMany({
orderBy: { id: 'asc' },
}),
).toEqual([
{
id: '1',
// We expect the disconnect to happen
aliceId: null,
},
{ id: '2', aliceId: '2' },
])
expect(
await prisma.bob.findMany({
orderBy: { id: 'asc' },
}),
).toEqual([
{
id: '1',
mainId: '1',
},
{ id: '2', mainId: '2' },
])
expect(
await prisma.alice.findMany({
orderBy: { id: 'asc' },
}),
).toEqual([
{
id: '1',
},
{ id: '2' },
])
})
})
},
// Use `optOut` to opt out from testing the default selected providers
// otherwise the suite will require all providers to be specified.
{
optOut: {
from: ['sqlite', 'mongodb', 'cockroachdb', 'sqlserver', 'mysql', 'postgresql'],
reason: 'Only testing xyz provider(s) so opting out of xxx',
},
},
)