Skip to content

Commit cdd2640

Browse files
committed
add tests for configuration functionality extension
1 parent e6df315 commit cdd2640

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed

test/config_test.js

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var vows = require('vows');
22
var assert = require('assert');
33
var config = require('../lib/config');
44
var path = require('path');
5+
var sinon = require('sinon');
56

67
var _configLoad = config.load;
78
var _configLoadUrl = config.loadUrl;
@@ -125,6 +126,199 @@ vows.describe('config').addBatch({
125126
assert.equal(current.settings.database, 'dbname');
126127
}
127128
}
129+
}).addBatch({
130+
'loading from an URL and extending it': {
131+
topic: function() {
132+
var databaseUrl = {
133+
'dev': {
134+
'url': 'postgres://uname:[email protected]/dbname',
135+
'overwrite': {
136+
'ssl': true
137+
}
138+
}
139+
};
140+
return config.loadObject(databaseUrl, 'dev');
141+
},
142+
143+
'should export the settings as the current environment': function (config) {
144+
assert.isDefined(config.dev);
145+
},
146+
147+
'should export a getCurrent function with all current environment settings': function (config) {
148+
assert.isDefined(config.getCurrent);
149+
var current = config.getCurrent();
150+
assert.equal(current.env, 'dev');
151+
assert.equal(current.settings.driver, 'postgres');
152+
assert.equal(current.settings.user, 'uname');
153+
assert.equal(current.settings.password, 'pw');
154+
assert.equal(current.settings.host, 'server.com');
155+
assert.equal(current.settings.database, 'dbname');
156+
assert.equal(current.settings.ssl, true);
157+
}
158+
}
159+
}).addBatch({
160+
'loading from an ENV URL and extending it': {
161+
topic: function() {
162+
process.env.DATABASE_URL = 'postgres://uname:[email protected]/dbname';
163+
var databaseUrl = {
164+
'dev': {
165+
'ENV': 'DATABASE_URL',
166+
'overwrite': {
167+
'ssl': true
168+
}
169+
}
170+
};
171+
return config.loadObject(databaseUrl, 'dev');
172+
},
173+
174+
'should export the settings as the current environment': function (config) {
175+
assert.isDefined(config.dev);
176+
},
177+
178+
'should export a getCurrent function with all current environment settings': function (config) {
179+
assert.isDefined(config.getCurrent);
180+
var current = config.getCurrent();
181+
assert.equal(current.env, 'dev');
182+
assert.equal(current.settings.driver, 'postgres');
183+
assert.equal(current.settings.user, 'uname');
184+
assert.equal(current.settings.password, 'pw');
185+
assert.equal(current.settings.host, 'server.com');
186+
assert.equal(current.settings.database, 'dbname');
187+
assert.equal(current.settings.ssl, true);
188+
}
189+
}
190+
}).addBatch({
191+
'loading from an ENV URL within the object and extending it': {
192+
topic: function() {
193+
process.env.DATABASE_URL = 'postgres://uname:[email protected]/dbname';
194+
var databaseUrl = {
195+
'dev': {
196+
'url': { 'ENV': 'DATABASE_URL' },
197+
'overwrite': {
198+
'ssl': true
199+
}
200+
}
201+
};
202+
return config.loadObject(databaseUrl, 'dev');
203+
},
204+
205+
'should export the settings as the current environment': function (config) {
206+
assert.isDefined(config.dev);
207+
},
208+
209+
'should export a getCurrent function with all current environment settings': function (config) {
210+
assert.isDefined(config.getCurrent);
211+
var current = config.getCurrent();
212+
assert.equal(current.env, 'dev');
213+
assert.equal(current.settings.driver, 'postgres');
214+
assert.equal(current.settings.user, 'uname');
215+
assert.equal(current.settings.password, 'pw');
216+
assert.equal(current.settings.host, 'server.com');
217+
assert.equal(current.settings.database, 'dbname');
218+
assert.equal(current.settings.ssl, true);
219+
}
220+
}
221+
}).addBatch({
222+
'loading from an ENV URL within the object and extending it from the ENV': {
223+
topic: function() {
224+
process.env.DATABASE_URL = 'postgres://uname:[email protected]/dbname';
225+
var databaseUrl = {
226+
'dev': {
227+
'url': {
228+
'ENV': 'DATABASE_URL',
229+
'overwrite': {
230+
'ssl': true
231+
}
232+
}
233+
}
234+
};
235+
return config.loadObject(databaseUrl, 'dev');
236+
},
237+
238+
'should export the settings as the current environment': function (config) {
239+
assert.isDefined(config.dev);
240+
},
241+
242+
'should export a getCurrent function with all current environment settings': function (config) {
243+
assert.isDefined(config.getCurrent);
244+
var current = config.getCurrent();
245+
assert.equal(current.env, 'dev');
246+
assert.equal(current.settings.driver, 'postgres');
247+
assert.equal(current.settings.user, 'uname');
248+
assert.equal(current.settings.password, 'pw');
249+
assert.equal(current.settings.host, 'server.com');
250+
assert.equal(current.settings.database, 'dbname');
251+
assert.equal(current.settings.ssl, true);
252+
}
253+
}
254+
}).addBatch({
255+
'loading from an ENV URL within the object and extending it from the ENV': {
256+
topic: function() {
257+
process.env.DATABASE_URL = 'postgres://uname:[email protected]/dbname?ssl=false&testing=false';
258+
var databaseUrl = {
259+
'dev': {
260+
'url': {
261+
'ENV': 'DATABASE_URL',
262+
'overwrite': {
263+
'ssl': true,
264+
'cache': false
265+
},
266+
'addIfNotExist': {
267+
'native': true,
268+
'testing': true
269+
}
270+
}
271+
}
272+
};
273+
return config.loadObject(databaseUrl, 'dev');
274+
},
275+
276+
'should export the settings as the current environment': function (config) {
277+
assert.isDefined(config.dev);
278+
},
279+
280+
'should export a getCurrent function with all current environment settings and correctly overwrite and add': function (config) {
281+
assert.isDefined(config.getCurrent);
282+
var current = config.getCurrent();
283+
assert.equal(current.env, 'dev');
284+
assert.equal(current.settings.driver, 'postgres');
285+
assert.equal(current.settings.user, 'uname');
286+
assert.equal(current.settings.password, 'pw');
287+
assert.equal(current.settings.host, 'server.com');
288+
assert.equal(current.settings.database, 'dbname');
289+
assert.equal(current.settings.native, true);
290+
assert.equal(current.settings.testing, false);
291+
assert.equal(current.settings.cache, false);
292+
assert.equal(current.settings.ssl, true);
293+
}
294+
}
295+
}).addBatch({
296+
'should throw on duplicate entries on two levels': {
297+
topic: function() {
298+
process.env.DATABASE_URL = 'postgres://uname:[email protected]/dbname';
299+
var databaseUrl = {
300+
'dev': {
301+
'url': {
302+
'ENV': 'DATABASE_URL',
303+
'overwrite': {
304+
'ssl': true
305+
}
306+
},
307+
'overwrite': {
308+
'ssl': true
309+
}
310+
}
311+
};
312+
313+
var configSpy = sinon.spy(config, 'loadObject');
314+
this.callback(config.loadObject(databaseUrl, 'dev'), configSpy);
315+
},
316+
317+
'should throw and deliver no config': function (config, spy) {
318+
assert(spy.threw());
319+
assert.isNotDefined(config.dev);
320+
}
321+
}
128322
}).addBatch({
129323
'loading a config with null values': {
130324
topic: function() {

0 commit comments

Comments
 (0)