|
| 1 | +import { createPolicyTestClient } from '@zenstackhq/testtools'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +// https://github.com/zenstackhq/zenstack/issues/2351 |
| 5 | +describe('Regression for issue 2351', () => { |
| 6 | + it('should correctly query delegate model that inherits from a model using a mixin abstract type', async () => { |
| 7 | + const db = await createPolicyTestClient( |
| 8 | + ` |
| 9 | +type BaseEntity { |
| 10 | + id String @id @default(cuid()) |
| 11 | + createdOn DateTime @default(now()) |
| 12 | + updatedOn DateTime @updatedAt |
| 13 | + isDeleted Boolean @default(false) |
| 14 | + isArchived Boolean @default(false) |
| 15 | +} |
| 16 | +
|
| 17 | +enum DataType { |
| 18 | + DataText |
| 19 | + DataNumber |
| 20 | +} |
| 21 | +
|
| 22 | +model RoutineData with BaseEntity { |
| 23 | + dataType DataType |
| 24 | + routineId String |
| 25 | + Routine Routine @relation(fields: [routineId], references: [id]) |
| 26 | + @@delegate(dataType) |
| 27 | + @@allow('all', auth().id == Routine.userId) |
| 28 | +} |
| 29 | +
|
| 30 | +model Routine { |
| 31 | + id String @id @default(cuid()) |
| 32 | + userId String |
| 33 | + User User @relation(fields: [userId], references: [id]) |
| 34 | + data RoutineData[] |
| 35 | + @@allow('all', true) |
| 36 | +} |
| 37 | +
|
| 38 | +model User { |
| 39 | + id String @id @default(cuid()) |
| 40 | + name String |
| 41 | + routines Routine[] |
| 42 | + @@allow('all', true) |
| 43 | +} |
| 44 | +
|
| 45 | +model DataText extends RoutineData { |
| 46 | + textValue String |
| 47 | +} |
| 48 | + `, |
| 49 | + { usePrismaPush: true }, |
| 50 | + ); |
| 51 | + |
| 52 | + const user = await db.user.create({ |
| 53 | + data: { |
| 54 | + name: 'Test User', |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + const routine = await db.routine.create({ |
| 59 | + data: { |
| 60 | + userId: user.id, |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + const authDb = db.$setAuth({ id: user.id }); |
| 65 | + const created = await authDb.dataText.create({ |
| 66 | + data: { textValue: 'hello', routineId: routine.id }, |
| 67 | + }); |
| 68 | + expect(created.textValue).toBe('hello'); |
| 69 | + expect(created.isDeleted).toBe(false); |
| 70 | + expect(created.isArchived).toBe(false); |
| 71 | + |
| 72 | + const found = await authDb.dataText.findUnique({ |
| 73 | + where: { id: created.id }, |
| 74 | + }); |
| 75 | + expect(found).not.toBeNull(); |
| 76 | + expect(found!.textValue).toBe('hello'); |
| 77 | + expect(found!.createdOn).toBeDefined(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments