Files
thrillwiki_django_no_react/backend/tests/api/test_pagination.py

119 lines
4.5 KiB
Python

"""
Tests for API pagination consistency.
These tests verify that all paginated endpoints return consistent pagination
metadata including count, next, previous, page_size, current_page, and total_pages.
"""
from django.test import TestCase
from rest_framework.test import APIClient
from rest_framework import status
class PaginationMetadataTestCase(TestCase):
"""Tests for standardized pagination metadata."""
def setUp(self):
"""Set up test client."""
self.client = APIClient()
def test_pagination_metadata_fields(self):
"""Test that paginated responses include standard metadata fields."""
response = self.client.get("/api/v1/parks/")
if response.status_code == status.HTTP_200_OK:
data = response.json()
# Check for pagination metadata in either root or nested format
if "count" in data:
# Standard DRF pagination format
self.assertIn("count", data)
self.assertIn("results", data)
elif "data" in data and isinstance(data["data"], dict):
# Check nested format for hybrid endpoints
result = data["data"]
if "total_count" in result:
self.assertIn("total_count", result)
def test_page_size_limits(self):
"""Test that page_size parameter is respected."""
response = self.client.get("/api/v1/parks/", {"page_size": 5})
if response.status_code == status.HTTP_200_OK:
data = response.json()
if "results" in data:
self.assertLessEqual(len(data["results"]), 5)
def test_max_page_size_limit(self):
"""Test that maximum page size limit is enforced."""
# Request more than max (100 items)
response = self.client.get("/api/v1/parks/", {"page_size": 200})
if response.status_code == status.HTTP_200_OK:
data = response.json()
if "results" in data:
# Should be capped at 100
self.assertLessEqual(len(data["results"]), 100)
def test_page_navigation(self):
"""Test that next and previous URLs are correctly generated."""
response = self.client.get("/api/v1/parks/", {"page": 1, "page_size": 10})
if response.status_code == status.HTTP_200_OK:
data = response.json()
if "count" in data and data["count"] > 10:
# Should have a next URL
self.assertIsNotNone(data.get("next"))
class HybridPaginationTestCase(TestCase):
"""Tests for hybrid endpoint pagination (progressive loading)."""
def setUp(self):
"""Set up test client."""
self.client = APIClient()
def test_hybrid_parks_pagination(self):
"""Test hybrid parks endpoint pagination structure."""
response = self.client.get("/api/v1/parks/hybrid/")
if response.status_code == status.HTTP_200_OK:
data = response.json()
if data.get("data"):
result = data["data"]
self.assertIn("total_count", result)
self.assertIn("has_more", result)
self.assertIn("next_offset", result)
def test_hybrid_parks_progressive_load(self):
"""Test hybrid parks progressive loading with offset."""
response = self.client.get("/api/v1/parks/hybrid/", {"offset": 50})
if response.status_code == status.HTTP_200_OK:
data = response.json()
self.assertIn("success", data)
self.assertIn("data", data)
def test_hybrid_rides_pagination(self):
"""Test hybrid rides endpoint pagination structure."""
response = self.client.get("/api/v1/rides/hybrid/")
if response.status_code == status.HTTP_200_OK:
data = response.json()
if data.get("data"):
result = data["data"]
self.assertIn("total_count", result)
self.assertIn("has_more", result)
self.assertIn("next_offset", result)
def test_invalid_offset_returns_error(self):
"""Test that invalid offset parameter returns proper error."""
response = self.client.get("/api/v1/rides/hybrid/", {"offset": "invalid"})
if response.status_code == status.HTTP_400_BAD_REQUEST:
data = response.json()
# Should have error information
self.assertTrue(
"error" in data or "status" in data,
"Error response should contain error information"
)