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
|
import { test as bunTest, expect, describe } from "bun:test";
import { generateClient } from "./helper.ts";
import type { PrismaClient } from "./prisma/types.d.ts";
function* TestIDGenerator() {
let i = 0;
while (true) {
yield i++;
}
}
const test_id = TestIDGenerator();
["sqlite" /*"postgres", "mongodb"*/].forEach(async type => {
let Client: typeof PrismaClient;
try {
Client = await generateClient(type);
} catch (err: any) {
console.warn(`Skipping ${type} tests, failed to generate/migrate`, err.message);
}
async function test(label: string, callback: Function, timeout: number = 5000) {
const it = Client ? bunTest : bunTest.skip;
it(
label,
async () => {
const prisma = new Client();
try {
await callback(prisma, test_id.next().value);
} finally {
await prisma.$disconnect();
}
},
timeout,
);
}
describe(`prisma ${type}`, () => {
test(
"CRUD basics",
async (prisma: PrismaClient, testId: number) => {
const user = await prisma.user.create({
data: {
testId,
name: "Test",
email: "test@oven.sh",
},
});
expect(user?.name).toBe("Test");
expect(user?.email).toBe("test@oven.sh");
expect(user?.testId).toBe(testId);
const users = await prisma.user.findMany({
where: {
testId,
name: "Test",
},
});
expect(users.length).toBe(1);
const updatedUser = await prisma.user.update({
where: {
id: user.id,
},
data: {
name: "Test2",
},
});
expect(updatedUser?.name).toBe("Test2");
const deletedUser = await prisma.user.delete({ where: { id: user.id } });
expect(deletedUser?.name).toBe("Test2");
},
20000,
);
test(
"CRUD with relations",
async (prisma: PrismaClient, testId: number) => {
const user = await prisma.user.create({
data: {
testId,
name: "Test",
email: "test@oven.sh",
posts: {
create: {
testId,
title: "Hello World",
},
},
},
});
expect(user?.name).toBe("Test");
expect(user?.email).toBe("test@oven.sh");
expect(user?.testId).toBe(testId);
const usersWithPosts = await prisma.user.findMany({
include: {
posts: true,
},
});
expect(usersWithPosts.length).toBe(1);
expect(usersWithPosts[0]?.posts?.length).toBe(1);
expect(usersWithPosts[0]?.posts[0]?.title).toBe("Hello World");
expect(async () => await prisma.user.deleteMany({ where: { testId } })).toThrow();
const deletedPosts = await prisma.post.deleteMany({ where: { testId } });
expect(deletedPosts?.count).toBe(1);
const deletedUser = await prisma.user.deleteMany({ where: { testId } });
expect(deletedUser?.count).toBe(1);
},
20000,
);
test(
"Should execute multiple commands at the same time",
async (prisma: PrismaClient, testId: number) => {
const users = await Promise.all(
new Array(10).fill(0).map((_, i) =>
prisma.user.create({
data: {
testId,
name: `Test${i}`,
email: `test${i}@oven.sh`,
},
}),
),
);
expect(users.length).toBe(10);
users.forEach((user, i) => {
expect(user?.name).toBe(`Test${i}`);
expect(user?.email).toBe(`test${i}@oven.sh`);
});
const deletedUser = await prisma.user.deleteMany({ where: { testId } });
expect(deletedUser?.count).toBe(10);
},
20000,
);
});
});
|