153 lines
5.5 KiB
Python
153 lines
5.5 KiB
Python
|
|
"""Tests for the equipment API endpoints."""
|
||
|
|
|
||
|
|
from decimal import Decimal
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
from httpx import AsyncClient
|
||
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
|
|
||
|
|
from app.models.equipment import EquipmentCache
|
||
|
|
|
||
|
|
|
||
|
|
async def seed_equipment(db: AsyncSession) -> list[EquipmentCache]:
|
||
|
|
"""Insert test equipment rows and return them."""
|
||
|
|
items = [
|
||
|
|
EquipmentCache(
|
||
|
|
rentman_id="100", name="L-Acoustics K2", number="K2-001",
|
||
|
|
category="Lautsprecher", description="Line Array Element",
|
||
|
|
rental_price=Decimal("250.00"), brand="L-Acoustics", available=True,
|
||
|
|
),
|
||
|
|
EquipmentCache(
|
||
|
|
rentman_id="101", name="L-Acoustics KS28", number="KS28-001",
|
||
|
|
category="Subwoofer", description="Subwoofer",
|
||
|
|
rental_price=Decimal("180.00"), brand="L-Acoustics", available=True,
|
||
|
|
),
|
||
|
|
EquipmentCache(
|
||
|
|
rentman_id="102", name="d&b T10", number="T10-001",
|
||
|
|
category="Lautsprecher", description="Point Source",
|
||
|
|
rental_price=Decimal("120.00"), brand="d&b audiotechnik", available=True,
|
||
|
|
),
|
||
|
|
EquipmentCache(
|
||
|
|
rentman_id="103", name="Robe Pointe", number="PT-001",
|
||
|
|
category="Licht", description="Moving Head",
|
||
|
|
rental_price=Decimal("95.00"), brand="Robe", available=True,
|
||
|
|
),
|
||
|
|
]
|
||
|
|
db.add_all(items)
|
||
|
|
await db.commit()
|
||
|
|
for item in items:
|
||
|
|
await db.refresh(item)
|
||
|
|
return items
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_list_equipment_empty(client: AsyncClient):
|
||
|
|
"""GET /api/equipment on empty DB returns empty paginated response."""
|
||
|
|
response = await client.get("/api/equipment")
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
assert data["items"] == []
|
||
|
|
assert data["total"] == 0
|
||
|
|
assert data["page"] == 1
|
||
|
|
assert data["page_size"] == 20
|
||
|
|
assert data["total_pages"] == 0
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_list_equipment_with_data(client: AsyncClient, db_session: AsyncSession):
|
||
|
|
"""GET /api/equipment returns paginated items after seeding."""
|
||
|
|
await seed_equipment(db_session)
|
||
|
|
response = await client.get("/api/equipment")
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
assert data["total"] == 4
|
||
|
|
assert len(data["items"]) == 4
|
||
|
|
assert data["page"] == 1
|
||
|
|
assert data["page_size"] == 20
|
||
|
|
assert data["total_pages"] == 1
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_list_equipment_search(client: AsyncClient, db_session: AsyncSession):
|
||
|
|
"""GET /api/equipment?search=K2 returns only matching items."""
|
||
|
|
await seed_equipment(db_session)
|
||
|
|
response = await client.get("/api/equipment?search=K2")
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
assert data["total"] == 1
|
||
|
|
assert data["items"][0]["name"] == "L-Acoustics K2"
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_list_equipment_category_filter(client: AsyncClient, db_session: AsyncSession):
|
||
|
|
"""GET /api/equipment?category=Lautsprecher returns only matching items."""
|
||
|
|
await seed_equipment(db_session)
|
||
|
|
response = await client.get("/api/equipment?category=Lautsprecher")
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
assert data["total"] == 2
|
||
|
|
for item in data["items"]:
|
||
|
|
assert item["category"] == "Lautsprecher"
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_list_equipment_sort_name_desc(client: AsyncClient, db_session: AsyncSession):
|
||
|
|
"""GET /api/equipment?sort=name_desc returns items sorted descending."""
|
||
|
|
await seed_equipment(db_session)
|
||
|
|
response = await client.get("/api/equipment?sort=name_desc")
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
names = [item["name"] for item in data["items"]]
|
||
|
|
assert names == sorted(names, reverse=True)
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_list_equipment_pagination(client: AsyncClient, db_session: AsyncSession):
|
||
|
|
"""GET /api/equipment with page_size=2 returns correct pagination."""
|
||
|
|
await seed_equipment(db_session)
|
||
|
|
response = await client.get("/api/equipment?page=1&page_size=2")
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
assert len(data["items"]) == 2
|
||
|
|
assert data["total"] == 4
|
||
|
|
assert data["total_pages"] == 2
|
||
|
|
|
||
|
|
response2 = await client.get("/api/equipment?page=2&page_size=2")
|
||
|
|
data2 = response2.json()
|
||
|
|
assert len(data2["items"]) == 2
|
||
|
|
assert data2["page"] == 2
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_get_equipment_detail(client: AsyncClient, db_session: AsyncSession):
|
||
|
|
"""GET /api/equipment/{id} returns the full detail."""
|
||
|
|
items = await seed_equipment(db_session)
|
||
|
|
eq_id = items[0].id
|
||
|
|
response = await client.get(f"/api/equipment/{eq_id}")
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
assert data["id"] == eq_id
|
||
|
|
assert data["name"] == "L-Acoustics K2"
|
||
|
|
assert data["brand"] == "L-Acoustics"
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_get_equipment_not_found(client: AsyncClient):
|
||
|
|
"""GET /api/equipment/999999 returns 404."""
|
||
|
|
response = await client.get("/api/equipment/999999")
|
||
|
|
assert response.status_code == 404
|
||
|
|
assert "not found" in response.json()["detail"].lower()
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_get_categories(client: AsyncClient, db_session: AsyncSession):
|
||
|
|
"""GET /api/equipment/categories returns distinct categories."""
|
||
|
|
await seed_equipment(db_session)
|
||
|
|
response = await client.get("/api/equipment/categories")
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
assert isinstance(data, list)
|
||
|
|
assert "Lautsprecher" in data
|
||
|
|
assert "Subwoofer" in data
|
||
|
|
assert "Licht" in data
|