-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
89 lines (78 loc) · 2.38 KB
/
index.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
const openDatabase = require('./index');
const assert = require('assert').strict;
const it = function() {
return process['isBun'] ? test : require('node:test').it;
}();
//given
const db = openDatabase(":memory:", '1.0', 'description', 1);
it('should write to database', async () => {
//when
const result = await new Promise((resolve, reject) => {
db.transaction((tx) => {
tx.executeSql(`
create table test(
id integer primary key,
name text not null
);
`, [], (tx, r) => {
const insertSql = "insert into test (name) values (?);"
tx.executeSql(insertSql, ["test1"], (tx, r) => {
tx.executeSql(insertSql, ["test2"], (tx, r) => {
resolve(r);
}, (tx, e) => {
reject(e);
});
}, (tx, e) => {
reject(e);
});
}, (tx, e) => {
reject(e);
});
});
});
//then
assert.deepEqual(result.insertId, 2);
assert.deepEqual(result.rowsAffected, 1);
assert.deepEqual(result.rows._array, []);
});
it('should read all records from database', async () => {
//when
const result = await new Promise((resolve, reject) => {
db.readTransaction((tx) => {
tx.executeSql("select * from test order by id;", [], (tx, r) => {
resolve(r);
}, (tx, e) => {
reject(e);
});
});
});
//then
assert.deepEqual(result.insertId, undefined);
assert.deepEqual(result.rowsAffected, 0);
assert.deepEqual(result.rows._array, [{
id: 1,
name: "test1"
}, {
id: 2,
name: "test2"
}]);
});
it('should read record by id from database', async () => {
//when
const result = await new Promise((resolve, reject) => {
db.readTransaction((tx) => {
tx.executeSql("select * from test where id=?;", [2], (tx, r) => {
resolve(r);
}, (tx, e) => {
reject(e);
});
});
});
//then
assert.deepEqual(result.insertId, undefined);
assert.deepEqual(result.rowsAffected, 0);
assert.deepEqual(result.rows._array, [{
id: 2,
name: "test2"
}]);
});