-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
test.util.ts
45 lines (41 loc) · 999 Bytes
/
test.util.ts
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
import { Client } from "./mod.ts";
const { DB_PORT, DB_NAME, DB_PASSWORD, DB_USER, DB_HOST } = Deno.env();
const port = DB_PORT ? parseInt(DB_PORT) : 3306;
const db = DB_NAME || "test";
const password = DB_PASSWORD;
const username = DB_USER || "root";
const hostname = DB_HOST || "127.0.0.1";
const config = {
timeout: 10000,
poolSize: 3,
debug: false,
hostname,
username,
port,
db,
password,
};
export function testWithClient(fn: (client: Client) => void | Promise<void>) {
Deno.test({
name: fn.name,
async fn() {
const client = await new Client().connect(config);
try {
await fn(client);
await client.close();
} catch (error) {
await client.close();
throw error;
}
},
});
}
export async function createTestDB() {
const client = await new Client().connect({
...config,
poolSize: 1,
db: undefined,
});
await client.execute(`CREATE DATABASE IF NOT EXISTS ${db}`);
await client.close();
}