From ee3b204af6df654b0f5024ff9485a3556a6e21aa Mon Sep 17 00:00:00 2001 From: Zach Aysan Date: Mon, 28 Oct 2024 12:50:28 +0000 Subject: [PATCH] Test segment matching for sub rules and conditions --- .../unit/segments/test_unit_segments_views.py | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/api/tests/unit/segments/test_unit_segments_views.py b/api/tests/unit/segments/test_unit_segments_views.py index 680a67ec7963..417b077b7e84 100644 --- a/api/tests/unit/segments/test_unit_segments_views.py +++ b/api/tests/unit/segments/test_unit_segments_views.py @@ -600,6 +600,148 @@ def test_update_segment_add_new_condition( assert nested_rule.conditions.order_by("-id").first().value == new_condition_value +def test_update_mismatched_rule_and_segment( + project: Project, + admin_client_new: APIClient, + segment: Segment, + segment_rule: SegmentRule, +) -> None: + # Given + url = reverse( + "api-v1:projects:project-segments-detail", args=[project.id, segment.id] + ) + false_segment = Segment.objects.create(name="False segment", project=project) + segment_rule.segment = false_segment + segment_rule.save() + + nested_rule = SegmentRule.objects.create( + rule=segment_rule, type=SegmentRule.ANY_RULE + ) + existing_condition = Condition.objects.create( + rule=nested_rule, property="foo", operator=EQUAL, value="bar" + ) + + new_condition_property = "foo2" + new_condition_value = "bar" + data = { + "name": segment.name, + "project": project.id, + "rules": [ + { + "id": segment_rule.id, + "type": segment_rule.type, + "rules": [ + { + "id": nested_rule.id, + "type": nested_rule.type, + "rules": [], + "conditions": [ + # existing condition + { + "id": existing_condition.id, + "property": existing_condition.property, + "operator": existing_condition.operator, + "value": existing_condition.value, + }, + # new condition + { + "property": new_condition_property, + "operator": EQUAL, + "value": new_condition_value, + }, + ], + } + ], + "conditions": [], + } + ], + } + + # When + response = admin_client_new.put( + url, data=json.dumps(data), content_type="application/json" + ) + + # Then + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert response.json() == {"segment": "Mismatched segment is not allowed"} + segment_rule.refresh_from_db() + assert segment_rule.segment == false_segment + + +def test_update_mismatched_condition_and_segment( + project: Project, + admin_client_new: APIClient, + segment: Segment, + segment_rule: SegmentRule, +) -> None: + # Given + url = reverse( + "api-v1:projects:project-segments-detail", args=[project.id, segment.id] + ) + false_segment = Segment.objects.create(name="False segment", project=project) + false_segment_rule = SegmentRule.objects.create( + segment=false_segment, type=SegmentRule.ALL_RULE + ) + false_nested_rule = SegmentRule.objects.create( + rule=false_segment_rule, type=SegmentRule.ANY_RULE + ) + nested_rule = SegmentRule.objects.create( + rule=segment_rule, type=SegmentRule.ANY_RULE + ) + + existing_condition = Condition.objects.create( + rule=false_nested_rule, property="foo", operator=EQUAL, value="bar" + ) + + new_condition_property = "foo2" + new_condition_value = "bar" + data = { + "name": segment.name, + "project": project.id, + "rules": [ + { + "id": segment_rule.id, + "type": segment_rule.type, + "rules": [ + { + "id": nested_rule.id, + "type": nested_rule.type, + "rules": [], + "conditions": [ + # existing condition + { + "id": existing_condition.id, + "property": existing_condition.property, + "operator": existing_condition.operator, + "value": existing_condition.value, + }, + # new condition + { + "property": new_condition_property, + "operator": EQUAL, + "value": new_condition_value, + }, + ], + } + ], + "conditions": [], + } + ], + } + + # When + response = admin_client_new.put( + url, data=json.dumps(data), content_type="application/json" + ) + + # Then + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert response.json() == {"segment": "Mismatched segment is not allowed"} + existing_condition.refresh_from_db() + assert existing_condition._get_segment() != segment + + def test_update_segment_versioned_segment( project: Project, admin_client_new: APIClient,