Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
31 changes: 29 additions & 2 deletions api/features/multivariate/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,41 @@ class Meta(NestedMultivariateFeatureOptionSerializer.Meta):

def validate(self, attrs): # type: ignore[no-untyped-def]
attrs = super().validate(attrs)

# For partial updates or nested routes, use existing instance or URL value as fallback
feature = attrs.get("feature")
if feature is None and self.instance is not None:
feature = self.instance.feature

if feature is None:
view = self.context.get("view")
feature_pk = getattr(view, "kwargs", {}).get("feature_pk") if view else None
project_pk = getattr(view, "kwargs", {}).get("project_pk") if view else None
if feature_pk is not None:
qs = Feature.objects.filter(pk=feature_pk)
if project_pk is not None:
qs = qs.filter(project__id=project_pk)
feature = qs.first()
Comment thread
cursor[bot] marked this conversation as resolved.

if feature is None:
raise serializers.ValidationError({"feature": "Feature is required"})

Comment thread
cursor[bot] marked this conversation as resolved.
# Ensure feature is always present in validated data (e.g. for creates via nested routes)
attrs["feature"] = feature

default_allocation = attrs.get(
"default_percentage_allocation",
self.instance.default_percentage_allocation if self.instance else 100,
)
Comment thread
cursor[bot] marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.

total_sibling_percentage_allocation = (
self._get_siblings(attrs["feature"]).aggregate(
self._get_siblings(feature).aggregate(
Comment thread
cursor[bot] marked this conversation as resolved.
total_percentage_allocation=Sum("default_percentage_allocation")
)["total_percentage_allocation"]
or 0
)
total_percentage_allocation = (
total_sibling_percentage_allocation + attrs["default_percentage_allocation"]
total_sibling_percentage_allocation + default_allocation
)

if total_percentage_allocation > 100:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,141 @@ def test_can_create_mv_option(client, project, mv_option_50_percent, feature):
assert set(data.items()).issubset(set(response.json().items()))


@pytest.mark.parametrize(
"client",
[lazy_fixture("admin_master_api_key_client"), lazy_fixture("admin_client")],
)
def test_create_mv_option__without_feature_in_payload__uses_feature_from_url(
client: APIClient,
project: int,
feature: int,
) -> None:
# Given
url = reverse(
"api-v1:projects:feature-mv-options-list",
args=[project, feature],
)
data = {
"type": "unicode",
"string_value": "bigger",
"default_percentage_allocation": 50,
}
# When
response = client.post(
url,
data=json.dumps(data),
content_type="application/json",
)
# Then
assert response.status_code == status.HTTP_201_CREATED
body = response.json()
assert body["id"]
assert body["feature"] == feature
assert body["string_value"] == "bigger"
assert body["default_percentage_allocation"] == 50


@pytest.mark.parametrize(
"client",
[lazy_fixture("admin_master_api_key_client"), lazy_fixture("admin_client")],
)
def test_create_mv_option__without_default_percentage_allocation__uses_default(
client: APIClient,
project: int,
feature: int,
) -> None:
# Given
url = reverse(
"api-v1:projects:feature-mv-options-list",
args=[project, feature],
)
data = {
"type": "unicode",
"feature": feature,
"string_value": "test_value",
}

# When
response = client.post(
url,
data=json.dumps(data),
content_type="application/json",
)

# Then
assert response.status_code == status.HTTP_201_CREATED
assert response.json()["id"]
assert response.json()["default_percentage_allocation"] == 100


@pytest.mark.parametrize(
"client",
[lazy_fixture("admin_master_api_key_client"), lazy_fixture("admin_client")],
)
def test_create_mv_option__without_default_percentage_allocation_with_existing_sibling_and_total_gt_100__returns_400( # noqa: E501
client: APIClient,
project: int,
feature: int,
mv_option_50_percent: int,
) -> None:
# Given
url = reverse(
"api-v1:projects:feature-mv-options-list",
args=[project, feature],
)
data = {
"type": "unicode",
"feature": feature,
"string_value": "another_value",
}

# When
response = client.post(
url,
data=json.dumps(data),
content_type="application/json",
)

# Then
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json()["default_percentage_allocation"] == [
"Invalid percentage allocation"
]


@pytest.mark.parametrize(
"client",
[lazy_fixture("admin_master_api_key_client"), lazy_fixture("admin_client")],
)
def test_partial_update_mv_option__without_feature_and_allocation__uses_existing_values(
client: APIClient,
project: int,
feature: int,
mv_option_50_percent: int,
) -> None:
# Given
url = reverse(
"api-v1:projects:feature-mv-options-detail",
args=[project, feature, mv_option_50_percent],
)
data = {
"string_value": "updated_value",
}

# When
response = client.patch(
url,
data=json.dumps(data),
content_type="application/json",
)

# Then
assert response.status_code == status.HTTP_200_OK
assert response.json()["string_value"] == "updated_value"
assert response.json()["default_percentage_allocation"] == 50
assert response.json()["feature"] == feature


@pytest.mark.parametrize(
"client, feature_id",
[
Expand Down Expand Up @@ -183,6 +318,56 @@ def test_can_update_default_percentage_allocation( # type: ignore[no-untyped-de
assert set(data.items()).issubset(set(response.json().items()))


@pytest.mark.parametrize(
"client",
[lazy_fixture("admin_master_api_key_client"), lazy_fixture("admin_client")],
)
def test_partial_update_default_percentage_allocation__sibling_total_gt_100__returns_400( # noqa: E501
project: int,
mv_option_50_percent: int,
client: APIClient,
feature: int,
) -> None:
# First let's create another mv_option with 30 percent allocation
url = reverse(
"api-v1:projects:feature-mv-options-list",
args=[project, feature],
)
data = {
"type": "unicode",
"feature": feature,
"string_value": "bigger",
"default_percentage_allocation": 30,
}
response = client.post(
url,
data=json.dumps(data),
content_type="application/json",
)
assert response.status_code == status.HTTP_201_CREATED
mv_option_30_percent = response.json()["id"]

Comment thread
cursor[bot] marked this conversation as resolved.
# Next, let's partially update the 30 percent mv option to 51 percent
url = reverse(
"api-v1:projects:feature-mv-options-detail",
args=[project, feature, mv_option_30_percent],
)
data = {
"default_percentage_allocation": 51,
}
# When
response = client.patch(
url,
data=json.dumps(data),
content_type="application/json",
)
# Then
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json()["default_percentage_allocation"] == [
"Invalid percentage allocation"
]


@pytest.mark.parametrize(
"client",
[lazy_fixture("admin_master_api_key_client"), lazy_fixture("admin_client")],
Expand Down