aboutsummaryrefslogtreecommitdiff
path: root/test/database_test.ts
blob: 145e0b9852b1c7e6a0399c4cfc270dfff87a55f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import "https://deno.land/x/dotenv/load.ts";
import { assertThrowsAsync, assert } 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";

const controller = new DBController();

Deno.test("database connection", async () => {
    await controller.connect();
});

Deno.test({
    name: "database initialization",
    sanitizeResources: false, // TODO: Previous bug!
    async fn() {
        await controller.init();
    },
});

Deno.test({
    name: "database table creation",
    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",
    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",
    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",
    sanitizeResources: false, // TODO: Previous bug!
    async fn() {
        const element = await controller.query("SELECT ?? FROM ?? WHERE id=?", ["name", "test", "4"]);
        assert(element[0].name == "Melvin");
    },
});

Deno.test({
    name: "database select statements",
    sanitizeResources: false, // TODO: Previous bug!
    async fn() {
        //await controller.execute("DROP TABLE test");
        await controller.close(); // TODO: Fix 'Bad resource ID'!
    },
});