forked from stipsan/ioredis-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Scan command implementation (stipsan#334)
* scan command implementation * Chore: Update feature compat table * rm package-lock * Chore: Update feature compat table * more scan tests
- Loading branch information
1 parent
60f1019
commit aae16cd
Showing
6 changed files
with
189 additions
and
6 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
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,59 @@ | ||
function pattern(str) { | ||
let string = str.replace(/([+{($^|.\\])/g, '\\$1'); | ||
string = string.replace(/(^|[^\\])([*?])/g, '$1.$2'); | ||
string = `^${string}$`; | ||
|
||
const p = new RegExp(string); | ||
return p.test.bind(p); | ||
} | ||
|
||
export function scanHelper( | ||
allKeys, | ||
size, | ||
cursorStart, | ||
opt1Name, | ||
opt1val, | ||
opt2Name, | ||
opt2val | ||
) { | ||
const cursor = parseInt(cursorStart, 10); | ||
if (Number.isNaN(cursor)) throw new Error('Cursor must be integer'); | ||
|
||
let count = 10; | ||
let matchPattern = null; | ||
|
||
const opt1 = (opt1Name || '').toUpperCase(); | ||
const opt2 = (opt2Name || '').toUpperCase(); | ||
if (opt1 === 'MATCH') { | ||
matchPattern = pattern(opt1val); | ||
if (opt2 === 'COUNT') count = parseInt(opt2val, 10); | ||
else if (opt2) { | ||
throw new Error('BAD Syntax'); | ||
} | ||
} else if (opt1 === 'COUNT') { | ||
if (opt2) { | ||
throw new Error('BAD Syntax'); | ||
} | ||
count = parseInt(opt1val, 10); | ||
} else if (opt1) { | ||
throw new Error(`Uknown option ${opt1}`); | ||
} | ||
|
||
if (Number.isNaN(count)) throw new Error('count must be integer'); | ||
|
||
let nextCursor = cursor + count; | ||
const keys = allKeys.slice(cursor, nextCursor); | ||
|
||
// Apply MATCH filtering _after_ getting number of keys | ||
if (matchPattern) { | ||
let i = 0; | ||
while (i < keys.length) | ||
if (!matchPattern(keys[i])) keys.splice(i, size); | ||
else i += size; | ||
} | ||
|
||
// Return 0 when iteration is complete. | ||
if (nextCursor >= allKeys.length) nextCursor = 0; | ||
|
||
return [nextCursor, keys]; | ||
} |
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,6 @@ | ||
import { scanHelper } from './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); | ||
} |
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,117 @@ | ||
import expect from 'expect'; | ||
import MockRedis from '../../src'; | ||
|
||
describe('scan', () => { | ||
it('should return null array if nothing in db', () => { | ||
const redis = new MockRedis(); | ||
return redis.scan(0).then(result => { | ||
expect(result[0]).toBe(0); | ||
expect(result[1]).toEqual([]); | ||
}); | ||
}); | ||
|
||
it('should return keys in db', () => { | ||
const redis = new MockRedis({ | ||
data: { | ||
foo: 'bar', | ||
test: 'bar', | ||
}, | ||
}); | ||
|
||
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 MockRedis(); | ||
return redis.scan('asdf').catch(result => { | ||
expect(result).toBeA(Error); | ||
}); | ||
}); | ||
it('should return fail if incorrect command', () => { | ||
const redis = new MockRedis(); | ||
return redis.scan(0, 'ZU').catch(result => { | ||
expect(result).toBeA(Error); | ||
}); | ||
}); | ||
it('should return fail if incorrect MATCH usage', () => { | ||
const redis = new MockRedis(); | ||
return redis.scan(0, 'MATCH', 'sadf', 'ZU').catch(result => { | ||
expect(result).toBeA(Error); | ||
}); | ||
}); | ||
it('should return fail if incorrect COUNT usage', () => { | ||
const redis = new MockRedis(); | ||
return redis.scan(0, 'COUNT', 10, 'ZU').catch(result => { | ||
expect(result).toBeA(Error); | ||
}); | ||
}); | ||
it('should return fail if incorrect COUNT usage 2', () => { | ||
const redis = new MockRedis(); | ||
return redis.scan(0, 'COUNT', 'adsf').catch(result => { | ||
expect(result).toBeA(Error); | ||
}); | ||
}); | ||
it('should return only mathced keys', () => { | ||
const redis = new MockRedis({ | ||
data: { | ||
foo0: 'x', | ||
foo1: 'x', | ||
foo2: 'x', | ||
test0: 'x', | ||
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 MockRedis({ | ||
data: { | ||
foo0: 'x', | ||
foo1: 'x', | ||
foo2: 'x', | ||
test0: 'x', | ||
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 MockRedis({ | ||
data: { | ||
foo0: 'x', | ||
foo1: 'x', | ||
test0: 'x', | ||
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']); | ||
}); | ||
}); | ||
}); |