Skip to content

Commit

Permalink
test: ensure the emulated referential action NoAction is invalid fo…
Browse files Browse the repository at this point in the history
…r postgres and sqlite (#15880)

* [skip ci] test: ensure NoAction is invalid for Postgres and sqlite when relationMode = "prisma"

* chore: trigger CI

* test: add Validate.test.ts, moved referential-actions tests from DbPush.test.ts to Validate.test.ts

* chore: add successful NoAction test in Validate.test.ts

* chore: add missing folder

* chore: fix ci? add env to cli-commands

* chore: add normalization to Validate.test.ts
  • Loading branch information
jkomyno committed Oct 21, 2022
1 parent f176d1d commit 0b204fa
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,10 @@ jobs:
os: [ubuntu-latest]
node: [16, 18]

env:
TEST_POSTGRES_URI: postgres://prisma:prisma@localhost:5432/tests
TEST_MYSQL_URI: mysql://root:root@localhost:3306/tests

steps:
- uses: actions/checkout@v3

Expand Down
81 changes: 81 additions & 0 deletions packages/cli/src/__tests__/commands/Validate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { jestContext, serializeQueryEngineName } from '@prisma/internals'

import { Validate } from '../../Validate'

const ctx = jestContext.new().assemble()

it('validate should throw if schema is broken', async () => {
ctx.fixture('example-project/prisma')
await expect(Validate.new().parse(['--schema=broken.prisma'])).rejects.toThrowError()
})

describe('referential actions', () => {
beforeEach(() => {
ctx.fixture('referential-actions/no-action/relationMode-prisma')
})

it('validate should reject NoAction referential action on Postgres when relationMode = "prisma"', async () => {
expect.assertions(1)

try {
await Validate.new().parse(['--schema', './prisma/postgres.prisma'])
} catch (e) {
expect(serializeQueryEngineName(e.message)).toMatchInlineSnapshot(`
Schema validation error - Error (query-engine-NORMALIZED)
Error code: P1012
error: Error validating: Invalid referential action: \`NoAction\`. Allowed values: (\`Cascade\`, \`Restrict\`, \`SetNull\`). \`NoAction\` is not implemented for Postgres when using \`relationMode = "prisma"\`, you could try using \`Restrict\` instead. Learn more at https://pris.ly/d/relationMode
--> schema.prisma:21
|
20 | id String @id @default(cuid())
21 | user SomeUser @relation(fields: [userId], references: [id], onUpdate: NoAction)
|
error: Error validating: Invalid referential action: \`NoAction\`. Allowed values: (\`Cascade\`, \`Restrict\`, \`SetNull\`). \`NoAction\` is not implemented for Postgres when using \`relationMode = "prisma"\`, you could try using \`Restrict\` instead. Learn more at https://pris.ly/d/relationMode
--> schema.prisma:28
|
27 | id String @id @default(cuid())
28 | user SomeUser @relation(fields: [userId], references: [id], onDelete: NoAction)
|
Validation Error Count: 2
[Context: getDmmf]
Prisma CLI Version : 0.0.0
`)
}
})

it('validate should reject NoAction referential action on sqlite when relationMode = "prisma"', async () => {
expect.assertions(1)

try {
await Validate.new().parse(['--schema', './prisma/postgres.prisma'])
} catch (e) {
expect(serializeQueryEngineName(e.message)).toMatchInlineSnapshot(`
Schema validation error - Error (query-engine-NORMALIZED)
Error code: P1012
error: Error validating: Invalid referential action: \`NoAction\`. Allowed values: (\`Cascade\`, \`Restrict\`, \`SetNull\`). \`NoAction\` is not implemented for Postgres when using \`relationMode = "prisma"\`, you could try using \`Restrict\` instead. Learn more at https://pris.ly/d/relationMode
--> schema.prisma:21
|
20 | id String @id @default(cuid())
21 | user SomeUser @relation(fields: [userId], references: [id], onUpdate: NoAction)
|
error: Error validating: Invalid referential action: \`NoAction\`. Allowed values: (\`Cascade\`, \`Restrict\`, \`SetNull\`). \`NoAction\` is not implemented for Postgres when using \`relationMode = "prisma"\`, you could try using \`Restrict\` instead. Learn more at https://pris.ly/d/relationMode
--> schema.prisma:28
|
27 | id String @id @default(cuid())
28 | user SomeUser @relation(fields: [userId], references: [id], onDelete: NoAction)
|
Validation Error Count: 2
[Context: getDmmf]
Prisma CLI Version : 0.0.0
`)
}
})

it('validate should accept NoAction referential action on e.g. MySQL when relationMode = "prisma"', async () => {
const result = await Validate.new().parse(['--schema', './prisma/mysql.prisma'])
expect(result).toBeTruthy()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}

datasource db {
provider = "mysql"
url = env("TEST_MYSQL_URI")
relationMode = "prisma"
}

model SomeUser {
id String @id @default(cuid())
profileUpdate ProfileUpdateNoAction?
profileDelete ProfileDeleteNoAction?
enabled Boolean?
}

model ProfileUpdateNoAction {
id String @id @default(cuid())
user SomeUser @relation(fields: [userId], references: [id], onUpdate: NoAction)
userId String @unique
enabled Boolean?
}

model ProfileDeleteNoAction {
id String @id @default(cuid())
user SomeUser @relation(fields: [userId], references: [id], onDelete: NoAction)
userId String @unique
enabled Boolean?
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}

datasource db {
provider = "postgres"
url = env("TEST_POSTGRES_URI")
relationMode = "prisma"
}

model SomeUser {
id String @id @default(cuid())
profileUpdate ProfileUpdateNoAction?
profileDelete ProfileDeleteNoAction?
enabled Boolean?
}

model ProfileUpdateNoAction {
id String @id @default(cuid())
user SomeUser @relation(fields: [userId], references: [id], onUpdate: NoAction)
userId String @unique
enabled Boolean?
}

model ProfileDeleteNoAction {
id String @id @default(cuid())
user SomeUser @relation(fields: [userId], references: [id], onDelete: NoAction)
userId String @unique
enabled Boolean?
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}

datasource db {
provider = "sqlite"
url = "file:./db.dev"
relationMode = "prisma"
}

model SomeUser {
id String @id @default(cuid())
profileNoAction ProfileNoAction?
enabled Boolean?
}

model ProfileNoAction {
id String @id @default(cuid())
user SomeUser @relation(fields: [userId], references: [id], onUpdate: NoAction, onDelete: NoAction)
userId String @unique
enabled Boolean?
}

0 comments on commit 0b204fa

Please sign in to comment.