|
6 | 6 | import unittest.mock as mock |
7 | 7 | import pytest |
8 | 8 |
|
| 9 | +from splitio.optional.loaders import asyncio |
9 | 10 | from splitio.client.util import get_metadata, SdkMetadata |
10 | | -from splitio.storage.redis import RedisEventsStorage, RedisImpressionsStorage, \ |
| 11 | +from splitio.storage.redis import RedisEventsStorage, RedisImpressionsStorage, RedisImpressionsStorageAsync, \ |
11 | 12 | RedisSegmentStorage, RedisSplitStorage, RedisTelemetryStorage |
12 | | -from splitio.storage.adapters.redis import RedisAdapter, RedisAdapterException, build |
| 13 | +from splitio.storage.adapters.redis import RedisAdapter, RedisAdapterAsync, RedisAdapterException, build |
13 | 14 | from splitio.models.segments import Segment |
14 | 15 | from splitio.models.impressions import Impression |
15 | 16 | from splitio.models.events import Event, EventWrapper |
@@ -334,6 +335,124 @@ def test_add_impressions_to_pipe(self, mocker): |
334 | 335 | storage.add_impressions_to_pipe(impressions, adapter) |
335 | 336 | assert adapter.rpush.mock_calls == [mocker.call('SPLITIO.impressions', *to_validate)] |
336 | 337 |
|
| 338 | +class RedisImpressionsStorageAsyncTests(object): # pylint: disable=too-few-public-methods |
| 339 | + """Redis Impressions async storage test cases.""" |
| 340 | + |
| 341 | + def test_wrap_impressions(self, mocker): |
| 342 | + """Test wrap impressions.""" |
| 343 | + adapter = mocker.Mock(spec=RedisAdapterAsync) |
| 344 | + metadata = get_metadata({}) |
| 345 | + storage = RedisImpressionsStorageAsync(adapter, metadata) |
| 346 | + |
| 347 | + impressions = [ |
| 348 | + Impression('key1', 'feature1', 'on', 'some_label', 123456, 'buck1', 321654), |
| 349 | + Impression('key2', 'feature2', 'on', 'some_label', 123456, 'buck1', 321654), |
| 350 | + Impression('key3', 'feature2', 'on', 'some_label', 123456, 'buck1', 321654), |
| 351 | + Impression('key4', 'feature1', 'on', 'some_label', 123456, 'buck1', 321654) |
| 352 | + ] |
| 353 | + |
| 354 | + to_validate = [json.dumps({ |
| 355 | + 'm': { # METADATA PORTION |
| 356 | + 's': metadata.sdk_version, |
| 357 | + 'n': metadata.instance_name, |
| 358 | + 'i': metadata.instance_ip, |
| 359 | + }, |
| 360 | + 'i': { # IMPRESSION PORTION |
| 361 | + 'k': impression.matching_key, |
| 362 | + 'b': impression.bucketing_key, |
| 363 | + 'f': impression.feature_name, |
| 364 | + 't': impression.treatment, |
| 365 | + 'r': impression.label, |
| 366 | + 'c': impression.change_number, |
| 367 | + 'm': impression.time, |
| 368 | + } |
| 369 | + }) for impression in impressions] |
| 370 | + |
| 371 | + assert storage._wrap_impressions(impressions) == to_validate |
| 372 | + |
| 373 | + @pytest.mark.asyncio |
| 374 | + async def test_add_impressions(self, mocker): |
| 375 | + """Test that adding impressions to storage works.""" |
| 376 | + adapter = mocker.Mock(spec=RedisAdapterAsync) |
| 377 | + metadata = get_metadata({}) |
| 378 | + storage = RedisImpressionsStorageAsync(adapter, metadata) |
| 379 | + |
| 380 | + impressions = [ |
| 381 | + Impression('key1', 'feature1', 'on', 'some_label', 123456, 'buck1', 321654), |
| 382 | + Impression('key2', 'feature2', 'on', 'some_label', 123456, 'buck1', 321654), |
| 383 | + Impression('key3', 'feature2', 'on', 'some_label', 123456, 'buck1', 321654), |
| 384 | + Impression('key4', 'feature1', 'on', 'some_label', 123456, 'buck1', 321654) |
| 385 | + ] |
| 386 | + self.key = None |
| 387 | + self.imps = None |
| 388 | + async def rpush(key, *imps): |
| 389 | + self.key = key |
| 390 | + self.imps = imps |
| 391 | + |
| 392 | + adapter.rpush = rpush |
| 393 | + assert await storage.put(impressions) is True |
| 394 | + |
| 395 | + to_validate = [json.dumps({ |
| 396 | + 'm': { # METADATA PORTION |
| 397 | + 's': metadata.sdk_version, |
| 398 | + 'n': metadata.instance_name, |
| 399 | + 'i': metadata.instance_ip, |
| 400 | + }, |
| 401 | + 'i': { # IMPRESSION PORTION |
| 402 | + 'k': impression.matching_key, |
| 403 | + 'b': impression.bucketing_key, |
| 404 | + 'f': impression.feature_name, |
| 405 | + 't': impression.treatment, |
| 406 | + 'r': impression.label, |
| 407 | + 'c': impression.change_number, |
| 408 | + 'm': impression.time, |
| 409 | + } |
| 410 | + }) for impression in impressions] |
| 411 | + |
| 412 | + assert self.key == 'SPLITIO.impressions' |
| 413 | + assert self.imps == tuple(to_validate) |
| 414 | + |
| 415 | + # Assert that if an exception is thrown it's caught and False is returned |
| 416 | + adapter.reset_mock() |
| 417 | + |
| 418 | + async def rpush2(key, *imps): |
| 419 | + raise RedisAdapterException('something') |
| 420 | + adapter.rpush = rpush2 |
| 421 | + assert await storage.put(impressions) is False |
| 422 | + |
| 423 | + def test_add_impressions_to_pipe(self, mocker): |
| 424 | + """Test that adding impressions to storage works.""" |
| 425 | + adapter = mocker.Mock(spec=RedisAdapterAsync) |
| 426 | + metadata = get_metadata({}) |
| 427 | + storage = RedisImpressionsStorageAsync(adapter, metadata) |
| 428 | + |
| 429 | + impressions = [ |
| 430 | + Impression('key1', 'feature1', 'on', 'some_label', 123456, 'buck1', 321654), |
| 431 | + Impression('key2', 'feature2', 'on', 'some_label', 123456, 'buck1', 321654), |
| 432 | + Impression('key3', 'feature2', 'on', 'some_label', 123456, 'buck1', 321654), |
| 433 | + Impression('key4', 'feature1', 'on', 'some_label', 123456, 'buck1', 321654) |
| 434 | + ] |
| 435 | + |
| 436 | + to_validate = [json.dumps({ |
| 437 | + 'm': { # METADATA PORTION |
| 438 | + 's': metadata.sdk_version, |
| 439 | + 'n': metadata.instance_name, |
| 440 | + 'i': metadata.instance_ip, |
| 441 | + }, |
| 442 | + 'i': { # IMPRESSION PORTION |
| 443 | + 'k': impression.matching_key, |
| 444 | + 'b': impression.bucketing_key, |
| 445 | + 'f': impression.feature_name, |
| 446 | + 't': impression.treatment, |
| 447 | + 'r': impression.label, |
| 448 | + 'c': impression.change_number, |
| 449 | + 'm': impression.time, |
| 450 | + } |
| 451 | + }) for impression in impressions] |
| 452 | + |
| 453 | + storage.add_impressions_to_pipe(impressions, adapter) |
| 454 | + assert adapter.rpush.mock_calls == [mocker.call('SPLITIO.impressions', *to_validate)] |
| 455 | + |
337 | 456 |
|
338 | 457 | class RedisEventsStorageTests(object): # pylint: disable=too-few-public-methods |
339 | 458 | """Redis Impression storage test cases.""" |
|
0 commit comments