Skip to content

Commit

Permalink
test: add coverage for gh-12862
Browse files Browse the repository at this point in the history
  • Loading branch information
danstarns committed Sep 29, 2022
1 parent 2f1a984 commit 4dd3827
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/client/tests/functional/issues/12862/_matrix.ts
@@ -0,0 +1,4 @@
import { defineMatrix } from '../../_utils/defineMatrix'
import { allProviders } from '../../_utils/providers'

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

export default testMatrix.setupSchema(({ provider }) => {
return /* Prisma */ `
generator client {
provider = "prisma-client-js"
previewFeatures = ["interactiveTransactions"]
engineType = "binary"
}
datasource db {
provider = "${provider}"
url = env("DATABASE_URI_${provider}")
}
model User {
id ${idForProvider(provider)}
email String @unique
name String?
posts Post[]
}
model Post {
id ${idForProvider(provider)}
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
content String?
published Boolean @default(false)
viewCount Int @default(0)
author User? @relation(fields: [authorId], references: [id])
authorId String?
}
`
})
67 changes: 67 additions & 0 deletions packages/client/tests/functional/issues/12862/tests.ts
@@ -0,0 +1,67 @@
import { faker } from '@faker-js/faker'
import { ClientEngineType, getClientEngineType } from '@prisma/internals'

import testMatrix from './_matrix'
// @ts-ignore
import type { PrismaClient } from './node_modules/@prisma/client'

declare let prisma: PrismaClient

// https://github.com/prisma/prisma/issues/12862
testMatrix.setupTestSuite(
() => {
if (getClientEngineType() !== ClientEngineType.Binary) {
return
}

test('should propagate the correct error when a method fails inside an transaction', async () => {
const user = await prisma.user.create({
data: {
email: faker.internet.email(),
name: faker.name.firstName(),
},
})

await expect(
prisma.$transaction([
prisma.post.create({
data: {
authorId: user.id,
title: faker.lorem.sentence(),
viewCount: -1, // should fail, must be >= 0
},
}),
]),
).rejects.toThrowError('violates check constraint \\"Post_viewCount_check\\"')
})

test('should propagate the correct error when a method fails inside an interactive transaction', async () => {
await expect(
prisma.$transaction(async (client) => {
const user = await client.user.create({
data: {
email: faker.internet.email(),
name: faker.name.firstName(),
},
})

const post = await client.post.create({
data: {
authorId: user.id,
title: faker.lorem.sentence(),
viewCount: -1, // should fail, must be >= 0
},
})

return post
}),
).rejects.toThrowError('violates check constraint \\"Post_viewCount_check\\"')
})
},
{
optOut: {
from: ['cockroachdb', 'mongodb', 'mysql', 'sqlite', 'sqlserver'],
reason: 'bla',
},
},
)

0 comments on commit 4dd3827

Please sign in to comment.