Skip to content

Commit 99b35dc

Browse files
fix: use data instead and add test
1 parent 379afd3 commit 99b35dc

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

api/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ features/workflows/logic/
88

99
# Unit test coverage
1010
.coverage
11+
.coverage.*

api/app_analytics/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ def get_fields(self): # type: ignore[no-untyped-def]
8989
def save(self, **kwargs: typing.Any) -> None:
9090
request = self.context["request"]
9191
# validated_data splits out request body with '.' in feature name (e.g a.b.c).
92-
# Instead, it's safe to use self.initial_data as keys are not altered.
93-
for feature_name, evaluation_count in self.initial_data.items():
92+
# Instead, it's safe to use self.data as keys are not altered.
93+
for feature_name, evaluation_count in self.data.items():
9494
feature_evaluation_cache.track_feature_evaluation(
9595
environment_id=request.environment.id,
9696
feature_name=feature_name,

api/conftest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,10 @@ def api_client() -> APIClient:
555555
def feature(project: Project) -> Feature:
556556
return Feature.objects.create(name="Test Feature1", project=project) # type: ignore[no-any-return]
557557

558+
@pytest.fixture()
559+
def feauture_with_dots(project: Project) -> Feature:
560+
return Feature.objects.create(name="feature.name", project=project) # type: ignore[no-any-return]
561+
558562

559563
@pytest.fixture()
560564
def change_request(environment: Environment, admin_user: FFAdminUser) -> ChangeRequest:

api/tests/unit/app_analytics/test_unit_app_analytics_views.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,38 @@ def test_sdk_analytics_ignores_bad_data(
5858
labels={},
5959
)
6060

61+
def test_sdk_analytics_ignores_feature_data_with_dots(
62+
mocker: MockerFixture,
63+
environment: Environment,
64+
feauture_with_dots: Feature,
65+
api_client: APIClient,
66+
) -> None:
67+
# Given
68+
api_client.credentials(HTTP_X_ENVIRONMENT_KEY=environment.api_key)
69+
70+
data = { feauture_with_dots.name: 20 }
71+
mocked_feature_eval_cache = mocker.patch(
72+
"app_analytics.views.feature_evaluation_cache"
73+
)
74+
75+
url = reverse("api-v1:analytics-flags")
76+
77+
# When
78+
response = api_client.post(
79+
url, data=json.dumps(data), content_type="application/json"
80+
)
81+
82+
# Then
83+
assert response.status_code == status.HTTP_200_OK
84+
assert mocked_feature_eval_cache.track_feature_evaluation.call_count == 1
85+
86+
mocked_feature_eval_cache.track_feature_evaluation.assert_called_once_with(
87+
environment_id=environment.id,
88+
feature_name=feauture_with_dots.name,
89+
evaluation_count=data[feauture_with_dots.name],
90+
labels={},
91+
)
92+
6193

6294
def test_get_usage_data(mocker, admin_client, organisation): # type: ignore[no-untyped-def]
6395
# Given

0 commit comments

Comments
 (0)