Skip to content

Commit 376f5ec

Browse files
committed
Cover for Postgres' physical ordering
1 parent d7eb88e commit 376f5ec

2 files changed

Lines changed: 58 additions & 3 deletions

File tree

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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,69 @@
22

33
import pytest
44
from flag_engine.segments.constants import EQUAL, PERCENTAGE_SPLIT
5+
from pytest_django.fixtures import SettingsWrapper
56
from pytest_mock import MockerFixture
67

78
from segments.models import Condition, Segment, SegmentRule
89
from segments.services import SegmentCloneService
910

1011

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+
1168
def test_get_segment_returns_parent_segment_for_nested_rule(
1269
segment: Segment,
1370
) -> None:

0 commit comments

Comments
 (0)