/
mtxn.mysql.test.ts
108 lines (79 loc) · 3.72 KB
/
mtxn.mysql.test.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { afterAll, beforeAll, describe, expect, test } from '@jest/globals';
import log4js from "log4js";
import { FunctionContext, MultiTxnMngr, Task } from "multiple-transaction-manager";
import mysql from "mysql2";
import { MysqlDBContext } from "../src/index";
log4js.configure({
appenders: { 'out': { type: 'stdout' } },
categories: { default: { appenders: ['out'], level: 'debug' } }
});
const pool = mysql.createPool({
connectionLimit: 3,
host: "localhost",
user: "root",
password: "changeme",
database: "mtxnmngr"
});
describe("Multiple transaction manager mysql workflow test...", () => {
beforeAll(() => { global.console = require('console'); });
test("Success-commit case", async () => {
// init manager & context
const txnMngr: MultiTxnMngr = new MultiTxnMngr();
const mysqlContext = new MysqlDBContext(txnMngr, pool);
const functionContext = new FunctionContext(txnMngr);
// Add first step
mysqlContext.addTask("DELETE FROM test_table");
// Add second step
mysqlContext.addTask("INSERT INTO test_table(id, name) VALUES (:id, :name)", { "id": 1, "name": "Dave" });
// Add third step
functionContext.addTask(
(task) => { return new Promise((resolve, _reject) => { console.log("All done."); resolve(task); }); },
null, // optional params
(task) => { return new Promise((resolve, _reject) => { console.log("On Txn Commit..."); resolve(task); }); },
(task) => { return new Promise((resolve, _reject) => { console.log("On Txn Rollback..."); resolve(task); }); }
);
await expect(txnMngr.exec()).resolves.not.toBeNull();
});
test("Fail-rollback case", async () => {
// init manager & context
const txnMngr: MultiTxnMngr = new MultiTxnMngr();
const mysqlContext = new MysqlDBContext(txnMngr, pool);
const functionContext = new FunctionContext(txnMngr);
// Add first step
mysqlContext.addTask("DELETE FROM test_table");
// Add second step
mysqlContext.addTask("INSERT INTO test_table(id, name) VALUES (:id, :name)", { "id": 1, "name": "Dave" });
// Add third step -> Causes primary key violation
mysqlContext.addTask("INSERT INTO test_table(id, name) VALUES (:id, :name)", { "id": 1, "name": "Kevin" });
// Add last step -> should not execute
functionContext.addTask(
(task) => { return new Promise((resolve, _reject) => { console.log("Face the thing that should not be..."); resolve(task); }); }
);
await expect(txnMngr.exec()).rejects.not.toBeNull();
});
test("Function task example", async () => {
// init manager & context
const txnMngr: MultiTxnMngr = new MultiTxnMngr();
const mysqlContext = new MysqlDBContext(txnMngr, pool);
// Add first step
mysqlContext.addTask("DELETE FROM test_table");
// Add second step
mysqlContext.addFunctionTask(
(txn, _task) => {
return new Promise<unknown | undefined>((resolve, reject) => {
txn.query("INSERT INTO test_table(id, name) VALUES (1, 'Stuart')", null, (err, results, fields) => {
if (err) {
reject(err);
} else {
resolve({ results, fields });
}
});
});
});
// Add control step
const controlTask: Task = mysqlContext.addTask("SELECT * FROM test_table");
await txnMngr.exec();
expect(controlTask.getResult().results[0]["name"]).toEqual("Stuart");
});
afterAll(() => { pool.end(); });
});