181 lines
6.0 KiB
Python
181 lines
6.0 KiB
Python
#!/usr/bin/env python3
|
|
"""快递物流配送履约系统 — 样例测试用例 (sample)
|
|
|
|
╔═══════════════════════════════════════════════════════════════════════════╗
|
|
║ 重要原则: sample 只测「连通性」, 不暴露隐形规则, 也不演示任何 create body ║
|
|
║ - 只测: 种子实体读取 / 网点·客户创建 / 默认值 / 时钟 / 404 ║
|
|
║ - 不测: 逆向退回 / 理赔分档 / COD 链 / 阈值 / 级联 / 调度算法 ║
|
|
║ - 不测建包裹/揽收/运单: 刻意不演示 parcel create 的必填字段(如 address), ║
|
|
║ 避免给考生作答 body 模板; 包裹只做 404 读连通 ║
|
|
║ - 用例数: ≤ 15 条 ║
|
|
║ - 通过全部 sample ≠ 通过最终判分 ║
|
|
╚═══════════════════════════════════════════════════════════════════════════╝
|
|
|
|
运行: 先启动服务监听 PORT, 再执行
|
|
python3 sample_testcases/run_sample_testcases.py
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import requests
|
|
|
|
BASE_URL = "http://localhost:" + os.environ.get("PORT", "8000")
|
|
S = requests.Session()
|
|
S.trust_env = False
|
|
TIMEOUT = 5
|
|
|
|
|
|
def GET(path):
|
|
return S.get(f"{BASE_URL}{path}", timeout=TIMEOUT)
|
|
|
|
def POST(path, data=None):
|
|
return S.post(f"{BASE_URL}{path}", json=data or {}, timeout=TIMEOUT)
|
|
|
|
|
|
def _need(obj, fields):
|
|
miss = [f for f in fields if f not in obj]
|
|
return f"缺少字段 {miss}" if miss else ""
|
|
|
|
|
|
def _to_list(r):
|
|
data = r.json()
|
|
if isinstance(data, list):
|
|
return data
|
|
if isinstance(data, dict):
|
|
for v in data.values():
|
|
if isinstance(v, list):
|
|
return v
|
|
return data
|
|
|
|
|
|
def _reset():
|
|
POST("/system/reset")
|
|
|
|
|
|
# (1) 网点列表 — 初始 seed 5 个
|
|
def test_stations_seeded():
|
|
_reset()
|
|
r = GET("/stations")
|
|
if r.status_code != 200:
|
|
return False, f"网点列表失败: {r.status_code}"
|
|
lst = _to_list(r)
|
|
if not isinstance(lst, list) or len(lst) < 5:
|
|
return False, f"初始网点应≥5, 得到 {len(lst) if isinstance(lst, list) else 'non-list'}"
|
|
return True, ""
|
|
|
|
|
|
# (2) 网点详情字段
|
|
def test_station_detail():
|
|
r = GET("/stations/ST_HUB")
|
|
if r.status_code != 200:
|
|
return False, f"网点详情失败: {r.status_code}"
|
|
return (lambda m: (not m, m))(_need(r.json(), ["id", "capacity", "in_stock", "status"]))
|
|
|
|
|
|
# (3) 客户列表 — 初始 seed 3 个
|
|
def test_customers_seeded():
|
|
r = GET("/customers")
|
|
if r.status_code != 200:
|
|
return False, f"客户列表失败: {r.status_code}"
|
|
lst = _to_list(r)
|
|
if len(lst) < 3:
|
|
return False, f"初始客户应≥3, 得到 {len(lst)}"
|
|
return True, ""
|
|
|
|
|
|
# (4) 快递员列表 — 初始 seed 3 个
|
|
def test_couriers_seeded():
|
|
r = GET("/couriers")
|
|
if r.status_code != 200:
|
|
return False, f"快递员列表失败: {r.status_code}"
|
|
lst = _to_list(r)
|
|
if len(lst) < 3:
|
|
return False, f"初始快递员应≥3, 得到 {len(lst)}"
|
|
d = lst[0]
|
|
return (lambda m: (not m, m))(_need(d, ["id", "station_id", "active_count", "status"]))
|
|
|
|
|
|
# (5) 包裹不存在 → 404
|
|
def test_parcel_404():
|
|
r = GET("/parcels/NO_SUCH_ID")
|
|
if r.status_code != 404:
|
|
return False, f"应 404, 得到 {r.status_code}"
|
|
return True, ""
|
|
|
|
|
|
# (6) 创建网点
|
|
def test_station_create():
|
|
r = POST("/stations", {"id": "ST_X1", "name": "测试网点", "capacity": 30})
|
|
if r.status_code not in (200, 201):
|
|
return False, f"创建网点失败: {r.status_code}"
|
|
return (GET("/stations/ST_X1").status_code == 200, "")
|
|
|
|
|
|
# (7) 创建客户 + 信用等级
|
|
def test_customer_create():
|
|
r = POST("/customers", {"id": "CUS_T1", "credit_level": "银"})
|
|
if r.status_code not in (200, 201):
|
|
return False, f"创建客户失败: {r.status_code}"
|
|
d = GET("/customers/CUS_T1").json()
|
|
if d.get("credit_level") != "银":
|
|
return False, "credit_level 未写入"
|
|
return True, ""
|
|
|
|
|
|
# (8) 逻辑时钟推进
|
|
def test_clock_tick():
|
|
t0 = GET("/system/clock").json().get("now")
|
|
POST("/system/clock/tick", {"steps": 3})
|
|
t1 = GET("/system/clock").json().get("now")
|
|
if t1 != t0 + 3:
|
|
return False, f"时钟推进异常: {t0} → {t1}"
|
|
return True, ""
|
|
|
|
|
|
# (9) claims / cod 列表连通
|
|
def test_claims_cod_list():
|
|
if GET("/claims").status_code != 200:
|
|
return False, "claims 列表失败"
|
|
if GET("/cod").status_code != 200:
|
|
return False, "cod 列表失败"
|
|
return True, ""
|
|
|
|
|
|
def get_sample_cases():
|
|
return [
|
|
("网点初始数据", test_stations_seeded),
|
|
("网点详情字段", test_station_detail),
|
|
("客户初始数据", test_customers_seeded),
|
|
("快递员初始数据", test_couriers_seeded),
|
|
("包裹 404", test_parcel_404),
|
|
("网点创建", test_station_create),
|
|
("客户创建", test_customer_create),
|
|
("逻辑时钟推进", test_clock_tick),
|
|
("理赔/COD 列表", test_claims_cod_list),
|
|
]
|
|
|
|
|
|
def main():
|
|
cases = get_sample_cases()
|
|
passed = failed = 0
|
|
print(f"测试服务: {BASE_URL}\n")
|
|
for name, fn in cases:
|
|
try:
|
|
ok, msg = fn()
|
|
if ok:
|
|
print(f" PASS {name}")
|
|
passed += 1
|
|
else:
|
|
print(f" FAIL {name}: {msg}")
|
|
failed += 1
|
|
except Exception as e:
|
|
print(f" ERROR {name}: {e}")
|
|
failed += 1
|
|
print(f"\n结果: {passed} 通过, {failed} 失败, 共 {passed + failed} 条")
|
|
print("\n注意: 这些只是基础连通性验证, 通过全部样例不代表最终得分。")
|
|
sys.exit(0 if failed == 0 else 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|