[api test doc] Internal fixes from testing. More tests and docs
This commit is contained in:
parent
73bf78339f
commit
09b8c75383
9 changed files with 160 additions and 23 deletions
|
@ -19,7 +19,7 @@ nconf.stores = require('nconf/stores');
|
|||
// specified `type`.
|
||||
//
|
||||
nconf.use = function (type, options) {
|
||||
nconf.store = new nconf.stores[type](options);
|
||||
nconf.store = new nconf.stores.create(type, options);
|
||||
};
|
||||
|
||||
//
|
||||
|
|
|
@ -7,6 +7,21 @@
|
|||
|
||||
var stores = exports;
|
||||
|
||||
function capitalize (str) {
|
||||
return str && str[0].toUpperCase() + str.slice(1);
|
||||
};
|
||||
|
||||
stores.Memory = require('nconf/stores/memory').Memory;
|
||||
stores.File = require('nconf/stores/file').File;
|
||||
stores.Redis = require('nconf/stores/redis').Redis;
|
||||
|
||||
//
|
||||
// ### function create (type, options)
|
||||
// #### @type {string} Type of the nconf store to use.
|
||||
// #### @options {Object} Options for the store instance.
|
||||
// Creates a store of the specified `type` using the
|
||||
// specified `options`.
|
||||
//
|
||||
stores.create = function (type, options) {
|
||||
return new stores[capitalize(type.toLowerCase())](options);
|
||||
};
|
|
@ -20,7 +20,7 @@ var File = exports.File = function (options) {
|
|||
throw new Error ('Missing required option `files`');
|
||||
}
|
||||
|
||||
nconf.stores.Memory.call(this, options);
|
||||
Memory.call(this, options);
|
||||
|
||||
this.file = options.file;
|
||||
this.format = options.format || JSON;
|
||||
|
@ -42,7 +42,7 @@ File.prototype.save = function (value, callback) {
|
|||
value = null;
|
||||
}
|
||||
|
||||
fs.save(this.file, this.format.stringify(this.store), function (err) {
|
||||
fs.writeFile(this.file, this.format.stringify(this.store), function (err) {
|
||||
return err ? callback(err) : callback();
|
||||
});
|
||||
};
|
||||
|
@ -54,12 +54,13 @@ File.prototype.save = function (value, callback) {
|
|||
//
|
||||
File.prototype.load = function (callback) {
|
||||
var self = this;
|
||||
fs.load(this.file, function (err, data) {
|
||||
fs.readFile(this.file, function (err, data) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
self.store = self.format.parse(data.toString());
|
||||
data = self.format.parse(data.toString());
|
||||
self.store = data;
|
||||
callback(null, self.store);
|
||||
});
|
||||
};
|
56
test/file-store-test.js
Normal file
56
test/file-store-test.js
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* file-store-test.js: Tests for the nconf File store.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins
|
||||
*
|
||||
*/
|
||||
|
||||
require.paths.unshift(require('path').join(__dirname, '..', 'lib'));
|
||||
|
||||
var fs = require('fs'),
|
||||
path = require('path'),
|
||||
vows = require('vows'),
|
||||
assert = require('assert'),
|
||||
nconf = require('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));
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}).addBatch({
|
||||
"When using the nconf file store": {
|
||||
"the set() method": {
|
||||
"should respond with true": function () {
|
||||
assert.isTrue(store.set('foo:bar:bazz', 'buzz'));
|
||||
}
|
||||
},
|
||||
"the get() method": {
|
||||
"should respond with the correct value": function () {
|
||||
assert.equal(store.get('foo:bar:bazz'), 'buzz');
|
||||
}
|
||||
},
|
||||
"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);
|
20
test/fixtures/data.js
vendored
Normal file
20
test/fixtures/data.js
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* data.js: Simple data fixture for configuration test.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins
|
||||
*
|
||||
*/
|
||||
|
||||
exports.data = {
|
||||
literal: 'bazz',
|
||||
arr: ['one', 2, true, { value: 'foo' }],
|
||||
obj: {
|
||||
host: 'localhost',
|
||||
port: 5984,
|
||||
array: ['one', 2, true, { foo: 'bar' }],
|
||||
auth: {
|
||||
username: 'admin',
|
||||
password: 'password'
|
||||
}
|
||||
}
|
||||
}
|
1
test/fixtures/store.json
vendored
Normal file
1
test/fixtures/store.json
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
{"literal":"bazz","arr":["one",2,true,{"value":"foo"}],"obj":{"host":"localhost","port":5984,"array":["one",2,true,{"foo":"bar"}],"auth":{"username":"admin","password":"password"}}}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* memory.js: Simple memory storage engine for nconf configuration(s)
|
||||
* memory-store-test.js: Tests for the nconf Memory store.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins
|
||||
*
|
||||
|
|
57
test/nconf-test.js
Normal file
57
test/nconf-test.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* file-store-test.js: Tests for the nconf File store.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins
|
||||
*
|
||||
*/
|
||||
|
||||
require.paths.unshift(require('path').join(__dirname, '..', 'lib'));
|
||||
|
||||
var fs = require('fs'),
|
||||
path = require('path'),
|
||||
vows = require('vows'),
|
||||
assert = require('assert'),
|
||||
nconf = require('nconf'),
|
||||
data = require('./fixtures/data').data;
|
||||
|
||||
vows.describe('nconf').addBatch({
|
||||
"When using the nconf": {
|
||||
"should have the correct methods set": function () {
|
||||
assert.isFunction(nconf.key);
|
||||
assert.isFunction(nconf.path);
|
||||
assert.isFunction(nconf.use);
|
||||
assert.isFunction(nconf.get);
|
||||
assert.isFunction(nconf.set);
|
||||
assert.isFunction(nconf.clear);
|
||||
assert.isFunction(nconf.load);
|
||||
assert.isFunction(nconf.save);
|
||||
assert.isFunction(nconf.reset);
|
||||
},
|
||||
"the use() method": {
|
||||
"should instaniate the correct store": function () {
|
||||
nconf.use('redis');
|
||||
assert.instanceOf(nconf.store, nconf.stores.Redis);
|
||||
}
|
||||
}
|
||||
}
|
||||
})/*.addBatch({
|
||||
"When using the nconf file store": {
|
||||
"the set() method": {
|
||||
"should respond with true": function () {
|
||||
assert.isTrue(store.set('foo:bar:bazz', 'buzz'));
|
||||
}
|
||||
},
|
||||
"the get() method": {
|
||||
"should respond with the correct value": function () {
|
||||
assert.equal(store.get('foo:bar:bazz'), 'buzz');
|
||||
}
|
||||
},
|
||||
"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);
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* redis-test.js: Tests for the redis nconf storage engine.
|
||||
* redis-store-test.js: Tests for the redis nconf storage engine.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins
|
||||
*
|
||||
|
@ -9,21 +9,8 @@ require.paths.unshift(require('path').join(__dirname, '..', 'lib'));
|
|||
|
||||
var vows = require('vows'),
|
||||
assert = require('assert'),
|
||||
nconf = require('nconf');
|
||||
|
||||
var data = {
|
||||
literal: 'bazz',
|
||||
arr: ['one', 2, true, { value: 'foo' }],
|
||||
obj: {
|
||||
host: 'localhost',
|
||||
port: 5984,
|
||||
array: ['one', 2, true, { foo: 'bar' }],
|
||||
auth: {
|
||||
username: 'admin',
|
||||
password: 'password'
|
||||
}
|
||||
}
|
||||
}
|
||||
nconf = require('nconf'),
|
||||
data = require('./fixtures/data').data;
|
||||
|
||||
vows.describe('nconf/stores/redis').addBatch({
|
||||
"When using the nconf redis store": {
|
||||
|
|
Loading…
Reference in a new issue