From 8eb9ba8dd1a370883314ba2d01da7eb2cc20166e Mon Sep 17 00:00:00 2001 From: Andreas Thomas Date: Mon, 19 Dec 2022 15:40:21 +0100 Subject: [PATCH 1/2] feat: lmove --- pkg/commands/lmove.test.ts | 57 ++++++++++++++++++++++++++++++++++++++ pkg/commands/lmove.ts | 18 ++++++++++++ pkg/commands/mod.ts | 1 + pkg/pipeline.test.ts | 3 +- pkg/pipeline.ts | 7 +++++ pkg/redis.ts | 7 +++++ 6 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 pkg/commands/lmove.test.ts create mode 100644 pkg/commands/lmove.ts diff --git a/pkg/commands/lmove.test.ts b/pkg/commands/lmove.test.ts new file mode 100644 index 00000000..a9f52cee --- /dev/null +++ b/pkg/commands/lmove.test.ts @@ -0,0 +1,57 @@ +import { keygen, newHttpClient, randomID } from "../test-utils.ts"; + +import { afterAll } from "https://deno.land/std@0.152.0/testing/bdd.ts"; +import { LPushCommand } from "./lpush.ts"; +import { assertEquals } from "https://deno.land/std@0.152.0/testing/asserts.ts"; +import { LMoveCommand } from "./lmove.ts"; +import { LIndexCommand } from "./lindex.ts"; +import { LPopCommand } from "./lpop.ts"; +import { LLenCommand } from "./llen.ts"; + +const client = newHttpClient(); + +const { newKey, cleanup } = keygen(); +afterAll(cleanup); + +Deno.test("moves the entry from left to left", async () => { + const source = newKey(); + const destination = newKey(); + const value = randomID(); + await new LPushCommand([source, value]).exec( + client, + ); + + const res = await new LMoveCommand([source, destination, "left", "left"]) + .exec(client); + assertEquals(res, value); + + const elementInSource = await new LPopCommand([source]).exec(client); + assertEquals(elementInSource, null); + + const elementInDestination = await new LPopCommand([destination]).exec( + client, + ); + assertEquals(elementInDestination, value); +}); + +Deno.test("moves the entry from left to right", async () => { + const source = newKey(); + const destination = newKey(); + const values = new Array(5).fill(0).map((_) => randomID()); + + await new LPushCommand([source, ...values]).exec( + client, + ); + + const res = await new LMoveCommand([source, destination, "left", "right"]) + .exec(client); + assertEquals(res, values.at(-1)); + + const elementsInSource = await new LLenCommand([source]).exec(client); + assertEquals(elementsInSource, values.length - 1); + + const elementInDestination = await new LPopCommand([destination]).exec( + client, + ); + assertEquals(elementInDestination, values.at(-1)); +}); diff --git a/pkg/commands/lmove.ts b/pkg/commands/lmove.ts new file mode 100644 index 00000000..42118112 --- /dev/null +++ b/pkg/commands/lmove.ts @@ -0,0 +1,18 @@ +import { Command, CommandOptions } from "./command.ts"; + +/** + * @see https://redis.io/commands/lmove + */ +export class LMoveCommand extends Command { + constructor( + cmd: [ + source: string, + destination: string, + whereFrom: "left" | "right", + whereTo: "left" | "right", + ], + opts?: CommandOptions, + ) { + super(["lmove", ...cmd], opts); + } +} diff --git a/pkg/commands/mod.ts b/pkg/commands/mod.ts index d032df34..44c1410d 100644 --- a/pkg/commands/mod.ts +++ b/pkg/commands/mod.ts @@ -43,6 +43,7 @@ export * from "./keys.ts"; export * from "./lindex.ts"; export * from "./linsert.ts"; export * from "./llen.ts"; +export * from "./lmove.ts"; export * from "./lpop.ts"; export * from "./lpos.ts"; export * from "./lpush.ts"; diff --git a/pkg/pipeline.test.ts b/pkg/pipeline.test.ts index ce73ea2b..6583c613 100644 --- a/pkg/pipeline.test.ts +++ b/pkg/pipeline.test.ts @@ -141,6 +141,7 @@ Deno.test("use all the things", async (t) => { .lindex(newKey(), 0) .linsert(newKey(), "before", "pivot", "value") .llen(newKey()) + .lmove(newKey(), newKey(), "left", "right") .lpop(newKey()) .lpos(newKey(), "value") .lpush(persistentKey, "element") @@ -218,6 +219,6 @@ Deno.test("use all the things", async (t) => { .zunionstore(newKey(), 1, [newKey()]); const res = await p.exec(); - assertEquals(res.length, 118); + assertEquals(res.length, 119); }); }); diff --git a/pkg/pipeline.ts b/pkg/pipeline.ts index c98fc571..2b5581f6 100644 --- a/pkg/pipeline.ts +++ b/pkg/pipeline.ts @@ -42,6 +42,7 @@ import { LIndexCommand, LInsertCommand, LLenCommand, + LMoveCommand, LPopCommand, LPosCommand, LPushCommand, @@ -531,6 +532,12 @@ export class Pipeline { llen = (...args: CommandArgs) => this.chain(new LLenCommand(args, this.commandOptions)); + /** + * @see https://redis.io/commands/lmove + */ + lmove = (...args: CommandArgs) => + this.chain(new LMoveCommand(args, this.commandOptions)); + /** * @see https://redis.io/commands/lpop */ diff --git a/pkg/redis.ts b/pkg/redis.ts index 19d71ac9..1cf2349c 100644 --- a/pkg/redis.ts +++ b/pkg/redis.ts @@ -44,6 +44,7 @@ import { LIndexCommand, LInsertCommand, LLenCommand, + LMoveCommand, LPopCommand, LPosCommand, LPushCommand, @@ -509,6 +510,12 @@ export class Redis { llen = (...args: CommandArgs) => new LLenCommand(args, this.opts).exec(this.client); + /** + * @see https://redis.io/commands/lmove + */ + lmove = (...args: CommandArgs) => + new LMoveCommand(args, this.opts).exec(this.client); + /** * @see https://redis.io/commands/lpop */ From e96d7c4b70782cb7ed33bb43264a545be887872b Mon Sep 17 00:00:00 2001 From: Andreas Thomas Date: Mon, 19 Dec 2022 15:42:38 +0100 Subject: [PATCH 2/2] chore: remove unused imports --- pkg/commands/lmove.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/commands/lmove.test.ts b/pkg/commands/lmove.test.ts index a9f52cee..0a80c229 100644 --- a/pkg/commands/lmove.test.ts +++ b/pkg/commands/lmove.test.ts @@ -4,7 +4,6 @@ import { afterAll } from "https://deno.land/std@0.152.0/testing/bdd.ts"; import { LPushCommand } from "./lpush.ts"; import { assertEquals } from "https://deno.land/std@0.152.0/testing/asserts.ts"; import { LMoveCommand } from "./lmove.ts"; -import { LIndexCommand } from "./lindex.ts"; import { LPopCommand } from "./lpop.ts"; import { LLenCommand } from "./llen.ts";