Skip to content

Commit

Permalink
test: tests for referential actions
Browse files Browse the repository at this point in the history
  • Loading branch information
WhyAsh5114 committed Dec 29, 2024
1 parent 47fc7cc commit b3faf0a
Show file tree
Hide file tree
Showing 8 changed files with 436 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function addIDBModelClass(file: SourceFile, model: Model, models: readonl
addCreateManyAndReturn(modelClass, model);

addDeleteMethod(modelClass, model, models);
addDeleteManyMethod(modelClass, model, models);
addDeleteManyMethod(modelClass, model);

addUpdateMethod(modelClass, model, models);
addUpdateMany(modelClass, model);
Expand Down
235 changes: 208 additions & 27 deletions packages/usage/src/prisma/prisma-idb/prisma-idb-client.ts

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/usage/src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ model Profile {
model Post {
id Int @id @default(autoincrement())
title String
author User? @relation(fields: [authorId], references: [id])
author User? @relation(fields: [authorId], references: [id], onDelete: SetNull)
authorId Int?
comments Comment[]
tags String[]
Expand All @@ -61,8 +61,8 @@ model Comment {
id String @id @default(cuid())
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
postId Int
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int
user User @relation(fields: [userId], references: [id], onDelete: SetDefault)
userId Int @default(0)
text String
}

Expand Down
10 changes: 7 additions & 3 deletions packages/usage/tests/api/modelQueryOptions/include.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createId } from "@paralleldrive/cuid2";
import { test } from "../../fixtures";
import { expectQueryToSucceed } from "../../queryRunnerHelper";

Expand Down Expand Up @@ -32,6 +33,9 @@ test("include_WithOneToManyRelation_ReturnsRelatedData", async ({ page }) => {
});

test("include_WithNestedRelationships_ReturnsAllData", async ({ page }) => {
const cuid1 = createId();
const cuid2 = createId();

await expectQueryToSucceed({
page,
model: "user",
Expand All @@ -45,8 +49,8 @@ test("include_WithNestedRelationships_ReturnsAllData", async ({ page }) => {
title: "post1",
comments: {
create: [
{ text: "1st comment", userId: 1, id: "d9lyjcbysitdi9gpjqlfbu7b" },
{ text: "2nd comment", userId: 1, id: "qjvdd5we849m0xwepeclnemv" },
{ text: "1st comment", userId: 1, id: cuid1 },
{ text: "2nd comment", userId: 1, id: cuid2 },
],
},
},
Expand All @@ -60,6 +64,6 @@ test("include_WithNestedRelationships_ReturnsAllData", async ({ page }) => {
page,
model: "user",
operation: "findMany",
query: { include: { posts: { include: { comments: true } } } },
query: { include: { posts: { include: { comments: { orderBy: { text: "asc" } } } } } },
});
});
77 changes: 77 additions & 0 deletions packages/usage/tests/schema/ReferentialActions/cascade.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { test } from "../../fixtures";
import { expectQueryToSucceed } from "../../queryRunnerHelper";

test("cascade_DeleteParentRecord_DeletesChildRecords", async ({ page }) => {
await expectQueryToSucceed({
page,
model: "user",
operation: "create",
query: { data: { name: "John", posts: { create: [{ title: "Post1" }, { title: "Post2" }] } } },
});

await expectQueryToSucceed({
page,
model: "user",
operation: "delete",
query: { where: { id: 1 } },
});

await expectQueryToSucceed({
page,
model: "post",
operation: "findMany",
query: { where: { authorId: 1 } },
});
});

test("cascade_DeleteParentRecordWithNoChildren_Success", async ({ page }) => {
await expectQueryToSucceed({
page,
model: "user",
operation: "create",
query: { data: { name: "Alice" } },
});

await expectQueryToSucceed({
page,
model: "user",
operation: "delete",
query: { where: { id: 1 } },
});

await expectQueryToSucceed({
page,
model: "user",
operation: "findMany",
});
});

test("cascade_DeleteParentRecordWithMixedChildren_DeletesOnlyRelatedChildren", async ({ page }) => {
await expectQueryToSucceed({
page,
model: "user",
operation: "create",
query: { data: { name: "John", posts: { create: [{ title: "Post1" }, { title: "Post2" }] } } },
});

await expectQueryToSucceed({
page,
model: "user",
operation: "create",
query: { data: { name: "Alice", posts: { create: [{ title: "Post3" }] } } },
});

await expectQueryToSucceed({
page,
model: "user",
operation: "delete",
query: { where: { id: 1 } },
});

await expectQueryToSucceed({
page,
model: "post",
operation: "findMany",
query: { where: { authorId: 2 } },
});
});
55 changes: 55 additions & 0 deletions packages/usage/tests/schema/ReferentialActions/restrict.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { test } from "../../fixtures";
import { expectQueryToFail, expectQueryToSucceed } from "../../queryRunnerHelper";

test("restrict_DeleteUserWithGroups_ThrowsError", async ({ page }) => {
await expectQueryToSucceed({
page,
model: "group",
operation: "create",
query: { data: { name: "Admins" } },
});

await expectQueryToSucceed({
page,
model: "user",
operation: "create",
query: { data: { name: "John Doe" } },
});

await expectQueryToSucceed({
page,
model: "userGroup",
operation: "create",
query: { data: { userId: 1, groupId: 1, joinedOn: new Date() } },
});

await expectQueryToFail({
page,
model: "user",
operation: "delete",
query: { where: { id: 1 } },
errorMessage: "Cannot delete record, other records depend on it",
});
});

test("restrict_DeleteUserWithoutComments_SuccessfullyDeletesUser", async ({ page }) => {
await expectQueryToSucceed({
page,
model: "user",
operation: "create",
query: { data: { name: "Alice Smith" } },
});

await expectQueryToSucceed({
page,
model: "user",
operation: "delete",
query: { where: { id: 1 } },
});

await expectQueryToSucceed({
page,
model: "user",
operation: "findMany",
});
});
60 changes: 60 additions & 0 deletions packages/usage/tests/schema/ReferentialActions/setDefault.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { test } from "../../fixtures";
import { expectQueryToFail, expectQueryToSucceed } from "../../queryRunnerHelper";

test("setDefault_DeleteUserWithPosts_ThrowsErrorIfNoDefaultUserIdExists", async ({ page }) => {
await expectQueryToSucceed({
page,
model: "user",
operation: "create",
query: {
data: {
name: "John Doe",
posts: { create: { title: "Post1", comments: { create: { text: "comment1", userId: 1 } } } },
},
},
});

await expectQueryToFail({
page,
model: "user",
operation: "delete",
query: { where: { id: 1 } },
errorMessage: "Related record not found",
});
});

test("setDefault_DeleteUserWithPosts_SuccessIfDefaultUserIdExists", async ({ page }) => {
await expectQueryToSucceed({
page,
model: "user",
operation: "create",
query: { data: { id: 0, name: "Anonymous" } },
});

await expectQueryToSucceed({
page,
model: "user",
operation: "create",
query: {
data: {
name: "Alice",
id: 1,
posts: { create: { title: "Post1", comments: { create: { text: "comment1", userId: 1 } } } },
},
},
});

await expectQueryToSucceed({
page,
model: "user",
operation: "delete",
query: { where: { id: 1 } },
});

await expectQueryToSucceed({
page,
model: "user",
operation: "findMany",
query: { include: { posts: { include: { comments: true } } } },
});
});
25 changes: 25 additions & 0 deletions packages/usage/tests/schema/ReferentialActions/setNull.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { test } from "../../fixtures";
import { expectQueryToSucceed } from "../../queryRunnerHelper";

test("setNull_DeleteUserWithPosts_SetsUserIdToNull", async ({ page }) => {
await expectQueryToSucceed({
page,
model: "user",
operation: "create",
query: { data: { name: "John Doe", posts: { create: { title: "Post1" } } } },
});

await expectQueryToSucceed({
page,
model: "user",
operation: "delete",
query: { where: { id: 1 } },
});

await expectQueryToSucceed({
page,
model: "post",
operation: "findMany",
query: { where: { authorId: null } },
});
});

0 comments on commit b3faf0a

Please sign in to comment.