Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Segment matching for sub rules and conditions #4776

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions api/tests/unit/segments/test_unit_segments_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading