Skip to content

Commit

Permalink
added incrByFloat command in node.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan Wattad authored Sep 18, 2023
1 parent 9f61ab7 commit 700c62e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
21 changes: 18 additions & 3 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
createGet,
createIncr,
createIncrBy,
createIncrByFloat,
createSet,
} from "./Commands";
import {
Expand Down Expand Up @@ -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<number> {
return this.createWritePromise(createIncrBy(key, increment));
public incrBy(key: string, amount: number): Promise<number> {
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<string> {
return this.createWritePromise(createIncrByFloat(key, amount));
}

private readonly MAP_READ_FROM_REPLICA_STRATEGY: Record<
Expand Down
11 changes: 9 additions & 2 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand Down
21 changes: 18 additions & 3 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
createGet,
createIncr,
createIncrBy,
createIncrByFloat,
createInfo,
createPing,
createSelect,
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 18 additions & 4 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type BaseClient = {
info(options?: InfoOptions[]): Promise<string | string[][]>;
configResetStat: () => Promise<"OK">;
incr: (key: string) => Promise<number>;
incrBy: (key: string, increment: number) => Promise<number>;
incrBy: (key: string, amount: number) => Promise<number>;
incrByFloat: (key: string, amount: number) => Promise<string>;
customCommand: (commandName: string, args: string[]) => Promise<ReturnType>;
};

Expand Down Expand Up @@ -258,7 +259,7 @@ export function runBaseTests<Context>(config: {
);

it(
"incr and incrBy with existing key",
"incr, incrBy and incrByFloat with existing key",
async () => {
await runTest(async (client: BaseClient) => {
const key = uuidv4();
Expand All @@ -267,29 +268,34 @@ export function runBaseTests<Context>(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();
Expand All @@ -309,6 +315,14 @@ export function runBaseTests<Context>(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
Expand Down

0 comments on commit 700c62e

Please sign in to comment.