-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(delegate): avoid merging into object of Decimal, Date, etc.
- Loading branch information
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { createPostgresDb, dropPostgresDb, loadSchema } from '@zenstackhq/testtools'; | ||
import Decimal from 'decimal.js'; | ||
|
||
describe('issue 1487', () => { | ||
it('regression2', async () => { | ||
const dbUrl = await createPostgresDb('issue-1487'); | ||
let prisma: any; | ||
|
||
try { | ||
const r = await loadSchema( | ||
` | ||
model LineItem { | ||
id Int @id @default(autoincrement()) | ||
price Decimal | ||
createdAt DateTime @default(now()) | ||
orderId Int | ||
order Order @relation(fields: [orderId], references: [id]) | ||
} | ||
model Order extends BaseType { | ||
total Decimal | ||
createdAt DateTime @default(now()) | ||
lineItems LineItem[] | ||
} | ||
model BaseType { | ||
id Int @id @default(autoincrement()) | ||
entityType String | ||
@@delegate(entityType) | ||
} | ||
`, | ||
{ | ||
provider: 'postgresql', | ||
dbUrl, | ||
enhancements: ['omit', 'delegate'], | ||
} | ||
); | ||
|
||
prisma = r.prisma; | ||
const db = r.enhance(); | ||
|
||
const create = await db.Order.create({ | ||
data: { | ||
total: new Decimal(100_100.99), | ||
lineItems: { create: [{ price: 90_000.66 }, { price: 20_100.33 }] }, | ||
}, | ||
}); | ||
|
||
const order = await db.Order.findFirst({ where: { id: create.id }, include: { lineItems: true } }); | ||
expect(Decimal.isDecimal(order.total)).toBe(true); | ||
expect(order.createdAt instanceof Date).toBe(true); | ||
expect(order.total.toString()).toEqual('100100.99'); | ||
order.lineItems.forEach((item: any) => { | ||
expect(Decimal.isDecimal(item.price)).toBe(true); | ||
expect(item.price.toString()).not.toEqual('[object Object]'); | ||
}); | ||
|
||
const lineItems = await db.LineItem.findMany(); | ||
lineItems.forEach((item: any) => { | ||
expect(item.createdAt instanceof Date).toBe(true); | ||
expect(Decimal.isDecimal(item.price)).toBe(true); | ||
expect(item.price.toString()).not.toEqual('[object Object]'); | ||
}); | ||
} finally { | ||
if (prisma) { | ||
await prisma.$disconnect(); | ||
} | ||
await dropPostgresDb('issue-1487'); | ||
} | ||
}); | ||
}); |