-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
query.test.js
88 lines (72 loc) · 3.61 KB
/
query.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
'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', () => {
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);
});
afterEach(() => {
sandbox.restore();
});
describe('beginTransaction', () => {
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);
});
});
});
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);
});
});
describe('getSQLTypeFromJsType', () => {
const TYPES = tedious.TYPES;
it('should return correct parameter type', () => {
expect(query.getSQLTypeFromJsType(2147483647, TYPES)).to.eql({ type: TYPES.Int, typeOptions: {} });
expect(query.getSQLTypeFromJsType(-2147483648, TYPES)).to.eql({ type: TYPES.Int, typeOptions: {} });
expect(query.getSQLTypeFromJsType(2147483648, TYPES)).to.eql({ type: TYPES.BigInt, typeOptions: {} });
expect(query.getSQLTypeFromJsType(-2147483649, TYPES)).to.eql({ type: TYPES.BigInt, typeOptions: {} });
expect(query.getSQLTypeFromJsType(Buffer.from('abc'), TYPES)).to.eql({ type: TYPES.VarBinary, typeOptions: {} });
});
it('should return parameter type correct scale for float', () => {
expect(query.getSQLTypeFromJsType(1.23, TYPES)).to.eql({ type: TYPES.Numeric, typeOptions: { precision: 30, scale: 2 } });
expect(query.getSQLTypeFromJsType(0.30000000000000004, TYPES)).to.eql({ type: TYPES.Numeric, typeOptions: { precision: 30, scale: 17 } });
expect(query.getSQLTypeFromJsType(2.5e-15, TYPES)).to.eql({ type: TYPES.Numeric, typeOptions: { precision: 30, scale: 16 } });
});
});
});
}