-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add lmpop command * fix: add missing check
- Loading branch information
1 parent
c9dc7e8
commit 36bcd25
Showing
7 changed files
with
142 additions
and
34 deletions.
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,86 @@ | ||
import { keygen, newHttpClient, randomID } from "../test-utils"; | ||
|
||
import { afterAll, describe, expect, test } from "bun:test"; | ||
|
||
import { LmPopCommand } from "./lmpop"; | ||
import { LPushCommand } from "./lpush"; | ||
const client = newHttpClient(); | ||
|
||
const { newKey, cleanup } = keygen(); | ||
afterAll(cleanup); | ||
|
||
describe("LMPOP", () => { | ||
test("should pop elements from the left-most end of the list", async () => { | ||
const key = newKey(); | ||
const lpushElement1 = { name: randomID(), surname: randomID() }; | ||
const lpushElement2 = { name: randomID(), surname: randomID() }; | ||
|
||
await new LPushCommand([key, lpushElement1, lpushElement2]).exec(client); | ||
|
||
const result = await new LmPopCommand<{ name: string; surname: string }>([ | ||
1, | ||
[key], | ||
"LEFT", | ||
2, | ||
]).exec(client); | ||
|
||
expect(result?.[1][0].name).toEqual(lpushElement2.name); | ||
}); | ||
|
||
test("should pop elements from the right-most end of the list", async () => { | ||
const key = newKey(); | ||
const lpushElement1 = randomID(); | ||
const lpushElement2 = randomID(); | ||
|
||
await new LPushCommand([key, lpushElement1, lpushElement2]).exec(client); | ||
|
||
const result = await new LmPopCommand<string>([1, [key], "RIGHT", 2]).exec(client); | ||
|
||
expect(result?.[1][0]).toEqual(lpushElement1); | ||
}); | ||
|
||
test("should pop elements from the first list then second list", async () => { | ||
const key = newKey(); | ||
const lpushElement1 = randomID(); | ||
const lpushElement2 = randomID(); | ||
|
||
const key2 = newKey(); | ||
const lpushElement2_1 = randomID(); | ||
const lpushElement2_2 = randomID(); | ||
|
||
await new LPushCommand([key, lpushElement1, lpushElement2]).exec(client); | ||
await new LPushCommand([key2, lpushElement2_1, lpushElement2_2]).exec(client); | ||
|
||
const result = await new LmPopCommand<string>([2, [key, key2], "RIGHT", 4]).exec(client); | ||
expect(result).toEqual([key, [lpushElement1, lpushElement2]]); | ||
|
||
const result1 = await new LmPopCommand<string>([2, [key, key2], "RIGHT", 4]).exec(client); | ||
expect(result1).toEqual([key2, [lpushElement2_1, lpushElement2_2]]); | ||
}); | ||
|
||
test("should return null after first attempt", async () => { | ||
const key = newKey(); | ||
const lpushElement1 = randomID(); | ||
const lpushElement2 = randomID(); | ||
|
||
await new LPushCommand([key, lpushElement1, lpushElement2]).exec(client); | ||
|
||
await new LmPopCommand([1, [key], "LEFT", 2]).exec(client); | ||
|
||
const result1 = await new LmPopCommand([1, [key], "LEFT", 2]).exec(client); | ||
|
||
expect(result1).toBeNull(); | ||
}); | ||
|
||
test("should return without count", async () => { | ||
const key = newKey(); | ||
const lpushElement1 = randomID(); | ||
const lpushElement2 = randomID(); | ||
|
||
await new LPushCommand([key, lpushElement1, lpushElement2]).exec(client); | ||
|
||
const result1 = await new LmPopCommand([1, [key], "LEFT"]).exec(client); | ||
|
||
expect(result1).toEqual([key, [lpushElement2]]); | ||
}); | ||
}); |
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,18 @@ | ||
import { Command, CommandOptions } from "./command"; | ||
|
||
/** | ||
* @see https://redis.io/commands/lmpop | ||
*/ | ||
export class LmPopCommand<TValues> extends Command< | ||
[string, TValues[]] | null, | ||
[string, TValues[]] | null | ||
> { | ||
constructor( | ||
cmd: [numkeys: number, keys: string[], "LEFT" | "RIGHT", count?: number], | ||
opts?: CommandOptions<[string, TValues[]] | null, [string, TValues[]] | null>, | ||
) { | ||
const [numkeys, keys, direction, count] = cmd; | ||
|
||
super(["LMPOP", numkeys, ...keys, direction, ...(count ? ["COUNT", count] : [])], opts); | ||
} | ||
} |
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