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