nconf-lite/test/file-store-test.js

104 lines
3.2 KiB
JavaScript

/*
* file-store-test.js: Tests for the nconf File store.
*
* (C) 2011, Charlie Robbins
*
*/
var fs = require('fs'),
path = require('path'),
vows = require('vows'),
assert = require('assert'),
nconf = require('../lib/nconf'),
data = require('./fixtures/data').data,
store;
vows.describe('nconf/stores/file').addBatch({
"When using the nconf file store": {
topic: function () {
var filePath = path.join(__dirname, 'fixtures', 'store.json');
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
store = new nconf.stores.File({ file: filePath });
return null;
},
"the load() method": {
topic: function () {
store.load(this.callback);
},
"should load the data correctly": function (err, data) {
assert.isNull(err);
assert.deepEqual(data, store.store);
}
}
},
"When using the nconf file store": {
topic: function () {
var filePath = path.join(__dirname, 'fixtures', 'malformed.json');
store = new nconf.stores.File({ file: filePath });
return null;
},
"the load() method with a malformed JSON config file": {
topic: function () {
store.load(this.callback.bind(null, null));
},
"should respond with an error": function (ign, err) {
console.dir(err);
}
}
}
}).addBatch({
"When using the nconf file store": {
topic: function () {
var tmpPath = path.join(__dirname, 'fixtures', 'tmp.json'),
tmpStore = new nconf.stores.File({ file: tmpPath });
return tmpStore;
},
"the save() method": {
topic: function (tmpStore) {
var that = this;
Object.keys(store.store).forEach(function (key) {
tmpStore.set(key, store.store[key]);
});
tmpStore.save(function () {
fs.readFile(tmpStore.file, function (err, data) {
return err ? that.callback(err) : that.callback(err, JSON.parse(data.toString()));
});
});
},
"should save the data correctly": function (err, data) {
assert.isNull(err);
assert.deepEqual(data, store.store);
}
}
}
}).addBatch({
"When using the nconf file store": {
"the set() method": {
"should respond with true": function () {
assert.isTrue(store.set('foo:bar:bazz', 'buzz'));
assert.isTrue(store.set('falsy:number', 0));
assert.isTrue(store.set('falsy:string', ''));
assert.isTrue(store.set('falsy:boolean', false));
assert.isTrue(store.set('falsy:object', null));
}
},
"the get() method": {
"should respond with the correct value": function () {
assert.equal(store.get('foo:bar:bazz'), 'buzz');
assert.equal(store.get('falsy:number'), 0);
assert.equal(store.get('falsy:string'), '');
assert.equal(store.get('falsy:boolean'), false);
assert.equal(store.get('falsy:object'), null);
}
},
"the clear() method": {
"should respond with the true": function () {
assert.equal(store.get('foo:bar:bazz'), 'buzz');
assert.isTrue(store.clear('foo:bar:bazz'));
assert.isTrue(typeof store.get('foo:bar:bazz') === 'undefined');
}
}
}
}).export(module);