mirror of
https://github.com/netbox-community/netbox.git
synced 2025-09-06 14:23:36 -06:00
Adds basic tests for raising QuerySetNotOrdered
This commit is contained in:
parent
8e39481c6b
commit
7fa81f1498
@ -1,8 +1,13 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
from django.test import RequestFactory, TestCase
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
from rest_framework.request import Request
|
||||||
|
|
||||||
|
from netbox.api.exceptions import QuerySetNotOrdered
|
||||||
|
from netbox.api.pagination import OptionalLimitOffsetPagination
|
||||||
from utilities.testing import APITestCase
|
from utilities.testing import APITestCase
|
||||||
|
from users.models import Token
|
||||||
|
|
||||||
|
|
||||||
class AppTest(APITestCase):
|
class AppTest(APITestCase):
|
||||||
@ -26,3 +31,40 @@ class AppTest(APITestCase):
|
|||||||
response = self.client.get(f'{url}?format=api', **self.header)
|
response = self.client.get(f'{url}?format=api', **self.header)
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
|
class OptionalLimitOffsetPaginationTest(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.paginator = OptionalLimitOffsetPagination()
|
||||||
|
self.factory = RequestFactory()
|
||||||
|
|
||||||
|
def _make_drf_request(self, path='/', query_params=None):
|
||||||
|
"""Helper to create a proper DRF Request object"""
|
||||||
|
return Request(self.factory.get(path, query_params or {}))
|
||||||
|
|
||||||
|
def test_raises_exception_for_unordered_queryset(self):
|
||||||
|
"""Should raise QuerySetNotOrdered for unordered QuerySet"""
|
||||||
|
queryset = Token.objects.all().order_by()
|
||||||
|
request = self._make_drf_request()
|
||||||
|
|
||||||
|
with self.assertRaises(QuerySetNotOrdered) as cm:
|
||||||
|
self.paginator.paginate_queryset(queryset, request)
|
||||||
|
|
||||||
|
error_msg = str(cm.exception)
|
||||||
|
self.assertIn("Paginating over an unordered queryset is unreliable", error_msg)
|
||||||
|
self.assertIn("Ensure that a minimal ordering has been applied", error_msg)
|
||||||
|
|
||||||
|
def test_allows_ordered_queryset(self):
|
||||||
|
"""Should not raise exception for ordered QuerySet"""
|
||||||
|
queryset = Token.objects.all().order_by('created')
|
||||||
|
request = self._make_drf_request()
|
||||||
|
|
||||||
|
self.paginator.paginate_queryset(queryset, request) # Should not raise exception
|
||||||
|
|
||||||
|
def test_allows_non_queryset_iterables(self):
|
||||||
|
"""Should not raise exception for non-QuerySet iterables"""
|
||||||
|
iterable = [1, 2, 3, 4, 5]
|
||||||
|
request = self._make_drf_request()
|
||||||
|
|
||||||
|
self.paginator.paginate_queryset(iterable, request) # Should not raise exception
|
||||||
|
Loading…
Reference in New Issue
Block a user