-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
test.ts
121 lines (101 loc) · 2.81 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
import { getTestClient } from '../../../../utils/getTestClient'
describe('middleware', () => {
test('basic', async () => {
const PrismaClient = await getTestClient()
const db = new PrismaClient()
const allResults: any[] = []
db.$use(async (params, next) => {
const result = await next(params)
allResults.push(result)
return result
})
await db.user.findMany()
await db.post.findMany()
expect(allResults).toEqual([[], []])
db.$disconnect()
})
test('order', async () => {
const PrismaClient = await getTestClient()
const db = new PrismaClient()
const order: number[] = []
db.$use(async (params, next) => {
order.push(1)
const result = await next(params)
order.push(4)
return result
})
db.$use(async (params, next) => {
order.push(2)
const result = await next(params)
order.push(3)
return result
})
await db.user.findMany()
await db.post.findMany()
expect(order).toEqual([1, 2, 3, 4, 1, 2, 3, 4])
db.$disconnect()
})
test('engine middleware', async () => {
const PrismaClient = await getTestClient()
const db = new PrismaClient()
const engineResults: any[] = []
db.$use('engine', async (params, next) => {
const result = await next(params)
engineResults.push(result)
return result
})
await db.user.findMany()
await db.post.findMany()
expect(engineResults.map((r) => r.data)).toEqual([
{
data: {
findManyUser: [],
},
},
{
data: {
findManyPost: [],
},
},
])
expect(typeof engineResults[0].elapsed).toEqual('number')
expect(typeof engineResults[1].elapsed).toEqual('number')
db.$disconnect()
})
test('modify params', async () => {
const PrismaClient = await getTestClient()
const db = new PrismaClient()
const user = await db.user.create({
data: {
email: 'test@test.com',
name: 'test',
},
})
db.$use(async (params, next) => {
if (params.action === 'findFirst' && params.model === 'User') {
params.args = { ...params.args, where: { name: 'test' } }
}
const result = await next(params)
return result
})
const users = await db.user.findMany()
console.warn(users)
// The name should be overwritten by the middleware
const u = await db.user.findFirst({
where: {
name: 'fake',
},
})
expect(u.id).toBe(user.id)
await db.user.deleteMany()
db.$disconnect()
})
test('count unpack', async () => {
const PrismaClient = await getTestClient()
const db = new PrismaClient()
db.$use((params, next) => next(params))
const result = await db.user.count()
expect(typeof result).toBe('number')
db.$disconnect()
})
})