Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added zscore command in node. #889

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## 0.2.0 (2024-02-11)

#### Changes
* Python, Node: Added ZCARD command ([#877](https://github.com/aws/glide-for-redis/pull/885), [#885](https://github.com/aws/glide-for-redis/pull/885))
* Python, Node: Added ZCARD command ([#871](https://github.com/aws/glide-for-redis/pull/871), [#885](https://github.com/aws/glide-for-redis/pull/885))
* Python, Node: Added ZADD and ZADDINCR commands ([#814](https://github.com/aws/glide-for-redis/pull/814), [#830](https://github.com/aws/glide-for-redis/pull/830))
* Python, Node: Added ZREM command ([#834](https://github.com/aws/glide-for-redis/pull/834), [#831](https://github.com/aws/glide-for-redis/pull/831))
* Python, Node: Added ZSCORE command ([#885](https://github.com/aws/glide-for-redis/pull/885), [#871](https://github.com/aws/glide-for-redis/pull/871))
* Python, Node: Added ZSCORE command ([#877](https://github.com/aws/glide-for-redis/pull/877), [#889](https://github.com/aws/glide-for-redis/pull/889))
* Use jemalloc as default allocator. ([#847](https://github.com/aws/glide-for-redis/pull/847))
* Python, Node: Added RPOPCOUNT and LPOPCOUNT to transaction ([#874](https://github.com/aws/glide-for-redis/pull/874))
* Standalone client: Improve connection errors. ([#854](https://github.com/aws/glide-for-redis/pull/854))
Expand Down
15 changes: 15 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
createZadd,
createZcard,
createZrem,
createZscore,
} from "./Commands";
import {
ClosingError,
Expand Down Expand Up @@ -1062,6 +1063,20 @@ export class BaseClient {
return this.createWritePromise(createZcard(key));
}

/** Returns the score of `member` in the sorted set stored at `key`.
* See https://redis.io/commands/zscore/ for more details.
*
* @param key - The key of the sorted set.
* @param member - The member whose score is to be retrieved.
* @returns The score of the member.
* If `member` does not exist in the sorted set, null is returned.
* If `key` does not exist, null is returned.
* If `key` holds a value that is not a sorted set, an error is returned.
*/
public zscore(key: string, member: string): Promise<number | null> {
return this.createWritePromise(createZscore(key, member));
}

private readonly MAP_READ_FROM_STRATEGY: Record<
ReadFrom,
connection_request.ReadFrom
Expand Down
7 changes: 7 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,3 +760,10 @@ export function createZrem(
export function createZcard(key: string): redis_request.Command {
return createCommand(RequestType.Zcard, [key]);
}

/**
* @internal
*/
export function createZscore(key: string, member: string): redis_request.Command {
return createCommand(RequestType.ZScore, [key, member]);
}
16 changes: 16 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
createZadd,
createZcard,
createZrem,
createZscore,
} from "./Commands";
import { redis_request } from "./ProtobufMessage";

Expand Down Expand Up @@ -821,6 +822,21 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createZcard(key));
}

/** Returns the score of `member` in the sorted set stored at `key`.
* See https://redis.io/commands/zscore/ for more details.
*
* @param key - The key of the sorted set.
* @param member - The member whose score is to be retrieved.
*
* Command Response - The score of the member.
* If `member` does not exist in the sorted set, null is returned.
* If `key` does not exist, null is returned.
* If `key` holds a value that is not a sorted set, an error is returned.
*/
public zscore(key: string, member: string) {
this.commands.push(createZscore(key, member));
}

/** Executes a single command, without checking inputs. Every part of the command, including subcommands,
* should be added as a separate value in args.
*
Expand Down
24 changes: 24 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,30 @@ export function runBaseTests<Context>(config: {
},
config.timeout
);


it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`zscore test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key1 = uuidv4();
const key2 = uuidv4();
const membersScores = { one: 1, two: 2, three: 3 };
expect(await client.zadd(key1, membersScores)).toEqual(3);
expect(await client.zscore(key1, "one")).toEqual(1.0);
expect(await client.zscore(key1, "nonExistingMember")).toEqual(
null
);
expect(
await client.zscore("nonExistingKey", "nonExistingMember")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test for a key that holds a value which isn't sorted set

).toEqual(null);

expect(await client.set(key2, "foo")).toEqual("OK");
await expect(client.zscore(key2, "foo")).rejects.toThrow();
}, protocol);
},
config.timeout
);
}

export function runCommonTests<Context>(config: {
Expand Down
4 changes: 3 additions & 1 deletion node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export function transactionTest(
.zadd(key8, { member1: 1, member2: 2 })
.zaddIncr(key8, "member2", 1)
.zrem(key8, ["member1"])
.zcard(key8);
.zcard(key8)
.zscore(key8, "member2");
return [
"OK",
null,
Expand Down Expand Up @@ -127,6 +128,7 @@ export function transactionTest(
3,
1,
1,
3.0,
];
}

Expand Down
Loading