Refactor dynamic search app API
Convert the API to use viewsets. The search function is now a service of the search model resource. The simple and advance search are now the same service. The difference is determined by the URL query. A ?q= means a simple search. For advanced search pass the search model fields in the URL query, example: ?q=document_type__label= Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.urls import reverse
|
||||
|
||||
from rest_framework import status
|
||||
|
||||
from mayan.apps.documents.permissions import permission_document_view
|
||||
@@ -12,46 +10,10 @@ from mayan.apps.rest_api.tests import BaseAPITestCase
|
||||
from ..classes import SearchModel
|
||||
|
||||
|
||||
class SearchAPITestCase(DocumentTestMixin, BaseAPITestCase):
|
||||
auto_upload_document = False
|
||||
|
||||
def setUp(self):
|
||||
super(SearchAPITestCase, self).setUp()
|
||||
self.login_user()
|
||||
|
||||
def _request_search_view(self):
|
||||
return self.get(
|
||||
path='{}?q={}'.format(
|
||||
reverse(
|
||||
viewname='rest_api:search-view', kwargs={
|
||||
'search_model': document_search.get_full_name()
|
||||
}
|
||||
), self.document.label
|
||||
)
|
||||
)
|
||||
|
||||
def test_search_no_access(self):
|
||||
self.document = self.upload_document()
|
||||
response = self._request_search_view()
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['count'], 0)
|
||||
|
||||
def test_search_with_access(self):
|
||||
self.document = self.upload_document()
|
||||
self.grant_access(
|
||||
permission=permission_document_view, obj=self.document
|
||||
)
|
||||
response = self._request_search_view()
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
self.assertEqual(
|
||||
response.data['results'][0]['label'], self.document.label
|
||||
)
|
||||
self.assertEqual(response.data['count'], 1)
|
||||
|
||||
def test_search_models_view(self):
|
||||
class SearchModelAPITestCase(BaseAPITestCase):
|
||||
def test_search_models_list_view(self):
|
||||
response = self.get(
|
||||
viewname='rest_api:searchmodel-list'
|
||||
viewname='rest_api:search_model-list'
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
@@ -59,3 +21,67 @@ class SearchAPITestCase(DocumentTestMixin, BaseAPITestCase):
|
||||
[search_model['pk'] for search_model in response.data['results']],
|
||||
[search_model.pk for search_model in SearchModel.all()]
|
||||
)
|
||||
|
||||
|
||||
class SearchModelSearchAPITestCase(DocumentTestMixin, BaseAPITestCase):
|
||||
auto_create_document_type = False
|
||||
auto_upload_document = False
|
||||
|
||||
def setUp(self):
|
||||
super(SearchModelSearchAPITestCase, self).setUp()
|
||||
self._create_document_type_random()
|
||||
self._create_document()
|
||||
self._create_document_type_random()
|
||||
self._create_document()
|
||||
|
||||
def _request_search_view(self):
|
||||
query = {'q': self.test_document.document_type.label}
|
||||
|
||||
return self.get(
|
||||
viewname='rest_api:search_model-search', kwargs={
|
||||
'search_model_name': document_search.get_full_name()
|
||||
}, query=query
|
||||
)
|
||||
|
||||
def test_search_api_view_no_permission(self):
|
||||
response = self._request_search_view()
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['count'], 0)
|
||||
|
||||
def test_search_api_view_with_access(self):
|
||||
self.grant_access(
|
||||
obj=self.test_document, permission=permission_document_view,
|
||||
)
|
||||
|
||||
response = self._request_search_view()
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(
|
||||
response.data['results'][0]['label'], self.test_document.label
|
||||
)
|
||||
self.assertEqual(response.data['count'], 1)
|
||||
|
||||
def _request_advanced_search_view(self):
|
||||
query = {'document_type__label': self.test_document.document_type.label}
|
||||
|
||||
return self.get(
|
||||
viewname='rest_api:search_model-search', kwargs={
|
||||
'search_model_name': document_search.get_full_name()
|
||||
}, query=query
|
||||
)
|
||||
|
||||
def test_advanced_search_api_view_no_permission(self):
|
||||
response = self._request_advanced_search_view()
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['count'], 0)
|
||||
|
||||
def test_advanced_search_api_view_with_access(self):
|
||||
self.grant_access(
|
||||
obj=self.test_document, permission=permission_document_view,
|
||||
)
|
||||
|
||||
response = self._request_advanced_search_view()
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(
|
||||
response.data['results'][0]['label'], self.test_document.label
|
||||
)
|
||||
self.assertEqual(response.data['count'], 1)
|
||||
|
||||
Reference in New Issue
Block a user