Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(client): Serialize all Floats in exponential notation (#15879)
* 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
1 parent
89ef63e
commit 4732879
Showing
9 changed files
with
169 additions
and
7 deletions.
There are no files selected for viewing
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
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
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,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 | ||
} | ||
} | ||
`) | ||
}) |
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
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
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,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
20
packages/client/tests/functional/large-floats/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,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 | ||
} | ||
` | ||
}) |
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,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) | ||
}) | ||
}) |
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