-
Notifications
You must be signed in to change notification settings - Fork 2
/
mysql.spec.ts
58 lines (43 loc) · 1.43 KB
/
mysql.spec.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
46
47
48
49
50
51
52
53
54
55
56
57
58
import expect from "expect";
import * as Mysql from "mysql";
import { sql, createSqlWithDefaults } from "./mysql";
let connection: Mysql.Connection;
beforeAll(() => {
connection = connect();
});
afterAll(() => {
connection.end();
});
test("with default options", async () => {
const sql = createSqlWithDefaults({ connection });
const simpleQuery = sql<{ name: string }, { providedName: string }>`
SELECT ${p => p.name} AS providedName FROM DUAL
`;
const result = await simpleQuery.execute({ name: "Gal" });
expect(result[0].providedName).toBe("Gal");
});
test("simple query", async () => {
const simpleQuery = sql<{ name: string }, { providedName: string }>`
SELECT ${p => p.name} AS providedName FROM DUAL
`;
const result = await simpleQuery.execute({ name: "Gal" }, { connection });
expect(result[0].providedName).toBe("Gal");
});
test("composition", async () => {
const simpleQuery = sql<{ name: string }, { providedName: string }>`
SELECT ${p => p.name} AS name FROM DUAL
`;
const composition = sql<{ name: string }, { uppercased: string }>`
SELECT UPPER(name) AS uppercased FROM (${simpleQuery}) simple_query
`;
const result = await composition.execute({ name: "Gal" }, { connection });
expect(result[0].uppercased).toBe("GAL");
});
function connect() {
const connection = Mysql.createConnection({
host: "localhost",
user: "root",
password: "password"
});
return connection;
}