Skip to content

Commit

Permalink
fix: zrange with 'withScore' option (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
uki00a authored and keroxp committed Dec 23, 2019
1 parent 01a6f98 commit 8e8db4f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,7 @@ class RedisImpl implements Redis {
private pushZrangeOpts(args, opts?) {
if (opts) {
if (opts.withScore) {
args.push("WITHSCORE");
args.push("WITHSCORES");
}
if (opts.offset !== void 0 && opts.count !== void 0) {
args.push("LIMIT", opts.offset, opts.count);
Expand Down
78 changes: 77 additions & 1 deletion redis_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ test(async function beforeAll() {
"del1",
"del2",
"spop",
"spopWithCount"
"spopWithCount",
"zrange",
"zrangeWithScores",
"zrevrange",
"zrevrangeWithScores",
"zrangebyscore",
"zrangebyscoreWithScores",
"zrevrangebyscore",
"zrevrangebyscoreWithScores"
);
});

Expand Down Expand Up @@ -112,6 +120,74 @@ test(async function testSpopWithCount() {
assertArrayContains(v, ["a", "b"]);
});

test(async function testZrange() {
redis.zadd("zrange", 1, "one");
redis.zadd("zrange", 2, "two");
redis.zadd("zrange", 3, "three");
const v = await redis.zrange("zrange", 0, 1);
assertEquals(v, ["one", "two"]);
});

test(async function testZrangeWithScores() {
redis.zadd("zrangeWithScores", 1, "one");
redis.zadd("zrangeWithScores", 2, "two");
redis.zadd("zrangeWithScores", 3, "three");
const v = await redis.zrange("zrangeWithScores", 0, 1, { withScore: true });
assertEquals(v, ["one", "1", "two", "2"]);
});

test(async function testZrevrange() {
redis.zadd("zrevrange", 1, "one");
redis.zadd("zrevrange", 2, "two");
redis.zadd("zrevrange", 3, "three");
const v = await redis.zrevrange("zrevrange", 0, 1)
assertEquals(v, ["three", "two"]);
});

test(async function testZrevrangeWithScores() {
redis.zadd("zrevrangeWithScores", 1, "one");
redis.zadd("zrevrangeWithScores", 2, "two");
redis.zadd("zrevrangeWithScores", 3, "three");
const v = await redis.zrevrange("zrevrangeWithScores", 0, 1, { withScore: true });
assertEquals(v, ["three", "3", "two", "2"]);
});

test(async function testZrangebyscore() {
redis.zadd("zrangebyscore", 2, "m1");
redis.zadd("zrangebyscore", 5, "m2");
redis.zadd("zrangebyscore", 8, "m3");
redis.zadd("zrangebyscore", 10, "m4");
const v = await redis.zrangebyscore("zrangebyscore", 3, 9);
assertEquals(v, ["m2", "m3"]);
});

test(async function testZrangebyscoreWithScores() {
redis.zadd("zrangebyscoreWithScores", 2, "m1");
redis.zadd("zrangebyscoreWithScores", 5, "m2");
redis.zadd("zrangebyscoreWithScores", 8, "m3");
redis.zadd("zrangebyscoreWithScores", 10, "m4");
const v = await redis.zrangebyscore("zrangebyscoreWithScores", 3, 9, { withScore: true });
assertEquals(v, ["m2", "5", "m3", "8"]);
});

test(async function testZrevrangebyscore() {
redis.zadd("zrevrangebyscore", 2, "m1");
redis.zadd("zrevrangebyscore", 5, "m2");
redis.zadd("zrevrangebyscore", 8, "m3");
redis.zadd("zrevrangebyscore", 10, "m4");
const v = await redis.zrevrangebyscore("zrevrangebyscore", 9, 4);
assertEquals(v, ["m3", "m2"]);
});

test(async function testZrevrangebyscore() {
redis.zadd("zrevrangebyscoreWithScores", 2, "m1");
redis.zadd("zrevrangebyscoreWithScores", 5, "m2");
redis.zadd("zrevrangebyscoreWithScores", 8, "m3");
redis.zadd("zrevrangebyscoreWithScores", 10, "m4");
const v = await redis.zrevrangebyscore("zrevrangebyscoreWithScores", 9, 4, { withScore: true });
assertEquals(v, ["m3", "8", "m2", "5"]);
});

test(async function testConcurrent() {
let promises: Promise<any>[] = [];
for (const key of ["a", "b", "c"]) {
Expand Down

0 comments on commit 8e8db4f

Please sign in to comment.