Browse Source

removing more ttl integer calls from tests, filling in missing tests, removing obsolete tests.

feature/1.0-prep
Bryan Donovan 10 years ago
parent
commit
a3387fe439
  1. 1
      History.md
  2. 2
      lib/caching.js
  3. 2
      lib/multi_caching.js
  4. 12
      test/caching.unit.js
  5. 98
      test/multi_caching.unit.js
  6. 209
      test/stores/options.unit.js
  7. 3
      test/support.js

1
History.md

@ -8,6 +8,7 @@
`function set(key, val, options, cb) { }`
- (Breaking change) caching/multicaching methods no longer accept a `ttl` param. You must instead pass
in an options object which will be passed to the cache store's `set` method.
- (Breaking change) caching.js no longer accepts a path to cache store. Pass in an object or 'memory' instead.
- 0.19.0 2015-03-29
- Pass dispose, length & stale options to lru-cache (#22). - @gmaclennan

2
lib/caching.js

@ -22,8 +22,6 @@ var caching = function(args) {
} else {
self.store = args.store;
}
} else if (typeof args.store === 'string' && args.store.match(/\//)) {
self.store = require(args.store).create(args);
} else {
var storeName = args.store || 'memory';
self.store = require('./stores/' + storeName).create(args);

2
lib/multi_caching.js

@ -165,7 +165,7 @@ var multiCaching = function(caches, options) {
* @param {string} key
* @param {*} value
* @param {object} [options] to pass to underlying set function.
* @param {function} cb
* @param {function} [cb]
*/
self.set = function(key, value, options, cb) {
if (typeof options === 'function') {

12
test/caching.unit.js

@ -695,18 +695,6 @@ describe("caching", function() {
});
});
it("allows us to pass in a path to our own store", function(done) {
var storePath = '../lib/stores/memory';
cache = caching({store: storePath});
cache.set(key, value, {ttl: defaultTtl}, function(err) {
checkErr(err);
cache.get(key, function(err, result) {
assert.equal(result, value);
done();
});
});
});
it("allows us to pass in a module (uninstantiated)", function(done) {
var store = memoryStore;
cache = caching({store: store});

98
test/multi_caching.unit.js

@ -23,10 +23,11 @@ describe("multiCaching", function() {
var key;
var memoryTtl;
var name;
var ttl = 5;
var defaultTtl;
beforeEach(function() {
memoryTtl = 0.1;
defaultTtl = 5;
memoryCache = caching({store: 'memory', ttl: memoryTtl});
memoryCache2 = caching({store: 'memory', ttl: memoryTtl});
@ -47,7 +48,7 @@ describe("multiCaching", function() {
describe("set()", function() {
it("lets us set data in all caches", function(done) {
multiCache.set(key, value, ttl, function(err) {
multiCache.set(key, value, {ttl: defaultTtl}, function(err) {
checkErr(err);
memoryCache.get(key, function(err, result) {
@ -69,7 +70,7 @@ describe("multiCaching", function() {
});
it("lets us set data without a callback", function(done) {
multiCache.set(key, value, ttl);
multiCache.set(key, value, {ttl: defaultTtl});
setTimeout(function() {
multiCache.get(key, function(err, result) {
checkErr(err);
@ -94,7 +95,34 @@ describe("multiCaching", function() {
}, 20);
});
it("lets us set data without a ttl or callback", function(done) {
it("lets us set data without an options param", function(done) {
multiCache.set(key, value, function(err) {
checkErr(err);
multiCache.get(key, function(err, result) {
checkErr(err);
assert.equal(result, value);
memoryCache.get(key, function(err, result) {
checkErr(err);
assert.equal(result, value);
memoryCache2.get(key, function(err, result) {
checkErr(err);
assert.equal(result, value);
memoryCache3.get(key, function(err, result) {
checkErr(err);
assert.equal(result, value);
done();
});
});
});
});
});
});
it("lets us set data without options or callback", function(done) {
multiCache.set(key, value);
setTimeout(function() {
multiCache.get(key, function(err, result) {
@ -123,7 +151,7 @@ describe("multiCaching", function() {
describe("get()", function() {
it("gets data from first cache that has it", function(done) {
memoryCache3.set(key, value, ttl, function(err) {
memoryCache3.set(key, value, function(err) {
checkErr(err);
multiCache.get(key, function(err, result) {
@ -133,11 +161,31 @@ describe("multiCaching", function() {
});
});
});
it("passes any options to underlying caches", function(done) {
multiCache.set(key, value, function(err) {
checkErr(err);
sinon.spy(memoryCache.store, 'get');
var opts = {foo: 'bar'};
multiCache.get(key, opts, function(err, result) {
checkErr(err);
assert.equal(result, value);
assert.ok(memoryCache.store.get.calledWith(key, opts));
memoryCache.store.get.restore();
done();
});
});
});
});
describe("del()", function() {
it("lets us delete data in all caches", function(done) {
multiCache.set(key, value, ttl, function(err) {
multiCache.set(key, value, function(err) {
checkErr(err);
multiCache.del(key, function(err) {
@ -162,7 +210,7 @@ describe("multiCaching", function() {
});
it("lets us delete data without a callback", function(done) {
multiCache.set(key, value, ttl, function(err) {
multiCache.set(key, value, function(err) {
checkErr(err);
multiCache.del(key);
@ -201,7 +249,7 @@ describe("multiCaching", function() {
});
it("gets data from first cache that has it", function(done) {
memoryCache3.set(key, value, ttl, function(err) {
memoryCache3.set(key, value, function(err) {
checkErr(err);
multiCache.getAndPassUp(key, function(err, result) {
@ -257,7 +305,7 @@ describe("multiCaching", function() {
});
it("checks to see if higher levels have item", function(done) {
memoryCache3.set(key, value, ttl, function(err) {
memoryCache3.set(key, value, function(err) {
checkErr(err);
multiCache.getAndPassUp(key, function(err, result) {
@ -280,9 +328,9 @@ describe("multiCaching", function() {
var memoryStoreStub;
beforeEach(function() {
memoryStoreStub = memoryStore.create({ttl: ttl});
memoryStoreStub = memoryStore.create({ttl: defaultTtl});
sinon.stub(memoryStore, 'create').returns(memoryStoreStub);
memoryCache = caching({store: 'memory', ttl: ttl});
memoryCache = caching({store: 'memory', ttl: defaultTtl});
multiCache = multiCaching([memoryCache]);
fakeError = new Error(support.random.string());
sinon.stub(memoryStoreStub, 'get').yields(fakeError);
@ -318,13 +366,17 @@ describe("multiCaching", function() {
memoryCache3.store.set.restore();
});
/**
* Note: it's up to the underlying cache implementation to handle the ttl number.
* We're just testing that the ttl gets passed to the underlying store.
*/
it('when a ttl number is passed in', function(done) {
multiCache.wrap(key, function(cb) {
methods.getWidget(name, cb);
}, ttl, function(err, widget) {
}, defaultTtl, function(err, widget) {
checkErr(err);
assert.deepEqual(widget, {name: name});
sinon.assert.calledWith(memoryCache3.store.set, key, {name: name}, ttl);
sinon.assert.calledWith(memoryCache3.store.set, key, {name: name}, defaultTtl);
done();
});
});
@ -332,15 +384,15 @@ describe("multiCaching", function() {
it('when a ttl option is passed in', function(done) {
multiCache.wrap(key, function(cb) {
methods.getWidget(name, cb);
}, {ttl: ttl}, function(err, widget) {
}, {ttl: defaultTtl}, function(err, widget) {
checkErr(err);
assert.deepEqual(widget, {name: name});
sinon.assert.calledWith(memoryCache3.store.set, key, {name: name}, {ttl: ttl});
sinon.assert.calledWith(memoryCache3.store.set, key, {name: name}, {ttl: defaultTtl});
done();
});
});
it('when a ttl is not passed in', function(done) {
it('when no options are passed in', function(done) {
multiCache.wrap(key, function(cb) {
methods.getWidget(name, cb);
}, function(err, widget) {
@ -381,7 +433,7 @@ describe("multiCaching", function() {
function getCachedFalseyValue(cb) {
multiCache.wrap(key, function(cacheCb) {
getFalseyValue(cacheCb);
}, ttl, cb);
}, cb);
}
beforeEach(function(done) {
@ -442,7 +494,7 @@ describe("multiCaching", function() {
getValue(name, function(err, result) {
cacheCb(err, result);
});
}, ttl, cb);
}, cb);
}
beforeEach(function() {
@ -521,7 +573,7 @@ describe("multiCaching", function() {
context("when value exists in first store but not second", function() {
it("returns value from first store, does not set it in second", function(done) {
memoryCache.set(key, {name: name}, ttl, function(err) {
memoryCache.set(key, {name: name}, function(err) {
checkErr(err);
multiCache.wrap(key, function(cb) {
@ -542,7 +594,7 @@ describe("multiCaching", function() {
context("when value exists in second store but not first", function() {
it("returns value from second store, sets it in first store", function(done) {
memoryCache3.set(key, {name: name}, ttl, function(err) {
memoryCache3.set(key, {name: name}, function(err) {
checkErr(err);
multiCache.wrap(key, function(cb) {
@ -604,7 +656,7 @@ describe("multiCaching", function() {
context("when value exists in first store only", function() {
it("returns value from first store, does not set it in second or third", function(done) {
memoryCache.set(key, {name: name}, ttl, function(err) {
memoryCache.set(key, {name: name}, function(err) {
checkErr(err);
multiCache.wrap(key, function(cb) {
@ -630,7 +682,7 @@ describe("multiCaching", function() {
context("when value exists in second store only", function() {
it("returns value from second store, sets it in first store, does not set third store", function(done) {
memoryCache3.set(key, {name: name}, ttl, function(err) {
memoryCache3.set(key, {name: name}, function(err) {
checkErr(err);
multiCache.wrap(key, function(cb) {
@ -656,7 +708,7 @@ describe("multiCaching", function() {
context("when value exists in third store only", function() {
it("returns value from third store, sets it in first and second stores", function(done) {
memoryCache2.set(key, {name: name}, ttl, function(err) {
memoryCache2.set(key, {name: name}, function(err) {
checkErr(err);
multiCache.wrap(key, function(cb) {

209
test/stores/options.unit.js

@ -1,209 +0,0 @@
var caching = require("../../index");
var assert = require("assert");
var support = require("../support");
var checkErr = support.checkErr;
var memoryFlag = "";
var key;
var value;
var testStore = function(args) {
args = args || {};
var self = {};
self.name = "options";
self.store = {};
self.get = function(key, options, cb) {
var optionsMapped = false;
if (typeof options === "function") {
cb = options;
options = false;
optionsMapped = true;
}
if (options && options.value) {
return cb(null, options.value + "ValueOption");
} else if (options && options.fn) {
options.fn("GetFunctionOption");
return cb(null, "GetFunctionOption");
} else if (options && options.runNormal) {
return cb(null, self.store[key]);
} else if (optionsMapped) {
return cb();
}
return cb("Error No Options");
};
self.set = function(key, value, options, cb) {
var optionsMapped = false;
if (typeof options === "function") {
cb = options;
options = false;
optionsMapped = true;
} else if (typeof options !== "object") {
options = {ttl: options, runNormal: true};
}
if (options && options.value) {
memoryFlag = options.value + "ValueOption";
return cb();
} else if (options && options.fn) {
options.fn("SetFunctionOption");
return cb();
} else if (options && options.runNormal) {
self.store[key] = value;
return cb(null, self.store[key]);
} else if (optionsMapped) {
return cb();
}
return cb("Error No Options");
};
self.del = function(key, options, cb) {
var optionsMapped = false;
if (typeof options === "function") {
cb = options;
options = false;
optionsMapped = true;
}
if (options && options.value) {
memoryFlag = options.value + "ValueOption";
return cb();
} else if (options && options.fn) {
options.fn("DeleteFunctionOption");
return cb();
} else if (options && options.runNormal) {
delete self.store[key];
return cb(null, "");
} else if (optionsMapped) {
return cb();
}
return cb("Error No Options");
};
return {
create: function() {
return self;
}
};
};
describe("Methods with options", function() {
var testInstance = caching.caching({store: testStore()});
var testCache;
before(function() {
key = support.random.string(20);
value = support.random.string(20);
testCache = caching.multiCaching([testInstance]);
});
describe("get with options", function() {
it("lets us pass options by value", function(done) {
var options = {value: value};
testCache.get(key, options, function(err, response) {
assert.equal(response, value + "ValueOption");
done();
});
});
it("lets us pass options by function", function(done) {
var options = {
fn: function(response) {
assert.equal(response, "GetFunctionOption");
done();
}
};
testCache.get(key, options, function(err, response) {
assert.equal(response, "GetFunctionOption");
});
});
});
describe("set with options", function() {
var ttl = 60;
it("lets us pass options by value", function(done) {
var options = {ttl: ttl, value: value};
testCache.set(key, value, options, function() {
assert.equal(memoryFlag, value + "ValueOption");
done();
});
});
it("lets us pass options by function", function(done) {
var options = {
ttl: ttl,
fn: function(response) {
assert.equal(response, "SetFunctionOption");
done();
}
};
testCache.set(key, value, options, function() {}, options);
});
});
describe("delete with options", function() {
it("lets us pass options by value", function(done) {
var options = {value: value};
testCache.del(key, options, function() {
assert.equal(memoryFlag,value + "ValueOption");
done();
});
});
it("lets us pass options by function", function(done) {
var options = {
fn: function(response) {
assert.equal(response, "DeleteFunctionOption");
done();
}
};
testCache.del(key, options, function() {}, options);
});
});
});
describe("Multiple stores with options", function() {
var testInstance = caching.caching({store: testStore()});
var memInstance = caching.caching({store: "memory"});
var testCache;
var options = {runNormal: true};
var ttl = 1;
before(function() {
key = support.random.string(20);
value = support.random.string(20);
testCache = caching.multiCaching([testInstance, memInstance]);
});
it("lets us pass options which only one store uses", function() {
testCache.set(key, value, options, function(err) {
checkErr(err);
testCache.get(key, options, function(err, response) {
checkErr(err);
assert.equal(response, value);
testCache.del(key, options, function(err) {
checkErr(err);
testCache.get(key, options, function(err, response) {
checkErr(err);
assert.equal(response, undefined);
});
});
});
});
});
it("lets us not pass options which only one store uses", function() {
testCache.set(key, value, ttl, function(err) {
checkErr(err);
testCache.get(key, function(err, response) {
checkErr(err);
assert.equal(response, value);
testCache.del(key, function(err) {
checkErr(err);
testCache.get(key, function(err, response) {
checkErr(err);
assert.equal(response, undefined);
});
});
});
});
});
});

3
test/support.js

@ -90,9 +90,8 @@ var support = {
testSetGetDel: function(cache, cb) {
var key = 'TEST' + support.random.string();
var val = support.random.string();
var ttl;
cache.set(key, val, ttl, function(err) {
cache.set(key, val, function(err) {
if (err) { return cb(err); }
cache.get(key, function(err, result) {

Loading…
Cancel
Save