configuration.test.js
8.72 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
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
'use strict';
/* jshint -W030 */
var chai = require('chai')
, expect = chai.expect
, config = require(__dirname + '/../config/config')
, Support = require(__dirname + '/support')
, dialect = Support.getTestDialect()
, Sequelize = Support.Sequelize
, fs = require('fs')
, path = require('path');
describe(Support.getTestDialectTeaser('Configuration'), function() {
describe('Connections problems should fail with a nice message', function() {
it('when we don\'t have the correct server details', function() {
if (dialect === 'mariadb') {
console.log('This dialect doesn\'t support me :(');
expect(true).to.be.true; // Silence Buster
return;
}
var seq = new Sequelize(config[dialect].database, config[dialect].username, config[dialect].password, {storage: '/path/to/no/where/land', logging: false, host: '0.0.0.1', port: config[dialect].port, dialect: dialect});
if (dialect === 'sqlite') {
// SQLite doesn't have a breakdown of error codes, so we are unable to discern between the different types of errors.
return expect(seq.query('select 1 as hello')).to.eventually.be.rejectedWith(seq.ConnectionError, 'SQLITE_CANTOPEN: unable to open database file');
} else {
return expect(seq.query('select 1 as hello')).to.eventually.be.rejectedWith([seq.HostNotReachableError, seq.InvalidConnectionError]);
}
});
it('when we don\'t have the correct login information', function() {
if (dialect === 'mariadb') {
console.log('This dialect doesn\'t support me :(');
expect(true).to.be.true; // Silence Buster
return;
}
if (dialect === 'mssql') {
// NOTE: Travis seems to be having trouble with this test against the
// AWS instance. Works perfectly fine on a local setup.
expect(true).to.be.true;
return;
}
var seq = new Sequelize(config[dialect].database, config[dialect].username, 'fakepass123', {logging: false, host: config[dialect].host, port: 1, dialect: dialect});
if (dialect === 'sqlite') {
// SQLite doesn't require authentication and `select 1 as hello` is a valid query, so this should be fulfilled not rejected for it.
return expect(seq.query('select 1 as hello')).to.eventually.be.fulfilled;
} else {
return expect(seq.query('select 1 as hello')).to.eventually.be.rejectedWith(seq.ConnectionRefusedError, 'connect ECONNREFUSED');
}
});
it('when we don\'t have a valid dialect.', function() {
expect(function() {
new Sequelize(config[dialect].database, config[dialect].username, config[dialect].password, {host: '0.0.0.1', port: config[dialect].port, dialect: undefined});
}).to.throw(Error, 'The dialect undefined is not supported. Supported dialects: mariadb, mssql, mysql, postgres, and sqlite.');
});
});
describe('Instantiation with a URL string', function() {
it('should accept username, password, host, port, and database', function() {
var sequelize = new Sequelize('mysql://user:pass@example.com:9821/dbname');
var config = sequelize.config;
var options = sequelize.options;
expect(options.dialect).to.equal('mysql');
expect(config.database).to.equal('dbname');
expect(config.host).to.equal('example.com');
expect(config.username).to.equal('user');
expect(config.password).to.equal('pass');
expect(config.port).to.equal('9821');
});
it('should work with no authentication options', function() {
var sequelize = new Sequelize('mysql://example.com:9821/dbname');
var config = sequelize.config;
expect(config.username).to.not.be.ok;
expect(config.password).to.be.null;
});
it('should work with no authentication options and passing additional options', function() {
var sequelize = new Sequelize('mysql://example.com:9821/dbname', {});
var config = sequelize.config;
expect(config.username).to.not.be.ok;
expect(config.password).to.be.null;
});
it('should use the default port when no other is specified', function() {
var sequelize = new Sequelize('dbname', 'root', 'pass', {
dialect: dialect
})
, config = sequelize.config
, port;
if (Support.dialectIsMySQL()) {
port = 3306;
} else if (dialect === 'postgres' || dialect === 'postgres-native') {
port = 5432;
} else {
// sqlite has no concept of ports when connecting
return;
}
expect(config.port).to.equal(port);
});
});
describe('Instantiation with arguments', function() {
it('should accept two parameters (database, username)', function() {
var sequelize = new Sequelize('dbname', 'root');
var config = sequelize.config;
expect(config.database).to.equal('dbname');
expect(config.username).to.equal('root');
});
it('should accept three parameters (database, username, password)', function() {
var sequelize = new Sequelize('dbname', 'root', 'pass');
var config = sequelize.config;
expect(config.database).to.equal('dbname');
expect(config.username).to.equal('root');
expect(config.password).to.equal('pass');
});
it('should accept four parameters (database, username, password, options)', function() {
var sequelize = new Sequelize('dbname', 'root', 'pass', {
port: 999,
dialectOptions: {
supportBigNumbers: true,
bigNumberStrings: true
}
});
var config = sequelize.config;
expect(config.database).to.equal('dbname');
expect(config.username).to.equal('root');
expect(config.password).to.equal('pass');
expect(config.port).to.equal(999);
expect(config.dialectOptions.supportBigNumbers).to.be.true;
expect(config.dialectOptions.bigNumberStrings).to.be.true;
});
if (dialect === 'sqlite') {
var sqlite3 = require('sqlite3');
it('should respect READONLY / READWRITE connection modes', function() {
var p = path.join(__dirname, '../tmp', 'foo.sqlite');
var createTableFoo = 'CREATE TABLE foo (faz TEXT);';
var createTableBar = 'CREATE TABLE bar (baz TEXT);';
var testAccess = Sequelize.Promise.method(function() {
if (fs.access) {
return Sequelize.Promise.promisify(fs.access)(p, fs.R_OK | fs.W_OK);
} else { // Node v0.10 and older don't have fs.access
return Sequelize.Promise.promisify(fs.open)(p, 'r+')
.then(function(fd) {
return Sequelize.Promise.promisify(fs.close)(fd);
});
}
});
return Sequelize.Promise.promisify(fs.unlink)(p)
.catch(function(err) {
expect(err.code).to.equal('ENOENT');
})
.then(function() {
var sequelizeReadOnly = new Sequelize('sqlite://foo', {
storage: p,
dialectOptions: {
mode: sqlite3.OPEN_READONLY
}
});
var sequelizeReadWrite = new Sequelize('sqlite://foo', {
storage: p,
dialectOptions: {
mode: sqlite3.OPEN_READWRITE
}
});
expect(sequelizeReadOnly.config.dialectOptions.mode).to.equal(sqlite3.OPEN_READONLY);
expect(sequelizeReadWrite.config.dialectOptions.mode).to.equal(sqlite3.OPEN_READWRITE);
return Sequelize.Promise.join(
sequelizeReadOnly.query(createTableFoo)
.should.be.rejectedWith(Error, 'SQLITE_CANTOPEN: unable to open database file'),
sequelizeReadWrite.query(createTableFoo)
.should.be.rejectedWith(Error, 'SQLITE_CANTOPEN: unable to open database file')
);
})
.then(function() {
// By default, sqlite creates a connection that's READWRITE | CREATE
var sequelize = new Sequelize('sqlite://foo', {
storage: p
});
return sequelize.query(createTableFoo);
})
.then(testAccess)
.then(function() {
var sequelizeReadOnly = new Sequelize('sqlite://foo', {
storage: p,
dialectOptions: {
mode: sqlite3.OPEN_READONLY
}
});
var sequelizeReadWrite = new Sequelize('sqlite://foo', {
storage: p,
dialectOptions: {
mode: sqlite3.OPEN_READWRITE
}
});
return Sequelize.Promise.join(
sequelizeReadOnly.query(createTableBar)
.should.be.rejectedWith(Error, 'SQLITE_READONLY: attempt to write a readonly database'),
sequelizeReadWrite.query(createTableBar)
);
})
.finally(function() {
return Sequelize.Promise.promisify(fs.unlink)(p);
});
});
}
});
});