forked from ali-sdk/ali-rds
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoperator.test.ts
139 lines (126 loc) · 5.2 KB
/
operator.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { strict as assert } from 'node:assert';
import { Operator } from '../src/operator';
class CustomOperator extends Operator {}
describe('test/operator.test.ts', () => {
describe('_where(where)', () => {
it('should get where sql', () => {
const op: any = new CustomOperator();
assert.equal(op._where(), '');
assert.equal(op._where({}), '');
assert.equal(op._where({ id: 1 }), ' WHERE `id` = 1');
assert.equal(op._where({ id: 1, name: 'foo' }), ' WHERE `id` = 1 AND `name` = \'foo\'');
assert.equal(op._where({ id: 1, name2: null }), ' WHERE `id` = 1 AND `name2` IS NULL');
assert.equal(op._where({ id: 1, name3: undefined }), ' WHERE `id` = 1 AND `name3` IS NULL');
assert.equal(op._where({ 'test.id': 1 }), ' WHERE `test`.`id` = 1');
assert.equal(op._where({ id: [ 1, 2 ], name: 'foo' }), ' WHERE `id` IN (1, 2) AND `name` = \'foo\'');
assert.equal(op._where({ id: [ 1 ], name: 'foo' }), ' WHERE `id` IN (1) AND `name` = \'foo\'');
// assert.equal(op.where({ 'test.id': new Date(), name: 'foo' }, 'test.id'), ' WHERE `test`.`id` = 1');
assert.equal(op._where({ name: 'foo\'\"' }), ' WHERE `name` = \'foo\\\'\\\"\'');
assert.equal(op._where({ id: 1, name: 'foo\'\"' }), ' WHERE `id` = 1 AND `name` = \'foo\\\'\\\"\'');
assert.equal(op._where({ id: 1, name: 'foo\'\"', user: 'fengmk2' }),
' WHERE `id` = 1 AND `name` = \'foo\\\'\\\"\' AND `user` = \'fengmk2\'');
});
});
describe('get threadId()', () => {
it('should get return undefined when connection not exists', () => {
const op = new CustomOperator();
assert.equal(op.threadId, undefined);
});
});
describe('format()', () => {
it('should get literal string', () => {
const op = new CustomOperator();
assert.equal(op.format('SET ?? = ?', [ 'dt', op.literals.now ], true), 'SET `dt` = now()');
});
it('should get literal string by string', () => {
const op = new CustomOperator();
assert.equal(op.format('SET name = ?', 'test'), 'SET name = \'test\'');
});
it('should get literal string by object', () => {
const op = new CustomOperator();
assert.equal(op.format('SET dt = :now and name = :name and age = :age', {
now: op.literals.now,
name: 'test',
}), 'SET dt = now() and name = \'test\' and age = :age');
});
it('should get literal string by boundary', () => {
const op = new CustomOperator();
assert.equal(op.format('SET name = ?', null), 'SET name = ?');
assert.equal(op.format('SET name = ?', undefined), 'SET name = ?');
assert.equal(op.format('SET name = ?', 0), 'SET name = 0');
assert.equal(op.format('SET name = ?', 1), 'SET name = 1');
assert.equal(op.format('SET name = ?', 'foo'), 'SET name = \'foo\'');
assert.equal(op.format('SET name = ?', true), 'SET name = true');
assert.equal(op.format('SET name = ?', false), 'SET name = false');
});
});
describe('_query()', () => {
it('should throw error when SubClass not impl', async () => {
const op = new CustomOperator();
try {
await op.query('foo');
} catch (err) {
assert.equal(err.message.indexOf('SubClass must impl this'), 0);
}
});
});
describe('beforeQuery(), afterQuery()', () => {
class CustomOperator extends Operator {
protected async _query(sql: string): Promise<any> {
// console.log(sql);
if (sql === 'error') throw new Error('mock error');
return { sql };
}
}
it('should override query sql', async () => {
const op = new CustomOperator();
op.beforeQuery(sql => {
return `hello ${sql}`;
});
const result = await op.query('foo');
assert.equal(result.sql, 'hello foo');
});
it('should not override query sql', async () => {
const op = new CustomOperator();
op.beforeQuery(sql => {
assert(sql);
});
const result = await op.query('foo');
assert.equal(result.sql, 'foo');
});
it('should get query result on after hook', async () => {
const op = new CustomOperator();
let called = false;
op.afterQuery((sql, result, execDuration, err) => {
assert.equal(sql, 'foo');
assert.deepEqual(result, { sql });
assert.equal(typeof execDuration, 'number');
assert(execDuration >= 0);
assert.equal(err, undefined);
called = true;
});
const result = await op.query('foo');
assert.equal(result.sql, 'foo');
assert(called);
});
it('should get query error on after hook', async () => {
const op = new CustomOperator();
op.afterQuery((sql, result, execDuration, err) => {
assert.equal(sql, 'error');
assert.equal(result, undefined);
assert.equal(typeof execDuration, 'number');
assert(execDuration >= 0);
assert(err instanceof Error);
assert.equal(err.message, 'mock error');
assert.match(err.stack!, /sql: error/);
});
await assert.rejects(async () => {
await op.query('error');
}, (err: any) => {
assert.equal(err.message, 'mock error');
assert.match(err.stack, /sql: error/);
return true;
});
});
});
});