1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
|
import time
from time import sleep
import pytest
import redis.asyncio as redis
from tests.conftest import skip_ifmodversion_lt
pytestmark = pytest.mark.asyncio
@pytest.mark.redismod
async def test_create(modclient: redis.Redis):
assert await modclient.ts().create(1)
assert await modclient.ts().create(2, retention_msecs=5)
assert await modclient.ts().create(3, labels={"Redis": "Labs"})
assert await modclient.ts().create(4, retention_msecs=20, labels={"Time": "Series"})
info = await modclient.ts().info(4)
assert 20 == info.retention_msecs
assert "Series" == info.labels["Time"]
# Test for a chunk size of 128 Bytes
assert await modclient.ts().create("time-serie-1", chunk_size=128)
info = await modclient.ts().info("time-serie-1")
assert 128, info.chunk_size
@pytest.mark.redismod
@skip_ifmodversion_lt("1.4.0", "timeseries")
async def test_create_duplicate_policy(modclient: redis.Redis):
# Test for duplicate policy
for duplicate_policy in ["block", "last", "first", "min", "max"]:
ts_name = f"time-serie-ooo-{duplicate_policy}"
assert await modclient.ts().create(ts_name, duplicate_policy=duplicate_policy)
info = await modclient.ts().info(ts_name)
assert duplicate_policy == info.duplicate_policy
@pytest.mark.redismod
async def test_alter(modclient: redis.Redis):
assert await modclient.ts().create(1)
res = await modclient.ts().info(1)
assert 0 == res.retention_msecs
assert await modclient.ts().alter(1, retention_msecs=10)
res = await modclient.ts().info(1)
assert {} == res.labels
res = await modclient.ts().info(1)
assert 10 == res.retention_msecs
assert await modclient.ts().alter(1, labels={"Time": "Series"})
res = await modclient.ts().info(1)
assert "Series" == res.labels["Time"]
res = await modclient.ts().info(1)
assert 10 == res.retention_msecs
@pytest.mark.redismod
@skip_ifmodversion_lt("1.4.0", "timeseries")
async def test_alter_diplicate_policy(modclient: redis.Redis):
assert await modclient.ts().create(1)
info = await modclient.ts().info(1)
assert info.duplicate_policy is None
assert await modclient.ts().alter(1, duplicate_policy="min")
info = await modclient.ts().info(1)
assert "min" == info.duplicate_policy
@pytest.mark.redismod
async def test_add(modclient: redis.Redis):
assert 1 == await modclient.ts().add(1, 1, 1)
assert 2 == await modclient.ts().add(2, 2, 3, retention_msecs=10)
assert 3 == await modclient.ts().add(3, 3, 2, labels={"Redis": "Labs"})
assert 4 == await modclient.ts().add(
4, 4, 2, retention_msecs=10, labels={"Redis": "Labs", "Time": "Series"}
)
res = await modclient.ts().add(5, "*", 1)
assert abs(time.time() - round(float(res) / 1000)) < 1.0
info = await modclient.ts().info(4)
assert 10 == info.retention_msecs
assert "Labs" == info.labels["Redis"]
# Test for a chunk size of 128 Bytes on TS.ADD
assert await modclient.ts().add("time-serie-1", 1, 10.0, chunk_size=128)
info = await modclient.ts().info("time-serie-1")
assert 128 == info.chunk_size
@pytest.mark.redismod
@skip_ifmodversion_lt("1.4.0", "timeseries")
async def test_add_duplicate_policy(modclient: redis.Redis):
# Test for duplicate policy BLOCK
assert 1 == await modclient.ts().add("time-serie-add-ooo-block", 1, 5.0)
with pytest.raises(Exception):
await modclient.ts().add(
"time-serie-add-ooo-block", 1, 5.0, duplicate_policy="block"
)
# Test for duplicate policy LAST
assert 1 == await modclient.ts().add("time-serie-add-ooo-last", 1, 5.0)
assert 1 == await modclient.ts().add(
"time-serie-add-ooo-last", 1, 10.0, duplicate_policy="last"
)
res = await modclient.ts().get("time-serie-add-ooo-last")
assert 10.0 == res[1]
# Test for duplicate policy FIRST
assert 1 == await modclient.ts().add("time-serie-add-ooo-first", 1, 5.0)
assert 1 == await modclient.ts().add(
"time-serie-add-ooo-first", 1, 10.0, duplicate_policy="first"
)
res = await modclient.ts().get("time-serie-add-ooo-first")
assert 5.0 == res[1]
# Test for duplicate policy MAX
assert 1 == await modclient.ts().add("time-serie-add-ooo-max", 1, 5.0)
assert 1 == await modclient.ts().add(
"time-serie-add-ooo-max", 1, 10.0, duplicate_policy="max"
)
res = await modclient.ts().get("time-serie-add-ooo-max")
assert 10.0 == res[1]
# Test for duplicate policy MIN
assert 1 == await modclient.ts().add("time-serie-add-ooo-min", 1, 5.0)
assert 1 == await modclient.ts().add(
"time-serie-add-ooo-min", 1, 10.0, duplicate_policy="min"
)
res = await modclient.ts().get("time-serie-add-ooo-min")
assert 5.0 == res[1]
@pytest.mark.redismod
async def test_madd(modclient: redis.Redis):
await modclient.ts().create("a")
assert [1, 2, 3] == await modclient.ts().madd(
[("a", 1, 5), ("a", 2, 10), ("a", 3, 15)]
)
@pytest.mark.redismod
async def test_incrby_decrby(modclient: redis.Redis):
for _ in range(100):
assert await modclient.ts().incrby(1, 1)
sleep(0.001)
assert 100 == (await modclient.ts().get(1))[1]
for _ in range(100):
assert await modclient.ts().decrby(1, 1)
sleep(0.001)
assert 0 == (await modclient.ts().get(1))[1]
assert await modclient.ts().incrby(2, 1.5, timestamp=5)
assert (5, 1.5) == await modclient.ts().get(2)
assert await modclient.ts().incrby(2, 2.25, timestamp=7)
assert (7, 3.75) == await modclient.ts().get(2)
assert await modclient.ts().decrby(2, 1.5, timestamp=15)
assert (15, 2.25) == await modclient.ts().get(2)
# Test for a chunk size of 128 Bytes on TS.INCRBY
assert await modclient.ts().incrby("time-serie-1", 10, chunk_size=128)
info = await modclient.ts().info("time-serie-1")
assert 128 == info.chunk_size
# Test for a chunk size of 128 Bytes on TS.DECRBY
assert await modclient.ts().decrby("time-serie-2", 10, chunk_size=128)
info = await modclient.ts().info("time-serie-2")
assert 128 == info.chunk_size
@pytest.mark.redismod
async def test_create_and_delete_rule(modclient: redis.Redis):
# test rule creation
time = 100
await modclient.ts().create(1)
await modclient.ts().create(2)
await modclient.ts().createrule(1, 2, "avg", 100)
for i in range(50):
await modclient.ts().add(1, time + i * 2, 1)
await modclient.ts().add(1, time + i * 2 + 1, 2)
await modclient.ts().add(1, time * 2, 1.5)
assert round((await modclient.ts().get(2))[1], 5) == 1.5
info = await modclient.ts().info(1)
assert info.rules[0][1] == 100
# test rule deletion
await modclient.ts().deleterule(1, 2)
info = await modclient.ts().info(1)
assert not info.rules
@pytest.mark.redismod
@skip_ifmodversion_lt("99.99.99", "timeseries")
async def test_del_range(modclient: redis.Redis):
try:
await modclient.ts().delete("test", 0, 100)
except Exception as e:
assert e.__str__() != ""
for i in range(100):
await modclient.ts().add(1, i, i % 7)
assert 22 == await modclient.ts().delete(1, 0, 21)
assert [] == await modclient.ts().range(1, 0, 21)
assert [(22, 1.0)] == await modclient.ts().range(1, 22, 22)
@pytest.mark.redismod
async def test_range(modclient: redis.Redis):
for i in range(100):
await modclient.ts().add(1, i, i % 7)
assert 100 == len(await modclient.ts().range(1, 0, 200))
for i in range(100):
await modclient.ts().add(1, i + 200, i % 7)
assert 200 == len(await modclient.ts().range(1, 0, 500))
# last sample isn't returned
assert 20 == len(
await modclient.ts().range(
1, 0, 500, aggregation_type="avg", bucket_size_msec=10
)
)
assert 10 == len(await modclient.ts().range(1, 0, 500, count=10))
@pytest.mark.redismod
@skip_ifmodversion_lt("99.99.99", "timeseries")
async def test_range_advanced(modclient: redis.Redis):
for i in range(100):
await modclient.ts().add(1, i, i % 7)
await modclient.ts().add(1, i + 200, i % 7)
assert 2 == len(
await modclient.ts().range(
1,
0,
500,
filter_by_ts=[i for i in range(10, 20)],
filter_by_min_value=1,
filter_by_max_value=2,
)
)
assert [(0, 10.0), (10, 1.0)] == await modclient.ts().range(
1, 0, 10, aggregation_type="count", bucket_size_msec=10, align="+"
)
assert [(0, 5.0), (5, 6.0)] == await modclient.ts().range(
1, 0, 10, aggregation_type="count", bucket_size_msec=10, align=5
)
@pytest.mark.redismod
@skip_ifmodversion_lt("99.99.99", "timeseries")
async def test_rev_range(modclient: redis.Redis):
for i in range(100):
await modclient.ts().add(1, i, i % 7)
assert 100 == len(await modclient.ts().range(1, 0, 200))
for i in range(100):
await modclient.ts().add(1, i + 200, i % 7)
assert 200 == len(await modclient.ts().range(1, 0, 500))
# first sample isn't returned
assert 20 == len(
await modclient.ts().revrange(
1, 0, 500, aggregation_type="avg", bucket_size_msec=10
)
)
assert 10 == len(await modclient.ts().revrange(1, 0, 500, count=10))
assert 2 == len(
await modclient.ts().revrange(
1,
0,
500,
filter_by_ts=[i for i in range(10, 20)],
filter_by_min_value=1,
filter_by_max_value=2,
)
)
assert [(10, 1.0), (0, 10.0)] == await modclient.ts().revrange(
1, 0, 10, aggregation_type="count", bucket_size_msec=10, align="+"
)
assert [(1, 10.0), (0, 1.0)] == await modclient.ts().revrange(
1, 0, 10, aggregation_type="count", bucket_size_msec=10, align=1
)
@pytest.mark.redismod
@pytest.mark.onlynoncluster
async def testMultiRange(modclient: redis.Redis):
await modclient.ts().create(1, labels={"Test": "This", "team": "ny"})
await modclient.ts().create(
2, labels={"Test": "This", "Taste": "That", "team": "sf"}
)
for i in range(100):
await modclient.ts().add(1, i, i % 7)
await modclient.ts().add(2, i, i % 11)
res = await modclient.ts().mrange(0, 200, filters=["Test=This"])
assert 2 == len(res)
assert 100 == len(res[0]["1"][1])
res = await modclient.ts().mrange(0, 200, filters=["Test=This"], count=10)
assert 10 == len(res[0]["1"][1])
for i in range(100):
await modclient.ts().add(1, i + 200, i % 7)
res = await modclient.ts().mrange(
0, 500, filters=["Test=This"], aggregation_type="avg", bucket_size_msec=10
)
assert 2 == len(res)
assert 20 == len(res[0]["1"][1])
# test withlabels
assert {} == res[0]["1"][0]
res = await modclient.ts().mrange(0, 200, filters=["Test=This"], with_labels=True)
assert {"Test": "This", "team": "ny"} == res[0]["1"][0]
@pytest.mark.redismod
@pytest.mark.onlynoncluster
@skip_ifmodversion_lt("99.99.99", "timeseries")
async def test_multi_range_advanced(modclient: redis.Redis):
await modclient.ts().create(1, labels={"Test": "This", "team": "ny"})
await modclient.ts().create(
2, labels={"Test": "This", "Taste": "That", "team": "sf"}
)
for i in range(100):
await modclient.ts().add(1, i, i % 7)
await modclient.ts().add(2, i, i % 11)
# test with selected labels
res = await modclient.ts().mrange(
0, 200, filters=["Test=This"], select_labels=["team"]
)
assert {"team": "ny"} == res[0]["1"][0]
assert {"team": "sf"} == res[1]["2"][0]
# test with filterby
res = await modclient.ts().mrange(
0,
200,
filters=["Test=This"],
filter_by_ts=[i for i in range(10, 20)],
filter_by_min_value=1,
filter_by_max_value=2,
)
assert [(15, 1.0), (16, 2.0)] == res[0]["1"][1]
# test groupby
res = await modclient.ts().mrange(
0, 3, filters=["Test=This"], groupby="Test", reduce="sum"
)
assert [(0, 0.0), (1, 2.0), (2, 4.0), (3, 6.0)] == res[0]["Test=This"][1]
res = await modclient.ts().mrange(
0, 3, filters=["Test=This"], groupby="Test", reduce="max"
)
assert [(0, 0.0), (1, 1.0), (2, 2.0), (3, 3.0)] == res[0]["Test=This"][1]
res = await modclient.ts().mrange(
0, 3, filters=["Test=This"], groupby="team", reduce="min"
)
assert 2 == len(res)
assert [(0, 0.0), (1, 1.0), (2, 2.0), (3, 3.0)] == res[0]["team=ny"][1]
assert [(0, 0.0), (1, 1.0), (2, 2.0), (3, 3.0)] == res[1]["team=sf"][1]
# test align
res = await modclient.ts().mrange(
0,
10,
filters=["team=ny"],
aggregation_type="count",
bucket_size_msec=10,
align="-",
)
assert [(0, 10.0), (10, 1.0)] == res[0]["1"][1]
res = await modclient.ts().mrange(
0,
10,
filters=["team=ny"],
aggregation_type="count",
bucket_size_msec=10,
align=5,
)
assert [(0, 5.0), (5, 6.0)] == res[0]["1"][1]
@pytest.mark.redismod
@pytest.mark.onlynoncluster
@skip_ifmodversion_lt("99.99.99", "timeseries")
async def test_multi_reverse_range(modclient: redis.Redis):
await modclient.ts().create(1, labels={"Test": "This", "team": "ny"})
await modclient.ts().create(
2, labels={"Test": "This", "Taste": "That", "team": "sf"}
)
for i in range(100):
await modclient.ts().add(1, i, i % 7)
await modclient.ts().add(2, i, i % 11)
res = await modclient.ts().mrange(0, 200, filters=["Test=This"])
assert 2 == len(res)
assert 100 == len(res[0]["1"][1])
res = await modclient.ts().mrange(0, 200, filters=["Test=This"], count=10)
assert 10 == len(res[0]["1"][1])
for i in range(100):
await modclient.ts().add(1, i + 200, i % 7)
res = await modclient.ts().mrevrange(
0, 500, filters=["Test=This"], aggregation_type="avg", bucket_size_msec=10
)
assert 2 == len(res)
assert 20 == len(res[0]["1"][1])
assert {} == res[0]["1"][0]
# test withlabels
res = await modclient.ts().mrevrange(
0, 200, filters=["Test=This"], with_labels=True
)
assert {"Test": "This", "team": "ny"} == res[0]["1"][0]
# test with selected labels
res = await modclient.ts().mrevrange(
0, 200, filters=["Test=This"], select_labels=["team"]
)
assert {"team": "ny"} == res[0]["1"][0]
assert {"team": "sf"} == res[1]["2"][0]
# test filterby
res = await modclient.ts().mrevrange(
0,
200,
filters=["Test=This"],
filter_by_ts=[i for i in range(10, 20)],
filter_by_min_value=1,
filter_by_max_value=2,
)
assert [(16, 2.0), (15, 1.0)] == res[0]["1"][1]
# test groupby
res = await modclient.ts().mrevrange(
0, 3, filters=["Test=This"], groupby="Test", reduce="sum"
)
assert [(3, 6.0), (2, 4.0), (1, 2.0), (0, 0.0)] == res[0]["Test=This"][1]
res = await modclient.ts().mrevrange(
0, 3, filters=["Test=This"], groupby="Test", reduce="max"
)
assert [(3, 3.0), (2, 2.0), (1, 1.0), (0, 0.0)] == res[0]["Test=This"][1]
res = await modclient.ts().mrevrange(
0, 3, filters=["Test=This"], groupby="team", reduce="min"
)
assert 2 == len(res)
assert [(3, 3.0), (2, 2.0), (1, 1.0), (0, 0.0)] == res[0]["team=ny"][1]
assert [(3, 3.0), (2, 2.0), (1, 1.0), (0, 0.0)] == res[1]["team=sf"][1]
# test align
res = await modclient.ts().mrevrange(
0,
10,
filters=["team=ny"],
aggregation_type="count",
bucket_size_msec=10,
align="-",
)
assert [(10, 1.0), (0, 10.0)] == res[0]["1"][1]
res = await modclient.ts().mrevrange(
0,
10,
filters=["team=ny"],
aggregation_type="count",
bucket_size_msec=10,
align=1,
)
assert [(1, 10.0), (0, 1.0)] == res[0]["1"][1]
@pytest.mark.redismod
async def test_get(modclient: redis.Redis):
name = "test"
await modclient.ts().create(name)
assert await modclient.ts().get(name) is None
await modclient.ts().add(name, 2, 3)
assert 2 == (await modclient.ts().get(name))[0]
await modclient.ts().add(name, 3, 4)
assert 4 == (await modclient.ts().get(name))[1]
@pytest.mark.redismod
@pytest.mark.onlynoncluster
async def test_mget(modclient: redis.Redis):
await modclient.ts().create(1, labels={"Test": "This"})
await modclient.ts().create(2, labels={"Test": "This", "Taste": "That"})
act_res = await modclient.ts().mget(["Test=This"])
exp_res = [{"1": [{}, None, None]}, {"2": [{}, None, None]}]
assert act_res == exp_res
await modclient.ts().add(1, "*", 15)
await modclient.ts().add(2, "*", 25)
res = await modclient.ts().mget(["Test=This"])
assert 15 == res[0]["1"][2]
assert 25 == res[1]["2"][2]
res = await modclient.ts().mget(["Taste=That"])
assert 25 == res[0]["2"][2]
# test with_labels
assert {} == res[0]["2"][0]
res = await modclient.ts().mget(["Taste=That"], with_labels=True)
assert {"Taste": "That", "Test": "This"} == res[0]["2"][0]
@pytest.mark.redismod
async def test_info(modclient: redis.Redis):
await modclient.ts().create(
1, retention_msecs=5, labels={"currentLabel": "currentData"}
)
info = await modclient.ts().info(1)
assert 5 == info.retention_msecs
assert info.labels["currentLabel"] == "currentData"
@pytest.mark.redismod
@skip_ifmodversion_lt("1.4.0", "timeseries")
async def testInfoDuplicatePolicy(modclient: redis.Redis):
await modclient.ts().create(
1, retention_msecs=5, labels={"currentLabel": "currentData"}
)
info = await modclient.ts().info(1)
assert info.duplicate_policy is None
await modclient.ts().create("time-serie-2", duplicate_policy="min")
info = await modclient.ts().info("time-serie-2")
assert "min" == info.duplicate_policy
@pytest.mark.redismod
@pytest.mark.onlynoncluster
async def test_query_index(modclient: redis.Redis):
await modclient.ts().create(1, labels={"Test": "This"})
await modclient.ts().create(2, labels={"Test": "This", "Taste": "That"})
assert 2 == len(await modclient.ts().queryindex(["Test=This"]))
assert 1 == len(await modclient.ts().queryindex(["Taste=That"]))
assert [2] == await modclient.ts().queryindex(["Taste=That"])
# @pytest.mark.redismod
# async def test_pipeline(modclient: redis.Redis):
# pipeline = await modclient.ts().pipeline()
# pipeline.create("with_pipeline")
# for i in range(100):
# pipeline.add("with_pipeline", i, 1.1 * i)
# pipeline.execute()
# info = await modclient.ts().info("with_pipeline")
# assert info.lastTimeStamp == 99
# assert info.total_samples == 100
# assert await modclient.ts().get("with_pipeline")[1] == 99 * 1.1
@pytest.mark.redismod
async def test_uncompressed(modclient: redis.Redis):
await modclient.ts().create("compressed")
await modclient.ts().create("uncompressed", uncompressed=True)
compressed_info = await modclient.ts().info("compressed")
uncompressed_info = await modclient.ts().info("uncompressed")
assert compressed_info.memory_usage != uncompressed_info.memory_usage
|