mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
[tests] prisma tests (#3197)
This commit is contained in:
53
test/js/third_party/prisma/helper.ts
vendored
Normal file
53
test/js/third_party/prisma/helper.ts
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
import path from "path";
|
||||
import { bunExe, bunEnv } from "harness";
|
||||
const cwd = import.meta.dir;
|
||||
|
||||
export async function generateClient(type: string) {
|
||||
generate(type);
|
||||
|
||||
// This should run the first time on a fresh db
|
||||
try {
|
||||
migrate(type);
|
||||
} catch (err: any) {
|
||||
if (err.message.indexOf("Environment variable not found:") !== -1) throw err;
|
||||
}
|
||||
|
||||
return (await import(`./prisma/${type}/client`)).PrismaClient;
|
||||
}
|
||||
export function migrate(type: string) {
|
||||
const result = Bun.spawnSync(
|
||||
[
|
||||
bunExe(),
|
||||
"x",
|
||||
"prisma",
|
||||
"migrate",
|
||||
"dev",
|
||||
"--name",
|
||||
"init",
|
||||
"--schema",
|
||||
path.join(cwd, "prisma", type, "schema.prisma"),
|
||||
],
|
||||
{
|
||||
cwd,
|
||||
env: {
|
||||
...bunEnv,
|
||||
NODE_ENV: undefined,
|
||||
},
|
||||
},
|
||||
);
|
||||
if (!result.success) throw new Error(result.stderr.toString("utf8"));
|
||||
}
|
||||
|
||||
export function generate(type: string) {
|
||||
const result = Bun.spawnSync(
|
||||
[bunExe(), "prisma", "generate", "--schema", path.join(cwd, "prisma", type, "schema.prisma")],
|
||||
{
|
||||
cwd,
|
||||
env: {
|
||||
...bunEnv,
|
||||
NODE_ENV: undefined,
|
||||
},
|
||||
},
|
||||
);
|
||||
if (!result.success) throw new Error(result.stderr.toString("utf8"));
|
||||
}
|
||||
18
test/js/third_party/prisma/package.json
vendored
Normal file
18
test/js/third_party/prisma/package.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "prisma",
|
||||
"module": "index.ts",
|
||||
"type": "module",
|
||||
"devDependencies": {
|
||||
"bun-types": "^0.6.0",
|
||||
"prisma": "4.15.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@prisma/client": "4.15.0"
|
||||
},
|
||||
"scripts": {
|
||||
"postinstall": "prisma generate --schema=./prisma/schema.prisma"
|
||||
}
|
||||
}
|
||||
155
test/js/third_party/prisma/prisma.test.ts
vendored
Normal file
155
test/js/third_party/prisma/prisma.test.ts
vendored
Normal file
@@ -0,0 +1,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,
|
||||
);
|
||||
});
|
||||
});
|
||||
30
test/js/third_party/prisma/prisma/mongodb/schema.prisma
vendored
Normal file
30
test/js/third_party/prisma/prisma/mongodb/schema.prisma
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
// This is your Prisma schema file,
|
||||
// learn more about it in the docs: https://pris.ly/d/prisma-schema
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
output = "client"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "mongodb"
|
||||
url = env("PRISMA_MONGODB_DATABASE_URL")
|
||||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
testId Int
|
||||
email String
|
||||
name String?
|
||||
posts Post[]
|
||||
}
|
||||
|
||||
model Post {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
testId Int
|
||||
title String
|
||||
content String?
|
||||
published Boolean @default(false)
|
||||
author User @relation(fields: [authorId], references: [id])
|
||||
authorId String @db.ObjectId
|
||||
}
|
||||
30
test/js/third_party/prisma/prisma/postgres/schema.prisma
vendored
Normal file
30
test/js/third_party/prisma/prisma/postgres/schema.prisma
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
// This is your Prisma schema file,
|
||||
// learn more about it in the docs: https://pris.ly/d/prisma-schema
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
output = "client"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgres"
|
||||
url = env("PRISMA_POSTGRES_DATABASE_URL")
|
||||
}
|
||||
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
testId Int
|
||||
email String
|
||||
name String?
|
||||
posts Post[]
|
||||
}
|
||||
|
||||
model Post {
|
||||
id Int @id @default(autoincrement())
|
||||
testId Int
|
||||
title String
|
||||
content String?
|
||||
published Boolean @default(false)
|
||||
author User @relation(fields: [authorId], references: [id])
|
||||
authorId Int
|
||||
}
|
||||
30
test/js/third_party/prisma/prisma/sqlite/schema.prisma
vendored
Normal file
30
test/js/third_party/prisma/prisma/sqlite/schema.prisma
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
// This is your Prisma schema file,
|
||||
// learn more about it in the docs: https://pris.ly/d/prisma-schema
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
output = "client"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "sqlite"
|
||||
url = "file:./dev.db"
|
||||
}
|
||||
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
testId Int
|
||||
email String
|
||||
name String?
|
||||
posts Post[]
|
||||
}
|
||||
|
||||
model Post {
|
||||
id Int @id @default(autoincrement())
|
||||
testId Int
|
||||
title String
|
||||
content String?
|
||||
published Boolean @default(false)
|
||||
author User @relation(fields: [authorId], references: [id])
|
||||
authorId Int
|
||||
}
|
||||
3481
test/js/third_party/prisma/prisma/types.d.ts
vendored
Normal file
3481
test/js/third_party/prisma/prisma/types.d.ts
vendored
Normal file
File diff suppressed because it is too large
Load Diff
@@ -19,7 +19,9 @@
|
||||
"undici": "^5.20.0",
|
||||
"socket.io": "^4.6.1",
|
||||
"socket.io-client": "^4.6.1",
|
||||
"supertest": "^6.1.6"
|
||||
"supertest": "^6.1.6",
|
||||
"@prisma/client": "4.15.0",
|
||||
"prisma": "4.15.0"
|
||||
},
|
||||
"private": true,
|
||||
"scripts": {
|
||||
|
||||
Reference in New Issue
Block a user