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

114 lines
3.4 KiB
Python

"""市场行情接口测试"""
import pytest
from httpx import AsyncClient
from app.models import Project, Competitor, CompetitorPrice
from tests.conftest import assert_response_success
class TestMarketAPI:
"""市场行情 API 测试"""
@pytest.mark.api
@pytest.mark.asyncio
async def test_create_competitor(self, client: AsyncClient):
"""测试创建竞品机构"""
response = await client.post(
"/api/v1/competitors",
json={
"competitor_name": "测试竞品",
"address": "测试地址",
"distance_km": 3.5,
"positioning": "medium",
"is_key_competitor": True
}
)
data = assert_response_success(response)
assert data["competitor_name"] == "测试竞品"
assert float(data["distance_km"]) == 3.5
@pytest.mark.api
@pytest.mark.asyncio
async def test_get_competitors_list(
self,
client: AsyncClient,
sample_competitor: Competitor
):
"""测试获取竞品列表"""
response = await client.get("/api/v1/competitors")
data = assert_response_success(response)
assert "items" in data
assert data["total"] >= 1
@pytest.mark.api
@pytest.mark.asyncio
async def test_add_competitor_price(
self,
client: AsyncClient,
sample_competitor: Competitor,
sample_project: Project
):
"""测试添加竞品价格"""
response = await client.post(
f"/api/v1/competitors/{sample_competitor.id}/prices",
json={
"project_id": sample_project.id,
"project_name": "光子嫩肤",
"original_price": 800.00,
"promo_price": 600.00,
"price_source": "meituan",
"collected_at": "2026-01-20"
}
)
data = assert_response_success(response)
assert float(data["original_price"]) == 800.00
assert float(data["promo_price"]) == 600.00
@pytest.mark.api
@pytest.mark.asyncio
async def test_market_analysis(
self,
client: AsyncClient,
sample_project: Project,
sample_competitor_price: CompetitorPrice
):
"""测试市场分析"""
response = await client.post(
f"/api/v1/projects/{sample_project.id}/market-analysis",
json={
"include_benchmark": False
}
)
data = assert_response_success(response)
assert data["project_id"] == sample_project.id
assert "price_statistics" in data
assert "suggested_range" in data
@pytest.mark.api
@pytest.mark.asyncio
async def test_get_market_analysis(
self,
client: AsyncClient,
sample_project: Project,
sample_competitor_price: CompetitorPrice
):
"""测试获取市场分析结果"""
# 先执行分析
await client.post(
f"/api/v1/projects/{sample_project.id}/market-analysis",
json={"include_benchmark": False}
)
# 获取结果
response = await client.get(
f"/api/v1/projects/{sample_project.id}/market-analysis"
)
data = assert_response_success(response)
assert data["project_id"] == sample_project.id