-
Notifications
You must be signed in to change notification settings - Fork 125
/
scan.js
118 lines (111 loc) · 4.02 KB
/
scan.js
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
115
116
117
118
import Redis from 'ioredis'
import { convertBufferToString } from 'ioredis/built/utils'
import sortBy from 'lodash.sortby'
// eslint-disable-next-line import/no-relative-parent-imports
import { browserSafeDescribe, runTwinSuite } from '../../../test-utils'
runTwinSuite('scan', command => {
browserSafeDescribe(command)(command, () => {
it('should return null array if nothing in db', async () => {
const redis = new Redis()
const result = await redis[command](0)
expect(result).toMatchSnapshot()
})
it('should return keys in db', async () => {
const redis = new Redis()
await redis.set('foo', 'bar')
await redis.set('test', 'bar')
return redis[command](0).then(result => {
expect(convertBufferToString(result[0])).toBe('0')
expect(sortBy(convertBufferToString(result[1]))).toEqual([
'foo',
'test',
])
})
})
it('should return fail if incorrect count', () => {
const redis = new Redis()
return redis[command]('asdf').catch(result => {
expect(result).toBeInstanceOf(Error)
})
})
it('should return fail if incorrect command', () => {
const redis = new Redis()
return redis[command](0, 'ZU').catch(result => {
expect(result).toBeInstanceOf(Error)
})
})
it('should return fail if incorrect MATCH usage', () => {
const redis = new Redis()
return redis[command](0, 'MATCH', 'sadf', 'ZU').catch(result => {
expect(result).toBeInstanceOf(Error)
})
})
it('should return fail if incorrect COUNT usage', () => {
const redis = new Redis()
return redis[command](0, 'COUNT', 10, 'ZU').catch(result => {
expect(result).toBeInstanceOf(Error)
})
})
it('should return fail if incorrect COUNT usage 2', () => {
const redis = new Redis()
return redis[command](0, 'COUNT', 'adsf').catch(result => {
expect(result).toBeInstanceOf(Error)
})
})
it('should return only mathced keys', async () => {
const redis = new Redis()
await redis.set('foo0', 'x')
await redis.set('foo1', 'x')
await redis.set('foo2', 'x')
await redis.set('test0', 'x')
await redis.set('test1', 'x')
return redis[command](0, 'MATCH', 'foo*').then(result => {
expect(convertBufferToString(result[0])).toBe('0')
expect(sortBy(convertBufferToString(result[1]))).toEqual([
'foo0',
'foo1',
'foo2',
])
})
})
it('should return only mathced keys and limit by COUNT', async () => {
const redis = new Redis()
await redis.set('foo0', 'x')
await redis.set('foo1', 'x')
await redis.set('foo2', 'x')
await redis.set('test0', 'x')
await redis.set('test1', 'x')
return redis[command](0, 'MATCH', 'foo*', 'COUNT', 1)
.then(result => {
expect(convertBufferToString(result[0])).not.toBe('0') // more elements left, this is why cursor is not 0
expect(result[1]).toEqual(expect.any(Array))
return redis[command](result[0], 'MATCH', 'foo*', 'COUNT', 10)
})
.then(result2 => {
expect(convertBufferToString(result2[0])).toBe('0')
expect(result2[1]).toEqual(expect.any(Array))
})
})
it('should return number of keys set by COUNT and continue by cursor', async () => {
const redis = new Redis()
await redis.set('foo0', 'x')
await redis.set('foo1', 'x')
await redis.set('test0', 'x')
await redis.set('test1', 'x')
return redis[command](0, 'COUNT', 3)
.then(result => {
expect(convertBufferToString(result[0])).toBe('3')
expect(sortBy(convertBufferToString(result[1]))).toEqual([
'foo0',
'foo1',
'test0',
])
return redis[command](result[0], 'COUNT', 3)
})
.then(result2 => {
expect(convertBufferToString(result2[0])).toBe('0')
expect(convertBufferToString(result2[1])).toEqual(['test1'])
})
})
})
})