Skip to content

Commit

Permalink
tests(client): fix where null for singular relation
Browse files Browse the repository at this point in the history
Related #3342
  • Loading branch information
Jolg42 committed Aug 20, 2020
1 parent c0bc0a8 commit 700d525
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,61 @@ describe('minimal where transformation', () => {
}"
`)
})

test('implicit many-to-many relation: where null', () => {
const transformedDocument = getTransformedDocument({
select: {
posts: {
where: {
content: null,
},
},
},
})

expect(transformedDocument).toMatchInlineSnapshot(`
"query {
findManyUser {
posts(where: {
content: {
equals: null
}
}) {
id
createdAt
updatedAt
published
title
content
authorId
}
}
}"
`)
})

test('where null', () => {
const transformedDocument = getTransformedDocument({
where: {
name: null,
},
})

expect(transformedDocument).toMatchInlineSnapshot(`
"query {
findManyUser(where: {
name: {
equals: null
}
}) {
id
email
name
json
}
}"
`)
})
})

function getTransformedDocument(select) {
Expand Down
4 changes: 3 additions & 1 deletion src/packages/client/src/__tests__/optionalRelation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ describe('optional to one relation', () => {
expect(String(transformDocument(document))).toMatchInlineSnapshot(`
"query {
findManyPost(where: {
author: null
author: {
is: null
}
}) {
id
createdAt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ module.exports = async () => {
proxy.end()
try {
const users = await prisma.user.findMany()
} catch (e) { }
} catch (e) {}
proxy = tcpProxy.createProxy(newPort, credentials.host, sourcePort)
await new Promise((r) => setTimeout(r, 16000))
assert.equal(errorLogs.length, 1)
try {
const users = await prisma.user.findMany()
} catch (e) { }
} catch (e) {}
const users = await prisma.user.findMany()
assert.equal(users.length, 1)
const resultEmptyJson = await prisma.post.create({
Expand Down Expand Up @@ -235,6 +235,14 @@ module.exports = async () => {

assert.equal(result.length, 1, 'We should be able to query by json data')

const resultWhereNull = await prisma.post.findMany({
where: {
content: null,
},
})

assert.equal(resultWhereNull.length, 1, 'We should be able to query by null')

await prisma.post.delete({
where: { id: resultJsonArray.id },
})
Expand Down
Binary file not shown.
24 changes: 22 additions & 2 deletions src/packages/client/src/__tests__/runtime-tests/relations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const assert = require('assert')

module.exports = async () => {
const db = new PrismaClient()

const prisma = new PrismaClient()

const result = await prisma.sale.findMany({
where: {
persons: {
Expand All @@ -17,7 +17,27 @@ module.exports = async () => {
},
})

console.log(result)
assert.deepStrictEqual(result, [])

const resultWhereNull = await prisma.sale.findMany({
where: {
persons: {
some: {
canBeNull: null,
},
},
},
})

assert.deepStrictEqual(resultWhereNull, [])

const resultWhereNullSingularRelationField = await prisma.location.findMany({
where: {
company: null,
},
})

assert.deepStrictEqual(resultWhereNullSingularRelationField, [])

db.$disconnect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@ generator client {
model Person {
id String @id @default(cuid())
name String
canBeNull String?
sales Sale[] @relation("PersonsOnSale", references: [id])
}

model Sale {
id String @id @default(cuid())
date DateTime
canBeNull String?
persons Person[] @relation("PersonsOnSale", references: [id])
}

model Location {
id Int @id
companyId Int
company Company @relation(fields: companyId, references: id)
}

model Company {
id Int @id
location Location
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import chalk from 'chalk'
import { singularRelation } from '../fixtures/singularRelation'
import { DMMFClass, makeDocument, transformDocument } from '../runtime'
import { getDMMF } from '../generation/getDMMF'
chalk.level = 0

let dmmf
describe('minimal where transformation', () => {
beforeAll(async () => {
dmmf = new DMMFClass(await getDMMF({ datamodel: singularRelation }))
})

test('where null', () => {
const transformedDocument = getTransformedDocument({
where: {
company: null,
},
})

expect(transformedDocument).toMatchInlineSnapshot(`
"query {
findManyLocation(where: {
company: {
is: null
}
}) {
id
companyId
}
}"
`)
})
})

function getTransformedDocument(select) {
const document = makeDocument({
dmmf,
select,
rootTypeName: 'query',
rootField: 'findManyLocation',
})
return String(transformDocument(document))
}
23 changes: 23 additions & 0 deletions src/packages/client/src/fixtures/singularRelation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const singularRelation = /* GraphQL */ `
datasource db {
provider = "postgresql"
url = "postgresql://localhost:5432/db"
}
generator client {
provider = "prisma-client-js"
output = "@prisma/client"
transpile = false
}
model Location {
id Int @id
companyId Int
company Company @relation(fields: companyId, references: id)
}
model Company {
id Int @id
location Location
}
`
8 changes: 7 additions & 1 deletion src/packages/client/src/runtime/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,6 @@ export function transformDocument(document: Document): Document {
}
}
} else if (

typeof ar.value === 'object'
&& ar.schemaArg?.inputType[0].kind === 'object'
&& ar.key !== 'is'
Expand All @@ -1014,6 +1013,13 @@ export function transformDocument(document: Document): Document {
schemaArg: ar.schemaArg // probably wrong but fine
})])
}
} else if(ar.value === null) {
ar.value = new Args([new Arg({
key: 'is',
value: ar.value,
argType: ar.argType, // probably wrong but fine
schemaArg: ar.schemaArg // probably wrong but fine
})])
}
}
return ar
Expand Down

0 comments on commit 700d525

Please sign in to comment.