Skip to content
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
83 changes: 83 additions & 0 deletions packages/client/lib/commands/ZINTER.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ describe('ZINTER', () => {
['ZINTER', '1', 'key', 'AGGREGATE', 'SUM']
);
});

it('with AGGREGATE COUNT', () => {
assert.deepEqual(
parseArgs(ZINTER, 'key', {
AGGREGATE: 'COUNT'
}),
['ZINTER', '1', 'key', 'AGGREGATE', 'COUNT']
);
});
});

testUtils.testAll('zInter', async client => {
Expand All @@ -63,4 +72,78 @@ describe('ZINTER', () => {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});

testUtils.testAll('zInter with AGGREGATE COUNT', async client => {
const keys = [
'{tag}zinter-count-1',
'{tag}zinter-count-2',
'{tag}zinter-count-3'
];

await Promise.all([
client.zAdd(keys[0], [
{ value: 'common1', score: 1 },
{ value: 'common2', score: 2 },
{ value: 'only1', score: 3 }
]),
client.zAdd(keys[1], [
{ value: 'common1', score: 4 },
{ value: 'common2', score: 5 },
{ value: 'only2', score: 6 }
]),
client.zAdd(keys[2], [
{ value: 'common1', score: 7 },
{ value: 'common2', score: 8 },
{ value: 'only3', score: 9 }
])
]);

assert.deepEqual(
await client.zInter(keys, {
AGGREGATE: 'COUNT'
}),
['common1', 'common2']
);
}, {
client: { ...GLOBAL.SERVERS.OPEN, minimumDockerVersion: [8, 8] },
cluster: { ...GLOBAL.CLUSTERS.OPEN, minimumDockerVersion: [8, 8] }
});

testUtils.testAll('zInter with AGGREGATE SUM, MIN, MAX', async client => {
const keys = [
'{tag}zinter-other-1',
'{tag}zinter-other-2',
'{tag}zinter-other-3'
];

await Promise.all([
client.zAdd(keys[0], [
{ value: 'common1', score: 1 },
{ value: 'common2', score: 10 },
{ value: 'only1', score: 3 }
]),
client.zAdd(keys[1], [
{ value: 'common1', score: 5 },
{ value: 'common2', score: 2 },
{ value: 'only2', score: 6 }
]),
client.zAdd(keys[2], [
{ value: 'common1', score: 7 },
{ value: 'common2', score: 4 },
{ value: 'only3', score: 9 }
])
]);

const aggregators = ['SUM', 'MIN', 'MAX'] as const;
for (const aggregate of aggregators) {
const result = await client.zInter(keys, {
AGGREGATE: aggregate
});

assert.deepEqual(result.sort(), ['common1', 'common2']);
}
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
3 changes: 2 additions & 1 deletion packages/client/lib/commands/ZINTER.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export type ZInterKeys<T> = T | [T, ...Array<T>];
export type ZInterKeysType = ZInterKeys<RedisArgument> | ZInterKeys<ZInterKeyAndWeight>;

export interface ZInterOptions {
AGGREGATE?: 'SUM' | 'MIN' | 'MAX';
/** `COUNT` added in 8.8 */
AGGREGATE?: 'SUM' | 'MIN' | 'MAX' | 'COUNT';
}

export function parseZInterArguments(
Expand Down
117 changes: 117 additions & 0 deletions packages/client/lib/commands/ZINTERSTORE.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ describe('ZINTERSTORE', () => {
['ZINTERSTORE', 'destination', '1', 'source', 'AGGREGATE', 'SUM']
);
});

it('with AGGREGATE COUNT', () => {
assert.deepEqual(
parseArgs(ZINTERSTORE, 'destination', 'source', {
AGGREGATE: 'COUNT'
}),
['ZINTERSTORE', 'destination', '1', 'source', 'AGGREGATE', 'COUNT']
);
});
});

testUtils.testAll('zInterStore', async client => {
Expand All @@ -61,4 +70,112 @@ describe('ZINTERSTORE', () => {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});

testUtils.testAll('zInterStore with AGGREGATE COUNT', async client => {
const keys = [
'{tag}zinterstore-count-1',
'{tag}zinterstore-count-2',
'{tag}zinterstore-count-3'
];
const destination = '{tag}zinterstore-count-destination';

await Promise.all([
client.zAdd(keys[0], [
{ value: 'common1', score: 1 },
{ value: 'common2', score: 2 },
{ value: 'only1', score: 3 }
]),
client.zAdd(keys[1], [
{ value: 'common1', score: 4 },
{ value: 'common2', score: 5 },
{ value: 'only2', score: 6 }
]),
client.zAdd(keys[2], [
{ value: 'common1', score: 7 },
{ value: 'common2', score: 8 },
{ value: 'only3', score: 9 }
])
]);

assert.equal(
await client.zInterStore(destination, keys, {
AGGREGATE: 'COUNT'
}),
2
);

const result = await client.zRangeWithScores(destination, 0, -1);
assert.deepEqual(result, [
{ value: 'common1', score: 3 },
{ value: 'common2', score: 3 }
]);

const scores = result.map(item => item.score);
assert.equal(Math.min(...scores), 3);
assert.equal(Math.max(...scores), 3);
}, {
client: { ...GLOBAL.SERVERS.OPEN, minimumDockerVersion: [8, 8] },
cluster: { ...GLOBAL.CLUSTERS.OPEN, minimumDockerVersion: [8, 8] }
});

testUtils.testAll('zInterStore with AGGREGATE SUM, MIN, MAX', async client => {
const keys = [
'{tag}zinterstore-other-1',
'{tag}zinterstore-other-2',
'{tag}zinterstore-other-3'
];

await Promise.all([
client.zAdd(keys[0], [
{ value: 'common1', score: 1 },
{ value: 'common2', score: 10 },
{ value: 'only1', score: 3 }
]),
client.zAdd(keys[1], [
{ value: 'common1', score: 5 },
{ value: 'common2', score: 2 },
{ value: 'only2', score: 6 }
]),
client.zAdd(keys[2], [
{ value: 'common1', score: 7 },
{ value: 'common2', score: 4 },
{ value: 'only3', score: 9 }
])
]);

const expectedByAggregate = {
SUM: {
common1: 13,
common2: 16
},
MIN: {
common1: 1,
common2: 2
},
MAX: {
common1: 7,
common2: 10
}
};

const aggregators = ['SUM', 'MIN', 'MAX'] as const;
for (const aggregate of aggregators) {
const destination = `{tag}zinterstore-other-destination-${aggregate}`;
assert.equal(
await client.zInterStore(destination, keys, {
AGGREGATE: aggregate
}),
2
);

const result = await client.zRangeWithScores(destination, 0, -1);
assert.deepEqual(
Object.fromEntries(result.map(({ value, score }) => [value.toString(), score])),
expectedByAggregate[aggregate]
);
}
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
107 changes: 107 additions & 0 deletions packages/client/lib/commands/ZINTER_WITHSCORES.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ describe('ZINTER WITHSCORES', () => {
['ZINTER', '1', 'key', 'AGGREGATE', 'SUM', 'WITHSCORES']
);
});

it('with AGGREGATE COUNT', () => {
assert.deepEqual(
parseArgs(ZINTER_WITHSCORES, 'key', {
AGGREGATE: 'COUNT'
}),
['ZINTER', '1', 'key', 'AGGREGATE', 'COUNT', 'WITHSCORES']
);
});
});

testUtils.testAll('zInterWithScores', async client => {
Expand All @@ -63,4 +72,102 @@ describe('ZINTER WITHSCORES', () => {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});

testUtils.testAll('zInterWithScores with AGGREGATE COUNT', async client => {
const keys = [
'{tag}zinterws-count-1',
'{tag}zinterws-count-2',
'{tag}zinterws-count-3'
];

await Promise.all([
client.zAdd(keys[0], [
{ value: 'common1', score: 1 },
{ value: 'common2', score: 2 },
{ value: 'only1', score: 3 }
]),
client.zAdd(keys[1], [
{ value: 'common1', score: 4 },
{ value: 'common2', score: 5 },
{ value: 'only2', score: 6 }
]),
client.zAdd(keys[2], [
{ value: 'common1', score: 7 },
{ value: 'common2', score: 8 },
{ value: 'only3', score: 9 }
])
]);

const result = await client.zInterWithScores(keys, {
AGGREGATE: 'COUNT'
});

assert.deepEqual(result, [
{ value: 'common1', score: 3 },
{ value: 'common2', score: 3 }
]);

const scores = result.map(item => item.score);
assert.equal(Math.min(...scores), 3);
assert.equal(Math.max(...scores), 3);
}, {
client: { ...GLOBAL.SERVERS.OPEN, minimumDockerVersion: [8, 8] },
cluster: { ...GLOBAL.CLUSTERS.OPEN, minimumDockerVersion: [8, 8] }
});

testUtils.testAll('zInterWithScores with AGGREGATE SUM, MIN, MAX', async client => {
const keys = [
'{tag}zinterws-other-1',
'{tag}zinterws-other-2',
'{tag}zinterws-other-3'
];

await Promise.all([
client.zAdd(keys[0], [
{ value: 'common1', score: 1 },
{ value: 'common2', score: 10 },
{ value: 'only1', score: 3 }
]),
client.zAdd(keys[1], [
{ value: 'common1', score: 5 },
{ value: 'common2', score: 2 },
{ value: 'only2', score: 6 }
]),
client.zAdd(keys[2], [
{ value: 'common1', score: 7 },
{ value: 'common2', score: 4 },
{ value: 'only3', score: 9 }
])
]);

const expectedByAggregate = {
SUM: {
common1: 13,
common2: 16
},
MIN: {
common1: 1,
common2: 2
},
MAX: {
common1: 7,
common2: 10
}
};

const aggregators = ['SUM', 'MIN', 'MAX'] as const;
for (const aggregate of aggregators) {
const result = await client.zInterWithScores(keys, {
AGGREGATE: aggregate
});

assert.deepEqual(
Object.fromEntries(result.map(({ value, score }) => [value.toString(), score])),
expectedByAggregate[aggregate]
);
}
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
Loading
Loading