| 1 |
import { RedisCommandArguments } from '@redis/client/dist/lib/commands'; |
| 2 |
import { strict as assert } from 'assert'; |
| 3 |
import { |
| 4 |
transformTimestampArgument, |
| 5 |
pushRetentionArgument, |
| 6 |
TimeSeriesEncoding, |
| 7 |
pushEncodingArgument, |
| 8 |
pushChunkSizeArgument, |
| 9 |
pushDuplicatePolicy, |
| 10 |
pushLabelsArgument, |
| 11 |
transformIncrDecrArguments, |
| 12 |
transformSampleReply, |
| 13 |
TimeSeriesAggregationType, |
| 14 |
pushRangeArguments, |
| 15 |
pushMRangeGroupByArguments, |
| 16 |
TimeSeriesReducers, |
| 17 |
pushFilterArgument, |
| 18 |
pushMRangeArguments, |
| 19 |
pushWithLabelsArgument, |
| 20 |
pushMRangeWithLabelsArguments, |
| 21 |
transformRangeReply, |
| 22 |
transformMRangeReply, |
| 23 |
transformMRangeWithLabelsReply, |
| 24 |
TimeSeriesDuplicatePolicies, |
| 25 |
pushLatestArgument, |
| 26 |
TimeSeriesBucketTimestamp |
| 27 |
} from '.'; |
| 28 |
|
| 29 |
describe('transformTimestampArgument', () => { |
| 30 |
it('number', () => { |
| 31 |
assert.equal( |
| 32 |
transformTimestampArgument(0), |
| 33 |
'0' |
| 34 |
); |
| 35 |
}); |
| 36 |
|
| 37 |
it('Date', () => { |
| 38 |
assert.equal( |
| 39 |
transformTimestampArgument(new Date(0)), |
| 40 |
'0' |
| 41 |
); |
| 42 |
}); |
| 43 |
|
| 44 |
it('string', () => { |
| 45 |
assert.equal( |
| 46 |
transformTimestampArgument('*'), |
| 47 |
'*' |
| 48 |
); |
| 49 |
}); |
| 50 |
}); |
| 51 |
|
| 52 |
function testOptionalArgument(fn: (args: RedisCommandArguments) => unknown): void { |
| 53 |
it('undefined', () => { |
| 54 |
assert.deepEqual( |
| 55 |
fn([]), |
| 56 |
[] |
| 57 |
); |
| 58 |
}); |
| 59 |
} |
| 60 |
|
| 61 |
describe('pushRetentionArgument', () => { |
| 62 |
testOptionalArgument(pushRetentionArgument); |
| 63 |
|
| 64 |
it('number', () => { |
| 65 |
assert.deepEqual( |
| 66 |
pushRetentionArgument([], 1), |
| 67 |
['RETENTION', '1'] |
| 68 |
); |
| 69 |
}); |
| 70 |
}); |
| 71 |
|
| 72 |
describe('pushEncodingArgument', () => { |
| 73 |
testOptionalArgument(pushEncodingArgument); |
| 74 |
|
| 75 |
it('UNCOMPRESSED', () => { |
| 76 |
assert.deepEqual( |
| 77 |
pushEncodingArgument([], TimeSeriesEncoding.UNCOMPRESSED), |
| 78 |
['ENCODING', 'UNCOMPRESSED'] |
| 79 |
); |
| 80 |
}); |
| 81 |
}); |
| 82 |
|
| 83 |
describe('pushChunkSizeArgument', () => { |
| 84 |
testOptionalArgument(pushChunkSizeArgument); |
| 85 |
|
| 86 |
it('number', () => { |
| 87 |
assert.deepEqual( |
| 88 |
pushChunkSizeArgument([], 1), |
| 89 |
['CHUNK_SIZE', '1'] |
| 90 |
); |
| 91 |
}); |
| 92 |
}); |
| 93 |
|
| 94 |
describe('pushDuplicatePolicy', () => { |
| 95 |
testOptionalArgument(pushDuplicatePolicy); |
| 96 |
|
| 97 |
it('BLOCK', () => { |
| 98 |
assert.deepEqual( |
| 99 |
pushDuplicatePolicy([], TimeSeriesDuplicatePolicies.BLOCK), |
| 100 |
['DUPLICATE_POLICY', 'BLOCK'] |
| 101 |
); |
| 102 |
}); |
| 103 |
}); |
| 104 |
|
| 105 |
describe('pushLabelsArgument', () => { |
| 106 |
testOptionalArgument(pushLabelsArgument); |
| 107 |
|
| 108 |
it("{ label: 'value' }", () => { |
| 109 |
assert.deepEqual( |
| 110 |
pushLabelsArgument([], { label: 'value' }), |
| 111 |
['LABELS', 'label', 'value'] |
| 112 |
); |
| 113 |
}); |
| 114 |
}); |
| 115 |
|
| 116 |
describe('transformIncrDecrArguments', () => { |
| 117 |
it('without options', () => { |
| 118 |
assert.deepEqual( |
| 119 |
transformIncrDecrArguments('TS.INCRBY', 'key', 1), |
| 120 |
['TS.INCRBY', 'key', '1'] |
| 121 |
); |
| 122 |
}); |
| 123 |
|
| 124 |
it('with TIMESTAMP', () => { |
| 125 |
assert.deepEqual( |
| 126 |
transformIncrDecrArguments('TS.INCRBY', 'key', 1, { |
| 127 |
TIMESTAMP: '*' |
| 128 |
}), |
| 129 |
['TS.INCRBY', 'key', '1', 'TIMESTAMP', '*'] |
| 130 |
); |
| 131 |
}); |
| 132 |
|
| 133 |
it('with UNCOMPRESSED', () => { |
| 134 |
assert.deepEqual( |
| 135 |
transformIncrDecrArguments('TS.INCRBY', 'key', 1, { |
| 136 |
UNCOMPRESSED: true |
| 137 |
}), |
| 138 |
['TS.INCRBY', 'key', '1', 'UNCOMPRESSED'] |
| 139 |
); |
| 140 |
}); |
| 141 |
|
| 142 |
it('with UNCOMPRESSED false', () => { |
| 143 |
assert.deepEqual( |
| 144 |
transformIncrDecrArguments('TS.INCRBY', 'key', 1, { |
| 145 |
UNCOMPRESSED: false |
| 146 |
}), |
| 147 |
['TS.INCRBY', 'key', '1'] |
| 148 |
); |
| 149 |
}); |
| 150 |
}); |
| 151 |
|
| 152 |
it('transformSampleReply', () => { |
| 153 |
assert.deepEqual( |
| 154 |
transformSampleReply([1, '1.1']), |
| 155 |
{ |
| 156 |
timestamp: 1, |
| 157 |
value: 1.1 |
| 158 |
} |
| 159 |
); |
| 160 |
}); |
| 161 |
|
| 162 |
describe('pushRangeArguments', () => { |
| 163 |
it('without options', () => { |
| 164 |
assert.deepEqual( |
| 165 |
pushRangeArguments([], '-', '+'), |
| 166 |
['-', '+'] |
| 167 |
); |
| 168 |
}); |
| 169 |
|
| 170 |
describe('with FILTER_BY_TS', () => { |
| 171 |
it('string', () => { |
| 172 |
assert.deepEqual( |
| 173 |
pushRangeArguments([], '-', '+', { |
| 174 |
FILTER_BY_TS: ['ts'] |
| 175 |
}), |
| 176 |
['-', '+', 'FILTER_BY_TS', 'ts'] |
| 177 |
); |
| 178 |
}); |
| 179 |
|
| 180 |
it('Array', () => { |
| 181 |
assert.deepEqual( |
| 182 |
pushRangeArguments([], '-', '+', { |
| 183 |
FILTER_BY_TS: ['1', '2'] |
| 184 |
}), |
| 185 |
['-', '+', 'FILTER_BY_TS', '1', '2'] |
| 186 |
); |
| 187 |
}); |
| 188 |
}); |
| 189 |
|
| 190 |
it('with FILTER_BY_VALUE', () => { |
| 191 |
assert.deepEqual( |
| 192 |
pushRangeArguments([], '-', '+', { |
| 193 |
FILTER_BY_VALUE: { |
| 194 |
min: 1, |
| 195 |
max: 2 |
| 196 |
} |
| 197 |
}), |
| 198 |
['-', '+', 'FILTER_BY_VALUE', '1', '2'] |
| 199 |
); |
| 200 |
}); |
| 201 |
|
| 202 |
it('with COUNT', () => { |
| 203 |
assert.deepEqual( |
| 204 |
pushRangeArguments([], '-', '+', { |
| 205 |
COUNT: 1 |
| 206 |
}), |
| 207 |
['-', '+', 'COUNT', '1'] |
| 208 |
); |
| 209 |
}); |
| 210 |
|
| 211 |
it('with ALIGN', () => { |
| 212 |
assert.deepEqual( |
| 213 |
pushRangeArguments([], '-', '+', { |
| 214 |
ALIGN: 1 |
| 215 |
}), |
| 216 |
['-', '+', 'ALIGN', '1'] |
| 217 |
); |
| 218 |
}); |
| 219 |
|
| 220 |
describe('with AGGREGATION', () => { |
| 221 |
it('without options', () => { |
| 222 |
assert.deepEqual( |
| 223 |
pushRangeArguments([], '-', '+', { |
| 224 |
AGGREGATION: { |
| 225 |
type: TimeSeriesAggregationType.FIRST, |
| 226 |
timeBucket: 1 |
| 227 |
} |
| 228 |
}), |
| 229 |
['-', '+', 'AGGREGATION', 'FIRST', '1'] |
| 230 |
); |
| 231 |
}); |
| 232 |
|
| 233 |
it('with BUCKETTIMESTAMP', () => { |
| 234 |
assert.deepEqual( |
| 235 |
pushRangeArguments([], '-', '+', { |
| 236 |
AGGREGATION: { |
| 237 |
type: TimeSeriesAggregationType.FIRST, |
| 238 |
timeBucket: 1, |
| 239 |
BUCKETTIMESTAMP: TimeSeriesBucketTimestamp.LOW |
| 240 |
} |
| 241 |
}), |
| 242 |
['-', '+', 'AGGREGATION', 'FIRST', '1', 'BUCKETTIMESTAMP', '-'] |
| 243 |
); |
| 244 |
}); |
| 245 |
|
| 246 |
it('with BUCKETTIMESTAMP', () => { |
| 247 |
assert.deepEqual( |
| 248 |
pushRangeArguments([], '-', '+', { |
| 249 |
AGGREGATION: { |
| 250 |
type: TimeSeriesAggregationType.FIRST, |
| 251 |
timeBucket: 1, |
| 252 |
EMPTY: true |
| 253 |
} |
| 254 |
}), |
| 255 |
['-', '+', 'AGGREGATION', 'FIRST', '1', 'EMPTY'] |
| 256 |
); |
| 257 |
}); |
| 258 |
}); |
| 259 |
|
| 260 |
it('with FILTER_BY_TS, FILTER_BY_VALUE, COUNT, ALIGN, AGGREGATION', () => { |
| 261 |
assert.deepEqual( |
| 262 |
pushRangeArguments([], '-', '+', { |
| 263 |
FILTER_BY_TS: ['ts'], |
| 264 |
FILTER_BY_VALUE: { |
| 265 |
min: 1, |
| 266 |
max: 2 |
| 267 |
}, |
| 268 |
COUNT: 1, |
| 269 |
ALIGN: 1, |
| 270 |
AGGREGATION: { |
| 271 |
type: TimeSeriesAggregationType.FIRST, |
| 272 |
timeBucket: 1, |
| 273 |
BUCKETTIMESTAMP: TimeSeriesBucketTimestamp.LOW, |
| 274 |
EMPTY: true |
| 275 |
} |
| 276 |
}), |
| 277 |
['-', '+', 'FILTER_BY_TS', 'ts', 'FILTER_BY_VALUE', '1', '2', |
| 278 |
'COUNT', '1', 'ALIGN', '1', 'AGGREGATION', 'FIRST', '1', 'BUCKETTIMESTAMP', '-', 'EMPTY'] |
| 279 |
); |
| 280 |
}); |
| 281 |
}); |
| 282 |
|
| 283 |
describe('pushMRangeGroupByArguments', () => { |
| 284 |
it('undefined', () => { |
| 285 |
assert.deepEqual( |
| 286 |
pushMRangeGroupByArguments([]), |
| 287 |
[] |
| 288 |
); |
| 289 |
}); |
| 290 |
|
| 291 |
it('with GROUPBY', () => { |
| 292 |
assert.deepEqual( |
| 293 |
pushMRangeGroupByArguments([], { |
| 294 |
label: 'label', |
| 295 |
reducer: TimeSeriesReducers.MAXIMUM |
| 296 |
}), |
| 297 |
['GROUPBY', 'label', 'REDUCE', 'MAX'] |
| 298 |
); |
| 299 |
}); |
| 300 |
}); |
| 301 |
|
| 302 |
describe('pushFilterArgument', () => { |
| 303 |
it('string', () => { |
| 304 |
assert.deepEqual( |
| 305 |
pushFilterArgument([], 'label=value'), |
| 306 |
['FILTER', 'label=value'] |
| 307 |
); |
| 308 |
}); |
| 309 |
|
| 310 |
it('Array', () => { |
| 311 |
assert.deepEqual( |
| 312 |
pushFilterArgument([], ['1=1', '2=2']), |
| 313 |
['FILTER', '1=1', '2=2'] |
| 314 |
); |
| 315 |
}); |
| 316 |
}); |
| 317 |
|
| 318 |
describe('pushMRangeArguments', () => { |
| 319 |
it('without options', () => { |
| 320 |
assert.deepEqual( |
| 321 |
pushMRangeArguments([], '-', '+', 'label=value'), |
| 322 |
['-', '+', 'FILTER', 'label=value'] |
| 323 |
); |
| 324 |
}); |
| 325 |
|
| 326 |
it('with GROUPBY', () => { |
| 327 |
assert.deepEqual( |
| 328 |
pushMRangeArguments([], '-', '+', 'label=value', { |
| 329 |
GROUPBY: { |
| 330 |
label: 'label', |
| 331 |
reducer: TimeSeriesReducers.MAXIMUM |
| 332 |
} |
| 333 |
}), |
| 334 |
['-', '+', 'FILTER', 'label=value', 'GROUPBY', 'label', 'REDUCE', 'MAX'] |
| 335 |
); |
| 336 |
}); |
| 337 |
}); |
| 338 |
|
| 339 |
describe('pushWithLabelsArgument', () => { |
| 340 |
it('without selected labels', () => { |
| 341 |
assert.deepEqual( |
| 342 |
pushWithLabelsArgument([]), |
| 343 |
['WITHLABELS'] |
| 344 |
); |
| 345 |
}); |
| 346 |
|
| 347 |
it('with selected labels', () => { |
| 348 |
assert.deepEqual( |
| 349 |
pushWithLabelsArgument([], ['label']), |
| 350 |
['SELECTED_LABELS', 'label'] |
| 351 |
); |
| 352 |
}); |
| 353 |
}); |
| 354 |
|
| 355 |
it('pushMRangeWithLabelsArguments', () => { |
| 356 |
assert.deepEqual( |
| 357 |
pushMRangeWithLabelsArguments([], '-', '+', 'label=value'), |
| 358 |
['-', '+', 'WITHLABELS', 'FILTER', 'label=value'] |
| 359 |
); |
| 360 |
}); |
| 361 |
|
| 362 |
it('transformRangeReply', () => { |
| 363 |
assert.deepEqual( |
| 364 |
transformRangeReply([[1, '1.1'], [2, '2.2']]), |
| 365 |
[{ |
| 366 |
timestamp: 1, |
| 367 |
value: 1.1 |
| 368 |
}, { |
| 369 |
timestamp: 2, |
| 370 |
value: 2.2 |
| 371 |
}] |
| 372 |
); |
| 373 |
}); |
| 374 |
|
| 375 |
describe('transformMRangeReply', () => { |
| 376 |
assert.deepEqual( |
| 377 |
transformMRangeReply([[ |
| 378 |
'key', |
| 379 |
[], |
| 380 |
[[1, '1.1'], [2, '2.2']] |
| 381 |
]]), |
| 382 |
[{ |
| 383 |
key: 'key', |
| 384 |
samples: [{ |
| 385 |
timestamp: 1, |
| 386 |
value: 1.1 |
| 387 |
}, { |
| 388 |
timestamp: 2, |
| 389 |
value: 2.2 |
| 390 |
}] |
| 391 |
}] |
| 392 |
); |
| 393 |
}); |
| 394 |
|
| 395 |
describe('transformMRangeWithLabelsReply', () => { |
| 396 |
assert.deepEqual( |
| 397 |
transformMRangeWithLabelsReply([[ |
| 398 |
'key', |
| 399 |
[['label', 'value']], |
| 400 |
[[1, '1.1'], [2, '2.2']] |
| 401 |
]]), |
| 402 |
[{ |
| 403 |
key: 'key', |
| 404 |
labels: { |
| 405 |
label: 'value' |
| 406 |
}, |
| 407 |
samples: [{ |
| 408 |
timestamp: 1, |
| 409 |
value: 1.1 |
| 410 |
}, { |
| 411 |
timestamp: 2, |
| 412 |
value: 2.2 |
| 413 |
}] |
| 414 |
}] |
| 415 |
); |
| 416 |
}); |
| 417 |
|
| 418 |
describe('pushLatestArgument', () => { |
| 419 |
it('undefined', () => { |
| 420 |
assert.deepEqual( |
| 421 |
pushLatestArgument([]), |
| 422 |
[] |
| 423 |
); |
| 424 |
}); |
| 425 |
|
| 426 |
it('false', () => { |
| 427 |
assert.deepEqual( |
| 428 |
pushLatestArgument([], false), |
| 429 |
[] |
| 430 |
); |
| 431 |
}); |
| 432 |
|
| 433 |
it('true', () => { |
| 434 |
assert.deepEqual( |
| 435 |
pushLatestArgument([], true), |
| 436 |
['LATEST'] |
| 437 |
); |
| 438 |
}); |
| 439 |
}) |