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 zcard command stipsan#702 (stipsan#732)
- Loading branch information
Showing
4 changed files
with
50 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,12 @@ | ||
import Map from 'es6-map'; | ||
|
||
export function zcard(key) { | ||
const map = this.data.get(key); | ||
if (!map) { | ||
return 0; | ||
} | ||
if (!(map instanceof Map)) { | ||
throw new Error(`Key ${key} does not contain a sorted set`); | ||
} | ||
return this.data.get(key).size; | ||
} |
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,36 @@ | ||
import expect from 'expect'; | ||
import Map from 'es6-map'; | ||
|
||
import MockRedis from '../../src'; | ||
|
||
describe('zcard', () => { | ||
it('should return the number of items in the sorted set', () => { | ||
const redis = new MockRedis({ | ||
data: { | ||
foo: new Map([[1, 'one'], [3, 'three'], [4, 'four']]), | ||
}, | ||
}); | ||
|
||
return redis.zcard('foo').then(length => expect(length).toBe(3)); | ||
}); | ||
|
||
it('should return 0 if the sorted set does not exist', () => { | ||
const redis = new MockRedis(); | ||
|
||
return redis.zcard('foo').then(length => expect(length).toBe(0)); | ||
}); | ||
|
||
it('should throw an exception if the key contains something other than a sorted set', () => { | ||
const redis = new MockRedis({ | ||
data: { | ||
foo: 'not a sorted set', | ||
}, | ||
}); | ||
|
||
return redis | ||
.zcard('foo') | ||
.catch(err => | ||
expect(err.message).toBe('Key foo does not contain a sorted set') | ||
); | ||
}); | ||
}); |