From 700c62ea1f1e02360407709c6b69ab0d29fdc6a4 Mon Sep 17 00:00:00 2001 From: Adan Wattad Date: Mon, 18 Sep 2023 11:56:29 +0300 Subject: [PATCH] added incrByFloat command in node. --- node/src/BaseClient.ts | 21 ++++++++++++++++++--- node/src/Commands.ts | 11 +++++++++-- node/src/Transaction.ts | 21 ++++++++++++++++++--- node/tests/SharedTests.ts | 22 ++++++++++++++++++---- 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index 22b507bbfe..b3492e8ce5 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -11,6 +11,7 @@ import { createGet, createIncr, createIncrBy, + createIncrByFloat, createSet, } from "./Commands"; import { @@ -307,12 +308,26 @@ export class BaseClient { * See https://redis.io/commands/incrby/ for details. * * @param key - The key to increment it's value. - * @param increment - The increment to the key's value. + * @param amount - The amount to increment. * @returns the value of key after the increment, An error is returned if the key contains a value * of the wrong type or contains a string that can not be represented as integer. */ - public incrBy(key: string, increment: number): Promise { - return this.createWritePromise(createIncrBy(key, increment)); + public incrBy(key: string, amount: number): Promise { + return this.createWritePromise(createIncrBy(key, amount)); + } + + /** Increment the string representing a floating point number stored at key by the specified increment. + * By using a negative increment value, the result is that the value stored at the key is decremented. + * If the key does not exist, it is set to 0 before performing the operation. + * See https://redis.io/commands/incrbyfloat/ for details. + * + * @param key - The key to increment it's value. + * @param amount - The amount to increment. + * @returns the value of key after the increment as string, An error is returned if the key contains a value of the wrong type. + * + */ + public incrByFloat(key: string, amount: number): Promise { + return this.createWritePromise(createIncrByFloat(key, amount)); } private readonly MAP_READ_FROM_REPLICA_STRATEGY: Record< diff --git a/node/src/Commands.ts b/node/src/Commands.ts index d9be109d10..7aebecdcb5 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -189,9 +189,16 @@ export function createIncr(key: string): redis_request.Command { export function createIncrBy( key: string, - increment: number + amount: number ): redis_request.Command { - return createCommand(RequestType.IncrBy, [key, increment.toString()]); + return createCommand(RequestType.IncrBy, [key, amount.toString()]); +} + +export function createIncrByFloat( + key: string, + amount: number +): redis_request.Command { + return createCommand(RequestType.IncrByFloat, [key, amount.toString()]); } export function createCustomCommand(commandName: string, args: string[]) { diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index 177ac492f2..272bdb5c09 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -8,6 +8,7 @@ import { createGet, createIncr, createIncrBy, + createIncrByFloat, createInfo, createPing, createSelect, @@ -107,12 +108,26 @@ export class BaseTransaction { * See https://redis.io/commands/incrby/ for details. * * @param key - The key to increment it's value. - * @param increment - The increment to the key's value. + * @param amount - The amount to increment. * Returns the value of key after the increment, An error is returned if the key contains a value * of the wrong type or contains a string that can not be represented as integer. */ - public incrBy(key: string, increment: number) { - this.commands.push(createIncrBy(key, increment)); + public incrBy(key: string, amount: number) { + this.commands.push(createIncrBy(key, amount)); + } + + /** Increment the string representing a floating point number stored at key by the specified increment. + * By using a negative increment value, the result is that the value stored at the key is decremented. + * If the key does not exist, it is set to 0 before performing the operation. + * See https://redis.io/commands/incrbyfloat/ for details. + * + * @param key - The key to increment it's value. + * @param amount - The amount to increment. + * Returns the value of key after the increment as string, An error is returned if the key contains a value of the wrong type. + * + */ + public incrByFloat(key: string, amount: number) { + this.commands.push(createIncrByFloat(key, amount)); } /** Executes a single command, without checking inputs. Every part of the command, including subcommands, diff --git a/node/tests/SharedTests.ts b/node/tests/SharedTests.ts index 6bb67b98e5..ddbc613d49 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -17,7 +17,8 @@ type BaseClient = { info(options?: InfoOptions[]): Promise; configResetStat: () => Promise<"OK">; incr: (key: string) => Promise; - incrBy: (key: string, increment: number) => Promise; + incrBy: (key: string, amount: number) => Promise; + incrByFloat: (key: string, amount: number) => Promise; customCommand: (commandName: string, args: string[]) => Promise; }; @@ -258,7 +259,7 @@ export function runBaseTests(config: { ); it( - "incr and incrBy with existing key", + "incr, incrBy and incrByFloat with existing key", async () => { await runTest(async (client: BaseClient) => { const key = uuidv4(); @@ -267,29 +268,34 @@ export function runBaseTests(config: { expect(await client.get(key)).toEqual("11"); expect(await client.incrBy(key, 4)).toEqual(15); expect(await client.get(key)).toEqual("15"); + expect(await client.incrByFloat(key, 1.5)).toEqual("16.5"); + expect(await client.get(key)).toEqual("16.5"); }); }, config.timeout ); it( - "incr and incrBy with non existing key", + "incr, incrBy and incrByFloat with non existing key", async () => { await runTest(async (client: BaseClient) => { const key1 = uuidv4(); const key2 = uuidv4(); + const key3 = uuidv4(); /// key1 and key2 does not exist, so it set to 0 before performing the operation. expect(await client.incr(key1)).toEqual(1); expect(await client.get(key1)).toEqual("1"); expect(await client.incrBy(key2, 2)).toEqual(2); expect(await client.get(key2)).toEqual("2"); + expect(await client.incrByFloat(key3, -0.5)).toEqual("-0.5"); + expect(await client.get(key3)).toEqual("-0.5"); }); }, config.timeout ); it( - "incr and incrBy with a key that contains a value of string that can not be represented as integer", + "incr, incrBy and incrByFloat with a key that contains a value of string that can not be represented as integer", async () => { await runTest(async (client: BaseClient) => { const key = uuidv4(); @@ -309,6 +315,14 @@ export function runBaseTests(config: { "value is not an integer" ); } + + try { + expect(await client.incrByFloat(key, 1.5)).toThrow(); + } catch (e) { + expect((e as Error).message).toMatch( + "value is not a valid float" + ); + } }); }, config.timeout