cache.test.js 1.73 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) => {
        console.log(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, slaveTestValue).then(() => {
        return cache.getFromSlave(slaveTestKey);
    }).then((v) => {
        t.is(v, slaveTestValue);
        cache.del(slaveTestKey);
    });
});