This repository has been archived by the owner on Dec 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
80 lines (70 loc) · 2.8 KB
/
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
'use strict';
import { assert } from 'chai';
import AnySQLMySQL from './src';
async function catchError(fn) {
let err;
try {
await fn();
} catch (e) {
err = e;
}
return err;
}
describe('anysql-mysql', function() {
let mysql;
before(function() {
mysql = new AnySQLMySQL('mysql://test@localhost/test');
});
it('should perform a simple operation', async function() {
let rows = await mysql.query('SELECT ? + ? AS solution', [2, 3]);
assert.strictEqual(rows[0].solution, 5);
});
describe('transactions', function() {
before(async function() {
await mysql.query('CREATE TABLE people (name TEXT, age INTEGER)');
});
it('should commit the transaction when no error occurs', async function() {
await mysql.query('INSERT INTO people (name, age) VALUES (?, ?)', ['Jean Dupont', 33]);
let rows, person;
await mysql.transaction(async function(mysql) {
rows = await mysql.query('SELECT * FROM people WHERE name=?', 'Jean Dupont');
person = rows[0];
assert.deepEqual(person, { name: 'Jean Dupont', age: 33 });
await mysql.query('UPDATE people SET age=? WHERE name=?', [person.age + 1, person.name]);
rows = await mysql.query('SELECT * FROM people WHERE name=?', 'Jean Dupont');
person = rows[0];
assert.deepEqual(person, { name: 'Jean Dupont', age: 34 });
});
rows = await mysql.query('SELECT * FROM people WHERE name=?', 'Jean Dupont');
person = rows[0];
assert.deepEqual(person, { name: 'Jean Dupont', age: 34 });
});
it('should rollback the transaction when an error occurs', async function() {
await mysql.query('INSERT INTO people (name, age) VALUES (?, ?)', ['Pierre Durand', 23]);
let rows, person;
let err = await catchError(async function() {
await mysql.transaction(async function(mysql) {
rows = await mysql.query('SELECT * FROM people WHERE name=?', 'Pierre Durand');
person = rows[0];
assert.deepEqual(person, { name: 'Pierre Durand', age: 23 });
await mysql.query('UPDATE people SET age=? WHERE name=?', [person.age + 1, person.name]);
rows = await mysql.query('SELECT * FROM people WHERE name=?', 'Pierre Durand');
person = rows[0];
assert.deepEqual(person, { name: 'Pierre Durand', age: 24 });
throw new Error('something is wrong');
});
});
assert.instanceOf(err, Error);
assert.equal(err.message, 'something is wrong');
rows = await mysql.query('SELECT * FROM people WHERE name=?', 'Pierre Durand');
person = rows[0];
assert.deepEqual(person, { name: 'Pierre Durand', age: 23 });
});
after(async function() {
await mysql.query('DROP TABLE people');
});
});
after(async function() {
await mysql.close();
});
});