Skip to content

Commit

Permalink
fix(client): Serialize all Floats in exponential notation (#15879)
Browse files Browse the repository at this point in the history
* test(client): unit test for #12651

* test(client): integration test for #12651

* fix(client): Serialize all floats in exponential notation

Fix #12651, fix #13317

* test(internals): Fix normalizeMigrateTimestamps applying to all long numbers

* test(client): Convert large-floats tests to functional

* test(client): Revert snapshot serializer changes

It does not seem that it was indendened for snapshot serizier to apply
to select.test.ts. Instead, we are fixing the failure by changing the
number so it won't be replaced by serializer.

* Revert "test(client): Revert snapshot serializer changes"

This reverts commit 7756be2.

* Update packages/internals/src/utils/jestSnapshotSerializer.js

Co-authored-by: Sergey Tatarintsev <sergey@tatarintsev.me>
Co-authored-by: Alexey Orlenko <alex@aqrln.net>
  • Loading branch information
3 people committed Oct 19, 2022
1 parent 89ef63e commit 4732879
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 7 deletions.
Expand Up @@ -476,7 +476,7 @@ query {
age_gt: 10123123123
id_endsWith: "veryLongNameGoIntoaNewLineNow@gmail.com"
name_contains: "hans"
name_gt: 2020123100000023
name_gt: 2131203912039123
name_in: ["hans"]
AND: [
{
Expand Down Expand Up @@ -541,7 +541,7 @@ Invalid \`prisma.findManyUser()\` invocation:
~~~~~~~~~~~
name_contains: 'hans',
~~~~~~~~~~~~~
name_gt: 2020123100000023,
name_gt: 2131203912039123,
~~~~~~~
name_in: [
~~~~~~~
Expand Down Expand Up @@ -827,7 +827,7 @@ Invalid \`prisma.findManyUser()\` invocation:
~~~~~~~~~~~
name_contains: 'hans',
~~~~~~~~~~~~~
name_gt: 2020123100000023,
name_gt: 2131203912039123,
~~~~~~~
name_in: [
~~~~~~~
Expand Down
Expand Up @@ -40,7 +40,7 @@ describe('minimal atomic update transformation', () => {
mutation {
updateOneUser(
data: {
countFloat: 3.1415926
countFloat: 3.1415926e+0
countInt1: 3
}
) {
Expand Down
82 changes: 82 additions & 0 deletions packages/client/src/__tests__/query/floats.test.ts
@@ -0,0 +1,82 @@
import { getDMMF } from '../../generation/getDMMF'
import { DMMFClass, makeDocument, transformDocument } from '../../runtime'

const datamodel = /* Prisma */ `
datasource my_db {
provider = "postgres"
url = env("POSTGRES_URL")
}
model Floats {
id Int @id
value Float
}
`

let dmmf

function getTransformedDocument(select) {
const document = makeDocument({
dmmf,
select,
rootTypeName: 'mutation',
rootField: 'createOneFloats',
})
return String(transformDocument(document))
}

beforeAll(async () => {
dmmf = new DMMFClass(await getDMMF({ datamodel }))
})

test('serializes floats in exponential notation', () => {
const largeInt = getTransformedDocument({
data: {
value: 100_000_000_000_000_000_000,
},
})

expect(largeInt).toMatchInlineSnapshot(`
mutation {
createOneFloats(data: {
value: 1e+20
}) {
id
value
}
}
`)

const negativeInt = getTransformedDocument({
data: {
value: Number.MIN_SAFE_INTEGER,
},
})

expect(negativeInt).toMatchInlineSnapshot(`
mutation {
createOneFloats(data: {
value: -9.007199254740991e+15
}) {
id
value
}
}
`)

const otherFloat = getTransformedDocument({
data: {
value: 13.37,
},
})
expect(otherFloat).toMatchInlineSnapshot(`
mutation {
createOneFloats(data: {
value: 1.337e+1
}) {
id
value
}
}
`)
})
4 changes: 2 additions & 2 deletions packages/client/src/__tests__/scalarListCreate.test.ts
Expand Up @@ -54,7 +54,7 @@ describe('scalar where transformation', () => {
}
}
someFloats: {
set: [1, 1.2]
set: [1e+0, 1.2e+0]
}
}) {
id
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('scalar where transformation', () => {
id: 5
}
}
someFloats: [1, 1.2]
someFloats: [1e+0, 1.2e+0]
}) {
id
name
Expand Down
4 changes: 4 additions & 0 deletions packages/client/src/runtime/query.ts
Expand Up @@ -651,6 +651,10 @@ function stringify(value: any, inputType?: DMMF.SchemaArgInputType) {
return value
}

if (typeof value === 'number' && inputType?.type === 'Float') {
return value.toExponential()
}

return JSON.stringify(value, null, 2)
}

Expand Down
24 changes: 24 additions & 0 deletions packages/client/tests/functional/large-floats/_matrix.ts
@@ -0,0 +1,24 @@
import { defineMatrix } from '../_utils/defineMatrix'

export default defineMatrix(() => [
[
{
provider: 'sqlite',
},
{
provider: 'postgresql',
},
{
provider: 'mysql',
},
{
provider: 'mongodb',
},
{
provider: 'cockroachdb',
},
{
provider: 'sqlserver',
},
],
])
20 changes: 20 additions & 0 deletions packages/client/tests/functional/large-floats/prisma/_schema.ts
@@ -0,0 +1,20 @@
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 Floats {
id ${idForProvider(provider)}
value Float
}
`
})
32 changes: 32 additions & 0 deletions packages/client/tests/functional/large-floats/tests.ts
@@ -0,0 +1,32 @@
// @ts-ignore
import type { PrismaClient } from '@prisma/client'

import testMatrix from './_matrix'

declare let prisma: PrismaClient

testMatrix.setupTestSuite(() => {
test('floats', async () => {
const largeFloat = await prisma.floats.create({
data: { value: 1e20 },
})
const negativeFloat = await prisma.floats.create({
data: { value: -1e20 },
})
const largeInteger = await prisma.floats.create({
data: { value: Number.MAX_SAFE_INTEGER },
})
const negativeInteger = await prisma.floats.create({
data: { value: Number.MIN_SAFE_INTEGER },
})
const otherFloat = await prisma.floats.create({
data: { value: 13.37 },
})

expect(largeFloat.value).toEqual(1e20)
expect(negativeFloat.value).toEqual(-1e20)
expect(largeInteger.value).toEqual(Number.MAX_SAFE_INTEGER)
expect(negativeInteger.value).toEqual(Number.MIN_SAFE_INTEGER)
expect(otherFloat.value).toEqual(13.37)
})
})
2 changes: 1 addition & 1 deletion packages/internals/src/utils/jestSnapshotSerializer.js
Expand Up @@ -69,7 +69,7 @@ function normalizeBinaryFilePath(str) {
}

function normalizeMigrateTimestamps(str) {
return str.replace(/\d{14}/g, '20201231000000')
return str.replace(/(?<!\d)\d{14}(?!\d)/g, '20201231000000')
}

function normalizeDbUrl(str) {
Expand Down

0 comments on commit 4732879

Please sign in to comment.