-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
test.ts
150 lines (130 loc) · 3.09 KB
/
test.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
import { generateTestClient } from '../../../../utils/getTestClient'
import type { SetupParams } from '../../../../utils/setupPostgres'
import { setupPostgres, tearDownPostgres } from '../../../../utils/setupPostgres'
import { faker } from '@faker-js/faker'
// @ts-ignore trick to get typings at dev time
import type { Prisma, PrismaClient } from './node_modules/.prisma/client'
let prisma: PrismaClient
let PrismaUtil: typeof Prisma
const baseUri = process.env.TEST_POSTGRES_URI
const email = faker.internet.email()
const title = faker.name.jobTitle()
const newEmail = faker.internet.email()
const newTitle = faker.name.jobTitle()
describe('multi-schema', () => {
beforeAll(async () => {
process.env.TEST_POSTGRES_URI += '-multi-schema'
await tearDownPostgres(process.env.TEST_POSTGRES_URI!)
const SetupParams: SetupParams = {
connectionString: process.env.TEST_POSTGRES_URI!,
dirname: __dirname,
}
await setupPostgres(SetupParams).catch((e) => console.error(e))
await generateTestClient()
const { PrismaClient, Prisma } = require('./node_modules/.prisma/client')
prisma = new PrismaClient()
PrismaUtil = Prisma
})
afterAll(async () => {
await prisma.post.deleteMany()
await prisma.user.deleteMany()
await prisma.$disconnect()
process.env.TEST_POSTGRES_URI = baseUri
})
test('create', async () => {
const created = await prisma.user.create({
data: {
email,
posts: {
create: [{ title }],
},
},
select: {
email: true,
posts: true,
},
})
expect(created).toMatchObject({
email,
posts: [{ title }],
})
})
test('read', async () => {
const [read] = await prisma.user.findMany({
where: {
email,
posts: {
some: {
title,
},
},
},
select: {
email: true,
posts: true,
},
})
expect(read).toMatchObject({
email,
posts: [{ title }],
})
})
test('update', async () => {
await prisma.post.updateMany({
where: {
title,
},
data: { title: newTitle },
})
await prisma.user.updateMany({
where: {
email,
},
data: { email: newEmail },
})
const [read] = await prisma.user.findMany({
where: {
email: newEmail,
posts: {
some: {
title: newTitle,
},
},
},
select: {
email: true,
posts: true,
},
})
expect(read).toMatchObject({
email: newEmail,
posts: [{ title: newTitle }],
})
})
test('delete', async () => {
await prisma.post.deleteMany({
where: {
title: newTitle,
},
})
await prisma.user.deleteMany({
where: {
email: newEmail,
},
})
expect(
await prisma.post.findMany({
where: {
title: newTitle,
},
}),
).toHaveLength(0)
expect(
await prisma.user.findMany({
where: {
email: newEmail,
},
}),
).toHaveLength(0)
})
})