|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 | from flag_engine.segments.constants import EQUAL, PERCENTAGE_SPLIT |
| 5 | +from pytest_django.fixtures import SettingsWrapper |
5 | 6 | from pytest_mock import MockerFixture |
6 | 7 |
|
7 | 8 | from segments.models import Condition, Segment, SegmentRule |
8 | 9 | from segments.services import SegmentCloneService |
9 | 10 |
|
10 | 11 |
|
| 12 | +def test__ConditionManager__get_queryset__explicit_ordering_disabled__cant_guarantee_condition_ordering( |
| 13 | + segment_rule: SegmentRule, |
| 14 | + settings: SettingsWrapper, |
| 15 | +) -> None: |
| 16 | + # Given |
| 17 | + settings.SEGMENT_RULES_CONDITIONS_EXPLICIT_ORDERING_ENABLED = False |
| 18 | + condition1 = Condition.objects.create( |
| 19 | + rule=segment_rule, |
| 20 | + property="condition1", |
| 21 | + operator=EQUAL, |
| 22 | + value="original1", |
| 23 | + ) |
| 24 | + condition2 = Condition.objects.create( |
| 25 | + rule=segment_rule, |
| 26 | + property="condition1", |
| 27 | + operator=EQUAL, |
| 28 | + value="original2", |
| 29 | + ) |
| 30 | + condition1.value = "updated1" |
| 31 | + condition1.save() # UPDATE causes the condition1 tuple to be the most recent physically |
| 32 | + |
| 33 | + # When |
| 34 | + conditions = Condition.objects.filter(rule=segment_rule) |
| 35 | + |
| 36 | + # Then |
| 37 | + assert list(conditions) == [condition2, condition1] |
| 38 | + |
| 39 | + |
| 40 | +def test__ConditionManager__get_queryset__explicit_ordering_enabled__orders_conditions_by_id( |
| 41 | + segment_rule: SegmentRule, |
| 42 | + settings: SettingsWrapper, |
| 43 | +) -> None: |
| 44 | + # Given |
| 45 | + settings.SEGMENT_RULES_CONDITIONS_EXPLICIT_ORDERING_ENABLED = True |
| 46 | + condition1 = Condition.objects.create( |
| 47 | + rule=segment_rule, |
| 48 | + property="condition1", |
| 49 | + operator=EQUAL, |
| 50 | + value="original1", |
| 51 | + ) |
| 52 | + condition2 = Condition.objects.create( |
| 53 | + rule=segment_rule, |
| 54 | + property="condition1", |
| 55 | + operator=EQUAL, |
| 56 | + value="original2", |
| 57 | + ) |
| 58 | + condition1.value = "updated1" |
| 59 | + condition1.save() # Update leads to physical reordering of tuples |
| 60 | + |
| 61 | + # When |
| 62 | + conditions = Condition.objects.filter(rule=segment_rule) |
| 63 | + |
| 64 | + # Then |
| 65 | + assert list(conditions) == [condition1, condition2] |
| 66 | + |
| 67 | + |
11 | 68 | def test_get_segment_returns_parent_segment_for_nested_rule( |
12 | 69 | segment: Segment, |
13 | 70 | ) -> None: |
|
0 commit comments