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 zscore command (stipsan#577)
- Loading branch information
Showing
4 changed files
with
61 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Map from 'es6-map'; | ||
|
||
export function zscore(key, member) { | ||
const map = this.data.get(key); | ||
|
||
// @TODO investigate a more stable way to detect sorted lists | ||
if (!map || !(map instanceof Map)) { | ||
return null; | ||
} | ||
|
||
const entry = map.get(member); | ||
|
||
if (!entry) { | ||
return null; | ||
} | ||
|
||
return entry.score.toString(); | ||
} |
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,41 @@ | ||
import Map from 'es6-map'; | ||
import expect from 'expect'; | ||
|
||
import MockRedis from '../../src'; | ||
|
||
describe('zscore', () => { | ||
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' }], | ||
]), | ||
bar: 'not a sorted set', | ||
}; | ||
|
||
it('should return the score of an existing member as a string', () => { | ||
const redis = new MockRedis({ data }); | ||
|
||
return redis.zscore('foo', 'third').then(res => expect(res).toBe('3')); | ||
}); | ||
|
||
it('should return null when the member does not exist', () => { | ||
const redis = new MockRedis({ data }); | ||
|
||
return redis.zscore('foo', 'sixth').then(res => expect(res).toNotExist()); | ||
}); | ||
|
||
it('should return null when the key is not a sorted set', () => { | ||
const redis = new MockRedis({ data }); | ||
|
||
return redis.zscore('bar', 'first').then(res => expect(res).toNotExist()); | ||
}); | ||
|
||
it('should return null when the key does not exist', () => { | ||
const redis = new MockRedis({ data }); | ||
|
||
return redis.zscore('baz', 'first').then(res => expect(res).toNotExist()); | ||
}); | ||
}); |