Skip to content

Commit

Permalink
fix: improve signature of spop (#31)
Browse files Browse the repository at this point in the history
* fix: improve signature of spop

* fix: add tests for spop
  • Loading branch information
uki00a authored and keroxp committed Nov 24, 2019
1 parent 986461e commit 8a20d66
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 5 additions & 2 deletions redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ export type Redis = {
sismember(key: string, member: string): Promise<number>;
smembers(key: string): Promise<string[]>;
smove(source: string, destination: string, member: string): Promise<number>;
spop(key: string, count?: number): Promise<string>;
spop(key: string): Promise<string>;
spop(key: string, count: number): Promise<string[]>;
srandmember(key: string, count?: number): Promise<string>;
srem(key: string, ...members: string[]): Promise<number>;
sunion(...keys: string[]): Promise<string[]>;
Expand Down Expand Up @@ -1266,7 +1267,9 @@ class RedisImpl implements Redis {
}
}

spop(...args) {
spop(key: string): Promise<string>;
spop(key: string, count: number): Promise<string[]>;
spop(...args): Promise<string | string[]> {
return this.execStatusReply("SPOP", ...args);
}

Expand Down
19 changes: 17 additions & 2 deletions redis_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
} from "./vendor/https/deno.land/std/testing/mod.ts";
import {
assertEquals,
assertThrowsAsync
assertThrowsAsync,
assertArrayContains
} from "./vendor/https/deno.land/std/testing/asserts.ts";
// can be substituted with env variable
const addr = {
Expand All @@ -25,7 +26,9 @@ test(async function beforeAll() {
"get",
"getset",
"del1",
"del2"
"del2",
"spop",
"spopWithCount"
);
});

Expand Down Expand Up @@ -97,6 +100,18 @@ test(async function testDecrby() {
assertEquals(await redis.get("decryby"), "-101");
});

test(async function testSpop() {
await redis.sadd("spop", "a");
const v = await redis.spop("spop");
assertEquals(v, "a");
});

test(async function testSpopWithCount() {
await redis.sadd("spopWithCount", "a", "b");
const v = await redis.spop("spopWithCount", 2);
assertArrayContains(v, ["a", "b"]);
});

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

0 comments on commit 8a20d66

Please sign in to comment.