Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
121 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import { convertStringToBuffer } from '../commands-utils/convertStringToBuffer' | ||
import { scanHelper } from '../commands-utils/scan-command.common' | ||
|
||
export function scan(cursor, opt1, opt1val, opt2, opt2val) { | ||
const allKeys = this.data.keys() | ||
return scanHelper(allKeys, 1, cursor, opt1, opt1val, opt2, opt2val) | ||
} | ||
|
||
export function scanBuffer(...args) { | ||
const val = scan.apply(this, args) | ||
return convertStringToBuffer(val) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`scan should return null array if nothing in db 1`] = ` | ||
Array [ | ||
"0", | ||
Array [], | ||
] | ||
`; | ||
|
||
exports[`scanBuffer should return null array if nothing in db 1`] = ` | ||
Array [ | ||
Buffer<30>, | ||
Array [], | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,118 @@ | ||
import Redis from 'ioredis' | ||
import { convertBufferToString } from 'ioredis/built/utils' | ||
import sortBy from 'lodash.sortby' | ||
|
||
describe('scan', () => { | ||
it('should return null array if nothing in db', () => { | ||
const redis = new Redis() | ||
return redis.scan(0).then(result => { | ||
expect(result[0]).toBe('0') | ||
expect(result[1]).toEqual([]) | ||
}) | ||
}) | ||
// eslint-disable-next-line import/no-relative-parent-imports | ||
import { browserSafeDescribe, runTwinSuite } from '../../../test-utils' | ||
|
||
it('should return keys in db', () => { | ||
const redis = new Redis({ | ||
data: { | ||
foo: 'bar', | ||
test: 'bar', | ||
}, | ||
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() | ||
}) | ||
|
||
return redis.scan(0).then(result => { | ||
expect(result[0]).toBe('0') | ||
expect(result[1]).toEqual(['foo', 'test']) | ||
}) | ||
}) | ||
it('should return fail if incorrect count', () => { | ||
const redis = new Redis() | ||
return redis.scan('asdf').catch(result => { | ||
expect(result).toBeInstanceOf(Error) | ||
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 command', () => { | ||
const redis = new Redis() | ||
return redis.scan(0, 'ZU').catch(result => { | ||
expect(result).toBeInstanceOf(Error) | ||
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 MATCH usage', () => { | ||
const redis = new Redis() | ||
return redis.scan(0, 'MATCH', 'sadf', 'ZU').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 COUNT usage', () => { | ||
const redis = new Redis() | ||
return redis.scan(0, 'COUNT', 10, '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 2', () => { | ||
const redis = new Redis() | ||
return redis.scan(0, 'COUNT', 'adsf').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 only mathced keys', () => { | ||
const redis = new Redis({ | ||
data: { | ||
foo0: 'x', | ||
foo1: 'x', | ||
foo2: 'x', | ||
test0: 'x', | ||
test1: 'x', | ||
}, | ||
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.scan(0, 'MATCH', 'foo*').then(result => { | ||
expect(result[0]).toBe('0') | ||
expect(result[1]).toEqual(['foo0', 'foo1', 'foo2']) | ||
}) | ||
}) | ||
it('should return only mathced keys and limit by COUNT', () => { | ||
const redis = new Redis({ | ||
data: { | ||
foo0: 'x', | ||
foo1: 'x', | ||
foo2: 'x', | ||
test0: 'x', | ||
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 | ||
.scan(0, 'MATCH', 'foo*', 'COUNT', 1) | ||
.then(result => { | ||
expect(result[0]).toBe('1') // more elements left, this is why cursor is not 0 | ||
expect(result[1]).toEqual(['foo0']) | ||
return redis.scan(result[0], 'MATCH', 'foo*', 'COUNT', 10) | ||
}) | ||
.then(result2 => { | ||
expect(result2[0]).toBe('0') | ||
expect(result2[1]).toEqual(['foo1', 'foo2']) | ||
}) | ||
}) | ||
it('should return number of keys set by COUNT and continue by cursor', () => { | ||
const redis = new Redis({ | ||
data: { | ||
foo0: 'x', | ||
foo1: 'x', | ||
test0: 'x', | ||
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 | ||
.scan(0, 'COUNT', 3) | ||
.then(result => { | ||
expect(result[0]).toBe('3') | ||
expect(result[1]).toEqual(['foo0', 'foo1', 'test0']) | ||
return redis.scan(result[0], 'COUNT', 3) | ||
}) | ||
.then(result2 => { | ||
expect(result2[0]).toBe('0') | ||
expect(result2[1]).toEqual(['test1']) | ||
}) | ||
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']) | ||
}) | ||
}) | ||
}) | ||
}) |