Skip to content

Commit

Permalink
fix command name and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jun 13, 2024
1 parent 891bdb6 commit 9d40ef8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
16 changes: 8 additions & 8 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ import {
createZRemRangeByRank,
createZRemRangeByScore,
createZScore,
createZUnionstore
createZUnionStore,
} from "./Commands";
import {
ClosingError,
Expand Down Expand Up @@ -1992,22 +1992,22 @@ export class BaseClient {
*
* @example
* ```typescript
* // Example usage of zunionstore command with an existing key
* // Example usage of zunionStore command with an existing key
* await client.zadd("key1", {"member1": 10.5, "member2": 8.2})
* await client.zadd("key2", {"member1": 9.5})
* await client.zunionstore("my_sorted_set", ["key1", "key2"]) // Output: 2 - Indicates that the sorted set "my_sorted_set" contains two elements.
* await client.zrange_withscores("my_sorted_set", RangeByIndex(0, -1)) // Output: {'member1': 20, 'member2': 8.2} - "member1" is now stored in "my_sorted_set" with score of 20 and "member2" with score of 8.2.
* await client.zunionstore("my_sorted_set", ["key1", "key2"] , AggregationType.MAX ) // Output: 2 - Indicates that the sorted set "my_sorted_set" contains two elements, and each score is the maximum score between the sets.
* await client.zrange_withscores("my_sorted_set", RangeByIndex(0, -1)) // Output: {'member1': 10.5, 'member2': 8.2} - "member1" is now stored in "my_sorted_set" with score of 10.5 and "member2" with score of 8.2.
* await client.zunionStore("my_sorted_set", ["key1", "key2"]) // Output: 2 - Indicates that the sorted set "my_sorted_set" contains two elements.
* await client.zrangeWithScores("my_sorted_set", RangeByIndex(0, -1)) // Output: {'member1': 20, 'member2': 8.2} - "member1" is now stored in "my_sorted_set" with score of 20 and "member2" with score of 8.2.
* await client.zunionStore("my_sorted_set", ["key1", "key2"] , AggregationType.MAX ) // Output: 2 - Indicates that the sorted set "my_sorted_set" contains two elements, and each score is the maximum score between the sets.
* await client.zrangeWithScores("my_sorted_set", RangeByIndex(0, -1)) // Output: {'member1': 10.5, 'member2': 8.2} - "member1" is now stored in "my_sorted_set" with score of 10.5 and "member2" with score of 8.2.
* ```
*/
public zunionstore(
public zunionStore(
destination: string,
keys: string[] | KeyWeight[],
aggregationType?: AggregationType,
): Promise<number> {
return this.createWritePromise(
createZUnionstore(destination, keys, aggregationType),
createZUnionStore(destination, keys, aggregationType),
);
}

Expand Down
2 changes: 1 addition & 1 deletion node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ export function createZInterstore(
/**
* @internal
*/
export function createZUnionstore(
export function createZUnionStore(
destination: string,
keys: string[] | KeyWeight[],
aggregationType?: AggregationType,
Expand Down
6 changes: 3 additions & 3 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ import {
createZRemRangeByRank,
createZRemRangeByScore,
createZScore,
createZUnionstore
createZUnionStore,
} from "./Commands";
import { redis_request } from "./ProtobufMessage";

Expand Down Expand Up @@ -1118,13 +1118,13 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* @param aggregationType - Specifies the aggregation strategy to apply when combining the scores of elements. See `AggregationType`.
* Command Response - The number of elements in the resulting sorted set stored at `destination`.
*/
public zunionstore(
public zunionStore(
destination: string,
keys: string[] | KeyWeight[],
aggregationType?: AggregationType,
): T {
return this.addAndReturn(
createZUnionstore(destination, keys, aggregationType),
createZUnionStore(destination, keys, aggregationType),
);
}

Expand Down
66 changes: 50 additions & 16 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2117,8 +2117,8 @@ export function runBaseTests<Context>(config: {
config.timeout,
);

// ZUnionstore command tests
async function zunionstoreWithAggregation(client: BaseClient) {
// ZUnionStore command tests
async function zunionStoreWithMaxAggregation(client: BaseClient) {
const key1 = "{testKey}:1-" + uuidv4();
const key2 = "{testKey}:2-" + uuidv4();
const key3 = "{testKey}:3-" + uuidv4();
Expand All @@ -2134,27 +2134,59 @@ export function runBaseTests<Context>(config: {
expect(await client.zadd(key2, membersScores2)).toEqual(3);

// Union results are aggregated by the MAX score of elements
expect(await client.zunionstore(key3, [key1, key2], "MAX")).toEqual(3);
expect(await client.zunionStore(key3, [key1, key2], "MAX")).toEqual(3);
const zunionstoreMapMax = await client.zrangeWithScores(key3, range);
const expectedMapMax = {
one: 1.5,
two: 2.5,
three: 3.5,
};
expect(zunionstoreMapMax).toEqual(expectedMapMax);
}

async function zunionStoreWithMinAggregation(client: BaseClient) {
const key1 = "{testKey}:1-" + uuidv4();
const key2 = "{testKey}:2-" + uuidv4();
const key3 = "{testKey}:3-" + uuidv4();
const range = {
start: 0,
stop: -1,
};

const membersScores1 = { one: 1.0, two: 2.0 };
const membersScores2 = { one: 1.5, two: 2.5, three: 3.5 };

expect(await client.zadd(key1, membersScores1)).toEqual(2);
expect(await client.zadd(key2, membersScores2)).toEqual(3);

// Union results are aggregated by the MIN score of elements
expect(await client.zunionstore(key3, [key1, key2], "MIN")).toEqual(3);
expect(await client.zunionStore(key3, [key1, key2], "MIN")).toEqual(3);
const zunionstoreMapMin = await client.zrangeWithScores(key3, range);
const expectedMapMin = {
one: 1.0,
two: 2.0,
three: 3.5,
};
expect(zunionstoreMapMin).toEqual(expectedMapMin);
}

async function zunionStoreWithSumAggregation(client: BaseClient) {
const key1 = "{testKey}:1-" + uuidv4();
const key2 = "{testKey}:2-" + uuidv4();
const key3 = "{testKey}:3-" + uuidv4();
const range = {
start: 0,
stop: -1,
};

const membersScores1 = { one: 1.0, two: 2.0 };
const membersScores2 = { one: 1.5, two: 2.5, three: 3.5 };

expect(await client.zadd(key1, membersScores1)).toEqual(2);
expect(await client.zadd(key2, membersScores2)).toEqual(3);

// Union results are aggregated by the SUM score of elements
expect(await client.zunionstore(key3, [key1, key2], "SUM")).toEqual(3);
expect(await client.zunionStore(key3, [key1, key2], "SUM")).toEqual(3);
const zunionstoreMapSum = await client.zrangeWithScores(key3, range);
const expectedMapSum = {
one: 2.5,
Expand All @@ -2164,7 +2196,7 @@ export function runBaseTests<Context>(config: {
expect(zunionstoreMapSum).toEqual(expectedMapSum);
}

async function zunionstoreBasicTest(client: BaseClient) {
async function zunionStoreBasicTest(client: BaseClient) {
const key1 = "{testKey}:1-" + uuidv4();
const key2 = "{testKey}:2-" + uuidv4();
const key3 = "{testKey}:3-" + uuidv4();
Expand All @@ -2179,7 +2211,7 @@ export function runBaseTests<Context>(config: {
expect(await client.zadd(key1, membersScores1)).toEqual(2);
expect(await client.zadd(key2, membersScores2)).toEqual(3);

expect(await client.zunionstore(key3, [key1, key2])).toEqual(3);
expect(await client.zunionStore(key3, [key1, key2])).toEqual(3);
const zunionstoreMap = await client.zrangeWithScores(key3, range);
const expectedMap = {
one: 3.0,
Expand All @@ -2189,7 +2221,7 @@ export function runBaseTests<Context>(config: {
expect(zunionstoreMap).toEqual(expectedMap);
}

async function zunionstoreWithWeightsAndAggregation(client: BaseClient) {
async function zunionStoreWithWeightsAndAggregation(client: BaseClient) {
const key1 = "{testKey}:1-" + uuidv4();
const key2 = "{testKey}:2-" + uuidv4();
const key3 = "{testKey}:3-" + uuidv4();
Expand All @@ -2205,7 +2237,7 @@ export function runBaseTests<Context>(config: {

// Scores are multiplied by 2.0 for key1 and key2 during aggregation.
expect(
await client.zunionstore(
await client.zunionStore(
key3,
[
[key1, 2.0],
Expand All @@ -2226,7 +2258,7 @@ export function runBaseTests<Context>(config: {
expect(zunionstoreMapMultiplied).toEqual(expectedMapMultiplied);
}

async function zunionstoreEmptyCases(client: BaseClient) {
async function zunionStoreEmptyCases(client: BaseClient) {
const key1 = "{testKey}:1-" + uuidv4();
const key2 = "{testKey}:2-" + uuidv4();
const range = {
Expand All @@ -2239,7 +2271,7 @@ export function runBaseTests<Context>(config: {

// Non existing key
expect(
await client.zunionstore(key2, [
await client.zunionStore(key2, [
key1,
"{testKey}-non_existing_key",
]),
Expand All @@ -2257,17 +2289,19 @@ export function runBaseTests<Context>(config: {
expect(zunionstore_map_nonexistingkey).toEqual(expectedMapMultiplied);

// Empty list check
await expect(client.zunionstore("{xyz}", [])).rejects.toThrow();
await expect(client.zunionStore("{xyz}", [])).rejects.toThrow();
}

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`zunionstore test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
await zunionstoreBasicTest(client);
await zunionstoreWithAggregation(client);
await zunionstoreWithWeightsAndAggregation(client);
await zunionstoreEmptyCases(client);
await zunionStoreBasicTest(client);
await zunionStoreWithMaxAggregation(client);
await zunionStoreWithMinAggregation(client);
await zunionStoreWithSumAggregation(client);
await zunionStoreWithWeightsAndAggregation(client);
await zunionStoreEmptyCases(client);
}, protocol);
},
config.timeout,
Expand Down

0 comments on commit 9d40ef8

Please sign in to comment.