52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
from flask import Flask
|
|
|
|
|
|
def test_compare_prices_auto_fetches_momo_when_not_provided(monkeypatch):
|
|
from routes import price_comparison_routes as routes
|
|
|
|
captured = {}
|
|
|
|
def fake_search_momo(keyword, limit=10):
|
|
captured["momo_search"] = (keyword, limit)
|
|
return True, "ok", [
|
|
{
|
|
"name": "理膚寶水 B5 修復霜",
|
|
"price": 890,
|
|
"product_id": "12345678",
|
|
"product_url": "https://www.momoshop.com.tw/goods/GoodsDetail.jsp?i_code=12345678",
|
|
}
|
|
]
|
|
|
|
def fake_compare(brand, pchome_products, momo_products):
|
|
captured["compare"] = (brand, pchome_products, momo_products)
|
|
return {
|
|
"pchome_count": len(pchome_products),
|
|
"momo_count": len(momo_products),
|
|
"matched_count": 0,
|
|
"matches": [],
|
|
"stats": {},
|
|
}
|
|
|
|
monkeypatch.setattr(routes, "search_momo_products", fake_search_momo)
|
|
monkeypatch.setattr(routes, "compare_brand_prices", fake_compare)
|
|
|
|
app = Flask(__name__)
|
|
with app.test_request_context(
|
|
"/api/price_comparison/compare",
|
|
method="POST",
|
|
json={
|
|
"brand": "理膚寶水",
|
|
"pchome_products": [
|
|
{"name": "理膚寶水 B5 修復霜", "price": 900, "product_id": "PCHOME-1"},
|
|
],
|
|
},
|
|
):
|
|
response = routes.compare_prices.__wrapped__()
|
|
|
|
payload = response.get_json()
|
|
assert payload["success"] is True
|
|
assert payload["data"]["momo_count"] == 1
|
|
assert captured["momo_search"] == ("理膚寶水", 100)
|
|
assert captured["compare"][0] == "理膚寶水"
|
|
assert captured["compare"][2][0]["product_id"] == "12345678"
|