From 83559986c17ef932d31e498411c4b53fa2e6f687 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Sun, 23 Mar 2025 20:10:03 -0700 Subject: [PATCH] chore: more tests about delegate models for updateMany --- .../with-delegate/enhanced-client.test.ts | 48 ++++++++++++++++--- .../tests/enhancements/with-delegate/utils.ts | 1 + 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/tests/integration/tests/enhancements/with-delegate/enhanced-client.test.ts b/tests/integration/tests/enhancements/with-delegate/enhanced-client.test.ts index 7a555e0cd..1f7a40129 100644 --- a/tests/integration/tests/enhancements/with-delegate/enhanced-client.test.ts +++ b/tests/integration/tests/enhancements/with-delegate/enhanced-client.test.ts @@ -591,24 +591,58 @@ describe('Polymorphism Test', () => { }); it('update nested updateMany', async () => { - const { db, videoWithOwner: video, user } = await setup(); + const { db } = await setup(); - // updateMany - await db.user.update({ - where: { id: user.id }, + const user = await db.user.create({ data: { + email: 'a@b.com', ratedVideos: { - create: { url: 'xyz', duration: 111, rating: 222, owner: { connect: { id: user.id } } }, + create: { id: 10, url: 'xyz', duration: 1, rating: 111 }, }, }, }); + + // create another user and video + await db.user.create({ + data: { + email: 'b@c.com', + ratedVideos: { + create: { id: 20, url: 'abc', duration: 2, rating: 222 }, + }, + }, + }); + + // updateMany with filter await expect( db.user.update({ where: { id: user.id }, - data: { ratedVideos: { updateMany: { where: { duration: 111 }, data: { rating: 333 } } } }, + data: { + ratedVideos: { updateMany: { where: { duration: 1 }, data: { rating: 333 } } }, + }, include: { ratedVideos: true }, }) - ).resolves.toMatchObject({ ratedVideos: expect.arrayContaining([expect.objectContaining({ rating: 333 })]) }); + ).resolves.toMatchObject({ + ratedVideos: expect.arrayContaining([expect.objectContaining({ rating: 333 })]), + }); + + // updateMany without filter + await expect( + db.user.update({ + where: { email: 'a@b.com' }, + data: { + ratedVideos: { updateMany: { data: { duration: 3 } } }, + }, + include: { ratedVideos: true }, + }) + ).resolves.toMatchObject({ + ratedVideos: expect.arrayContaining([expect.objectContaining({ duration: 3 })]), + }); + + // user2's video should not be updated + await expect(db.ratedVideo.findUnique({ where: { id: 20 } })).resolves.toMatchObject({ + duration: 2, + rating: 222, + }); }); it('update nested deleteOne', async () => { diff --git a/tests/integration/tests/enhancements/with-delegate/utils.ts b/tests/integration/tests/enhancements/with-delegate/utils.ts index 66f29b221..41700bd4b 100644 --- a/tests/integration/tests/enhancements/with-delegate/utils.ts +++ b/tests/integration/tests/enhancements/with-delegate/utils.ts @@ -1,6 +1,7 @@ export const POLYMORPHIC_SCHEMA = ` model User { id Int @id @default(autoincrement()) + email String? @unique level Int @default(0) assets Asset[] ratedVideos RatedVideo[] @relation('direct')