|
11 | 11 | from splitio.models.impressions import Label |
12 | 12 | from splitio.models.grammar import condition |
13 | 13 | from splitio.models import rule_based_segments |
| 14 | +from splitio.models.fallback_treatment import FallbackTreatment |
| 15 | +from splitio.models.fallback_config import FallbackTreatmentsConfiguration |
14 | 16 | from splitio.engine import evaluator, splitters |
15 | 17 | from splitio.engine.evaluator import EvaluationContext |
16 | 18 | from splitio.storage.inmemmory import InMemorySplitStorage, InMemorySegmentStorage, InMemoryRuleBasedSegmentStorage, \ |
@@ -372,6 +374,54 @@ def test_prerequisites(self): |
372 | 374 | ctx = evaluation_facctory.context_for('mauro@split.io', ['prereq_chain']) |
373 | 375 | assert e.eval_with_context('mauro@split.io', 'mauro@split.io', 'prereq_chain', {'email': 'mauro@split.io'}, ctx)['treatment'] == "on_default" |
374 | 376 |
|
| 377 | + def test_evaluate_treatment_with_fallback(self, mocker): |
| 378 | + """Test that a evaluation return fallback treatment.""" |
| 379 | + splitter_mock = mocker.Mock(spec=splitters.Splitter) |
| 380 | + logger_mock = mocker.Mock(spec=logging.Logger) |
| 381 | + evaluator._LOGGER = logger_mock |
| 382 | + mocked_split = mocker.Mock(spec=Split) |
| 383 | + ctx = EvaluationContext(flags={'some': mocked_split}, segment_memberships=set(), rbs_segments={}) |
| 384 | + |
| 385 | + # should use global fallback |
| 386 | + logger_mock.reset_mock() |
| 387 | + e = evaluator.Evaluator(splitter_mock, FallbackTreatmentsConfiguration(FallbackTreatment("off-global", {"prop":"val"}))) |
| 388 | + result = e.eval_with_context('some_key', 'some_bucketing_key', 'some2', {}, ctx) |
| 389 | + assert result['treatment'] == 'off-global' |
| 390 | + assert result['configurations'] == '{"prop": "val"}' |
| 391 | + assert result['impression']['label'] == "fallback - " + Label.SPLIT_NOT_FOUND |
| 392 | + assert logger_mock.debug.mock_calls[0] == mocker.call("Using Global Fallback Treatment.") |
| 393 | + |
| 394 | + |
| 395 | + # should use by flag fallback |
| 396 | + logger_mock.reset_mock() |
| 397 | + e = evaluator.Evaluator(splitter_mock, FallbackTreatmentsConfiguration(None, {"some2": FallbackTreatment("off-some2", {"prop2":"val2"})})) |
| 398 | + result = e.eval_with_context('some_key', 'some_bucketing_key', 'some2', {}, ctx) |
| 399 | + assert result['treatment'] == 'off-some2' |
| 400 | + assert result['configurations'] == '{"prop2": "val2"}' |
| 401 | + assert result['impression']['label'] == "fallback - " + Label.SPLIT_NOT_FOUND |
| 402 | + assert logger_mock.debug.mock_calls[0] == mocker.call("Using Fallback Treatment for feature: %s", "some2") |
| 403 | + |
| 404 | + # should not use any fallback |
| 405 | + e = evaluator.Evaluator(splitter_mock, FallbackTreatmentsConfiguration(None, {"some2": FallbackTreatment("off-some2", {"prop2":"val2"})})) |
| 406 | + result = e.eval_with_context('some_key', 'some_bucketing_key', 'some3', {}, ctx) |
| 407 | + assert result['treatment'] == 'control' |
| 408 | + assert result['configurations'] == None |
| 409 | + assert result['impression']['label'] == Label.SPLIT_NOT_FOUND |
| 410 | + |
| 411 | + # should use by flag fallback |
| 412 | + e = evaluator.Evaluator(splitter_mock, FallbackTreatmentsConfiguration(FallbackTreatment("off-global", {"prop":"val"}), {"some2": FallbackTreatment("off-some2", {"prop2":"val2"})})) |
| 413 | + result = e.eval_with_context('some_key', 'some_bucketing_key', 'some2', {}, ctx) |
| 414 | + assert result['treatment'] == 'off-some2' |
| 415 | + assert result['configurations'] == '{"prop2": "val2"}' |
| 416 | + assert result['impression']['label'] == "fallback - " + Label.SPLIT_NOT_FOUND |
| 417 | + |
| 418 | + # should global flag fallback |
| 419 | + e = evaluator.Evaluator(splitter_mock, FallbackTreatmentsConfiguration(FallbackTreatment("off-global", {"prop":"val"}), {"some2": FallbackTreatment("off-some2", {"prop2":"val2"})})) |
| 420 | + result = e.eval_with_context('some_key', 'some_bucketing_key', 'some3', {}, ctx) |
| 421 | + assert result['treatment'] == 'off-global' |
| 422 | + assert result['configurations'] == '{"prop": "val"}' |
| 423 | + assert result['impression']['label'] == "fallback - " + Label.SPLIT_NOT_FOUND |
| 424 | + |
375 | 425 | @pytest.mark.asyncio |
376 | 426 | async def test_evaluate_treatment_with_rbs_in_condition_async(self): |
377 | 427 | e = evaluator.Evaluator(splitters.Splitter()) |
|
0 commit comments