cache.test.js 2.69 KB
/**
 * cache 测试
 *
 * @author: jf<jeff.jiang@yoho.cn>
 * @date: 2016/5/18
 */

'use strict';

import test from 'ava';

import cache from '../../library/cache';

let testKey = 'test_unit_key:' + (new Date()).getTime();
let testValue = 'anotherValue';
let anotherKey = 'test_unit_key2:' + (new Date()).getTime();
let anotherValue = {a: 1};

let slaveTestKey = 'test_unit_key3:' + (new Date()).getTime();
let slaveTestValue = 'anotherValue3';

test.before('set test key', (t) => {
    cache.set(testKey, testValue);
    cache.set(anotherKey, anotherValue);
    t.pass();
});

test.after('del test key', (t) => {
    cache.del(testKey);
    cache.del(anotherKey);
    t.pass();
});

test('cache get test', (t) => {
    return cache.get(testKey).then((v) => {
        t.is(v, testValue);
    });
});

test('cache get multi test', (t) => {
    cache.set(anotherKey, anotherValue);
    return cache.getMulti([testKey, anotherKey]).then((values) => {
        t.is(values[testKey], testValue);
        t.is(values[anotherKey], JSON.stringify(anotherValue));
    });
});

test('cache get from slave test', (t) => {
    return cache.getFromSlave(testKey).then((v) => {
        t.is(v, testValue);
    });
});

test('cache get multi from slave test', (t) => {
    cache.set(anotherKey, anotherValue);
    return cache.getMultiFromSlave([testKey, anotherKey]).then((values) => {
        t.is(values[testKey], testValue);
        t.is(values[anotherKey], JSON.stringify(anotherValue));
    });
});

test('cache set to slave', (t) => {
    return cache.setSlave(slaveTestKey, {
        value: slaveTestValue
    }).then(() => {
        return cache.getFromSlave(slaveTestKey);
    }).then((v) => {
        v = JSON.parse(v);
        t.is(v.value, slaveTestValue);
        cache.del(slaveTestKey);
    });
});

test('cache get test, key is not a string', (t) => {
    return cache.get(123).then((v) => {
        t.notOk(v);
    });
});

test('cache get multi test, key is not an array', (t) => {
    return cache.getMulti(123).then((v) => {
        t.notOk(v);
    });
});

test('cache get from slave test, key is not a string', (t) => {
    return cache.getFromSlave(123).then((v) => {
        t.notOk(v);
    });
});

test('cache get multi from slave test, key is not an array', (t) => {
    return cache.getMultiFromSlave(123).then((v) => {
        t.notOk(v);
    });
});

test('cache set test, key is not a string', (t) => {
    return cache.set(123).then((v) => {
        t.notOk(v);
    });
});

test('cache set multi test, key is not an array', (t) => {
    return cache.setSlave(123).then((v) => {
        t.notOk(v);
    });
});

test('cache del test, key is not a string', (t) => {
    return cache.del(123).then((v) => {
        t.notOk(v);
    });
});