135 lines
4.0 KiB
Python
135 lines
4.0 KiB
Python
"""智能定价接口测试"""
|
|
|
|
import pytest
|
|
from httpx import AsyncClient
|
|
from decimal import Decimal
|
|
from datetime import datetime
|
|
|
|
from app.models import Project, ProjectCostSummary, PricingPlan
|
|
from tests.conftest import assert_response_success
|
|
|
|
|
|
class TestPricingAPI:
|
|
"""智能定价 API 测试"""
|
|
|
|
@pytest.mark.api
|
|
@pytest.mark.asyncio
|
|
async def test_get_pricing_plans_list(
|
|
self,
|
|
client: AsyncClient,
|
|
sample_pricing_plan: PricingPlan
|
|
):
|
|
"""测试获取定价方案列表"""
|
|
response = await client.get("/api/v1/pricing-plans")
|
|
|
|
data = assert_response_success(response)
|
|
assert "items" in data
|
|
assert data["total"] >= 1
|
|
|
|
@pytest.mark.api
|
|
@pytest.mark.asyncio
|
|
async def test_get_pricing_plan_detail(
|
|
self,
|
|
client: AsyncClient,
|
|
sample_pricing_plan: PricingPlan
|
|
):
|
|
"""测试获取定价方案详情"""
|
|
response = await client.get(
|
|
f"/api/v1/pricing-plans/{sample_pricing_plan.id}"
|
|
)
|
|
|
|
data = assert_response_success(response)
|
|
assert data["id"] == sample_pricing_plan.id
|
|
assert data["plan_name"] == sample_pricing_plan.plan_name
|
|
|
|
@pytest.mark.api
|
|
@pytest.mark.asyncio
|
|
async def test_create_pricing_plan(
|
|
self,
|
|
client: AsyncClient,
|
|
db_session,
|
|
sample_project: Project
|
|
):
|
|
"""测试创建定价方案"""
|
|
# 先添加成本汇总
|
|
cost_summary = ProjectCostSummary(
|
|
project_id=sample_project.id,
|
|
material_cost=Decimal("100"),
|
|
equipment_cost=Decimal("50"),
|
|
labor_cost=Decimal("50"),
|
|
fixed_cost_allocation=Decimal("0"),
|
|
total_cost=Decimal("200"),
|
|
calculated_at=datetime.now()
|
|
)
|
|
db_session.add(cost_summary)
|
|
await db_session.commit()
|
|
|
|
response = await client.post(
|
|
"/api/v1/pricing-plans",
|
|
json={
|
|
"project_id": sample_project.id,
|
|
"plan_name": "测试定价方案",
|
|
"strategy_type": "profit",
|
|
"target_margin": 50
|
|
}
|
|
)
|
|
|
|
data = assert_response_success(response)
|
|
assert data["project_id"] == sample_project.id
|
|
assert data["plan_name"] == "测试定价方案"
|
|
assert data["strategy_type"] == "profit"
|
|
|
|
@pytest.mark.api
|
|
@pytest.mark.asyncio
|
|
async def test_update_pricing_plan(
|
|
self,
|
|
client: AsyncClient,
|
|
sample_pricing_plan: PricingPlan
|
|
):
|
|
"""测试更新定价方案"""
|
|
response = await client.put(
|
|
f"/api/v1/pricing-plans/{sample_pricing_plan.id}",
|
|
json={
|
|
"final_price": 599.00,
|
|
"plan_name": "更新后方案"
|
|
}
|
|
)
|
|
|
|
data = assert_response_success(response)
|
|
assert float(data["final_price"]) == 599.00
|
|
assert data["plan_name"] == "更新后方案"
|
|
|
|
@pytest.mark.api
|
|
@pytest.mark.asyncio
|
|
async def test_simulate_strategy(
|
|
self,
|
|
client: AsyncClient,
|
|
db_session,
|
|
sample_project: Project
|
|
):
|
|
"""测试策略模拟"""
|
|
# 添加成本汇总
|
|
cost_summary = ProjectCostSummary(
|
|
project_id=sample_project.id,
|
|
total_cost=Decimal("200"),
|
|
material_cost=Decimal("100"),
|
|
equipment_cost=Decimal("50"),
|
|
labor_cost=Decimal("50"),
|
|
fixed_cost_allocation=Decimal("0"),
|
|
calculated_at=datetime.now()
|
|
)
|
|
db_session.add(cost_summary)
|
|
await db_session.commit()
|
|
|
|
response = await client.post(
|
|
f"/api/v1/projects/{sample_project.id}/simulate-strategy",
|
|
json={
|
|
"strategies": ["traffic", "profit", "premium"],
|
|
"target_margin": 50
|
|
}
|
|
)
|
|
|
|
data = assert_response_success(response)
|
|
assert data["project_id"] == sample_project.id
|
|
assert len(data["results"]) == 3
|