Files
mayan-edms/mayan/apps/dynamic_search/tests/test_api.py
Roberto Rosario ad7c77b4f3 Update dynamic search app
Sort methods. Update use of .filter_by_access() to
.restrict_queryset().

Change the method to so the final object
filtering. Instead of expressing the pk list and remove the
duplicated using a set, pass the queryset as a subquery to
the object filter. This moves the processing to the database
instead of holding a list of an unknown number of primary
keys in the memory.

Add keyword arguments.

Update tests to use the latest user test case mixin interface.

Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
2019-01-21 03:53:55 -04:00

62 lines
2.0 KiB
Python

from __future__ import unicode_literals
from django.urls import reverse
from rest_framework import status
from mayan.apps.documents.permissions import permission_document_view
from mayan.apps.documents.search import document_search
from mayan.apps.documents.tests import DocumentTestMixin
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):
response = self.get(
viewname='rest_api:searchmodel-list'
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
[search_model['pk'] for search_model in response.data['results']],
[search_model.pk for search_model in SearchModel.all()]
)