-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathprepared.test.js
More file actions
executable file
·236 lines (208 loc) · 7.59 KB
/
Copy pathprepared.test.js
File metadata and controls
executable file
·236 lines (208 loc) · 7.59 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
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
'use strict'
/* globals describe it */
const { TestEnv } = require('./env/test-env')
const env = new TestEnv()
const chai = require('chai')
const assert = chai.assert
const expect = chai.expect
// Enable trace-level logging for debugging test failures
const sql = require('../lib/sql')
const { configureTestLogging } = require('./common/logging-helper')
// Configure logging based on environment variables
// By default, tests run silently. To enable logging:
// - MSNODESQLV8_TEST_VERBOSE=true npm test (for full trace logging)
// - MSNODESQLV8_TEST_LOG_LEVEL=DEBUG MSNODESQLV8_TEST_LOG_CONSOLE=true npm test
// - MSNODESQLV8_TEST_LOG_LEVEL=INFO MSNODESQLV8_TEST_LOG_FILE=/tmp/test.log npm test
configureTestLogging(sql)
describe('prepared', function () {
const tableName = 'employee'
let theConnection
let prepared
let parsedJSON
this.timeout(100 * 1000)
async function bootup () {
parsedJSON = env.helper.getJSON()
theConnection = env.theConnection
await env.promisedDropCreateTable({
tableName
})
const bulkMgr = await theConnection.promises.getTable(tableName)
await bulkMgr.promises.insert(parsedJSON)
prepared = await env.employee.prepare()
}
this.beforeEach(async function handler () {
await env.open()
prepared = {
update: null,
select: null,
delete: null,
scan: null
}
await bootup()
})
this.afterEach(done => {
env.employee.free(prepared)
.then(async () => env.close())
.then(() => { done() }).catch((err) => { done(err) })
})
it('use prepared statement with params returning 0 rows. - expect no error', async function handler () {
const select = prepared.select
const meta = select.getMeta()
const id1 = -1
assert.isTrue(meta.length > 0)
const res = await select.promises.query([id1])
assert(res != null)
assert.deepStrictEqual(res.first.length, 0)
})
describe('default max prepared size', () => {
it('use prepared and select nvarchar max with max default size on connection', async function handler () {
if (env.isEncryptedConnection()) return
const s = 'hello'
const max = 4
const q = `DECLARE @v NVARCHAR(MAX) = '${s}'; SELECT @v AS v`
env.theConnection.setMaxPreparedColumnSize(max)
const prepared = await env.theConnection.promises.prepare(q)
const res = await prepared.promises.query([])
assert.deepStrictEqual(res.first[0].v, s.slice(0, 4))
await prepared.promises.free()
})
it('use prepared and select nvarchar max with max default size on query', async function handler () {
if (env.isEncryptedConnection()) return
const s = 'hello'
const max = 4
const q = {
query_str: `DECLARE @v NVARCHAR(MAX) = '${s}'; SELECT @v AS v`,
max_prepared_column_size: max
}
const prepared = await env.theConnection.promises.prepare(q)
const res = await prepared.promises.query([])
assert.deepStrictEqual(res.first[0].v, s.slice(0, 4))
await prepared.promises.free()
})
})
it('use prepared and select nvarchar(max)', async function handler () {
if (env.isEncryptedConnection()) return
const s = 'hello'
const q = `DECLARE @v NVARCHAR(MAX) = '${s}'; SELECT @v AS v`
const prepared = await env.theConnection.promises.prepare(q)
const res = await prepared.promises.query([])
assert.deepStrictEqual(res.first[0].v, s)
await prepared.promises.free()
})
it('use prepared to reserve and read multiple rows.', async function handler () {
const sql = 'select top 5 * from master..syscomments'
const pq = await theConnection.promises.prepare(sql)
const res = await pq.promises.query([])
assert(res != null)
assert(res.first.length > 0)
await pq.promises.free()
})
it('use prepared to select 0 rows - expect no error (await promise)', async function handler () {
const sql = 'select * from master..syscomments where 1=0'
const preparedQuery = await theConnection.promises.prepare(sql)
const results = await preparedQuery.promises.query([])
assert(results != null)
assert(results.first.length === 0)
await preparedQuery.promises.free()
return null
})
it('use prepared to select 0 rows - expect no error (promise then)', testDone => {
const sql = 'select * from master..syscomments where 1=0'
theConnection.promises.prepare(sql)
.then(preparedQuery => {
preparedQuery.promises.query([])
.then(results => {
assert(results != null)
assert(results.first.length === 0)
preparedQuery.promises.free()
.then(() => {
testDone()
}).catch(err => {
testDone(err)
})
}).catch(err => {
testDone(err)
})
}).catch(err => {
testDone(err)
})
})
it('use prepared statement with params updating 0 rows - expect no error', async function handler () {
const update = prepared.update
const meta = update.getMeta()
const id1 = -1
assert(meta.length === 0)
await update.promises.query(['login1', id1])
})
it('use prepared statement twice with no parameters.', testDone => {
const select = prepared.scan
const meta = select.getMeta()
assert(meta.length > 0)
select.preparedQuery((err, res1) => {
assert.ifError(err)
assert.deepStrictEqual(parsedJSON, res1, 'results didn\'t match')
select.preparedQuery((err, res2) => {
assert.ifError(err)
assert.deepStrictEqual(parsedJSON, res2, 'results didn\'t match')
testDone()
})
})
})
it('use prepared statements to select a row, then delete it over each row.', testDone => {
const select = prepared.select
const meta = select.getMeta()
assert(meta.length > 0)
const remove = prepared.delete
const max = parsedJSON[parsedJSON.length - 1].BusinessEntityID
let businessId = 1
next(businessId, iterate)
function iterate () {
businessId++
if (businessId > max) check()
else next(businessId, iterate)
}
function check () {
theConnection.query('select count(*) as rows from Employee', (err, res) => {
assert.ifError(err)
assert(res[0].rows === 0)
testDone()
})
}
function next (businessId, done) {
select.preparedQuery([businessId], (err, res1) => {
assert.ifError(err)
const fetched = parsedJSON[businessId - 1]
assert.deepStrictEqual(fetched, res1[0], 'results didn\'t match')
remove.preparedQuery([businessId], (err) => {
assert.ifError(err)
done()
})
})
}
})
it('stress test prepared statement with 500 invocations cycling through primary key', async function handler () {
const select = prepared.select
const meta = select.getMeta()
assert(meta.length > 0)
const totalIterations = 500
const max = parsedJSON[parsedJSON.length - 1].BusinessEntityID
for (let i = 0; i < totalIterations; ++i) {
const businessId = i % max + 1
const res = await select.promises.query([businessId])
expect(res.first[0]).to.deep.equal(parsedJSON[businessId - 1])
}
})
it('use prepared statement twice with different params.', async function handler () {
const select = prepared.select
const meta = select.getMeta()
const id1 = 2
const id2 = 3
assert(meta.length > 0)
const res1 = await select.promises.query([id1])
const res2 = await select.promises.query([id2])
const o1 = parsedJSON[id1 - 1]
const o2 = parsedJSON[id2 - 1]
expect(res1.first[0]).to.deep.equal(o1)
expect(res2.first[0]).to.deep.equal(o2)
})
})