Files
smart-project-pricing/后端服务/tests/test_api/test_profit.py
2026-01-31 21:33:06 +08:00

134 lines
3.9 KiB
Python

"""利润模拟接口测试"""
import pytest
from httpx import AsyncClient
from app.models import PricingPlan
from tests.conftest import assert_response_success
class TestProfitAPI:
"""利润模拟 API 测试"""
@pytest.mark.api
@pytest.mark.asyncio
async def test_simulate_profit(
self,
client: AsyncClient,
sample_pricing_plan: PricingPlan
):
"""测试利润模拟"""
response = await client.post(
f"/api/v1/pricing-plans/{sample_pricing_plan.id}/simulate-profit",
json={
"price": 580.00,
"estimated_volume": 100,
"period_type": "monthly"
}
)
data = assert_response_success(response)
assert data["pricing_plan_id"] == sample_pricing_plan.id
assert "input" in data
assert "result" in data
assert "breakeven_analysis" in data
assert data["input"]["price"] == 580.00
@pytest.mark.api
@pytest.mark.asyncio
async def test_get_profit_simulations_list(
self,
client: AsyncClient,
sample_pricing_plan: PricingPlan
):
"""测试获取模拟列表"""
# 先创建一个模拟
await client.post(
f"/api/v1/pricing-plans/{sample_pricing_plan.id}/simulate-profit",
json={
"price": 580.00,
"estimated_volume": 100,
"period_type": "monthly"
}
)
response = await client.get("/api/v1/profit-simulations")
data = assert_response_success(response)
assert "items" in data
assert data["total"] >= 1
@pytest.mark.api
@pytest.mark.asyncio
async def test_sensitivity_analysis(
self,
client: AsyncClient,
sample_pricing_plan: PricingPlan
):
"""测试敏感性分析"""
# 先创建模拟
sim_response = await client.post(
f"/api/v1/pricing-plans/{sample_pricing_plan.id}/simulate-profit",
json={
"price": 580.00,
"estimated_volume": 100,
"period_type": "monthly"
}
)
sim_data = assert_response_success(sim_response)
# 执行敏感性分析
response = await client.post(
f"/api/v1/profit-simulations/{sim_data['simulation_id']}/sensitivity",
json={
"price_change_rates": [-20, -10, 0, 10, 20]
}
)
data = assert_response_success(response)
assert len(data["sensitivity_results"]) == 5
assert "insights" in data
@pytest.mark.api
@pytest.mark.asyncio
async def test_breakeven_analysis(
self,
client: AsyncClient,
sample_pricing_plan: PricingPlan
):
"""测试盈亏平衡分析"""
response = await client.get(
f"/api/v1/pricing-plans/{sample_pricing_plan.id}/breakeven"
)
data = assert_response_success(response)
assert data["pricing_plan_id"] == sample_pricing_plan.id
assert "breakeven_volume" in data
assert "current_margin" in data
@pytest.mark.api
@pytest.mark.asyncio
async def test_delete_simulation(
self,
client: AsyncClient,
sample_pricing_plan: PricingPlan
):
"""测试删除模拟"""
# 创建模拟
sim_response = await client.post(
f"/api/v1/pricing-plans/{sample_pricing_plan.id}/simulate-profit",
json={
"price": 580.00,
"estimated_volume": 100,
"period_type": "monthly"
}
)
sim_data = assert_response_success(sim_response)
# 删除
delete_response = await client.delete(
f"/api/v1/profit-simulations/{sim_data['simulation_id']}"
)
assert delete_response.status_code == 200