|
| 1 | +import { createPolicyTestClient } from '@zenstackhq/testtools'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +describe('Regression for issue #2424', () => { |
| 5 | + it('deep nested include with PolicyPlugin works with non-compact alias mode', async () => { |
| 6 | + const db = await createPolicyTestClient( |
| 7 | + ` |
| 8 | +model Store { |
| 9 | + id String @id |
| 10 | + customerOrders CustomerOrder[] |
| 11 | + productCatalogItems ProductCatalogItem[] |
| 12 | + @@allow('all', true) |
| 13 | +} |
| 14 | +
|
| 15 | +model CustomerOrder { |
| 16 | + id String @id |
| 17 | + storeId String |
| 18 | + store Store @relation(fields: [storeId], references: [id], onDelete: Cascade) |
| 19 | + customerOrderPaymentSummary CustomerOrderPaymentSummary[] |
| 20 | + @@allow('all', true) |
| 21 | +} |
| 22 | +
|
| 23 | +model CustomerOrderPaymentSummary { |
| 24 | + id String @id |
| 25 | + customerOrderId String |
| 26 | + customerOrder CustomerOrder @relation(fields: [customerOrderId], references: [id], onDelete: Cascade) |
| 27 | + customerOrderPaymentSummaryLine CustomerOrderPaymentSummaryLine[] |
| 28 | + @@allow('all', true) |
| 29 | +} |
| 30 | +
|
| 31 | +model PaymentTransaction { |
| 32 | + id String @id |
| 33 | + customerOrderPaymentSummaryLine CustomerOrderPaymentSummaryLine[] |
| 34 | + paymentTransactionLineItem PaymentTransactionLineItem[] |
| 35 | + @@allow('all', true) |
| 36 | +} |
| 37 | +
|
| 38 | +model CustomerOrderPaymentSummaryLine { |
| 39 | + customerOrderPaymentSummaryId String |
| 40 | + lineIndex Int |
| 41 | + paymentTransactionId String |
| 42 | + customerOrderPaymentSummary CustomerOrderPaymentSummary @relation(fields: [customerOrderPaymentSummaryId], references: [id], onDelete: Cascade) |
| 43 | + paymentTransaction PaymentTransaction @relation(fields: [paymentTransactionId], references: [id], onDelete: Cascade) |
| 44 | + @@id([customerOrderPaymentSummaryId, lineIndex]) |
| 45 | + @@allow('all', true) |
| 46 | +} |
| 47 | +
|
| 48 | +model ProductCatalogItem { |
| 49 | + storeId String |
| 50 | + sku String |
| 51 | + store Store @relation(fields: [storeId], references: [id], onDelete: Cascade) |
| 52 | + paymentTransactionLineItem PaymentTransactionLineItem[] |
| 53 | + @@id([storeId, sku]) |
| 54 | + @@allow('all', true) |
| 55 | +} |
| 56 | +
|
| 57 | +model InventoryReservation { |
| 58 | + id String @id |
| 59 | + paymentTransactionLineItem PaymentTransactionLineItem[] |
| 60 | + @@allow('all', true) |
| 61 | +} |
| 62 | +
|
| 63 | +model PaymentTransactionLineItem { |
| 64 | + paymentTransactionId String |
| 65 | + lineNumber Int |
| 66 | + storeId String |
| 67 | + productSku String |
| 68 | + inventoryReservationId String? |
| 69 | + paymentTransaction PaymentTransaction @relation(fields: [paymentTransactionId], references: [id], onDelete: Cascade) |
| 70 | + productCatalogItem ProductCatalogItem @relation(fields: [storeId, productSku], references: [storeId, sku]) |
| 71 | + inventoryReservation InventoryReservation? @relation(fields: [inventoryReservationId], references: [id], onDelete: SetNull) |
| 72 | + @@id([paymentTransactionId, lineNumber]) |
| 73 | + @@allow('all', true) |
| 74 | +} |
| 75 | + `, |
| 76 | + { provider: 'postgresql', useCompactAliasNames: false }, |
| 77 | + ); |
| 78 | + |
| 79 | + const rawDb = db.$unuseAll(); |
| 80 | + |
| 81 | + await rawDb.store.create({ data: { id: 'store_1' } }); |
| 82 | + await rawDb.customerOrder.create({ data: { id: 'order_1', storeId: 'store_1' } }); |
| 83 | + await rawDb.customerOrderPaymentSummary.create({ data: { id: 'summary_1', customerOrderId: 'order_1' } }); |
| 84 | + await rawDb.paymentTransaction.create({ data: { id: 'payment_1' } }); |
| 85 | + await rawDb.customerOrderPaymentSummaryLine.create({ |
| 86 | + data: { |
| 87 | + customerOrderPaymentSummaryId: 'summary_1', |
| 88 | + lineIndex: 0, |
| 89 | + paymentTransactionId: 'payment_1', |
| 90 | + }, |
| 91 | + }); |
| 92 | + await rawDb.productCatalogItem.create({ data: { storeId: 'store_1', sku: 'sku_1' } }); |
| 93 | + await rawDb.inventoryReservation.create({ data: { id: 'reservation_1' } }); |
| 94 | + await rawDb.paymentTransactionLineItem.create({ |
| 95 | + data: { |
| 96 | + paymentTransactionId: 'payment_1', |
| 97 | + lineNumber: 0, |
| 98 | + storeId: 'store_1', |
| 99 | + productSku: 'sku_1', |
| 100 | + inventoryReservationId: 'reservation_1', |
| 101 | + }, |
| 102 | + }); |
| 103 | + |
| 104 | + const result = await db.customerOrderPaymentSummary.findUnique({ |
| 105 | + where: { id: 'summary_1' }, |
| 106 | + include: { |
| 107 | + customerOrder: true, |
| 108 | + customerOrderPaymentSummaryLine: { |
| 109 | + include: { |
| 110 | + paymentTransaction: { |
| 111 | + include: { |
| 112 | + paymentTransactionLineItem: { |
| 113 | + include: { |
| 114 | + productCatalogItem: true, |
| 115 | + inventoryReservation: true, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }); |
| 124 | + |
| 125 | + expect(result).toMatchObject({ |
| 126 | + id: 'summary_1', |
| 127 | + customerOrder: { |
| 128 | + id: 'order_1', |
| 129 | + storeId: 'store_1', |
| 130 | + }, |
| 131 | + customerOrderPaymentSummaryLine: [ |
| 132 | + { |
| 133 | + customerOrderPaymentSummaryId: 'summary_1', |
| 134 | + lineIndex: 0, |
| 135 | + paymentTransactionId: 'payment_1', |
| 136 | + paymentTransaction: { |
| 137 | + id: 'payment_1', |
| 138 | + paymentTransactionLineItem: [ |
| 139 | + { |
| 140 | + paymentTransactionId: 'payment_1', |
| 141 | + lineNumber: 0, |
| 142 | + storeId: 'store_1', |
| 143 | + productSku: 'sku_1', |
| 144 | + inventoryReservationId: 'reservation_1', |
| 145 | + productCatalogItem: { |
| 146 | + storeId: 'store_1', |
| 147 | + sku: 'sku_1', |
| 148 | + }, |
| 149 | + inventoryReservation: { |
| 150 | + id: 'reservation_1', |
| 151 | + }, |
| 152 | + }, |
| 153 | + ], |
| 154 | + }, |
| 155 | + }, |
| 156 | + ], |
| 157 | + }); |
| 158 | + |
| 159 | + await db.$disconnect(); |
| 160 | + }); |
| 161 | +}); |
0 commit comments