Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(client): Regression test for gh-13766 #15399

Merged
merged 14 commits into from Sep 26, 2022
Merged
15 changes: 15 additions & 0 deletions packages/client/tests/functional/issues/13766/_matrix.ts
@@ -0,0 +1,15 @@
import { defineMatrix } from '../../_utils/defineMatrix'

export default defineMatrix(() => [
[
{
provider: 'postgresql',
},
{
provider: 'mysql',
},
{
provider: 'cockroachdb',
},
],
])
37 changes: 37 additions & 0 deletions packages/client/tests/functional/issues/13766/prisma/_schema.ts
@@ -0,0 +1,37 @@
import { idForProvider } from '../../../_utils/idForProvider'
import testMatrix from '../_matrix'

export default testMatrix.setupSchema(({ provider }) => {
return /* Prisma */ `
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}

datasource db {
provider = "${provider}"
url = env("DATABASE_URI_${provider}")
}

enum OrderStatus {
NEW
PICKED
STORED
DELIVERED
}

model Order {
orderId ${idForProvider(provider)}
paid Boolean?
statusMilestones OrderStatusHistory[]
}

model OrderStatusHistory {
orderStatusHistoryId ${idForProvider(provider)}
orderId String
status OrderStatus
createdAt DateTime @default(now())
order Order @relation(fields: [orderId], references: [orderId], onUpdate: Restrict, onDelete: Cascade)
}
`
})
88 changes: 88 additions & 0 deletions packages/client/tests/functional/issues/13766/tests.ts
@@ -0,0 +1,88 @@
import { faker } from '@faker-js/faker'
// @ts-ignore
import type { PrismaClient } from '@prisma/client'

import testMatrix from './_matrix'

declare let prisma: PrismaClient

// https://github.com/prisma/prisma/issues/13766
testMatrix.setupTestSuite(
({ provider }) => {
test('should not prevent any updates on a model when updating a field', async () => {
const orderId = faker.random.numeric().toString()

await prisma.order.create({
data: {
orderId,
paid: false,
statusMilestones: {
create: {
status: 'NEW',
},
},
},
})

const updatedOrder = await prisma.order.update({
where: {
orderId,
},
data: {
paid: true,
},
})

expect(updatedOrder).toMatchObject({ orderId, paid: true })
})

test('should prevent updates on a model if any other relation references an field', async () => {
const orderId1 = faker.random.numeric().toString()
const orderId2 = faker.random.numeric().toString()

await prisma.order.create({
data: {
orderId: orderId1,
paid: false,
statusMilestones: {
create: {
status: 'NEW',
},
},
},
})

await prisma.order.create({
data: {
orderId: orderId2,
paid: false,
statusMilestones: {
create: {
status: 'NEW',
},
},
},
})

await expect(() =>
prisma.order.update({
where: {
orderId: orderId1,
},
data: {
orderId: orderId2,
},
}),
).toThrowError()
danstarns marked this conversation as resolved.
Show resolved Hide resolved
})
},
{
optOut: {
from: ['sqlserver', 'sqlite', 'mongodb'],
reason: `
sqlserver, sqlite - dont support enum's
mongodb - Command failed (InvalidIndexSpecificationOption): The field 'unique' is not valid for an _id index specification - TODO
`,
},
},
)