-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtx.test.js
106 lines (95 loc) · 3.3 KB
/
tx.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
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
const expect = require('expect.js');
const createPostgres = require('../..');
const config = {
withSql: {
user: 'postgres',
database: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
max: 10,
idleTimeoutMillis: 30000,
isolationLevel: 'REPEATABLE READ',
sql: 'test/fixtures/sql',
},
};
describe('Handy pg transactions test', () => {
const pgComponent = createPostgres({ configPath: 'withSql' });
let withTransaction;
let formattedQuery;
const pf = { code: 'pulpf', title: 'Pulp Fiction', kind: 'cult', dateProd: null, len: 178 };
const rt = { code: 'royal', title: 'The Royal Tenenbaums', dateProd: new Date('2002-01-04'), kind: 'comedy', len: 110 };
const lb = { code: 'corey', title: 'The Lost Boys', dateProd: new Date('1987-07-31'), kind: 'drama', len: 97 };
before(async () => {
const api = await pgComponent.start(config);
withTransaction = api.withTransaction;
formattedQuery = api.formattedQuery;
await formattedQuery('drop-films');
await formattedQuery('create-films');
});
beforeEach(() => formattedQuery('truncate-films'));
after(async () => {
await formattedQuery('drop-films');
await pgComponent.stop();
});
it('inserts a record inside a transaction', () =>
withTransaction((tx) => tx.insert('films', pf))
.then(({ command }) => expect(command).to.equal('INSERT'))
.then(() => formattedQuery('select-all', ['films']))
.then(({ rows }) => expect(rows.length).to.equal(1))
.catch((err) => expect(err).to.be(null))
);
it('uses the transaction API namespaced by a schema', () =>
withTransaction((tx) => tx.schema('public').insert('films', pf))
.then(({ command }) => expect(command).to.equal('INSERT'))
.then(() => formattedQuery('select-all', ['films']))
.then(({ rows }) => expect(rows.length).to.equal(1))
.catch((err) => expect(err).to.be(null))
);
it('commits multiple parallel operations within a transaction', () =>
withTransaction((tx) =>
Promise.all([
tx.insert('films', pf),
tx.insert('films', rt),
])
)
.then(() =>
formattedQuery('select-all', ['films'])
.then(({ rows }) => {
const [first, second] = rows;
expect(first.code).to.equal('pulpf');
expect(second.code).to.equal('royal');
})
)
);
it('commits multiple serial operations within a transaction', () =>
withTransaction((tx) =>
tx.insert('films', pf)
.then(() => tx.insert('films', rt))
.then(() => tx.insert('films', lb))
.then(() => tx.query('DELETE FROM films WHERE code=\'pulpf\''))
)
.then(() =>
formattedQuery('select-all', ['films'])
.then(({ rows }) => {
const [first, second] = rows;
expect(rows.length).to.equal(2);
expect(first.code).to.equal('royal');
expect(second.code).to.equal('corey');
})
)
);
it('automatically rolls back if an error occurs within a transaction', () =>
withTransaction((tx) =>
tx.insert('films', pf)
.then(() => tx.insert('films', pf))
)
.catch((err) => expect(err.message).to.be('duplicate key value violates unique constraint "firstkey"'))
.then(() =>
formattedQuery('select-all', ['films'])
.then(({ rows }) => {
expect(rows.length).to.equal(0);
})
)
);
});