-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.test.js
72 lines (69 loc) · 2.55 KB
/
transaction.test.js
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
const Transaction = require('./transaction.js');
require('jest');
describe('#constructor', () => {
test('throws when a pool argument is not provided.', () => {
expect(() => { new Transaction(); }).toThrow('pool');
});
});
describe('#clear', () => {
test('clears all active queries and savepoints.', () => {
let t = new Transaction({});
t.query('SELECT 1');
t.savePoint();
t.clear();
expect(t.queries.length).toBe(0);
});
});
describe('#savePoint', () => {
test('adds a save-point to the query queue.', () => {
let t = new Transaction({});
t.query('SELECT 1');
t.savePoint();
expect(t.queries.length).toBe(2);
expect(t.queries[1].savepoint).toBe(true);
});
test('throws if the save-point is requested before any queries are added.', () => {
let t = new Transaction({});
expect(() => t.savePoint()).toThrow(/follow.+query/i);
expect(() => t.savePoint('abc')).toThrow(/follow.+query/i);
});
test('throws if the save-point is added right after another save-point', () => {
let t = new Transaction({});
t.query('SELECT 1');
t.savePoint();
expect(() => t.savePoint()).toThrow(/follow.+save/i);
expect(() => t.savePoint('abc')).toThrow(/follow.+save/i);
});
test('throws on a non-string @name', () => {
let t = new Transaction({});
t.query('SELECT 1');
expect(() => t.savePoint(true)).toThrow('name');
expect(() => t.savePoint(false)).toThrow('name');
expect(() => t.savePoint(new Date())).toThrow('name');
expect(() => t.savePoint(0)).toThrow('name');
expect(() => t.savePoint(1234)).toThrow('name');
expect(() => t.savePoint('ok')).not.toThrowError();
});
test('returns the @name provided.', () => {
let t = new Transaction({});
t.query('SELECT 1');
expect(t.savePoint('test')).toBe('test');
t.query('SELECT 1');
expect(t.savePoint('abc')).toBe('abc');
});
test('returns randomly generated save-point name.', () => {
let t = new Transaction({});
t.query('SELECT 1');
expect(typeof t.savePoint()).toBe('string');
t.query('SELECT 1');
expect(t.savePoint().length).toBeGreaterThan(0);
//make sure they are random
let lastNames = [];
for (let x = 0; x < 100; x++) {
t.query('SELECT 1');
let n = t.savePoint();
expect(lastNames).not.toContain(n);
lastNames.push(n);
}
});
});