Skip to content

Commit 91efcff

Browse files
authored
feat: added search query to get experiment endpoints (#7617)
1 parent e5f2f7c commit 91efcff

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

api/experimentation/views.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from typing import Any
33

4-
from django.db.models import QuerySet
4+
from django.db.models import Q, QuerySet
55
from rest_framework import mixins, status
66
from rest_framework.decorators import action
77
from rest_framework.permissions import IsAuthenticated
@@ -126,6 +126,11 @@ def get_queryset(self) -> "QuerySet[Experiment]":
126126
status_filter = self.request.query_params.get("status")
127127
if status_filter:
128128
qs = qs.filter(status=status_filter)
129+
130+
q = self.request.query_params.get("q")
131+
if q:
132+
qs = qs.filter(Q(name__icontains=q) | Q(feature__name__icontains=q))
133+
129134
return qs
130135

131136
def create(self, request: Request, *args: object, **kwargs: object) -> Response:

api/tests/unit/experimentation/test_experiment_views.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,62 @@ def test_get_list__filter_by_status__returns_filtered(
360360
assert len(response.json()) == expected_count
361361

362362

363+
def test_get_list__search_by_experiment_name__returns_matching(
364+
admin_client_new: APIClient,
365+
environment: Environment,
366+
experiment: Experiment,
367+
enable_features: EnableFeaturesFixture,
368+
) -> None:
369+
# Given
370+
enable_features(EXPERIMENT_FLAG)
371+
372+
# When
373+
response = admin_client_new.get(_list_url(environment), {"q": experiment.name[:4]})
374+
375+
# Then
376+
assert response.status_code == status.HTTP_200_OK
377+
assert len(response.json()) == 1
378+
assert response.json()[0]["id"] == experiment.id
379+
380+
381+
def test_get_list__search_by_feature_name__returns_matching(
382+
admin_client_new: APIClient,
383+
environment: Environment,
384+
experiment: Experiment,
385+
multivariate_feature: Feature,
386+
enable_features: EnableFeaturesFixture,
387+
) -> None:
388+
# Given
389+
enable_features(EXPERIMENT_FLAG)
390+
391+
# When
392+
response = admin_client_new.get(
393+
_list_url(environment), {"q": multivariate_feature.name}
394+
)
395+
396+
# Then
397+
assert response.status_code == status.HTTP_200_OK
398+
assert len(response.json()) == 1
399+
assert response.json()[0]["id"] == experiment.id
400+
401+
402+
def test_get_list__search_no_match__returns_empty(
403+
admin_client_new: APIClient,
404+
environment: Environment,
405+
experiment: Experiment,
406+
enable_features: EnableFeaturesFixture,
407+
) -> None:
408+
# Given
409+
enable_features(EXPERIMENT_FLAG)
410+
411+
# When
412+
response = admin_client_new.get(_list_url(environment), {"q": "nonexistent"})
413+
414+
# Then
415+
assert response.status_code == status.HTTP_200_OK
416+
assert len(response.json()) == 0
417+
418+
363419
def test_get_detail__exists__returns_200(
364420
admin_client_new: APIClient,
365421
environment: Environment,

0 commit comments

Comments
 (0)