57 lines
2.0 KiB
Python
57 lines
2.0 KiB
Python
# tests/test_pagination.py
|
|
import pytest
|
|
from models.database_models import Product
|
|
|
|
|
|
class TestPagination:
|
|
def test_product_pagination(self, client, auth_headers, db):
|
|
"""Test pagination for product listing"""
|
|
# Create multiple products
|
|
products = []
|
|
for i in range(25):
|
|
product = Product(
|
|
product_id=f"PAGE{i:03d}",
|
|
title=f"Pagination Test Product {i}",
|
|
marketplace="PaginationTest"
|
|
)
|
|
products.append(product)
|
|
|
|
db.add_all(products)
|
|
db.commit()
|
|
|
|
# Test first page
|
|
response = client.get("/api/v1/products?limit=10&skip=0", headers=auth_headers)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert len(data["products"]) == 10
|
|
assert data["total"] == 25
|
|
assert data["skip"] == 0
|
|
assert data["limit"] == 10
|
|
|
|
# Test second page
|
|
response = client.get("/api/v1/products?limit=10&skip=10", headers=auth_headers)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert len(data["products"]) == 10
|
|
assert data["skip"] == 10
|
|
|
|
# Test last page
|
|
response = client.get("/api/v1/products?limit=10&skip=20", headers=auth_headers)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert len(data["products"]) == 5 # Only 5 remaining
|
|
|
|
def test_pagination_boundaries(self, client, auth_headers):
|
|
"""Test pagination boundary conditions"""
|
|
# Test negative skip
|
|
response = client.get("/api/v1/products?skip=-1", headers=auth_headers)
|
|
assert response.status_code == 422 # Validation error
|
|
|
|
# Test zero limit
|
|
response = client.get("/api/v1/products?limit=0", headers=auth_headers)
|
|
assert response.status_code == 422 # Validation error
|
|
|
|
# Test excessive limit
|
|
response = client.get("/api/v1/products?limit=10000", headers=auth_headers)
|
|
assert response.status_code == 422 # Should be limited
|