@@ -50,6 +50,15 @@ describe('ZINTERSTORE', () => {
5050 [ 'ZINTERSTORE' , 'destination' , '1' , 'source' , 'AGGREGATE' , 'SUM' ]
5151 ) ;
5252 } ) ;
53+
54+ it ( 'with AGGREGATE COUNT' , ( ) => {
55+ assert . deepEqual (
56+ parseArgs ( ZINTERSTORE , 'destination' , 'source' , {
57+ AGGREGATE : 'COUNT'
58+ } ) ,
59+ [ 'ZINTERSTORE' , 'destination' , '1' , 'source' , 'AGGREGATE' , 'COUNT' ]
60+ ) ;
61+ } ) ;
5362 } ) ;
5463
5564 testUtils . testAll ( 'zInterStore' , async client => {
@@ -61,4 +70,112 @@ describe('ZINTERSTORE', () => {
6170 client : GLOBAL . SERVERS . OPEN ,
6271 cluster : GLOBAL . CLUSTERS . OPEN
6372 } ) ;
73+
74+ testUtils . testAll ( 'zInterStore with AGGREGATE COUNT' , async client => {
75+ const keys = [
76+ '{tag}zinterstore-count-1' ,
77+ '{tag}zinterstore-count-2' ,
78+ '{tag}zinterstore-count-3'
79+ ] ;
80+ const destination = '{tag}zinterstore-count-destination' ;
81+
82+ await Promise . all ( [
83+ client . zAdd ( keys [ 0 ] , [
84+ { value : 'common1' , score : 1 } ,
85+ { value : 'common2' , score : 2 } ,
86+ { value : 'only1' , score : 3 }
87+ ] ) ,
88+ client . zAdd ( keys [ 1 ] , [
89+ { value : 'common1' , score : 4 } ,
90+ { value : 'common2' , score : 5 } ,
91+ { value : 'only2' , score : 6 }
92+ ] ) ,
93+ client . zAdd ( keys [ 2 ] , [
94+ { value : 'common1' , score : 7 } ,
95+ { value : 'common2' , score : 8 } ,
96+ { value : 'only3' , score : 9 }
97+ ] )
98+ ] ) ;
99+
100+ assert . equal (
101+ await client . zInterStore ( destination , keys , {
102+ AGGREGATE : 'COUNT'
103+ } ) ,
104+ 2
105+ ) ;
106+
107+ const result = await client . zRangeWithScores ( destination , 0 , - 1 ) ;
108+ assert . deepEqual ( result , [
109+ { value : 'common1' , score : 3 } ,
110+ { value : 'common2' , score : 3 }
111+ ] ) ;
112+
113+ const scores = result . map ( item => item . score ) ;
114+ assert . equal ( Math . min ( ...scores ) , 3 ) ;
115+ assert . equal ( Math . max ( ...scores ) , 3 ) ;
116+ } , {
117+ client : { ...GLOBAL . SERVERS . OPEN , minimumDockerVersion : [ 8 , 8 ] } ,
118+ cluster : { ...GLOBAL . CLUSTERS . OPEN , minimumDockerVersion : [ 8 , 8 ] }
119+ } ) ;
120+
121+ testUtils . testAll ( 'zInterStore with AGGREGATE SUM, MIN, MAX' , async client => {
122+ const keys = [
123+ '{tag}zinterstore-other-1' ,
124+ '{tag}zinterstore-other-2' ,
125+ '{tag}zinterstore-other-3'
126+ ] ;
127+
128+ await Promise . all ( [
129+ client . zAdd ( keys [ 0 ] , [
130+ { value : 'common1' , score : 1 } ,
131+ { value : 'common2' , score : 10 } ,
132+ { value : 'only1' , score : 3 }
133+ ] ) ,
134+ client . zAdd ( keys [ 1 ] , [
135+ { value : 'common1' , score : 5 } ,
136+ { value : 'common2' , score : 2 } ,
137+ { value : 'only2' , score : 6 }
138+ ] ) ,
139+ client . zAdd ( keys [ 2 ] , [
140+ { value : 'common1' , score : 7 } ,
141+ { value : 'common2' , score : 4 } ,
142+ { value : 'only3' , score : 9 }
143+ ] )
144+ ] ) ;
145+
146+ const expectedByAggregate = {
147+ SUM : {
148+ common1 : 13 ,
149+ common2 : 16
150+ } ,
151+ MIN : {
152+ common1 : 1 ,
153+ common2 : 2
154+ } ,
155+ MAX : {
156+ common1 : 7 ,
157+ common2 : 10
158+ }
159+ } ;
160+
161+ const aggregators = [ 'SUM' , 'MIN' , 'MAX' ] as const ;
162+ for ( const aggregate of aggregators ) {
163+ const destination = `{tag}zinterstore-other-destination-${ aggregate } ` ;
164+ assert . equal (
165+ await client . zInterStore ( destination , keys , {
166+ AGGREGATE : aggregate
167+ } ) ,
168+ 2
169+ ) ;
170+
171+ const result = await client . zRangeWithScores ( destination , 0 , - 1 ) ;
172+ assert . deepEqual (
173+ Object . fromEntries ( result . map ( ( { value, score } ) => [ value . toString ( ) , score ] ) ) ,
174+ expectedByAggregate [ aggregate ]
175+ ) ;
176+ }
177+ } , {
178+ client : GLOBAL . SERVERS . OPEN ,
179+ cluster : GLOBAL . CLUSTERS . OPEN
180+ } ) ;
64181} ) ;
0 commit comments