-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.test.js
executable file
·68 lines (50 loc) · 1.83 KB
/
query.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
const _db = require('../mysql-magic');
const _assert = require('assert');
// generic checksum testing
describe('query', function(){
// prepare pool
before(async function(){
// somewhere during your applications bootstrap...initialize a custom pool
_db.initPool('userdb', {
host : 'localhost',
user : 'dev',
password : 'dev',
database : 'demo'
});
// default pool
_db.initPool(null, {
host : 'localhost',
user : 'dev',
password : 'dev',
database : 'demo'
});
});
it('should execute a query with 20 results', async function(){
// get pool
const pool = _db.getPool('userdb');
// retreive connection
const con = await pool.getConnection();
// run query
const [result, fields] = await con.query('SELECT * FROM users LIMIT 20;');
con.release();
_assert.equal(result.length, 20);
});
it('should execute a query with 1 result within connection scope', async function(){
// retrieve connection scope
const result = await _db.getConnection('userdb', async function(){
// run query
const [rows, fields] = await this.query('SELECT COUNT(*) as num FROM users;');
return rows[0].num;
});
_assert.equal(result, 100);
});
it('should execute a query within connection scope using default pool', async function(){
// retrieve connection scope
const result = await _db.getConnection(async function(){
// run query
const [rows, fields] = await this.query('SELECT COUNT(*) as num FROM users;');
return rows[0].num;
});
_assert.equal(result, 100);
});
});