Skip to content

Commit b3c51a0

Browse files
committed
fixed set command optional arguments and added tests to set command
1 parent 1415d4d commit b3c51a0

3 files changed

Lines changed: 125 additions & 7 deletions

File tree

node/src/Commands.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ export function createSet(
145145
if (options.expiry === "keepExisting") {
146146
args.push("KEEPTTL");
147147
} else if (options.expiry?.type === "seconds") {
148-
args.push("EX " + options.expiry.count);
148+
args.push("EX", String(options.expiry.count));
149149
} else if (options.expiry?.type === "milliseconds") {
150-
args.push("PX " + options.expiry.count);
150+
args.push("PX", String(options.expiry.count));
151151
} else if (options.expiry?.type === "unixSeconds") {
152-
args.push("EXAT " + options.expiry.count);
152+
args.push("EXAT", String(options.expiry.count));
153153
} else if (options.expiry?.type === "unixMilliseconds") {
154-
args.push("PXAT " + options.expiry.count);
154+
args.push("PXAT", String(options.expiry.count));
155155
}
156156
}
157157

node/tests/RedisClientInternals.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,12 +524,13 @@ describe("SocketConnectionInternals", () => {
524524
throw new Error("no args");
525525
}
526526

527-
expect(args.length).toEqual(5);
527+
expect(args.length).toEqual(6);
528528
expect(args[0]).toEqual("foo");
529529
expect(args[1]).toEqual("bar");
530530
expect(args[2]).toEqual("XX");
531531
expect(args[3]).toEqual("GET");
532-
expect(args[4]).toEqual("EX 10");
532+
expect(args[4]).toEqual("EX");
533+
expect(args[5]).toEqual("10");
533534
sendResponse(socket, ResponseType.OK, request.callbackIdx);
534535
});
535536
const request1 = connection.set("foo", "bar", {
@@ -538,7 +539,7 @@ describe("SocketConnectionInternals", () => {
538539
expiry: { type: "seconds", count: 10 },
539540
});
540541

541-
await expect(await request1).toMatch("OK");
542+
expect(await request1).toMatch("OK");
542543
});
543544
});
544545

node/tests/SharedTests.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,6 +2527,123 @@ export function runBaseTests<Context>(config: {
25272527
},
25282528
config.timeout,
25292529
);
2530+
2531+
async function classicSet(client: BaseClient, key: string, value: string) {
2532+
const setRes = await client.set(key, value);
2533+
expect(setRes).toEqual("OK");
2534+
const getRes = await client.get(key);
2535+
expect(getRes).toEqual(value);
2536+
}
2537+
2538+
async function setWithExpiryOptions(
2539+
client: BaseClient,
2540+
key: string,
2541+
value: string,
2542+
) {
2543+
const setRes = await client.set(key, value, {
2544+
expiry: {
2545+
type: "milliseconds",
2546+
count: 500,
2547+
},
2548+
});
2549+
expect(setRes).toEqual("OK");
2550+
const getRes = await client.get(key);
2551+
expect(getRes).toEqual(value);
2552+
2553+
const setRes2 = await client.set(key, value, {
2554+
expiry: {
2555+
type: "seconds",
2556+
count: 1,
2557+
},
2558+
});
2559+
expect(setRes2).toEqual("OK");
2560+
const getRes2 = await client.get(key);
2561+
expect(getRes2).toEqual(value);
2562+
2563+
const setRes3 = await client.set(key, value, {
2564+
expiry: "keepExisting",
2565+
});
2566+
expect(setRes3).toEqual("OK");
2567+
const getRes3 = await client.get(key);
2568+
expect(getRes3).toEqual(value);
2569+
2570+
const setRes4 = await client.set(key, value, {
2571+
expiry: {
2572+
type: "unixMilliseconds",
2573+
count: 2,
2574+
},
2575+
});
2576+
expect(setRes4).toEqual("OK");
2577+
setTimeout(() => {}, 5);
2578+
const getRes4 = await client.get(key);
2579+
expect(getRes4).toEqual(null);
2580+
}
2581+
2582+
async function setWithExistOptions(
2583+
client: BaseClient,
2584+
key: string,
2585+
value: string,
2586+
) {
2587+
const setRes = await client.set(key, value, {
2588+
conditionalSet: "onlyIfExists",
2589+
});
2590+
expect(setRes).toEqual("OK");
2591+
const getRes = await client.get(key);
2592+
expect(getRes).toEqual(value);
2593+
2594+
const setRes2 = await client.set(key, value, {
2595+
conditionalSet: "onlyIfDoesNotExist",
2596+
});
2597+
expect(setRes2).toEqual(null);
2598+
const getRes2 = await client.get(key);
2599+
expect(getRes2).toEqual(value);
2600+
}
2601+
2602+
async function setWithGetOldOptions(
2603+
client: BaseClient,
2604+
key: string,
2605+
value: string,
2606+
) {
2607+
const setRes = await client.set(key, value, {
2608+
returnOldValue: true,
2609+
});
2610+
expect(setRes).toEqual(value);
2611+
const getRes = await client.get(key);
2612+
expect(getRes).toEqual(value);
2613+
const setRes2 = await client.set(key + "1", value, {
2614+
returnOldValue: true,
2615+
});
2616+
expect(setRes2).toEqual(null);
2617+
const getRes2 = await client.get(key + "1");
2618+
expect(getRes2).toEqual(value);
2619+
}
2620+
2621+
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
2622+
"Set commands with options test_%p",
2623+
async (protocol) => {
2624+
await runTest(async (client: BaseClient) => {
2625+
const key = uuidv4();
2626+
const value = uuidv4();
2627+
await classicSet(client, key, value);
2628+
await setWithExpiryOptions(client, key, value);
2629+
await setWithExistOptions(client, key, value);
2630+
await setWithGetOldOptions(client, key, value);
2631+
// try to set with all options
2632+
const setRes = await client.set(key + "1", value + "1", {
2633+
expiry: {
2634+
type: "seconds",
2635+
count: 1,
2636+
},
2637+
conditionalSet: "onlyIfExists",
2638+
returnOldValue: true,
2639+
});
2640+
expect(setRes).toEqual(value);
2641+
const getRes = await client.get(key);
2642+
expect(getRes).toEqual(value + "1");
2643+
}, protocol);
2644+
},
2645+
config.timeout,
2646+
);
25302647
}
25312648

25322649
export function runCommonTests<Context>(config: {

0 commit comments

Comments
 (0)