diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index d5bff267c2..3e7c938f45 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -99,7 +99,7 @@ import { createZRemRangeByRank, createZRemRangeByScore, createZScore, - createZUnionstore, + createZUnionStore, } from "./Commands"; import { ClosingError, @@ -1967,22 +1967,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 { return this.createWritePromise( - createZUnionstore(destination, keys, aggregationType), + createZUnionStore(destination, keys, aggregationType), ); } diff --git a/node/src/Commands.ts b/node/src/Commands.ts index 7046c9ceb7..7170af0ab0 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -861,7 +861,7 @@ export function createZInterstore( /** * @internal */ -export function createZUnionstore( +export function createZUnionStore( destination: string, keys: string[] | KeyWeight[], aggregationType?: AggregationType, diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index 4444524140..24821f4450 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -104,7 +104,7 @@ import { createZRemRangeByRank, createZRemRangeByScore, createZScore, - createZUnionstore, + createZUnionStore, } from "./Commands"; import { redis_request } from "./ProtobufMessage"; @@ -1099,13 +1099,13 @@ export class BaseTransaction> { * @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), ); } diff --git a/node/tests/SharedTests.ts b/node/tests/SharedTests.ts index e9354b7994..b88a3e80c1 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -2074,8 +2074,8 @@ export function runBaseTests(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(); @@ -2091,7 +2091,7 @@ export function runBaseTests(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, @@ -2099,9 +2099,25 @@ export function runBaseTests(config: { 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, @@ -2109,9 +2125,25 @@ export function runBaseTests(config: { 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, @@ -2121,7 +2153,7 @@ export function runBaseTests(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(); @@ -2136,7 +2168,7 @@ export function runBaseTests(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, @@ -2146,7 +2178,7 @@ export function runBaseTests(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(); @@ -2162,7 +2194,7 @@ export function runBaseTests(config: { // Scores are multiplied by 2.0 for key1 and key2 during aggregation. expect( - await client.zunionstore( + await client.zunionStore( key3, [ [key1, 2.0], @@ -2183,7 +2215,7 @@ export function runBaseTests(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 = { @@ -2196,7 +2228,7 @@ export function runBaseTests(config: { // Non existing key expect( - await client.zunionstore(key2, [ + await client.zunionStore(key2, [ key1, "{testKey}-non_existing_key", ]), @@ -2214,17 +2246,19 @@ export function runBaseTests(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,