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: Implement ZREMRANGEBYSCORE (stipsan#539)
Implementing https://redis.io/commands/zremrangebyscore.
- Loading branch information
1 parent
e9a263c
commit dc124d5
Showing
4 changed files
with
104 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,13 @@ | ||
import { zrevrangebyscore } from './index'; | ||
|
||
export function zremrangebyscore(key, inputMax, inputMin) { | ||
const vals = zrevrangebyscore.call(this, key, inputMax, inputMin); | ||
|
||
const map = this.data.get(key); | ||
vals.forEach(val => { | ||
map.delete(val); | ||
}); | ||
this.data.set(key, map); | ||
|
||
return vals.length; | ||
} |
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,89 @@ | ||
import Map from 'es6-map'; | ||
import expect from 'expect'; | ||
|
||
import MockRedis from '../../src'; | ||
|
||
describe('zremrangebyscore', () => { | ||
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 remove using not strict compare', () => { | ||
const redis = new MockRedis({ data }); | ||
|
||
return redis | ||
.zremrangebyscore('foo', 3, 1) | ||
.then(res => expect(res).toBe(3)) | ||
.then(() => { | ||
expect(redis.data.get('foo').has('first')).toBe(false); | ||
expect(redis.data.get('foo').has('second')).toBe(false); | ||
expect(redis.data.get('foo').has('third')).toBe(false); | ||
expect(redis.data.get('foo').has('fourth')).toBe(true); | ||
expect(redis.data.get('foo').has('fifth')).toBe(true); | ||
}); | ||
}); | ||
|
||
it('should return using strict compare', () => { | ||
const redis = new MockRedis({ data }); | ||
|
||
return redis | ||
.zremrangebyscore('foo', 5, '(3') | ||
.then(res => expect(res).toEqual(2)) | ||
.then(() => { | ||
expect(redis.data.get('foo').has('first')).toBe(true); | ||
expect(redis.data.get('foo').has('second')).toBe(true); | ||
expect(redis.data.get('foo').has('third')).toBe(true); | ||
expect(redis.data.get('foo').has('fourth')).toBe(false); | ||
expect(redis.data.get('foo').has('fifth')).toBe(false); | ||
}); | ||
}); | ||
|
||
it('should accept infinity string', () => { | ||
const redis = new MockRedis({ data }); | ||
|
||
return redis | ||
.zremrangebyscore('foo', '+inf', '-inf') | ||
.then(res => expect(res).toEqual(5)) | ||
.then(() => { | ||
expect(redis.data.get('foo').has('first')).toBe(false); | ||
expect(redis.data.get('foo').has('second')).toBe(false); | ||
expect(redis.data.get('foo').has('third')).toBe(false); | ||
expect(redis.data.get('foo').has('fourth')).toBe(false); | ||
expect(redis.data.get('foo').has('fifth')).toBe(false); | ||
}); | ||
}); | ||
|
||
it('should return zero if out-of-range', () => { | ||
const redis = new MockRedis({ data }); | ||
|
||
return redis | ||
.zremrangebyscore('foo', 100, 10) | ||
.then(res => expect(res).toEqual(0)); | ||
}); | ||
|
||
it('should return zero if key not found', () => { | ||
const redis = new MockRedis({ data }); | ||
|
||
return redis | ||
.zremrangebyscore('boo', 100, 10) | ||
.then(res => expect(res).toEqual(0)); | ||
}); | ||
|
||
it('should return zero if the key contains something other than a list', () => { | ||
const redis = new MockRedis({ | ||
data: { | ||
foo: 'not a list', | ||
}, | ||
}); | ||
|
||
return redis | ||
.zremrangebyscore('foo', 2, 1) | ||
.then(res => expect(res).toEqual(0)); | ||
}); | ||
}); |