From 4883c885e7108090530c32b09e08bf4049e7ae25 Mon Sep 17 00:00:00 2001 From: igalklebanov Date: Sun, 1 Sep 2024 16:34:35 +0300 Subject: [PATCH] typings test inserts. --- test/typings/shared.d.ts | 2 +- test/typings/test-d/insert.test-d.ts | 125 ++++++++++++++++++++++++++- 2 files changed, 125 insertions(+), 2 deletions(-) diff --git a/test/typings/shared.d.ts b/test/typings/shared.d.ts index 9a7bc1afd..97cc35dc8 100644 --- a/test/typings/shared.d.ts +++ b/test/typings/shared.d.ts @@ -87,5 +87,5 @@ export interface PersonMetadata { > schedule: JSONColumnType<{ name: string; time: string }[][][]> record: JSONColumnType> - array: JSONColumnType> + array: JSONColumnType | null> } diff --git a/test/typings/test-d/insert.test-d.ts b/test/typings/test-d/insert.test-d.ts index e8f049ba1..f4325c99e 100644 --- a/test/typings/test-d/insert.test-d.ts +++ b/test/typings/test-d/insert.test-d.ts @@ -1,5 +1,5 @@ import { expectError, expectType } from 'tsd' -import { InsertResult, Kysely, sql } from '..' +import { ExpressionBuilder, InsertObject, InsertResult, Kysely, sql } from '..' import { Database } from '../shared' async function testInsert(db: Kysely) { @@ -268,3 +268,126 @@ async function testOutput(db: Kysely) { expectError(db.insertInto('person').output('deleted.age').values(person)) expectError(db.insertInto('person').outputAll('deleted').values(person)) } + +async function testValJson(db: Kysely) { + const getValues = < + O extends Partial>, + >( + { valJson }: ExpressionBuilder, + overrides?: O, + ) => ({ + array: valJson(['123']), + experience: valJson([{ establishment: 'New York Times' }]), + nicknames: valJson(['Jenny']), + person_id: 1, + profile: valJson({ + auth: { + roles: ['admin'], + }, + tags: ['important'], + }), + website: valJson({ url: 'http://example.com' }), + record: valJson({ key: 'value' }), + schedule: valJson([ + [ + [ + { + name: 'foo', + time: '2024-01-01T00:00:00.000Z', + }, + ], + ], + ]), + ...overrides, + }) + + db.insertInto('person_metadata').values(getValues).execute() + + db.insertInto('person_metadata').values((eb) => + getValues(eb, { + array: null, + }), + ) + + db.insertInto('person_metadata').values((eb) => + getValues(eb, { + array: eb.valJson(null), + }), + ) + + db.insertInto('person_metadata').values((eb) => + getValues(eb, { + array: sql.valJson(null), + }), + ) + + db.insertInto('person_metadata').values((eb) => + getValues(eb, { + website: sql.valJson({ url: 'http://example.com' }), + }), + ) + + expectError( + db.insertInto('person_metadata').values((eb) => + getValues(eb, { + array: ['123'], // expects `valJson(Array | null)` + }), + ), + ) + + expectError( + db.insertInto('person_metadata').values((eb) => + getValues(eb, { + array: eb.val(['123']), // expects `valJson(Array | null)` + }), + ), + ) + + expectError( + db.insertInto('person_metadata').values((eb) => + getValues(eb, { + array: eb.valJson({}), // expects `valJson(Array | null)` + }), + ), + ) + + expectError( + db.insertInto('person_metadata').values((eb) => + getValues(eb, { + array: eb.valJson([123]), // expects `valJson(Array | null)` + }), + ), + ) + + expectError( + db.insertInto('person_metadata').values((eb) => + getValues(eb, { + experience: [{ establishment: 'New York Times' }], // expects `valJson({ establishment: string }[])` + }), + ), + ) + + expectError( + db.insertInto('person_metadata').values((eb) => + getValues(eb, { + experience: eb.valJson({ establishment: 'New York Times' }), // expects `valJson({ establishment: string }[])` + }), + ), + ) + + expectError( + db.insertInto('person_metadata').values((eb) => + getValues(eb, { + experience: eb.valJson([{}]), // expects `valJson({ establishment: string }[])` + }), + ), + ) + + expectError( + db.insertInto('person_metadata').values((eb) => + getValues(eb, { + experience: eb.valJson([{ establishment: 2 }]), // expects `valJson({ establishment: string }[])` + }), + ), + ) +}