/
mssql.context.spec.js
335 lines (302 loc) · 9.75 KB
/
mssql.context.spec.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
var config = require('configya')('local-config.json');
var nodemssql = require('mssql');
var sqlContext = require('../src/mssql-context.js');
require('should');
var _ = require('lodash');
var uniqueRowId = 0;
var getConfig = function() {
return {
user: config.get('tdsUserName', 'user'),
password: config.get('tdsPassword', 'password'),
server: config.get('tdsServer', 'localhost'),
database: config.get('tdsDatabase', 'master'),
pool: {
max: 20,
min: 0,
idleTimeoutMillis: 30000
}
};
};
var getContext = function(done) {
return sqlContext.getNewConnectionContext(getConfig(), done);
}
var getTwoParmVals = function(i1Val, v1Val) {
return [{
v1: {
sqlType: nodemssql.NVarChar,
value: v1Val
}
}, {
i1: {
sqlType: nodemssql.Int,
value: i1Val
}
}];
};
before(function(done) {
//create SQL entities for the test methods to use
var beforeContext = getContext(function(err) {
console.log('issuing set single user');
beforeContext.execQuery("if db_id('tds_node_test') is not null ALTER DATABASE [tds_node_test] SET SINGLE_USER WITH ROLLBACK IMMEDIATE",
function(err, records) {
console.log('issuing drop database');
beforeContext.execQuery("if db_id('tds_node_test') is not null drop database tds_node_test",
function(err, records) {
if (err) throw err;
console.log('issuing create database');
beforeContext.execQuery("create database tds_node_test",
function(err, records) {
if (err) throw err;
console.log('issuing create table');
beforeContext.execQuery("create table tds_node_test..NodeTestTable (bi1 bigint not null identity(1,1) primary key, v1 varchar(255), i1 int null)",
function(err, rowCount) {
if (err) throw err;
beforeContext.execQuery("create table tds_node_test..NodeTestTableNoIdent (bi1 bigint not null primary key, v1 varchar(255), i1 int null)",
function(err, rowCount) {
done();
});
});
});
});
});
});
});
describe('SQL transaction ops', function() {
it('should rollback a transaction', function() {
var rowId = ++uniqueRowId;
var inssql = "insert into tds_node_test..NodeTestTable (v1, i1) values (@v1, @i1)";
var selsql = "select * from tds_node_test..NodeTestTable where i1 = @i1";
var ctxTrans = getContext(function(err) {
(err === null).should.be.true;
var ctxNoTrans = getContext(function(err) {
ctxTrans.startTransaction(function(err) {
(err === null).should.be.true;
ctxTrans.execPrepared(inssql, function(err, records) {
(err === null).should.be.true;
ctxTrans.rollback(function(err) {
(err === null).should.be.true;
ctxTrans.execPrepared(selsql, function(err, records) {
if (err) console.log(err);
(err === null).should.be.true;
//verify that no rows were returned
records.length.should.equal(0);
}, [{
i1: {
sqlType: nodemssql.Int,
value: rowId
}
}]);
});
}, getTwoParmVals(rowId, 'fuuuu'));
});
});
});
});
it('should participate in a transaction', function() {
var rowId = ++uniqueRowId;
var inssql = "insert into tds_node_test..NodeTestTable (v1, i1) values (@v1, @i1)";
var selsql = "select * from tds_node_test..NodeTestTable with (readpast) where i1 = @i1";
var ctxTrans = getContext(function(err) {
(err === null).should.be.true;
var ctxNoTrans = getContext(function(err) {
//start a trasaction in one context and insert a row
ctxTrans.startTransaction(function(err) {
(err === null).should.be.true;
ctxTrans.execPrepared(inssql, function(err, records) {
(err === null).should.be.true;
//attempt to read the row in a 2nd context
ctxNoTrans.execPrepared(selsql, function(err, records) {
if (err) console.log(err);
(err === null).should.be.true;
//verify that no rows were returned
records.length.should.equal(0);
//commit the transaction and re-attempt the read
ctxTrans.commit(function(err) {
(err === null).should.be.true;
ctxNoTrans.execPrepared(selsql, function(err, records) {
(err === null).should.be.true;
//verify that the new row is returned
records.length.should.equal(1);
}, [{
i1: {
sqlType: nodemssql.Int,
value: rowId
}
}]);
});
}, [{
i1: {
sqlType: nodemssql.Int,
value: rowId
}
}]);
}, getTwoParmVals(rowId, 'fuuuu'));
});
});
});
});
});
describe('SQL DML ops', function() {
it('should retrieve the identity key of an inserted row', function() {
var rowId = ++uniqueRowId;
var sql = "insert into tds_node_test..NodeTestTable (v1, i1) values (@v1, @i1)";
var context = getContext(function(err) {
context.execPrepared(sql, function(err, records) {
if (err) console.log('sql error', err);
(err === null).should.be.true;
records[0].should.be.ok;
var identity = records[0][''];
identity.should.be.above(0);
}, getTwoParmVals(rowId, 'fuuuu'));
});
});
it('should update a row', function() {
//insert a test row, update it and select to verify
var rowId = ++uniqueRowId;
var sql = "insert into tds_node_test..NodeTestTable (v1, i1) values (@v1, @i1)";
var context = getContext(function(err) {
context.execPrepared(sql, function(err, records) {
if (err) console.log('sql error', err);
(err === null).should.be.true;
var updSql = 'update tds_node_test..NodeTestTable set v1 = @v1 where i1 = @i1';
context.execPrepared(updSql, function(err, records) {
(err === null).should.be.true;
//select row and verify
var selSql = 'select * from tds_node_test..NodeTestTable where i1 = @i1';
context.execPrepared(selSql, function(err, records) {
(err === null).should.be.true;
records.length.should.equal(1);
records[0].v1.should.equal('blah blah');
records[0].i1.should.equal(rowId);
}, [{
i1: {
sqlType: nodemssql.Int,
value: rowId
}
}]);
}, getTwoParmVals(rowId, 'blah blah'));
}, getTwoParmVals(rowId, 'fuuuu'));
});
});
it('should insert a row', function() {
//insert a test row and select to verify
var rowId = ++uniqueRowId;
var sql = "insert into tds_node_test..NodeTestTableNoIdent (bi1, v1, i1) values (55, @v1, @i1)";
var context = getContext(function(err) {
context.execPrepared(sql, function(err, records) {
if (err) console.log('sql error', err);
(err === null).should.be.true;
//select row and verify
var selSql = 'select * from tds_node_test..NodeTestTableNoIdent where i1 = @i1';
context.execPrepared(selSql, function(err, records) {
(err === null).should.be.true;
records.length.should.equal(1);
records[0].v1.should.equal('fuuuu');
records[0].i1.should.equal(rowId);
}, [{
i1: {
sqlType: nodemssql.Int,
value: rowId
}
}]);
}, getTwoParmVals(rowId, 'fuuuu'));
});
});
});
describe('prepared statement ops', function() {
it('should execute prepared with single param', function() {
var sql = "select * from sys.tables where type_desc = @usertable";
var context = getContext(function(err) {
context.execPrepared(sql, function(err, records) {
if (err) console.log('sql error', err);
(err === null).should.be.true;
records.length.should.be.above(0);
_.forEach(records, function(record) {
record.object_id.should.be.above(0);
});
}, [{
usertable: {
sqlType: nodemssql.NVarChar,
value: 'USER_TABLE'
}
}]);
});
});
it('should execute prepared with multiple params', function() {
var sql = "select * from sys.tables where type_desc = @usertable and is_ms_shipped = @mss";
var context = getContext(function(err) {
context.execPrepared(sql, function(err, records) {
if (err) console.log('sql error', err);
(err === null).should.be.true;
records.length.should.be.above(0);
_.forEach(records, function(record) {
record.object_id.should.be.above(0);
});
}, [{
usertable: {
sqlType: nodemssql.NVarChar,
value: 'USER_TABLE'
}
}, {
mss: {
sqlType: nodemssql.Bit,
value: true
}
}]);
});
});
});
describe('basic db select ops', function() {
it('should exec no param query', function() {
var sql = "select 40 + 2 as answer, 'hello world' as greeting";
var context = getContext(function(err) {
context.execQuery(sql, function(err, records) {
if (err) console.log('sql error', err);
(err === null).should.be.true;
records.length.should.be.equal(1);
records[0].answer.should.equal(42);
records[0].greeting.should.equal('hello world');
});
});
});
it('should exec query with one param', function() {
var sql = "select * from sys.tables where type_desc = @usertable";
var context = getContext(function(err) {
context.execQuery(sql, function(err, records) {
if (err) console.log('sql error', err);
(err === null).should.be.true;
records.length.should.be.above(0);
_.forEach(records, function(record) {
record.object_id.should.be.above(0);
});
}, [{
usertable: {
sqlType: nodemssql.NVarChar,
value: 'USER_TABLE'
}
}]);
});
});
it('should exec query with multiple params', function() {
var sql = "select * from sys.tables where type_desc = @usertable and is_ms_shipped = @mss";
var context = getContext(function(err) {
context.execQuery(sql, function(err, records) {
if (err) console.log('sql error', err);
(err === null).should.be.true;
records.length.should.be.above(0);
_.forEach(records, function(record) {
record.object_id.should.be.above(0);
});
}, [{
usertable: {
value: 'USER_TABLE'
}
}, {
mss: {
sqlType: nodemssql.Bit,
value: true
}
}]);
});
});
});