|
| 1 | +import { createPostgresDb, loadSchema } from '@zenstackhq/testtools'; |
| 2 | + |
| 3 | +describe('issue 2028', () => { |
| 4 | + it('regression', async () => { |
| 5 | + const dbUrl = await createPostgresDb('issue-2028'); |
| 6 | + const { enhance, zodSchemas } = await loadSchema( |
| 7 | + ` |
| 8 | +enum FooType { |
| 9 | + Bar |
| 10 | + Baz |
| 11 | +} |
| 12 | +
|
| 13 | +model User { |
| 14 | + id String @id @default(cuid()) |
| 15 | + userFolders UserFolder[] |
| 16 | + @@allow('all', true) |
| 17 | +} |
| 18 | +
|
| 19 | +model Foo { |
| 20 | + id String @id @default(cuid()) |
| 21 | + type FooType |
| 22 | +
|
| 23 | + userFolders UserFolder[] |
| 24 | +
|
| 25 | + @@delegate(type) |
| 26 | + @@allow('all', true) |
| 27 | +} |
| 28 | +
|
| 29 | +model Bar extends Foo { |
| 30 | + name String |
| 31 | +} |
| 32 | +
|
| 33 | +model Baz extends Foo { |
| 34 | + age Int |
| 35 | +} |
| 36 | +
|
| 37 | +model UserFolder { |
| 38 | + id String @id @default(cuid()) |
| 39 | + userId String |
| 40 | + fooId String |
| 41 | +
|
| 42 | + user User @relation(fields: [userId], references: [id]) |
| 43 | + foo Foo @relation(fields: [fooId], references: [id]) |
| 44 | +
|
| 45 | + @@unique([userId, fooId]) |
| 46 | + @@allow('all', true) |
| 47 | +} |
| 48 | + `, |
| 49 | + { |
| 50 | + fullZod: true, |
| 51 | + provider: 'postgresql', |
| 52 | + dbUrl, |
| 53 | + } |
| 54 | + ); |
| 55 | + // Ensure Zod Schemas don't include the delegate fields |
| 56 | + expect( |
| 57 | + zodSchemas.objects.UserFolderWhereUniqueInputObjectSchema.safeParse({ |
| 58 | + userId_delegate_aux_UserFolder_fooId_Bar: { |
| 59 | + userId: '1', |
| 60 | + fooId: '2', |
| 61 | + }, |
| 62 | + }).success |
| 63 | + ).toBeFalsy(); |
| 64 | + |
| 65 | + expect( |
| 66 | + zodSchemas.objects.UserFolderWhereUniqueInputObjectSchema.safeParse({ |
| 67 | + userId_delegate_aux_UserFolder_fooId_Baz: { |
| 68 | + userId: '1', |
| 69 | + fooId: '2', |
| 70 | + }, |
| 71 | + }).success |
| 72 | + ).toBeFalsy(); |
| 73 | + |
| 74 | + // Ensure we can query by the CompoundUniqueInput |
| 75 | + const db = enhance(); |
| 76 | + const user = await db.user.create({ data: {} }); |
| 77 | + const bar = await db.bar.create({ data: { name: 'bar' } }); |
| 78 | + const baz = await db.baz.create({ data: { age: 1 } }); |
| 79 | + |
| 80 | + const userFolderA = await db.userFolder.create({ |
| 81 | + data: { |
| 82 | + userId: user.id, |
| 83 | + fooId: bar.id, |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + const userFolderB = await db.userFolder.create({ |
| 88 | + data: { |
| 89 | + userId: user.id, |
| 90 | + fooId: baz.id, |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + await expect( |
| 95 | + db.userFolder.findUnique({ |
| 96 | + where: { |
| 97 | + userId_fooId: { |
| 98 | + userId: user.id, |
| 99 | + fooId: bar.id, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }) |
| 103 | + ).resolves.toMatchObject(userFolderA); |
| 104 | + |
| 105 | + await expect( |
| 106 | + db.userFolder.findUnique({ |
| 107 | + where: { |
| 108 | + userId_fooId: { |
| 109 | + userId: user.id, |
| 110 | + fooId: baz.id, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }) |
| 114 | + ).resolves.toMatchObject(userFolderB); |
| 115 | + }); |
| 116 | +}); |
0 commit comments