89 lines
3.0 KiB
Python
89 lines
3.0 KiB
Python
# tests/integration/api/v1/test_pagination.py
|
|
import pytest
|
|
|
|
from models.database.product import Product
|
|
from models.database.shop import Shop
|
|
|
|
|
|
@pytest.mark.integration
|
|
@pytest.mark.api
|
|
@pytest.mark.database
|
|
@pytest.mark.products
|
|
@pytest.mark.shops
|
|
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/product?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/product?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/product?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/product?skip=-1", headers=auth_headers)
|
|
assert response.status_code == 422 # Validation error
|
|
|
|
# Test zero limit
|
|
response = client.get("/api/v1/product?limit=0", headers=auth_headers)
|
|
assert response.status_code == 422 # Validation error
|
|
|
|
# Test excessive limit
|
|
response = client.get("/api/v1/product?limit=10000", headers=auth_headers)
|
|
assert response.status_code == 422 # Should be limited
|
|
|
|
def test_shop_pagination(self, client, admin_headers, db, test_user):
|
|
"""Test pagination for shop listing"""
|
|
# Create multiple shops for pagination testing
|
|
shops = []
|
|
for i in range(15):
|
|
shop = Shop(
|
|
shop_code=f"PAGESHOP{i:03d}",
|
|
shop_name=f"Pagination Shop {i}",
|
|
owner_id=test_user.id,
|
|
is_active=True,
|
|
)
|
|
shops.append(shop)
|
|
|
|
db.add_all(shops)
|
|
db.commit()
|
|
|
|
# Test first page
|
|
response = client.get("/api/v1/admin/shops?limit=5&skip=0", headers=admin_headers)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert len(data["shops"]) == 5
|
|
assert data["total"] >= 15 # At least our test shops
|
|
assert data["skip"] == 0
|
|
assert data["limit"] == 5
|