61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
from datetime import date
|
|
|
|
from yaoex_client import day_range, export_orders, fetch_order_details
|
|
|
|
|
|
class FakeResponse:
|
|
def __init__(self, content=b"", json_data=None, content_type="application/vnd.ms-excel"):
|
|
self.content = content
|
|
self._json = json_data
|
|
self.headers = {"content-type": content_type}
|
|
self.text = content.decode("utf-8", errors="replace")
|
|
|
|
def raise_for_status(self):
|
|
return None
|
|
|
|
def json(self):
|
|
return self._json
|
|
|
|
|
|
class FakeSession:
|
|
def __init__(self, response: FakeResponse):
|
|
self.response = response
|
|
self.calls: list[tuple[str, dict]] = []
|
|
|
|
def post(self, url, data=None, headers=None, timeout=None, verify=None):
|
|
self.calls.append((url, data or {}))
|
|
return self.response
|
|
|
|
def close(self):
|
|
return None
|
|
|
|
|
|
def test_day_range_today_is_single_day():
|
|
begin, end = day_range(date(2026, 10, 3), 0)
|
|
assert begin == "2026-10-03 00:00:00"
|
|
assert end == "2026-10-03 23:59:59"
|
|
|
|
|
|
def test_export_orders_posts_json_params():
|
|
ole = b"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" + b"\x00" * 20
|
|
session = FakeSession(FakeResponse(content=ole))
|
|
data = export_orders("tok", begin="2026-10-03 00:00:00", end="2026-10-03 23:59:59", session=session)
|
|
assert data.startswith(b"\xd0\xcf")
|
|
url, payload = session.calls[0]
|
|
assert url.endswith("/api/order/export")
|
|
assert payload["token"] == "tok"
|
|
assert "ZQD" not in payload["jsonParams"]
|
|
assert "2026-10-03 00:00:00" in payload["jsonParams"]
|
|
|
|
|
|
def test_fetch_order_details_uses_order_id():
|
|
session = FakeSession(
|
|
FakeResponse(
|
|
json_data={"data": {"productList": [{"orderDetailId": 1}]}},
|
|
content_type="application/json",
|
|
)
|
|
)
|
|
details = fetch_order_details("tok", ["ZQD1"], session=session)
|
|
assert details["ZQD1"]["productList"][0]["orderDetailId"] == 1
|
|
assert session.calls[0][1]["jsonParams"] == '{"orderId": "ZQD1"}'
|