Skip to content

Commit

Permalink
fix(client): 2.29.1 fixes (#8720)
Browse files Browse the repository at this point in the history
  • Loading branch information
millsp committed Aug 12, 2021
1 parent 866cf2d commit 16e5876
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 84 deletions.
Expand Up @@ -303,6 +303,42 @@ Invalid \`prisma.user.create()\` invocation:
expect(users.length).toBe(0)
})

/**
* A bad batch should rollback using the interactive transaction logic
*/
test('batching raw rollback', async () => {
await prisma.user.create({
data: {
email: 'user_1@website.com',
},
})

const result = prisma.$transaction([
prisma.$executeRaw(
'INSERT INTO User (id, email) VALUES ("2", "user_2@website.com")',
),
prisma.$queryRaw('DELETE FROM User'),
prisma.$executeRaw(
'INSERT INTO User (id, email) VALUES ("1", "user_1@website.com")',
),
prisma.$executeRaw(
'INSERT INTO User (id, email) VALUES ("1", "user_1@website.com")',
),
])

await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
Invalid \`prisma.executeRaw()\` invocation:
Raw query failed. Code: \`2067\`. Message: \`UNIQUE constraint failed: User.email\`
`)

const users = await prisma.user.findMany()

expect(users.length).toBe(1)
})

/**
* Middlewares should work normally on batches
*/
Expand Down
@@ -0,0 +1 @@
!dev.db
Binary file not shown.
@@ -0,0 +1,29 @@
datasource db {
provider = "sqlite"
url = "file:dev.db"
}

generator client {
provider = "prisma-client-js"
}

// / User model comment
model User {
id String @default(uuid()) @id
email String @unique
age Int
// / name comment
name String?
posts Post[]
}

model Post {
id String @default(cuid()) @id
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean
title String
content String?
authorId String?
author User? @relation(fields: [authorId], references: [id])
}
@@ -0,0 +1,68 @@
import { getTestClient } from '../../../../utils/getTestClient'
describe('prisma promises', () => {
/**
* Requests must get sent if we call `.catch`
*/
test('catch', async () => {
const PrismaClient = await getTestClient()
const prisma = new PrismaClient()
const handler = (e) => Promise.reject(e)

const remove = await prisma.user.deleteMany().catch(handler)
const queryRaw = await prisma.$queryRaw('SELECT 1').catch(handler)
const executeRaw = await prisma
.$executeRaw('DELETE FROM User')
.catch(handler)
const findMany = await prisma.user.findMany().catch(handler)

expect(remove).toMatchInlineSnapshot(`
Object {
count: 0,
}
`)
expect(queryRaw).toMatchInlineSnapshot(`
Array [
Object {
1: 1,
},
]
`)
expect(executeRaw).toMatchInlineSnapshot(`0`)
expect(findMany).toMatchInlineSnapshot(`Array []`)

await prisma.$disconnect()
})

/**
* Requests must get sent if we call `.finally`
*/
test('finally', async () => {
const PrismaClient = await getTestClient()
const prisma = new PrismaClient()
const handler = () => {}

const remove = await prisma.user.deleteMany().finally(handler)
const queryRaw = await prisma.$queryRaw('SELECT 1').finally(handler)
const executeRaw = await prisma
.$executeRaw('DELETE FROM User')
.finally(handler)
const findMany = await prisma.user.findMany().finally(handler)

expect(remove).toMatchInlineSnapshot(`
Object {
count: 0,
}
`)
expect(queryRaw).toMatchInlineSnapshot(`
Array [
Object {
1: 1,
},
]
`)
expect(executeRaw).toMatchInlineSnapshot(`0`)
expect(findMany).toMatchInlineSnapshot(`Array []`)

await prisma.$disconnect()
})
})

0 comments on commit 16e5876

Please sign in to comment.