Skip to content

Commit 95a5116

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

File tree

3 files changed

+183
-7
lines changed

3 files changed

+183
-7
lines changed

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: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,6 +2527,181 @@ export function runBaseTests<Context>(config: {
25272527
},
25282528
config.timeout,
25292529
);
2530+
2531+
// Set command tests
2532+
2533+
async function classicSet(client: BaseClient) {
2534+
const key = uuidv4();
2535+
const value = uuidv4();
2536+
const setRes = await client.set(key, value);
2537+
expect(setRes).toEqual("OK");
2538+
const getRes = await client.get(key);
2539+
expect(getRes).toEqual(value);
2540+
}
2541+
2542+
async function setWithExpiryOptions(client: BaseClient) {
2543+
const key = uuidv4();
2544+
const value = uuidv4();
2545+
const setResWithExpirySetMilli = await client.set(key, value, {
2546+
expiry: {
2547+
type: "milliseconds",
2548+
count: 500,
2549+
},
2550+
});
2551+
expect(setResWithExpirySetMilli).toEqual("OK");
2552+
const getWithExpirySetMilli = await client.get(key);
2553+
expect(getWithExpirySetMilli).toEqual(value);
2554+
2555+
const setResWithExpirySec = await client.set(key, value, {
2556+
expiry: {
2557+
type: "seconds",
2558+
count: 1,
2559+
},
2560+
});
2561+
expect(setResWithExpirySec).toEqual("OK");
2562+
const getResWithExpirySec = await client.get(key);
2563+
expect(getResWithExpirySec).toEqual(value);
2564+
2565+
const setWithUnixSec = await client.set(key, value, {
2566+
expiry: {
2567+
type: "unixSeconds",
2568+
count: 1,
2569+
},
2570+
});
2571+
expect(setWithUnixSec).toEqual("OK");
2572+
const getWithUnixSec = await client.get(key);
2573+
expect(getWithUnixSec).toEqual(value);
2574+
2575+
const setResWithExpiryKeep = await client.set(key, value, {
2576+
expiry: "keepExisting",
2577+
});
2578+
expect(setResWithExpiryKeep).toEqual("OK");
2579+
const getResWithExpiryKeep = await client.get(key);
2580+
expect(getResWithExpiryKeep).toEqual(value);
2581+
// wait for the key to expire base on the previous set
2582+
setTimeout(() => {}, 1000);
2583+
const getResExpire = await client.get(key);
2584+
// key should have expired
2585+
expect(getResExpire).toEqual(null);
2586+
2587+
const setResWithExpiryWithUmilli = await client.set(key, value, {
2588+
expiry: {
2589+
type: "unixMilliseconds",
2590+
count: 2,
2591+
},
2592+
});
2593+
expect(setResWithExpiryWithUmilli).toEqual("OK");
2594+
// wait for the key to expire
2595+
setTimeout(() => {}, 3);
2596+
const gettResWithExpiryWithUmilli = await client.get(key);
2597+
// key should have expired
2598+
expect(gettResWithExpiryWithUmilli).toEqual(null);
2599+
}
2600+
2601+
async function setWithOnlyIfExistOptions(client: BaseClient) {
2602+
const key = uuidv4();
2603+
const value = uuidv4();
2604+
const setExistingKeyRes = await client.set(key, value, {
2605+
conditionalSet: "onlyIfExists",
2606+
});
2607+
expect(setExistingKeyRes).toEqual("OK");
2608+
const getExistingKeyRes = await client.get(key);
2609+
expect(getExistingKeyRes).toEqual(value);
2610+
2611+
const notExistingKeyRes = await client.set(key, value + "1", {
2612+
conditionalSet: "onlyIfExists",
2613+
});
2614+
// key does not exist, so it should not be set
2615+
expect(notExistingKeyRes).toEqual(null);
2616+
const getNotExistingKey = await client.get(key);
2617+
// key should not have been set
2618+
expect(getNotExistingKey).toEqual(null);
2619+
}
2620+
2621+
async function setWithOnlyIfNotExistOptions(client: BaseClient) {
2622+
const key = uuidv4();
2623+
const value = uuidv4();
2624+
const notExistingKeyRes = await client.set(key, value, {
2625+
conditionalSet: "onlyIfDoesNotExist",
2626+
});
2627+
// key does not exist, so it should be set
2628+
expect(notExistingKeyRes).toEqual("OK");
2629+
const getNotExistingKey = await client.get(key);
2630+
// key should have been set
2631+
expect(getNotExistingKey).toEqual(value);
2632+
2633+
const existingKeyRes = await client.set(key, value, {
2634+
conditionalSet: "onlyIfDoesNotExist",
2635+
});
2636+
// key exists, so it should not be set
2637+
expect(existingKeyRes).toEqual(null);
2638+
const getExistingKey = await client.get(key);
2639+
// key should not have been set
2640+
expect(getExistingKey).toEqual(value);
2641+
}
2642+
2643+
async function setWithGetOldOptions(client: BaseClient) {
2644+
const key = uuidv4();
2645+
const value = uuidv4();
2646+
2647+
const setResGetNotExistOld = await client.set(key, value, {
2648+
returnOldValue: true,
2649+
});
2650+
// key does not exist, so old value should be null
2651+
expect(setResGetNotExistOld).toEqual(null);
2652+
// key should have been set
2653+
const getResGetNotExistOld = await client.get(key);
2654+
expect(getResGetNotExistOld).toEqual(value);
2655+
2656+
const setResGetExistOld = await client.set(key, value, {
2657+
returnOldValue: true,
2658+
});
2659+
// key exists, so old value should be returned
2660+
expect(setResGetExistOld).toEqual(value);
2661+
// key should have been set
2662+
const getResGetExistOld = await client.get(key);
2663+
expect(getResGetExistOld).toEqual(value);
2664+
}
2665+
2666+
async function setWithAllOptions(client: BaseClient) {
2667+
const key = uuidv4();
2668+
const value = uuidv4();
2669+
2670+
const setResWithAllOptions = await client.set(key, value, {
2671+
expiry: {
2672+
type: "unixSeconds",
2673+
count: 1,
2674+
},
2675+
conditionalSet: "onlyIfExists",
2676+
returnOldValue: true,
2677+
});
2678+
// key does not exist, so old value should be null
2679+
expect(setResWithAllOptions).toEqual(null);
2680+
// key should have been set
2681+
const getResWithAllOptions = await client.get(key);
2682+
expect(getResWithAllOptions).toEqual(value);
2683+
2684+
// wait for the key to expire
2685+
setTimeout(() => {}, 1000);
2686+
// key should have expired
2687+
const gettResWithAllOptions = await client.get(key);
2688+
expect(gettResWithAllOptions).toEqual(null);
2689+
}
2690+
2691+
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
2692+
"Set commands with options test_%p",
2693+
async (protocol) => {
2694+
await runTest(async (client: BaseClient) => {
2695+
await classicSet(client);
2696+
await setWithExpiryOptions(client);
2697+
await setWithOnlyIfExistOptions(client);
2698+
await setWithOnlyIfNotExistOptions(client);
2699+
await setWithGetOldOptions(client);
2700+
await setWithAllOptions(client);
2701+
}, protocol);
2702+
},
2703+
config.timeout,
2704+
);
25302705
}
25312706

25322707
export function runCommonTests<Context>(config: {

0 commit comments

Comments
 (0)