api: Add search by playbook path and allow partial names
This will allow to search for a playbook based on the full path, full name or a part of either. Change-Id: I2bbdcd69e6deda2b491f86256d04dc6c8c0b17b1
This commit is contained in:
@@ -18,6 +18,8 @@
|
||||
import django_filters
|
||||
from django.db import models as django_models
|
||||
|
||||
from ara.api import models as ara_models
|
||||
|
||||
|
||||
class BaseFilter(django_filters.rest_framework.FilterSet):
|
||||
created_before = django_filters.IsoDateTimeFilter(field_name="created", lookup_expr="lte")
|
||||
@@ -54,8 +56,11 @@ class LabelFilter(BaseFilter):
|
||||
|
||||
|
||||
class PlaybookFilter(DateFilter):
|
||||
name = django_filters.CharFilter(field_name="name", lookup_expr="iexact")
|
||||
status = django_filters.CharFilter(field_name="status", lookup_expr="iexact")
|
||||
name = django_filters.CharFilter(field_name="name", lookup_expr="icontains")
|
||||
path = django_filters.CharFilter(field_name="path", lookup_expr="icontains")
|
||||
status = django_filters.MultipleChoiceFilter(
|
||||
field_name="status", choices=ara_models.Playbook.STATUS, lookup_expr="iexact"
|
||||
)
|
||||
|
||||
# fmt: off
|
||||
order = django_filters.OrderingFilter(
|
||||
|
||||
@@ -119,10 +119,29 @@ class PlaybookTestCase(APITestCase):
|
||||
def test_get_playbook_by_name(self):
|
||||
playbook = factories.PlaybookFactory(name="playbook1")
|
||||
factories.PlaybookFactory(name="playbook2")
|
||||
|
||||
# Test exact match
|
||||
request = self.client.get("/api/v1/playbooks?name=playbook1")
|
||||
self.assertEqual(1, len(request.data["results"]))
|
||||
self.assertEqual(playbook.name, request.data["results"][0]["name"])
|
||||
|
||||
# Test partial match
|
||||
request = self.client.get("/api/v1/playbooks?name=playbook")
|
||||
self.assertEqual(len(request.data["results"]), 2)
|
||||
|
||||
def test_get_playbook_by_path(self):
|
||||
playbook = factories.PlaybookFactory(path="/root/playbook.yml")
|
||||
factories.PlaybookFactory(path="/home/playbook.yml")
|
||||
|
||||
# Test exact match
|
||||
request = self.client.get("/api/v1/playbooks?path=/root/playbook.yml")
|
||||
self.assertEqual(1, len(request.data["results"]))
|
||||
self.assertEqual(playbook.path, request.data["results"][0]["path"])
|
||||
|
||||
# Test partial match
|
||||
request = self.client.get("/api/v1/playbooks?path=playbook.yml")
|
||||
self.assertEqual(len(request.data["results"]), 2)
|
||||
|
||||
def test_patch_playbook_name(self):
|
||||
playbook = factories.PlaybookFactory()
|
||||
new_name = "foo"
|
||||
|
||||
Reference in New Issue
Block a user