"""耗材管理接口测试""" import pytest from httpx import AsyncClient from app.models import Material from tests.conftest import assert_response_success class TestMaterialsAPI: """耗材管理 API 测试""" @pytest.mark.api @pytest.mark.asyncio async def test_create_material(self, client: AsyncClient): """测试创建耗材""" response = await client.post( "/api/v1/materials", json={ "material_code": "MAT_TEST001", "material_name": "测试耗材", "unit": "个", "unit_price": 10.50, "supplier": "测试供应商", "material_type": "consumable" } ) data = assert_response_success(response) assert data["material_code"] == "MAT_TEST001" assert data["material_name"] == "测试耗材" assert float(data["unit_price"]) == 10.50 @pytest.mark.api @pytest.mark.asyncio async def test_get_materials_list( self, client: AsyncClient, sample_material: Material ): """测试获取耗材列表""" response = await client.get("/api/v1/materials") data = assert_response_success(response) assert "items" in data assert data["total"] >= 1 @pytest.mark.api @pytest.mark.asyncio async def test_get_materials_with_filter( self, client: AsyncClient, sample_material: Material ): """测试带筛选的耗材列表""" response = await client.get( "/api/v1/materials", params={"material_type": "consumable"} ) data = assert_response_success(response) assert data["total"] >= 1 @pytest.mark.api @pytest.mark.asyncio async def test_update_material( self, client: AsyncClient, sample_material: Material ): """测试更新耗材""" response = await client.put( f"/api/v1/materials/{sample_material.id}", json={ "unit_price": 3.00, "supplier": "新供应商" } ) data = assert_response_success(response) assert float(data["unit_price"]) == 3.00 assert data["supplier"] == "新供应商" @pytest.mark.api @pytest.mark.asyncio async def test_create_duplicate_material_code( self, client: AsyncClient, sample_material: Material ): """测试创建重复编码的耗材""" response = await client.post( "/api/v1/materials", json={ "material_code": sample_material.material_code, "material_name": "重复编码耗材", "unit": "个", "unit_price": 10.00, "material_type": "consumable" } ) # API 返回 HTTP 400 + 错误详情 assert response.status_code == 400 data = response.json() assert data["detail"]["code"] == 10003 # 数据已存在