From 8a20d66428f9929460efa33af0281b4c9ad16093 Mon Sep 17 00:00:00 2001 From: uki00a Date: Sun, 24 Nov 2019 19:18:20 +0900 Subject: [PATCH] fix: improve signature of spop (#31) * fix: improve signature of spop * fix: add tests for spop --- redis.ts | 7 +++++-- redis_test.ts | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/redis.ts b/redis.ts index 5b41d4b0..fe16e8af 100644 --- a/redis.ts +++ b/redis.ts @@ -232,7 +232,8 @@ export type Redis = { sismember(key: string, member: string): Promise; smembers(key: string): Promise; smove(source: string, destination: string, member: string): Promise; - spop(key: string, count?: number): Promise; + spop(key: string): Promise; + spop(key: string, count: number): Promise; srandmember(key: string, count?: number): Promise; srem(key: string, ...members: string[]): Promise; sunion(...keys: string[]): Promise; @@ -1266,7 +1267,9 @@ class RedisImpl implements Redis { } } - spop(...args) { + spop(key: string): Promise; + spop(key: string, count: number): Promise; + spop(...args): Promise { return this.execStatusReply("SPOP", ...args); } diff --git a/redis_test.ts b/redis_test.ts index bb58466c..cb33cb33 100644 --- a/redis_test.ts +++ b/redis_test.ts @@ -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 = { @@ -25,7 +26,9 @@ test(async function beforeAll() { "get", "getset", "del1", - "del2" + "del2", + "spop", + "spopWithCount" ); }); @@ -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[] = []; for (const key of ["a", "b", "c"]) {