| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- from datetime import date
- from app.sql_store import OrderRepository, initialize_database, load_orders
- def test_parameterized_order_summary(tmp_path) -> None:
- path = tmp_path / "shop.db"
- total = initialize_database(path, reset=True)
- assert total == 11
- data, evidence = OrderRepository(path).summarize_recent("U1001", days=30)
- assert data["order_count"] == 4
- assert data["total_paid"] == 10396.0
- assert data["below_99_count"] == 0
- assert evidence.source_type == "sql"
- assert evidence.metadata["days"] == 30
- assert date.today().isoformat() not in evidence.content
- def test_invalid_days_are_rejected(tmp_path) -> None:
- path = tmp_path / "shop.db"
- initialize_database(path, reset=True)
- repository = OrderRepository(path)
- try:
- repository.summarize_recent("U1001", days=1000)
- except ValueError as exc:
- assert "1 到 365" in str(exc)
- else:
- raise AssertionError("非法 days 未被拒绝")
- def test_sqlite_rows_are_loaded_from_csv(tmp_path) -> None:
- source = tmp_path / "orders.csv"
- source.write_text(
- "days_ago,order_id,user_id,product_name,category,amount,status\n"
- "1,O9000001,U9999,Demo Phone,phone,999.00,paid\n"
- "2,O9000002,U9999,Demo Case,accessory,49.00,completed\n",
- encoding="utf-8",
- )
- rows = load_orders(source)
- assert len(rows) == 2
- database = tmp_path / "shop.db"
- total = initialize_database(database, source_path=source, reset=True)
- data, _ = OrderRepository(database).summarize_recent("U9999", days=30)
- assert total == 2
- assert data["order_count"] == 2
- assert data["total_paid"] == 1048.0
- assert data["below_99_count"] == 1
|