Skip to content

Commit

Permalink
feat: update zscan's options (#12)
Browse files Browse the repository at this point in the history
* feat: update zscan

Signed-off-by: jhpung <[email protected]>

* test: add zscan tests

Signed-off-by: jhpung <[email protected]>

* fix: update script error message

Signed-off-by: jhpung <[email protected]>

* test(scripting): change expect assertions from .equal to .match

Signed-off-by: jhpung <[email protected]>

---------

Signed-off-by: jhpung <[email protected]>
  • Loading branch information
jhpung authored Oct 30, 2024
1 parent 128c428 commit 9505d6b
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 6 deletions.
4 changes: 4 additions & 0 deletions lib/ScanStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface Options extends ReadableOptions {
key?: string;
match?: string;
type?: string;
noscores?: boolean;
command: string;
redis: any;
count?: string | number;
Expand Down Expand Up @@ -39,6 +40,9 @@ export default class ScanStream extends Readable {
if (this.opt.count) {
args.push("COUNT", String(this.opt.count));
}
if (this.opt.noscores) {
args.push("noscores");
}

this.opt.redis[this.opt.command](args, (err, res) => {
if (err) {
Expand Down
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export interface ScanStreamOptions {
match?: string;
type?: string;
count?: number;
noscores?: boolean;
}
64 changes: 64 additions & 0 deletions lib/utils/RedisCommander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13798,32 +13798,94 @@ interface RedisCommander<Context extends ClientContext = { type: "default" }> {
cursor: number | string,
callback?: Callback<[cursor: Buffer, elements: Buffer[]]>
): Result<[cursor: Buffer, elements: Buffer[]], Context>;
zscan(
key: RedisKey,
cursor: number | string,
noscoresToken: "noscores",
callback?: Callback<[cursor: string, elements: string[]]>
): Result<[cursor: string, elements: string[]], Context>;
zscanBuffer(
key: RedisKey,
cursor: number | string,
noscoresToken: "noscores",
callback?: Callback<[cursor: Buffer, elements: Buffer[]]>
): Result<[cursor: Buffer, elements: Buffer[]], Context>;
zscan(
key: RedisKey,
cursor: number | string,
countToken: "COUNT",
count: number | string,
callback?: Callback<[cursor: string, elements: string[]]>
): Result<[cursor: string, elements: string[]], Context>;
zscanBuffer(
key: RedisKey,
cursor: number | string,
countToken: "COUNT",
count: number | string,
callback?: Callback<[cursor: Buffer, elements: Buffer[]]>
): Result<[cursor: Buffer, elements: Buffer[]], Context>;
zscan(
key: RedisKey,
cursor: number | string,
countToken: "COUNT",
count: number | string,
noscoresToken: "noscores",
callback?: Callback<[cursor: string, elements: string[]]>
): Result<[cursor: string, elements: string[]], Context>;
zscanBuffer(
key: RedisKey,
cursor: number | string,
countToken: "COUNT",
count: number | string,
noscoresToken: "noscores",
callback?: Callback<[cursor: Buffer, elements: Buffer[]]>
): Result<[cursor: Buffer, elements: Buffer[]], Context>;
zscan(
key: RedisKey,
cursor: number | string,
patternToken: "MATCH",
pattern: string,
callback?: Callback<[cursor: string, elements: string[]]>
): Result<[cursor: string, elements: string[]], Context>;
zscanBuffer(
key: RedisKey,
cursor: number | string,
patternToken: "MATCH",
pattern: string,
callback?: Callback<[cursor: Buffer, elements: Buffer[]]>
): Result<[cursor: Buffer, elements: Buffer[]], Context>;
zscan(
key: RedisKey,
cursor: number | string,
patternToken: "MATCH",
pattern: string,
noscoresToken: "noscores",
callback?: Callback<[cursor: string, elements: string[]]>
): Result<[cursor: string, elements: string[]], Context>;
zscanBuffer(
key: RedisKey,
cursor: number | string,
patternToken: "MATCH",
pattern: string,
noscoresToken: "noscores",
callback?: Callback<[cursor: Buffer, elements: Buffer[]]>
): Result<[cursor: Buffer, elements: Buffer[]], Context>;
zscan(
key: RedisKey,
cursor: number | string,
patternToken: "MATCH",
pattern: string,
countToken: "COUNT",
count: number | string,
callback?: Callback<[cursor: string, elements: string[]]>
): Result<[cursor: string, elements: string[]], Context>;
zscanBuffer(
key: RedisKey,
cursor: number | string,
patternToken: "MATCH",
pattern: string,
countToken: "COUNT",
count: number | string,
callback?: Callback<[cursor: Buffer, elements: Buffer[]]>
): Result<[cursor: Buffer, elements: Buffer[]], Context>;
zscan(
Expand All @@ -13833,6 +13895,7 @@ interface RedisCommander<Context extends ClientContext = { type: "default" }> {
pattern: string,
countToken: "COUNT",
count: number | string,
noscoresToken: "noscores",
callback?: Callback<[cursor: string, elements: string[]]>
): Result<[cursor: string, elements: string[]], Context>;
zscanBuffer(
Expand All @@ -13842,6 +13905,7 @@ interface RedisCommander<Context extends ClientContext = { type: "default" }> {
pattern: string,
countToken: "COUNT",
count: number | string,
noscoresToken: "noscores",
callback?: Callback<[cursor: Buffer, elements: Buffer[]]>
): Result<[cursor: Buffer, elements: Buffer[]], Context>;

Expand Down
8 changes: 2 additions & 6 deletions test/functional/scripting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,8 @@ describe("scripting", () => {
// @ts-expect-error
const [a, b] = await redis.multi().test("foo").test("bar").exec();

expect(a[0].message).to.equal(
"NOSCRIPT No matching script. Please use EVAL."
);
expect(b[0].message).to.equal(
"NOSCRIPT No matching script. Please use EVAL."
);
expect(a[0].message).to.match(/^NOSCRIPT No matching script/);
expect(b[0].message).to.match(/^NOSCRIPT No matching script/);
});
spy.restore();
expect(spy.callCount).to.equal(4);
Expand Down
17 changes: 17 additions & 0 deletions test/unit/commander.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as sinon from "sinon";
import { expect } from "chai";
import Commander from "../../lib/utils/Commander";
import Command from "../../lib/Command";

describe("Commander", () => {
describe("#getBuiltinCommands()", () => {
Expand Down Expand Up @@ -63,4 +64,20 @@ describe("Commander", () => {

Commander.prototype.sendCommand.restore();
});

describe("#zscan", () => {
it("should pass noscores option", async (done) => {
const args: any[] = ["key", "0", "MATCH", "pattern", "COUNT", "10", "noscores"];
sinon.stub(Commander.prototype, "sendCommand").callsFake((command) => {
if(command.args.every((arg, index) => arg === args[index])) {
return done();
}
return done(new Error(`args should be ${args.join(", ")}`));
});
const c = new Commander();

await c.zscan(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
(Commander.prototype.sendCommand as any).restore();
});
});
});

0 comments on commit 9505d6b

Please sign in to comment.