Skip to content

Commit

Permalink
test: Segment matching for sub rules and conditions (#4776)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan authored Dec 4, 2024
1 parent 679acdd commit 27f50ca
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 8 deletions.
12 changes: 6 additions & 6 deletions api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ hubspot-api-client = "^8.2.1"
djangorestframework-dataclasses = "^1.3.1"
pyotp = "^2.9.0"
flagsmith-task-processor = { git = "https://github.com/Flagsmith/flagsmith-task-processor", tag = "v1.0.2" }
flagsmith-common = { git = "https://github.com/Flagsmith/flagsmith-common", tag = "v1.2.0" }
flagsmith-common = { git = "https://github.com/Flagsmith/flagsmith-common", tag = "v1.3.0" }
tzdata = "^2024.1"
djangorestframework-simplejwt = "^5.3.1"

Expand All @@ -196,7 +196,7 @@ flagsmith-ldap = { git = "https://github.com/flagsmith/flagsmith-ldap", tag = "v
optional = true

[tool.poetry.group.workflows.dependencies]
workflows-logic = { git = "https://github.com/flagsmith/flagsmith-workflows", tag = "v2.7.0" }
workflows-logic = { git = "https://github.com/flagsmith/flagsmith-workflows", tag = "v2.7.1" }

[tool.poetry.group.dev.dependencies]
django-test-migrations = "~1.2.0"
Expand Down
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 @@ -670,6 +670,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

0 comments on commit 27f50ca

Please sign in to comment.