Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/client/tests/functional/issues/16196-upsert-nested-select/_matrix.ts
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,15 @@ | ||
import { defineMatrix } from '../../_utils/defineMatrix' | ||
|
||
export default defineMatrix(() => [ | ||
[ | ||
{ | ||
provider: 'sqlite', | ||
}, | ||
{ | ||
provider: 'postgresql', | ||
}, | ||
{ | ||
provider: 'cockroachdb', | ||
}, | ||
], | ||
]) |
36 changes: 36 additions & 0 deletions
36
packages/client/tests/functional/issues/16196-upsert-nested-select/prisma/_schema.ts
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,36 @@ | ||
import { idForProvider } from '../../../_utils/idForProvider' | ||
import testMatrix from '../_matrix' | ||
|
||
export default testMatrix.setupSchema(({ provider }) => { | ||
return /* Prisma */ ` | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
datasource db { | ||
provider = "${provider}" | ||
url = env("DATABASE_URI_${provider}") | ||
} | ||
model User { | ||
id ${idForProvider(provider)} | ||
email String @unique | ||
posts Post[] | ||
profile Profile? | ||
} | ||
model Post { | ||
id ${idForProvider(provider)} | ||
title String | ||
user User @relation(fields: [userId], references: [id]) | ||
userId String | ||
} | ||
model Profile { | ||
id ${idForProvider(provider)} | ||
name String | ||
user User @relation(fields: [userId], references: [id]) | ||
userId String @unique | ||
} | ||
` | ||
}) |
131 changes: 131 additions & 0 deletions
131
packages/client/tests/functional/issues/16196-upsert-nested-select/tests.ts
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,131 @@ | ||
import { randomBytes } from 'crypto' | ||
|
||
import testMatrix from './_matrix' | ||
// @ts-ignore | ||
import type { PrismaClient } from './node_modules/@prisma/client' | ||
|
||
declare let prisma: PrismaClient | ||
|
||
const id = randomBytes(12).toString('hex') | ||
testMatrix.setupTestSuite( | ||
(suiteConfig, suiteMeta) => { | ||
beforeAll(async () => { | ||
await prisma.user.create({ | ||
data: { | ||
id: id, | ||
email: 'john@doe.io', | ||
posts: { | ||
create: [{ title: 'A' }, { title: 'B' }, { title: 'C' }], | ||
}, | ||
}, | ||
}) | ||
}) | ||
|
||
test('upsert (update) with nested select many relation', async () => { | ||
const data = await prisma.user.upsert({ | ||
where: { id }, | ||
create: { | ||
email: 'john@doe.io', | ||
}, | ||
update: { | ||
email: 'johnny@doe.io', | ||
}, | ||
select: { | ||
posts: { | ||
select: { | ||
user: { | ||
select: { | ||
email: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
expect(data).toMatchInlineSnapshot(` | ||
Object { | ||
posts: Array [ | ||
Object { | ||
user: Object { | ||
email: johnny@doe.io, | ||
}, | ||
}, | ||
Object { | ||
user: Object { | ||
email: johnny@doe.io, | ||
}, | ||
}, | ||
Object { | ||
user: Object { | ||
email: johnny@doe.io, | ||
}, | ||
}, | ||
], | ||
} | ||
`) | ||
}) | ||
|
||
test('upsert (update) with nested select one relation', async () => { | ||
const data = await prisma.user.upsert({ | ||
where: { id }, | ||
create: { | ||
email: 'john@doe.io', | ||
}, | ||
update: { | ||
email: 'johnny@doe.io', | ||
}, | ||
select: { | ||
profile: { | ||
select: { | ||
user: { | ||
select: { | ||
email: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
expect(data).toMatchInlineSnapshot(` | ||
Object { | ||
profile: null, | ||
} | ||
`) | ||
}) | ||
|
||
test('upsert (create) with nested select one relation', async () => { | ||
const data = await prisma.user.upsert({ | ||
where: { id: randomBytes(12).toString('hex') }, | ||
create: { | ||
email: 'jane@joe.io', | ||
}, | ||
update: { | ||
email: 'janette@doe.io', | ||
}, | ||
select: { | ||
email: true, | ||
profile: { | ||
select: { | ||
id: true, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
expect(data).toMatchInlineSnapshot(` | ||
Object { | ||
email: jane@joe.io, | ||
profile: null, | ||
} | ||
`) | ||
}) | ||
}, | ||
{ | ||
optOut: { | ||
from: ['mysql', 'mongodb', 'sqlserver'], | ||
reason: "We don't support native upserts for these", | ||
}, | ||
}, | ||
) |