cache.test.js
2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* 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);
});
});