From 33b8ed79105c24b89f0d520aac289ecdb9d858ad Mon Sep 17 00:00:00 2001 From: shunsuke fukuda Date: Mon, 6 Jan 2025 18:17:08 +0900 Subject: [PATCH] Enhance testing by increasing test coverage Add more unit tests to cover edge cases and potential error scenarios for insert, update, and select operations. * **`tests/insert_update.test.ts`** - Add tests for insert with missing fields, insert with null values, update with non-existing condition, and update with null values. * **`tests/select.test.ts`** - Add tests for select with non-existing condition, select with null values, select with multiple conditions, select with order by, and select with limit. * **`tests/test_common.ts`** - Add additional test data for edge cases and potential error scenarios. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/runoshun/kysely-duckdb?shareId=XXXX-XXXX-XXXX-XXXX). --- tests/insert_update.test.ts | 63 +++++++++++++++++++++++++++++++++++++ tests/select.test.ts | 61 +++++++++++++++++++++++++++++++++++ tests/test_common.ts | 5 +++ 3 files changed, 129 insertions(+) diff --git a/tests/insert_update.test.ts b/tests/insert_update.test.ts index 4b9aa62..27e1dc0 100644 --- a/tests/insert_update.test.ts +++ b/tests/insert_update.test.ts @@ -62,3 +62,66 @@ test("update table", async () => { expect(res.length).toBe(1); expect(res[0].numUpdatedRows).toBe(BigInt(1)); }); + +// Additional tests for edge cases and potential error scenarios + +test("insert with missing fields", async () => { + const kysely = await setupDb(); + + const res = await kysely.insertInto("t1") + .values([{ + a: 3, + }]) + .execute(); + + expect(res.length).toBe(1); + expect(res[0].numInsertedOrUpdatedRows).toBe(BigInt(1)); + + const selectRes = await kysely.selectFrom("t1").selectAll().execute(); + expect(selectRes.length).toBe(3); +}); + +test("insert with null values", async () => { + const kysely = await setupDb(); + + const res = await kysely.insertInto("t1") + .values([{ + a: null, + b: 4, + }]) + .execute(); + + expect(res.length).toBe(1); + expect(res[0].numInsertedOrUpdatedRows).toBe(BigInt(1)); + + const selectRes = await kysely.selectFrom("t1").selectAll().execute(); + expect(selectRes.length).toBe(3); +}); + +test("update with non-existing condition", async () => { + const kysely = await setupDb(); + + const res = await kysely.updateTable("t1") + .set({ + a: 20, + }) + .where("a", "=", 100) + .execute(); + + expect(res.length).toBe(1); + expect(res[0].numUpdatedRows).toBe(BigInt(0)); +}); + +test("update with null values", async () => { + const kysely = await setupDb(); + + const res = await kysely.updateTable("t1") + .set({ + a: null, + }) + .where("a", "=", 1) + .execute(); + + expect(res.length).toBe(1); + expect(res[0].numUpdatedRows).toBe(BigInt(1)); +}); diff --git a/tests/select.test.ts b/tests/select.test.ts index 2db79f1..0c6dfed 100644 --- a/tests/select.test.ts +++ b/tests/select.test.ts @@ -55,3 +55,64 @@ test("select complex data types with where", async () => { .execute(); expect(results.length).toBe(1); }); + +// Additional tests for edge cases and potential error scenarios + +test("select with non-existing condition", async () => { + const kysely = await setupDb(); + + const results = await kysely + .selectFrom("t1") + .selectAll() + .where("a", "=", 100) + .execute(); + expect(results.length).toBe(0); +}); + +test("select with null values", async () => { + const kysely = await setupDb(); + + const results = await kysely + .selectFrom("t1") + .selectAll() + .where("a", "is", null) + .execute(); + expect(results.length).toBe(0); +}); + +test("select with multiple conditions", async () => { + const kysely = await setupDb(); + + const results = await kysely + .selectFrom("t1") + .selectAll() + .where("a", "=", 1) + .where("b", "=", 2) + .execute(); + expect(results.length).toBe(1); + expect(results[0]).toEqual({ a: 1, b: 2 }); +}); + +test("select with order by", async () => { + const kysely = await setupDb(); + + const results = await kysely + .selectFrom("t1") + .selectAll() + .orderBy("a", "desc") + .execute(); + expect(results.length).toBe(1); + expect(results[0]).toEqual({ a: 1, b: 2 }); +}); + +test("select with limit", async () => { + const kysely = await setupDb(); + + const results = await kysely + .selectFrom("t1") + .selectAll() + .limit(1) + .execute(); + expect(results.length).toBe(1); + expect(results[0]).toEqual({ a: 1, b: 2 }); +}); diff --git a/tests/test_common.ts b/tests/test_common.ts index aff2c59..75cba8d 100644 --- a/tests/test_common.ts +++ b/tests/test_common.ts @@ -88,5 +88,10 @@ export const setupDb = async () => { ].join(", ") + ");", )); + // Additional test data for edge cases and potential error scenarios + await kysely.executeQuery(CompiledQuery.raw("INSERT INTO t1 VALUES (3, NULL);")); + await kysely.executeQuery(CompiledQuery.raw("INSERT INTO t1 VALUES (NULL, 4);")); + await kysely.executeQuery(CompiledQuery.raw("INSERT INTO t1 VALUES (5, 6);")); + return kysely; };