-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmysql.test.js
More file actions
94 lines (80 loc) · 2.56 KB
/
mysql.test.js
File metadata and controls
94 lines (80 loc) · 2.56 KB
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
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import Mysql from './mysql.js'
describe('mysql', () => {
let dbh
it('connects', async () => {
dbh = await Mysql.connect()
assert.ok(dbh.connection.connectionId)
})
if (process.env.NODE_ENV === 'cov') {
it('is noisy when debug=true', async () => {
Mysql.debug(true)
await Mysql.execute(`SHOW DATABASES`)
await Mysql.select(`SELECT * FROM nt_group`)
Mysql.debug(false)
})
}
it('formats SELECT queries', () => {
assert.deepEqual(
Mysql.select(`SELECT * FROM nt_user`, {
last_name: 'Test',
}),
[`SELECT * FROM nt_user WHERE last_name=?`, ['Test']],
)
})
it('formats INSERT query', () => {
const r = Mysql.insert(`nt_user`, {
first_name: 'uNite',
last_name: 'Test',
})
assert.deepEqual(r, [`INSERT INTO nt_user (first_name,last_name) VALUES(?,?)`, ['uNite', 'Test']])
})
describe('update', () => {
it('formats with one value', () => {
const r = Mysql.update(`nt_user`, `nt_user_id=4096`, {
first_name: 'uNite',
})
assert.deepEqual(r, [`UPDATE nt_user SET first_name=? WHERE nt_user_id=4096`, ['uNite']])
})
it('formats with two values', () => {
const r = Mysql.update(`nt_user`, `nt_user_id=4096`, {
last_name: 'Teste',
is_admin: 1,
})
assert.deepEqual(r, [`UPDATE nt_user SET last_name=?,is_admin=? WHERE nt_user_id=4096`, ['Teste', 1]])
})
it('formats with three values', () => {
const r = Mysql.update(`nt_user`, `nt_user_id=4096`, {
first_name: 'Unit',
last_name: 'Test',
is_admin: 0,
})
assert.deepEqual(r, [
`UPDATE nt_user SET first_name=?,last_name=?,is_admin=? WHERE nt_user_id=4096`,
['Unit', 'Test', 0],
])
})
})
describe('delete', () => {
it('no params', () => {
assert.deepEqual(Mysql.delete(`nt_user`, {}), [`DELETE FROM nt_user`, []])
})
it('with params', () => {
assert.deepEqual(Mysql.delete(`nt_user`, { last_name: 'Test' }), [
`DELETE FROM nt_user WHERE last_name=?`,
['Test'],
])
})
})
it('executes formatted queries', async () => {
const [query, argsArray] = Mysql.select(`SELECT * FROM nt_options`)
const r = await Mysql.execute(query, argsArray)
assert.deepEqual(r[0].option_id, 1)
// await Mysql.execute(...Mysql.select(`SELECT * FROM nt_options`))
})
it('disconnects', async () => {
assert.ok(dbh.connection.connectionId)
await Mysql.disconnect(dbh)
})
})