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: Add zrevrangebyscore command (stipsan#442)
* Add support for zrevrangebyscore * Support optional parameter WITHSCORES for zrangebyscore and zrevrangebyscore
- Loading branch information
Showing
7 changed files
with
154 additions
and
37 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
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,28 @@ | ||
import Map from 'es6-map'; | ||
import arrayFrom from 'array-from'; | ||
import { orderBy, filter, flatMap } from 'lodash'; | ||
import { parseLimit, filterPredicate } from './zrange-command.common'; | ||
|
||
export function zrevrangebyscore(key, inputMax, inputMin, withScores) { | ||
const map = this.data.get(key); | ||
if (!map) { | ||
return []; | ||
} | ||
|
||
if (this.data.has(key) && !(this.data.get(key) instanceof Map)) { | ||
return []; | ||
} | ||
|
||
const min = parseLimit(inputMin); | ||
const max = parseLimit(inputMax); | ||
const filteredArray = filter( | ||
arrayFrom(map.values()), | ||
filterPredicate(min, max) | ||
); | ||
|
||
const ordered = orderBy(filteredArray, 'score', 'desc'); | ||
if (withScores === 'WITHSCORES') { | ||
return flatMap(ordered, it => [it.value, it.score]); | ||
} | ||
return ordered.map(it => it.value); | ||
} |
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,76 @@ | ||
import Map from 'es6-map'; | ||
import expect from 'expect'; | ||
|
||
import MockRedis from '../../src'; | ||
|
||
describe('zrevrangebyscore', () => { | ||
const data = { | ||
foo: new Map([ | ||
['first', { score: 1, value: 'first' }], | ||
['second', { score: 2, value: 'second' }], | ||
['third', { score: 3, value: 'third' }], | ||
['fourth', { score: 4, value: 'fourth' }], | ||
['fifth', { score: 5, value: 'fifth' }], | ||
]), | ||
}; | ||
it('should return using not strict compare', () => { | ||
const redis = new MockRedis({ data }); | ||
|
||
return redis | ||
.zrevrangebyscore('foo', 3, 1) | ||
.then(res => expect(res).toEqual(['third', 'second', 'first'])); | ||
}); | ||
|
||
it('should return using strict compare', () => { | ||
const redis = new MockRedis({ data }); | ||
|
||
return redis | ||
.zrevrangebyscore('foo', 5, '(3') | ||
.then(res => expect(res).toEqual(['fifth', 'fourth'])); | ||
}); | ||
|
||
it('should accept infinity string', () => { | ||
const redis = new MockRedis({ data }); | ||
|
||
return redis | ||
.zrevrangebyscore('foo', '+inf', '-inf') | ||
.then(res => | ||
expect(res).toEqual(['fifth', 'fourth', 'third', 'second', 'first']) | ||
); | ||
}); | ||
|
||
it('should return empty array if out-of-range', () => { | ||
const redis = new MockRedis({ data }); | ||
|
||
return redis | ||
.zrevrangebyscore('foo', 100, 10) | ||
.then(res => expect(res).toEqual([])); | ||
}); | ||
|
||
it('should return empty array if key not found', () => { | ||
const redis = new MockRedis({ data }); | ||
|
||
return redis | ||
.zrevrangebyscore('boo', 100, 10) | ||
.then(res => expect(res).toEqual([])); | ||
}); | ||
|
||
it('should return empty array if the key contains something other than a list', () => { | ||
const redis = new MockRedis({ | ||
data: { | ||
foo: 'not a list', | ||
}, | ||
}); | ||
|
||
return redis | ||
.zrevrangebyscore('foo', 2, 1) | ||
.then(res => expect(res).toEqual([])); | ||
}); | ||
|
||
it('should include scores if WITHSCORES is specified', () => { | ||
const redis = new MockRedis({ data }); | ||
return redis | ||
.zrevrangebyscore('foo', 3, 1, 'WITHSCORES') | ||
.then(res => expect(res).toEqual(['third', 3, 'second', 2, 'first', 1])); | ||
}); | ||
}); |