6 changed files with 301 additions and 36 deletions
@ -0,0 +1,216 @@ |
|||||
|
var caching = require("../../index"); |
||||
|
var assert = require("assert"); |
||||
|
var support = require("../support"); |
||||
|
var check_err = support.check_err; |
||||
|
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() { |
||||
|
before(function() { |
||||
|
key = support.random.string(20); |
||||
|
value = support.random.string(20); |
||||
|
}); |
||||
|
describe("get with options", function() { |
||||
|
var testInstance = caching.caching({store: testStore()}); |
||||
|
var testCache; |
||||
|
before(function() { |
||||
|
testCache = caching.multi_caching([testInstance]); |
||||
|
}); |
||||
|
|
||||
|
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 testInstance = caching.caching({store: testStore()}); |
||||
|
var testCache; |
||||
|
var ttl = 60; |
||||
|
before(function() { |
||||
|
testCache = caching.multi_caching([testInstance]); |
||||
|
}); |
||||
|
|
||||
|
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() { |
||||
|
var testInstance = caching.caching({store: testStore()}); |
||||
|
var testCache; |
||||
|
before(function() { |
||||
|
testCache = caching.multi_caching([testInstance]); |
||||
|
}); |
||||
|
|
||||
|
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.multi_caching([testInstance, memInstance]); |
||||
|
}); |
||||
|
|
||||
|
it("lets us pass options which only one store uses", function() { |
||||
|
testCache.set(key, value, options, function(err) { |
||||
|
check_err(err); |
||||
|
testCache.get(key, options, function(err, response) { |
||||
|
check_err(err); |
||||
|
assert.equal(response, value); |
||||
|
testCache.del(key, options, function(err) { |
||||
|
check_err(err); |
||||
|
testCache.get(key, options, function(err, response) { |
||||
|
check_err(err); |
||||
|
assert.equal(response, undefined); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
it("lets us not pass options which only one store uses", function() { |
||||
|
testCache.set(key, value, ttl, function(err) { |
||||
|
check_err(err); |
||||
|
testCache.get(key, function(err, response) { |
||||
|
check_err(err); |
||||
|
assert.equal(response, value); |
||||
|
testCache.del(key, function(err) { |
||||
|
check_err(err); |
||||
|
testCache.get(key, function(err, response) { |
||||
|
check_err(err); |
||||
|
assert.equal(response, undefined); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
Loading…
Reference in new issue