connection-manager.test.js
2.37 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
'use strict';
/* jshint -W030 */
var chai = require('chai')
, sinon = require('sinon')
, expect = chai.expect
, Support = require(__dirname + '/support')
, Sequelize = require(__dirname + '/../../index')
, ConnectionManager = require(__dirname + '/../../lib/dialects/abstract/connection-manager')
, Promise = Sequelize.Promise;
describe('connection manager', function () {
describe('$connect', function () {
beforeEach(function () {
this.sinon = sinon.sandbox.create();
this.connection = {};
this.dialect = {
connectionManager: {
connect: this.sinon.stub().returns(Promise.resolve(this.connection))
}
};
this.sequelize = Support.createSequelizeInstance();
});
afterEach(function () {
this.sinon.restore();
});
it('should resolve connection on dialect connection manager', function () {
var connection = {};
this.dialect.connectionManager.connect.returns(Promise.resolve(connection));
var connectionManager = new ConnectionManager(this.dialect, this.sequelize);
var config = {};
return expect(connectionManager.$connect(config)).to.eventually.equal(connection).then(function () {
expect(this.dialect.connectionManager.connect).to.have.been.calledWith(config);
}.bind(this));
});
it('should let beforeConnect hook modify config', function () {
var username = Math.random().toString()
, password = Math.random().toString();
this.sequelize.beforeConnect(function (config) {
config.username = username;
config.password = password;
return config;
});
var connectionManager = new ConnectionManager(this.dialect, this.sequelize);
return connectionManager.$connect({}).then(function () {
expect(this.dialect.connectionManager.connect).to.have.been.calledWith({
username: username,
password: password
});
}.bind(this));
});
it('should call afterConnect', function() {
const spy = sinon.spy();
this.sequelize.afterConnect(spy);
var connectionManager = new ConnectionManager(this.dialect, this.sequelize);
return connectionManager.$connect({}).then(() => {
expect(spy.callCount).to.equal(1);
expect(spy.firstCall.args[0]).to.equal(this.connection);
expect(spy.firstCall.args[1]).to.eql({});
});
});
});
});