query.test.js
2.44 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
'use strict';
const path = require('path');
const Query = require(path.resolve('./lib/dialects/mssql/query.js'));
const Support = require('../../support');
const dialect = Support.getTestDialect();
const sequelize = Support.sequelize;
const sinon = require('sinon');
const expect = require('chai').expect;
const tedious = require('tedious');
const tediousIsolationLevel = tedious.ISOLATION_LEVEL;
const connectionStub = { beginTransaction: () => {}, lib: tedious };
let sandbox, query;
if (dialect === 'mssql') {
describe('[MSSQL Specific] Query', () => {
describe('beginTransaction', () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
const options = {
transaction: { name: 'transactionName' },
isolationLevel: 'REPEATABLE_READ',
logging: false
};
sandbox.stub(connectionStub, 'beginTransaction').callsArg(0);
query = new Query(connectionStub, sequelize, options);
});
it('should call beginTransaction with correct arguments', () => {
return query._run(connectionStub, 'BEGIN TRANSACTION')
.then(() => {
expect(connectionStub.beginTransaction.called).to.equal(true);
expect(connectionStub.beginTransaction.args[0][1]).to.equal('transactionName');
expect(connectionStub.beginTransaction.args[0][2]).to.equal(tediousIsolationLevel.REPEATABLE_READ);
});
});
afterEach(() => {
sandbox.restore();
});
});
describe('formatBindParameters', () => {
it('should convert Sequelize named binding format to MSSQL format', () => {
const sql = 'select $one as a, $two as b, $one as c, $three as d, $one as e';
const values = { one: 1, two: 2, three: 3 };
const expected = 'select @one as a, @two as b, @one as c, @three as d, @one as e';
const result = Query.formatBindParameters(sql, values, dialect);
expect(result[0]).to.be.a('string');
expect(result[0]).to.equal(expected);
});
it('should convert Sequelize numbered binding format to MSSQL format', () => {
const sql = 'select $1 as a, $2 as b, $1 as c, $3 as d, $1 as e';
const values = [1, 2, 3];
const expected = 'select @0 as a, @1 as b, @0 as c, @2 as d, @0 as e';
const result = Query.formatBindParameters(sql, values, dialect);
expect(result[0]).to.be.a('string');
expect(result[0]).to.equal(expected);
});
});
});
}