connection-manager.test.js
3.03 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
'use strict';
const chai = require('chai'),
sinon = require('sinon'),
expect = chai.expect,
Support = require('./support'),
ConnectionManager = require('../../lib/dialects/abstract/connection-manager');
describe('connection manager', () => {
describe('_connect', () => {
beforeEach(function() {
this.connection = {};
this.dialect = {
connectionManager: {
connect: sinon.stub().resolves(this.connection)
}
};
this.sequelize = Support.createSequelizeInstance();
});
it('should resolve connection on dialect connection manager', async function() {
const connection = {};
this.dialect.connectionManager.connect.resolves(connection);
const connectionManager = new ConnectionManager(this.dialect, this.sequelize);
const config = {};
await expect(connectionManager._connect(config)).to.eventually.equal(connection);
expect(this.dialect.connectionManager.connect).to.have.been.calledWith(config);
});
it('should let beforeConnect hook modify config', async function() {
const username = Math.random().toString(),
password = Math.random().toString();
this.sequelize.beforeConnect(config => {
config.username = username;
config.password = password;
return config;
});
const connectionManager = new ConnectionManager(this.dialect, this.sequelize);
await connectionManager._connect({});
expect(this.dialect.connectionManager.connect).to.have.been.calledWith({
username,
password
});
});
it('should call afterConnect', async function() {
const spy = sinon.spy();
this.sequelize.afterConnect(spy);
const connectionManager = new ConnectionManager(this.dialect, this.sequelize);
await connectionManager._connect({});
expect(spy.callCount).to.equal(1);
expect(spy.firstCall.args[0]).to.equal(this.connection);
expect(spy.firstCall.args[1]).to.eql({});
});
});
describe('_disconnect', () => {
beforeEach(function() {
this.connection = {};
this.dialect = {
connectionManager: {
disconnect: sinon.stub().resolves(this.connection)
}
};
this.sequelize = Support.createSequelizeInstance();
});
it('should call beforeDisconnect', async function() {
const spy = sinon.spy();
this.sequelize.beforeDisconnect(spy);
const connectionManager = new ConnectionManager(this.dialect, this.sequelize);
await connectionManager._disconnect(this.connection);
expect(spy.callCount).to.equal(1);
expect(spy.firstCall.args[0]).to.equal(this.connection);
});
it('should call afterDisconnect', async function() {
const spy = sinon.spy();
this.sequelize.afterDisconnect(spy);
const connectionManager = new ConnectionManager(this.dialect, this.sequelize);
await connectionManager._disconnect(this.connection);
expect(spy.callCount).to.equal(1);
expect(spy.firstCall.args[0]).to.equal(this.connection);
});
});
});