Skip to content

Commit 4ea6956

Browse files
committed
Cover for Postgres' physical ordering
1 parent d7eb88e commit 4ea6956

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

api/segments/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,7 @@ def deep_clone(self, cloned_segment: Segment) -> "SegmentRule":
302302

303303

304304
class ConditionManager(SoftDeleteExportableManager):
305-
def get_queryset(
306-
self,
307-
) -> models.QuerySet["Condition"]:
305+
def get_queryset(self) -> models.QuerySet["Condition"]:
308306
# Effectively `Condition.Meta.ordering = ("id",) if ... else ()`,
309307
# but avoid the weirdness of a setting-dependant migration
310308
# and having to reload everything in tests

api/tests/unit/segments/test_unit_segments_models.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,74 @@
11
from unittest.mock import PropertyMock
22

33
import pytest
4+
from django.db import transaction
45
from flag_engine.segments.constants import EQUAL, PERCENTAGE_SPLIT
6+
from pytest_django.fixtures import SettingsWrapper
57
from pytest_mock import MockerFixture
68

79
from segments.models import Condition, Segment, SegmentRule
810
from segments.services import SegmentCloneService
911

1012

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+
1172
def test_get_segment_returns_parent_segment_for_nested_rule(
1273
segment: Segment,
1374
) -> None:

0 commit comments

Comments
 (0)