-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
284 lines (173 loc) · 7.89 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
const Wrapsql = require('./wrapsql.js')
const mysql = require('mysql')
const expect = require('chai').expect;
// const sysConfig = {
// dbHost:'127.0.0.1',
// dbPort:8889,
// dbUsername:'',
// dbPassword:'',
// }
const sysConfig = require('./config-test.js')
describe('WrapSQL Unit Tests', async function() {
if ( sysConfig.dbUsername == "" || sysConfig.dbPassword == " " ){
console.log("Username and password must be set for 'unitTestDb'")
process.exit()
}
const sql = mysql.createConnection({
host: sysConfig.dbHost,
port: sysConfig.dbPort,
user: sysConfig.dbUsername,
password: sysConfig.dbPassword,
database: 'unitTestDb'
})
const wsql = new Wrapsql(sql,true)
describe('connection', async function() {
it('Should connect to db without error.', async function() {
try{
await wsql.connect()
} catch(e){
console.log(e)
expect()
}
})
})
describe('selectAll', async function() {
it('Should return all of test table 1', async function() {
let result = await wsql.selectAll('testTable')
console.log( result )
expect(result[0].value).to.equal("testRow1")
})
it('Should return error as table does not exist.', async function() {
try{
await wsql.selectAll('testTablesdf')
} catch(e){
expect()
}
})
})
describe('addOptions', async function() {
it(`Should create valid sql string with with default 'AND' comparison options.`, async function() {
let result = wsql.addOptions({user:"bill",pass:"test",id:1},'id','DESC',10,1)
expect(result).to.equal(" WHERE user = 'bill' AND pass = 'test' AND id = 1 ORDER BY id DESC LIMIT 10 OFFSET 1")
})
it(`Should create valid sql string with 'OR' comparison and options.`, async function() {
let result = wsql.addOptions(["or",{user:"bill",pass:"test",id:1}],'id','DESC',10,1)
expect(result).to.equal(" WHERE user = 'bill' OR pass = 'test' OR id = 1 ORDER BY id DESC LIMIT 10 OFFSET 1")
})
it(`Should create valid sql string with 'IN' comparison and options.`, async function() {
let result = wsql.addOptions(["IN",{user:[1,"bill","bill's"]}],'id','DESC',10,1)
expect(result).to.equal(" WHERE user IN (1,'bill','bill''s') ORDER BY id DESC LIMIT 10 OFFSET 1")
})
it(`Should create valid sql string with with default 'AND' comparison and customer '>' and '<' operators options.`, async function() {
let result = wsql.addOptions({user:[">","bill"],pass:["<","test"],id:1},'id','DESC',10,1)
expect(result).to.equal(" WHERE user > 'bill' AND pass < 'test' AND id = 1 ORDER BY id DESC LIMIT 10 OFFSET 1")
})
it(`Should return customer 'where' string.`, async function() {
let result = wsql.addOptions('user=bob AND dog IS NOT null AND black != white','id','DESC',10,1)
expect(result).to.equal(" WHERE user=bob AND dog IS NOT null AND black != white ORDER BY id DESC LIMIT 10 OFFSET 1")
})
})
describe('select', async function() {
it('Should return testRow2', async function() {
let result = await wsql.select('testTable','value',{value:"testRow2"},orderBy=false,"DESC",10,offset=false)
expect(result[0].value).to.equal("testRow2")
})
it('Should return testRow2 as first result.', async function() {
let result = await wsql.select('testTable','value',false,orderBy='id',"DESC",10,offset=false,"id")
expect(result[0].value).to.equal("testRow2")
})
it('Should return testRow2 using limit and offset.', async function() {
let result = await wsql.select('testTable','value',false,'id',"DESC",1,0)
expect(result[0].value).to.equal("testRow2")
})
it(`Should return testRow2 using '>' operator.`, async function() {
let result = await wsql.select('testTable','value',{id:[">",1]},'id',"DESC",1,0)
expect(result[0].value).to.equal("testRow2")
})
it(`Should return testRow2 using 'IN' comparison.`, async function() {
let result = await wsql.select('testTable','value',['IN',{id:[2]}],'id',"DESC",1,0)
expect(result[0].value).to.equal("testRow2")
})
})
describe('insert', async function() {
it('Should insert new value into insert table.', async function() {
let result = await wsql.insert('insertTest',{value:"testInsert"})
expect(result.affectedRows).to.equal(1)
})
it('Should insert a numeric value into the table.', async function() {
let result = await wsql.insert('insertTest',{value:1})
expect(result.affectedRows).to.equal(1)
})
it('Should insert multiple values into insert table.', async function() {
let result = await wsql.insert('insertTest',[{value:"testInsert2"},{value:"testInsert3"}])
expect(result.affectedRows).to.equal(2)
})
it(`Should insert values into insert table that contain "'" and escape them correctly.`, async function() {
let insertValue = "te'st''In'se'rt"
let insertResult = await wsql.insert('insertTest',{value:insertValue})
let selectResult = await wsql.select('insertTest','value',{value:insertValue})
expect(selectResult[0].value).to.equal(insertValue)
})
})
describe('update', async function() {
it('Should update first record in insert table.', async function() {
let result = await wsql.update('insertTest',{value:'updated'},{id:1})
expect(result.affectedRows).to.equal(1)
})
it('Should update 2nd and 3rd record in insert table.', async function() {
let result = await wsql.update('insertTest',{value:'updated'},["IN",{id:[2,3]}])
expect(result.affectedRows).to.equal(2)
})
})
describe('delete', async function() {
it('Should delete record.', async function() {
let result = await wsql.delete('insertTest',{value:'updated'})
expect(result.affectedRows).to.equal(3)
})
})
describe('count', async function() {
it('Should count number of records.', async function() {
let result = await wsql.count('testTable',{value:'testRow1'},'theCount')
console.log(result)
expect(result[0].theCount).to.equal(1)
})
})
describe('transaction', async function() {
it('Should select records twice.', async function() {
let queries = [
"SELECT * FROM testTable ORDER BY id DESC",
"SELECT * FROM testTable ORDER BY id ASC",
]
let result = await wsql.transaction(queries)
expect(result[0][0].id).to.equal(3)
expect(result[1][0].id).to.equal(1)
})
it('Should reject because of invalid sql.', async function() {
let queries = [
"SELECT * FROM testTable ORDER BY id DESC",
"Blaa",
]
try{
await wsql.transaction(queries)
}catch(e){
expect()
}
})
})
describe('constructor', async function() {
it('Should build mySQL connection inside class', async function() {
const wsql2 = new Wrapsql({
host: sysConfig.dbHost,
port: sysConfig.dbPort,
user: sysConfig.dbUsername,
password: sysConfig.dbPassword,
database: 'unitTestDb'
},true)
let result = await wsql2.select('testTable','value',{value:"testRow2"},orderBy=false,"DESC",10,offset=false)
expect(result[0].value).to.equal("testRow2")
})
})
after(async function() {
await wsql.truncate("insertTest")
})
})