ac-node
Version:
A common module for building Atlassian Connect add-ons
184 lines (149 loc) • 5.64 kB
JavaScript
var assert = require('assert');
var RedisStore = require('..').RedisStore;
var wait = require('co-wait');
if (!/\bno-redis\b/.test(process.env.NODE_TEST)) {
describe('ac redis store', function () {
var testScope = 'test-ac-store-redis';
describe('contructor', function () {
it('should have a create factory method', function *() {
assert.equal(typeof RedisStore.create, 'function');
});
});
describe('with default scope', function () {
var store;
beforeEach(function () {
store = RedisStore(null, testScope);
});
afterEach(function *() {
yield store.clear();
});
it('should store data when set', function *() {
var input = {key: 'test'};
yield store.set(input.key, input);
var output = yield store.get(input.key);
assert.deepEqual(input, output);
});
it('should remove data when deleted', function *() {
var input = {key: 'test'};
yield store.set(input.key, input);
yield store.del(input.key);
var output = yield store.get(input.key);
assert.ok(!output);
});
it('should return nothing when a key is unset', function *() {
var output = yield store.get('miss');
assert.ok(!output);
});
it('should store and retrieve strings', function *() {
var key = 'test-string-key';
var input = 'string';
yield store.set(key, input);
var output = yield store.get(key);
assert.equal(input, output);
});
it('should store and retrieve numbers', function *() {
var key = 'test-number-key';
var input = 20.5;
yield store.set(key, input);
var output = yield store.get(key);
assert.equal(input, output);
});
it('should store and retrieve booleans', function *() {
var key = 'test-boolean-key';
var input = true;
yield store.set(key, input);
var output = yield store.get(key);
assert.equal(input, output);
});
it('should store and retrieve json-compatible objects', function *() {
var key = 'test-object-key';
var input = {a: 1, b: '2', c: {d: true}, e: [3, 'f']};
yield store.set(key, input);
var output = yield store.get(key);
assert.deepEqual(input, output);
});
it('should store and retrieve json-compatible arrays', function *() {
var key = 'test-array-key';
var input = [
{a: 1, b: '2', c: {d: true}, e: [3, 'f']},
{g: 4, h: '5', i: {j: false}, k: [6, 'l']}
];
yield store.set(key, input);
var output = yield store.get(key);
assert.deepEqual(input, output);
});
it('should return a new store with a more specific scope when narrowed', function *() {
var substore = store.narrow('foo');
assert.equal(testScope + ':foo', substore._scope);
});
it('should get all scoped data', function *() {
var input1 = {key: 'test1'};
var input2 = {key: 'test2'};
var input3 = {key: 'test3'};
yield store.set(input1.key, input1);
yield store.set(input2.key, input2);
yield store.set(input3.key, input3);
var output = yield store.all();
assert.ok(output);
assert.deepEqual(output[input1.key], input1);
assert.deepEqual(output[input2.key], input2);
assert.deepEqual(output[input3.key], input3);
});
it('should clear all scoped data', function *() {
var input = {key: 'test'};
yield store.set(input.key, input);
yield store.clear();
var output = yield store.get(input.key);
assert.ok(!output);
});
if (!/\bfast\b/.test(process.env.NODE_TEST)) {
it('should expire set values when set\'s optional expiresIn argument is provided', function *() {
var key = 'test-expire-key';
var input = 'string';
yield store.set(key, input, 1);
var output = yield store.get(key);
assert.equal(input, output);
yield wait(100);
output = yield store.get(key);
assert.equal(input, output);
yield wait(1000);
output = yield store.get(key);
assert.ok(!output);
});
}
});
describe('with foo scope', function () {
var store;
beforeEach(function () {
store = RedisStore(null, testScope).narrow('foo');
});
afterEach(function *() {
yield store.clear();
});
it('should store data with scoped key when set', function *() {
var input = {key: 'test'};
yield store.set(input.key, input);
var output = yield store._get(store._scope + ':' + input.key);
assert.deepEqual(input, output);
});
it('should remove data with scoped key when deleted', function *() {
var input = {key: 'test'};
yield store._set(store._scope + ':' + input.key, input);
yield store.del(input.key);
var output = yield store.get(input.key);
assert.ok(!output);
});
it('should return a new store with a more specific scope when narrowed', function *() {
var substore = store.narrow('bar');
assert.equal(testScope + ':foo:bar', substore._scope);
});
it('should clear all scoped data', function *() {
var input = {key: 'test'};
yield store.set(input.key, input);
yield store.clear();
var output = yield store.get(input.key);
assert.ok(!output);
});
});
});
}