Skip to content

Commit e9bd72f

Browse files
authored
Adding Tdigest Tests (#12)
1 parent 99834b3 commit e9bd72f

File tree

7 files changed

+429
-86
lines changed

7 files changed

+429
-86
lines changed

src/NRedisStack.Core/Tdigest/TdigestCommands.cs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public async Task<bool> AddAsync(RedisKey key, double item, double weight)
3939
{
4040
if (weight < 0) throw new ArgumentException(nameof(weight));
4141

42-
var result = await _db.ExecuteAsync(TDIGEST.ADD, key, item);
42+
var result = await _db.ExecuteAsync(TDIGEST.ADD, key, item, weight);
4343
return ResponseParser.OKtoBoolean(result);
4444
}
4545

@@ -50,8 +50,11 @@ public async Task<bool> AddAsync(RedisKey key, double item, double weight)
5050
/// <param name="valueWeight">Tuple of the value of the observation and The weight of this observation.</param>
5151
/// <returns><see langword="true"/> if executed correctly, error otherwise</returns>
5252
/// <remarks><seealso href="https://redis.io/commands/tdigest.add"/></remarks>
53-
public bool Add(RedisKey key, Tuple<double, double>[] valueWeight)
53+
public bool Add(RedisKey key, params Tuple<double, double>[] valueWeight)
5454
{
55+
if (valueWeight.Length < 1)
56+
throw new ArgumentOutOfRangeException(nameof(valueWeight));
57+
5558
var args = new List<object> { key };
5659

5760
foreach (var pair in valueWeight)
@@ -70,8 +73,11 @@ public bool Add(RedisKey key, Tuple<double, double>[] valueWeight)
7073
/// <param name="valueWeight">Tuple of the value of the observation and The weight of this observation.</param>
7174
/// <returns><see langword="true"/> if executed correctly, error otherwise</returns>
7275
/// <remarks><seealso href="https://redis.io/commands/tdigest.add"/></remarks>
73-
public async Task<bool> AddAsync(RedisKey key, Tuple<double, double>[] valueWeight)
76+
public async Task<bool> AddAsync(RedisKey key, params Tuple<double, double>[] valueWeight)
7477
{
78+
if (valueWeight.Length < 1)
79+
throw new ArgumentOutOfRangeException(nameof(valueWeight));
80+
7581
var args = new List<object> { key };
7682

7783
foreach (var pair in valueWeight)
@@ -90,9 +96,9 @@ public async Task<bool> AddAsync(RedisKey key, Tuple<double, double>[] valueWeig
9096
/// <param name="value">upper limit of observation value.</param>
9197
/// <returns>double-reply - estimation of the fraction of all observations added which are <= value</returns>
9298
/// <remarks><seealso href="https://redis.io/commands/tdigest.cdf"/></remarks>
93-
public double CDF(RedisKey key, double item)
99+
public double CDF(RedisKey key, double value)
94100
{
95-
return ResponseParser.ToDouble(_db.Execute(TDIGEST.ADD, key, item));
101+
return ResponseParser.ToDouble(_db.Execute(TDIGEST.CDF, key, value));
96102
}
97103

98104
/// <summary>
@@ -102,9 +108,9 @@ public double CDF(RedisKey key, double item)
102108
/// <param name="value">upper limit of observation value.</param>
103109
/// <returns>double-reply - estimation of the fraction of all observations added which are <= value</returns>
104110
/// <remarks><seealso href="https://redis.io/commands/tdigest.cdf"/></remarks>
105-
public async Task<double> CDF(RedisKey key, double item, double weight)
111+
public async Task<double> CDFAsync(RedisKey key, double value)
106112
{
107-
var result = await _db.ExecuteAsync(TDIGEST.ADD, key, item);
113+
var result = await _db.ExecuteAsync(TDIGEST.CDF, key, value);
108114
return ResponseParser.ToDouble(result);
109115
}
110116

@@ -113,23 +119,23 @@ public async Task<double> CDF(RedisKey key, double item, double weight)
113119
/// </summary>
114120
/// <param name="key">The name of the sketch.</param>
115121
/// <param name="compression">The compression parameter.</param>
116-
/// <returns>double-reply - estimation of the fraction of all observations added which are <= value</returns>
122+
/// <returns><see langword="true"/> if executed correctly, error otherwise</returns>
117123
/// <remarks><seealso href="https://redis.io/commands/tdigest.create"/></remarks>
118-
public double Create(RedisKey key, long compression = 100)
124+
public bool Create(RedisKey key, long compression = 100)
119125
{
120-
return ResponseParser.ToDouble(_db.Execute(TDIGEST.CREATE, key, compression));
126+
return ResponseParser.OKtoBoolean(_db.Execute(TDIGEST.CREATE, key, compression));
121127
}
122128

123129
/// <summary>
124130
/// Allocate memory and initialize a t-digest sketch.
125131
/// </summary>
126132
/// <param name="key">The name of the sketch.</param>
127133
/// <param name="compression">The compression parameter.</param>
128-
/// <returns>double-reply - estimation of the fraction of all observations added which are <= value</returns>
134+
/// <returns><see langword="true"/> if executed correctly, error otherwise</returns>
129135
/// <remarks><seealso href="https://redis.io/commands/tdigest.create"/></remarks>
130-
public async Task<double> CreateAsync(RedisKey key, long compression = 100)
136+
public async Task<bool> CreateAsync(RedisKey key, long compression = 100)
131137
{
132-
return ResponseParser.ToDouble(await _db.ExecuteAsync(TDIGEST.CREATE, key, compression));
138+
return ResponseParser.OKtoBoolean(await _db.ExecuteAsync(TDIGEST.CREATE, key, compression));
133139
}
134140

135141
/// <summary>
@@ -161,9 +167,9 @@ public async Task<TdigestInformation> InfoAsync(RedisKey key)
161167
/// <param name="key">The name of the sketch.</param>
162168
/// <returns>the maximum observation value from the sketch</returns>
163169
/// <remarks><seealso href="https://redis.io/commands/tdigest.max"/></remarks>
164-
public RedisResult Max(RedisKey key)
170+
public double Max(RedisKey key)
165171
{
166-
return _db.Execute(TDIGEST.MAX, key);
172+
return ResponseParser.ToDouble(_db.Execute(TDIGEST.MAX, key));
167173
}
168174

169175
/// <summary>
@@ -172,9 +178,9 @@ public RedisResult Max(RedisKey key)
172178
/// <param name="key">The name of the sketch.</param>
173179
/// <returns>the maximum observation value from the sketch</returns>
174180
/// <remarks><seealso href="https://redis.io/commands/tdigest.max"/></remarks>
175-
public async Task<RedisResult> MaxAsync(RedisKey key)
181+
public async Task<double> MaxAsync(RedisKey key)
176182
{
177-
return await _db.ExecuteAsync(TDIGEST.MAX, key);
183+
return ResponseParser.ToDouble(await _db.ExecuteAsync(TDIGEST.MAX, key));
178184
}
179185

180186
/// <summary>
@@ -183,9 +189,9 @@ public async Task<RedisResult> MaxAsync(RedisKey key)
183189
/// <param name="key">The name of the sketch.</param>
184190
/// <returns>the minimum observation value from the sketch</returns>
185191
/// <remarks><seealso href="https://redis.io/commands/tdigest.min"/></remarks>
186-
public RedisResult Min(RedisKey key)
192+
public double Min(RedisKey key)
187193
{
188-
return _db.Execute(TDIGEST.MIN, key);
194+
return ResponseParser.ToDouble(_db.Execute(TDIGEST.MIN, key));
189195
}
190196

191197
/// <summary>
@@ -194,9 +200,9 @@ public RedisResult Min(RedisKey key)
194200
/// <param name="key">The name of the sketch.</param>
195201
/// <returns>the minimum observation value from the sketch</returns>
196202
/// <remarks><seealso href="https://redis.io/commands/tdigest.min"/></remarks>
197-
public async Task<RedisResult> MinAsync(RedisKey key)
203+
public async Task<double> MinAsync(RedisKey key)
198204
{
199-
return await _db.ExecuteAsync(TDIGEST.MIN, key);
205+
return ResponseParser.ToDouble(await _db.ExecuteAsync(TDIGEST.MIN, key));
200206
}
201207

202208
/// <summary>
@@ -277,7 +283,7 @@ public bool MergeStore(RedisKey destinationKey, long numkeys, long compression =
277283
args.Add(TdigestArgs.COMPRESSION);
278284
args.Add(compression);
279285

280-
return ResponseParser.OKtoBoolean(_db.Execute(TDIGEST.MERGE, args));
286+
return ResponseParser.OKtoBoolean(_db.Execute(TDIGEST.MERGESTORE, args));
281287
}
282288

283289
/// <summary>
@@ -298,7 +304,7 @@ public async Task<bool> MergeStoreAsync(RedisKey destinationKey, long numkeys, l
298304
args.Add(TdigestArgs.COMPRESSION);
299305
args.Add(compression);
300306

301-
var result = await _db.ExecuteAsync(TDIGEST.MERGE, args);
307+
var result = await _db.ExecuteAsync(TDIGEST.MERGESTORE, args);
302308
return ResponseParser.OKtoBoolean(result);
303309
}
304310

@@ -368,9 +374,9 @@ public async Task<bool> ResetAsync(RedisKey key, params double[] quantile)
368374
/// <param name="highCutQuantile">Exclude observation values higher than this quantile.</param>
369375
/// <returns>estimation of the mean value. Will return DBL_MAX if the sketch is empty.</returns>
370376
/// <remarks><seealso href="https://redis.io/commands/tdigest.reset"/></remarks>
371-
public RedisResult TrimmedMean(RedisKey key, double lowCutQuantile, double highCutQuantile)
377+
public double TrimmedMean(RedisKey key, double lowCutQuantile, double highCutQuantile)
372378
{
373-
return _db.Execute(TDIGEST.RESET, key, lowCutQuantile, highCutQuantile);
379+
return ResponseParser.ToDouble(_db.Execute(TDIGEST.TRIMMED_MEAN, key, lowCutQuantile, highCutQuantile));
374380
}
375381

376382
/// <summary>
@@ -381,9 +387,9 @@ public RedisResult TrimmedMean(RedisKey key, double lowCutQuantile, double highC
381387
/// <param name="highCutQuantile">Exclude observation values higher than this quantile.</param>
382388
/// <returns>estimation of the mean value. Will return DBL_MAX if the sketch is empty.</returns>
383389
/// <remarks><seealso href="https://redis.io/commands/tdigest.reset"/></remarks>
384-
public async Task<RedisResult> TrimmedMeanAsync(RedisKey key, double lowCutQuantile, double highCutQuantile)
390+
public async Task<double> TrimmedMeanAsync(RedisKey key, double lowCutQuantile, double highCutQuantile)
385391
{
386-
return await _db.ExecuteAsync(TDIGEST.RESET, key, lowCutQuantile, highCutQuantile);
392+
return ResponseParser.ToDouble(await _db.ExecuteAsync(TDIGEST.TRIMMED_MEAN, key, lowCutQuantile, highCutQuantile));
387393
}
388394

389395

tests/NRedisStack.Tests/Bloom/BloomTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void TestReserveBasic()
3131
}
3232

3333
[Fact]
34-
public async void TestReserveBasicAsync()
34+
public async Task TestReserveBasicAsync()
3535
{
3636
IDatabase db = redisFixture.Redis.GetDatabase();
3737
db.Execute("FLUSHALL");
@@ -56,7 +56,7 @@ public void TestAddWhenExist()
5656
}
5757

5858
[Fact]
59-
public async void TestAddWhenExistAsync()
59+
public async Task TestAddWhenExistAsync()
6060
{
6161
IDatabase db = redisFixture.Redis.GetDatabase();
6262
db.Execute("FLUSHALL");
@@ -78,7 +78,7 @@ public void TestAddExists()
7878
}
7979

8080
[Fact]
81-
public async void TestAddExistsAsync()
81+
public async Task TestAddExistsAsync()
8282
{
8383
IDatabase db = redisFixture.Redis.GetDatabase();
8484
db.Execute("FLUSHALL");
@@ -104,7 +104,7 @@ public void TestAddExistsMulti()
104104
}
105105

106106
[Fact]
107-
public async void TestAddExistsMultiAsync()
107+
public async Task TestAddExistsMultiAsync()
108108
{
109109
IDatabase db = redisFixture.Redis.GetDatabase();
110110
db.Execute("FLUSHALL");
@@ -147,7 +147,7 @@ public void TestExample()
147147
}
148148

149149
[Fact]
150-
public async void TestExampleAsync()
150+
public async Task TestExampleAsync()
151151
{
152152
IDatabase db = redisFixture.Redis.GetDatabase();
153153
db.Execute("FLUSHALL");
@@ -190,7 +190,7 @@ public void TestInsert()
190190
}
191191

192192
[Fact]
193-
public async void TestInsertAsync()
193+
public async Task TestInsertAsync()
194194
{
195195
IDatabase db = redisFixture.Redis.GetDatabase();
196196
db.Execute("FLUSHALL");
@@ -215,7 +215,7 @@ public void TestExistsNonExist()
215215
}
216216

217217
[Fact]
218-
public async void TestExistsNonExistAsync()
218+
public async Task TestExistsNonExistAsync()
219219
{
220220
IDatabase db = redisFixture.Redis.GetDatabase();
221221
db.Execute("FLUSHALL");
@@ -240,7 +240,7 @@ public void TestInfo()
240240
}
241241

242242
[Fact]
243-
public async void TestInfoAsync()
243+
public async Task TestInfoAsync()
244244
{
245245
IDatabase db = redisFixture.Redis.GetDatabase();
246246
db.Execute("FLUSHALL");
@@ -279,7 +279,7 @@ public void TestScanDumpAndLoadChunk()
279279
}
280280

281281
[Fact]
282-
public async void TestScanDumpAndLoadChunkAsync()
282+
public async Task TestScanDumpAndLoadChunkAsync()
283283
{
284284
IDatabase db = redisFixture.Redis.GetDatabase();
285285
db.Execute("FLUSHALL");

tests/NRedisStack.Tests/CountMinSketch/CmsTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void TestInitByDim()
3131
}
3232

3333
[Fact]
34-
public async void TestInitByDimAsync()
34+
public async Task TestInitByDimAsync()
3535
{
3636
IDatabase db = redisFixture.Redis.GetDatabase();
3737
db.Execute("FLUSHALL");
@@ -59,7 +59,7 @@ public void TestInitByProb()
5959
}
6060

6161
[Fact]
62-
public async void TestInitByProbAsync()
62+
public async Task TestInitByProbAsync()
6363
{
6464
IDatabase db = redisFixture.Redis.GetDatabase();
6565
db.Execute("FLUSHALL");
@@ -83,7 +83,7 @@ public void TestKeyAlreadyExists()
8383
}
8484

8585
[Fact]
86-
public async void TestKeyAlreadyExistsAsync()
86+
public async Task TestKeyAlreadyExistsAsync()
8787
{
8888
IDatabase db = redisFixture.Redis.GetDatabase();
8989
db.Execute("FLUSHALL");
@@ -110,7 +110,7 @@ public void TestIncrBy()
110110
}
111111

112112
[Fact]
113-
public async void TestIncrByAsync()
113+
public async Task TestIncrByAsync()
114114
{
115115
IDatabase db = redisFixture.Redis.GetDatabase();
116116
db.Execute("FLUSHALL");
@@ -149,7 +149,7 @@ public void TestIncrByMultipleArgs()
149149
}
150150

151151
[Fact]
152-
public async void TestIncrByMultipleArgsAsync()
152+
public async Task TestIncrByMultipleArgsAsync()
153153
{
154154
IDatabase db = redisFixture.Redis.GetDatabase();
155155
db.Execute("FLUSHALL");
@@ -189,7 +189,7 @@ public void TestQuery()
189189
}
190190

191191
[Fact]
192-
public async void TestQueryAsync()
192+
public async Task TestQueryAsync()
193193
{
194194
IDatabase db = redisFixture.Redis.GetDatabase();
195195
db.Execute("FLUSHALL");
@@ -254,7 +254,7 @@ public void TestMerge()
254254

255255

256256
[Fact]
257-
public async void TestMergeAsync()
257+
public async Task TestMergeAsync()
258258
{
259259
IDatabase db = redisFixture.Redis.GetDatabase();
260260
db.Execute("FLUSHALL");

0 commit comments

Comments
 (0)