Skip to content

Commit

Permalink
Fix test schema
Browse files Browse the repository at this point in the history
  • Loading branch information
hokaccha committed Mar 24, 2024
1 parent d5dd837 commit eee1e3f
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 60 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export const prisma = new PrismaClient();
// ./src/user.ts
import { prisma } from "./client";

export function createUser(email: string) {
return prisma.user.create({ ... });
export function createUser(data: { name: string }) {
return prisma.user.create({ data });
}
```

Expand Down Expand Up @@ -119,7 +119,7 @@ describe("user", () => {
it("should create a new user", async () => {
// this record will delete by prisma-cleaner in afterEach defined by setup.ts
const created = await createUser("xxx");
expect(created.email).toEqual("xxx");
expect(created.name).toEqual("xxx");
expect(await prisma.user.count()).toEqual(1);
});

Expand All @@ -135,7 +135,7 @@ describe("user", () => {
Prisma-cleaner adds tables targeted for deletion triggered by the execution of Prisma's `create`, `createMany`, `upsert`. If added via `$executeRaw` or similar, they will not be automatically deleted, so you will need to manually delete them.

```typescript
await prisma.$executeRaw`INSERT INTO "User" (email) VALUES ('xxx')`;
await prisma.$executeRaw`INSERT INTO "User" (name) VALUES ('xxx')`;

// You should delete manually.
await cleaner.cleanupTables(["User"]);
Expand Down
2 changes: 1 addition & 1 deletion example/with-fabbrica/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "with-jest-example",
"name": "with-fabbrica-example",
"private": true,
"scripts": {
"generate": "prisma generate",
Expand Down
16 changes: 7 additions & 9 deletions example/with-fabbrica/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ generator fabbrica {
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
id Int @id @default(autoincrement())
name String
posts Post[]
}

model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
id Int @id @default(autoincrement())
title String
content String
User User? @relation(fields: [userId], references: [id])
userId Int?
}
5 changes: 2 additions & 3 deletions example/with-fabbrica/src/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ describe("user", () => {
it("should create a new user", async () => {
// this record will delete by prisma-cleaner in afterEach defined by setup.ts
const created = await createUser("xxx");
expect(created.email).toEqual("xxx");
expect(created.name).toEqual("xxx");

const user = await UserFactory.create();
expect(user).toBeDefined();
await UserFactory.create();

expect(await prisma.user.count()).toEqual(2);
});
Expand Down
6 changes: 2 additions & 4 deletions example/with-fabbrica/src/user.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { prisma } from "./client";

export function createUser(email: string) {
export function createUser(name: string) {
return prisma.user.create({
data: {
email,
},
data: { name },
});
}

Expand Down
16 changes: 7 additions & 9 deletions example/with-jest/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ datasource db {
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
id Int @id @default(autoincrement())
name String
posts Post[]
}

model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
id Int @id @default(autoincrement())
title String
content String
User User? @relation(fields: [userId], references: [id])
userId Int?
}
2 changes: 1 addition & 1 deletion example/with-jest/src/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe("user", () => {
it("should create a new user", async () => {
// this record will delete by prisma-cleaner in afterEach defined by setup.ts
const created = await createUser("xxx");
expect(created.email).toEqual("xxx");
expect(created.name).toEqual("xxx");
expect(await prisma.user.count()).toEqual(1);
});

Expand Down
6 changes: 2 additions & 4 deletions example/with-jest/src/user.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { prisma } from "./client";

export function createUser(email: string) {
export function createUser(name: string) {
return prisma.user.create({
data: {
email,
},
data: { name },
});
}

Expand Down
16 changes: 7 additions & 9 deletions example/with-vitest/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ datasource db {
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
id Int @id @default(autoincrement())
name String
posts Post[]
}

model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
id Int @id @default(autoincrement())
title String
content String
User User? @relation(fields: [userId], references: [id])
userId Int?
}
2 changes: 1 addition & 1 deletion example/with-vitest/src/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("user", () => {
it("should create a new user", async () => {
// this record will delete by prisma-cleaner in afterEach defined by setup.ts
const created = await createUser("xxx");
expect(created.email).toEqual("xxx");
expect(created.name).toEqual("xxx");
expect(await prisma.user.count()).toEqual(1);
});

Expand Down
6 changes: 2 additions & 4 deletions example/with-vitest/src/user.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { prisma } from "./client";

export function createUser(email: string) {
export function createUser(name: string) {
return prisma.user.create({
data: {
email,
},
data: { name },
});
}

Expand Down
16 changes: 7 additions & 9 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ datasource db {
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
id Int @id @default(autoincrement())
name String
posts Post[]
}

model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
id Int @id @default(autoincrement())
title String
content String
User User? @relation(fields: [userId], references: [id])
userId Int?
}
4 changes: 2 additions & 2 deletions test/cleaner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("PrismaCleaner", () => {
test("cleanup user table", async () => {
await prisma.user.create({
data: {
email: "xxx",
name: "xxx",
},
});
expect(await prisma.user.count()).toBe(1);
Expand All @@ -27,7 +27,7 @@ describe("PrismaCleaner", () => {

test("manually cleanup", async () => {
const insert = () =>
prisma.$executeRaw`insert into "User" (email) values ('xxx')`;
prisma.$executeRaw`insert into "User" (name) values ('xxx')`;

await insert();
expect(await prisma.user.count()).toBe(1);
Expand Down

0 comments on commit eee1e3f

Please sign in to comment.