diff options
-rwxr-xr-x | run | 1 | ||||
-rw-r--r-- | src/db/DBController.ts | 36 | ||||
-rw-r--r-- | test/database_test.ts | 74 |
3 files changed, 94 insertions, 17 deletions
@@ -4,6 +4,7 @@ ARGS=(--allow-net --allow-env --allow-read --unstable) if grep -q "DEBUG=1" .env; then deno test "${ARGS[@]}" test/ && + echo "Tests succeeded!" && deno run "${ARGS[@]}" src/main.ts else deno run "${ARGS[@]}" src/main.ts diff --git a/src/db/DBController.ts b/src/db/DBController.ts index c01d7b4..274e446 100644 --- a/src/db/DBController.ts +++ b/src/db/DBController.ts @@ -15,8 +15,6 @@ export default class DBController { } catch (e) { console.error("Could not create tables"); throw e; - } finally { - this.client.close(); } } @@ -34,21 +32,33 @@ export default class DBController { } } + async query(query: string, params?: string[]) { + if (!this.client) throw Error("Database isn't initialized yet!"); + + try { + return await this.client.query(query, params); + } catch (e) { + throw e; + } + } + async execute(query: string, params?: string[]) { - if (this.client) { - try { - return await this.client.execute(query, params); - } catch (e) { - throw e; - } - } else throw Error("Database isn't initialized yet!"); + if (!this.client) throw Error("Database isn't initialized yet!"); + + try { + await this.client.execute(query, params); + } catch (e) { + throw e; + } } - async execute_multiple(queries: string[]) { + async execute_multiple(queries: any[][]) { + if (!this.client) throw Error("Database isn't initialized yet!"); + try { - return await this.client!.transaction(async (conn) => { + await this.client!.transaction(async (conn) => { queries.forEach(async (query) => { - await conn.execute(query); + await conn.execute(query[0], query[1]); }); }); } catch (e) { @@ -57,6 +67,8 @@ export default class DBController { } async close() { + if (!this.client) throw Error("Database isn't initialized yet!"); + await this.client!.close(); } } diff --git a/test/database_test.ts b/test/database_test.ts index ca2e5b0..07c5443 100644 --- a/test/database_test.ts +++ b/test/database_test.ts @@ -1,7 +1,71 @@ -import { assertEquals, assertArrayContains } from "https://deno.land/std/testing/asserts.ts"; +import "https://deno.land/x/dotenv/load.ts"; +import { assertThrowsAsync } from "https://deno.land/std/testing/asserts.ts"; +import { Client } from "https://deno.land/x/mysql/mod.ts"; +import DBController from "../src/db/DBController.ts"; -Deno.test("hello world", () => { - const x = 1 + 2; - assertEquals(x, 3); - assertArrayContains([1, 2, 3, 4, 5, 6], [3], "Expected 3 to be in the array"); +const controller = new DBController(); + +Deno.test("database connection", async () => { + await controller.connect(); +}); + +Deno.test({ + name: "database initialization", + sanitizeOps: false, // TODO: Find async leak in controller.execute! + async fn() { + await controller.init(); + }, +}); + +Deno.test({ + name: "database table creation", + sanitizeOps: false, // TODO: Previous bug! + sanitizeResources: false, // TODO: Previous bug! + async fn() { + await controller.execute("DROP TABLE IF EXISTS test"); + await controller.execute("CREATE TABLE test (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(16) UNIQUE)"); + }, +}); + +Deno.test({ + name: "database variable arguments", + sanitizeOps: false, // TODO: Previous bug! + sanitizeResources: false, // TODO: Previous bug! + async fn() { + await controller.execute("INSERT INTO test(name) VALUES(?)", ["Melvin"]); + assertThrowsAsync( + () => controller.execute("INSERT INTO test(name) VALUES(?)", ["Melvin"]), + Error, + "Duplicate entry 'Melvin' for key 'name'" + ); + await controller.execute("INSERT INTO test(name) VALUES(?)", ["LarsVomMars"]); + }, +}); + +Deno.test({ + name: "database multiple statements", + sanitizeOps: false, // TODO: Previous bug! + sanitizeResources: false, // TODO: Previous bug! + async fn() { + await controller.execute_multiple([ + ["DELETE FROM test WHERE ?? = ?", ["name", "Melvin"]], + ["INSERT INTO test(name) VALUES(?)", ["Melvin"]], + ]); + }, +}); + +Deno.test({ + name: "database select statements", + sanitizeOps: false, // TODO: Previous bug! + sanitizeResources: false, // TODO: Previous bug! + async fn() { + const count = await controller.query("SELECT ?? FROM ?? WHERE gd=4", ["name", "test"]); + // TODO: WHY DOESN'T THIS WORK? + console.log(count); + }, +}); + +Deno.test("database close", async () => { + await controller.execute("DROP TABLE test"); + await controller.close(); // TODO: Fix 'Bad resource ID'! }); |