test_e2e.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/usr/bin/env python3
  2. """End-to-end test for labor arbitration system: upload -> extract -> portrait -> similar"""
  3. import json
  4. import requests
  5. import sys
  6. BASE = "http://localhost:8000"
  7. NLP = "http://localhost:8001"
  8. def test_health():
  9. print("=== 0. Health Check ===")
  10. for name, url in [("Backend", f"{BASE}/health"), ("NLP", f"{NLP}/health")]:
  11. try:
  12. r = requests.get(url, timeout=5)
  13. print(f" {name}: {r.json()}")
  14. except Exception as e:
  15. print(f" {name}: ERROR - {e}")
  16. return False
  17. return True
  18. def test_upload():
  19. print("\n=== 1. Upload Case ===")
  20. text = (
  21. "申请人:张明,男,汉族,1990年5月15日出生。\n"
  22. "被申请人:北京恒达科技有限公司,住所北京市海淀区中关村大街1号。\n\n"
  23. "请求事项:\n"
  24. "1、请求支付拖欠工资8000元。\n"
  25. "2、请求支付违法解除劳动合同赔偿金24000元。\n\n"
  26. "事实与理由:申请人于2020年3月1日入职被申请人处,担任软件工程师,"
  27. "月工资12000元。2023年6月被申请人无故辞退申请人,"
  28. "且拖欠2023年5月、6月工资合计8000元。\n\n"
  29. "依据《劳动合同法》第四十七条、第八十七条规定。"
  30. )
  31. files = {"files": ("test_case.txt", text.encode("utf-8"), "text/plain")}
  32. data = {"case_name": "张明诉北京恒达科技劳动仲裁案"}
  33. try:
  34. r = requests.post(f"{BASE}/api/cases/upload", files=files, data=data, timeout=60)
  35. result = r.json()
  36. print(f" Status: {r.status_code}")
  37. # Extract case_id from various possible locations
  38. case_id = result.get("id") or result.get("case_id")
  39. if not case_id and "case" in result:
  40. case_id = result["case"].get("id")
  41. if not case_id:
  42. case_id = result.get("case_id_from_task")
  43. print(f" Case ID: {case_id}")
  44. if "elements" in result:
  45. el = result["elements"]
  46. print(f" Case Cause: {el.get('case_cause', 'N/A')}")
  47. print(f" Applicant: {el.get('applicant_name', 'N/A')}")
  48. print(f" Respondent: {el.get('respondent_name', 'N/A')}")
  49. return case_id
  50. except Exception as e:
  51. print(f" ERROR: {e}")
  52. import traceback
  53. traceback.print_exc()
  54. return None
  55. def test_elements(case_id):
  56. print(f"\n=== 2. Get Elements (case {case_id}) ===")
  57. try:
  58. r = requests.get(f"{BASE}/api/cases/{case_id}/elements", timeout=30)
  59. result = r.json()
  60. print(f" Status: {r.status_code}")
  61. # Show top-level fields
  62. show_keys = [
  63. "case_cause", "applicant_name", "respondent_name",
  64. "entry_date", "leave_date", "month_salary",
  65. "worker_position", "contract_type", "law_refs",
  66. "primary_cause_type",
  67. ]
  68. for k in show_keys:
  69. v = result.get(k, "N/A")
  70. print(f" {k}: {v}")
  71. # Show claims
  72. claims = result.get("claims", {})
  73. if claims:
  74. print(f" claims: {claims}")
  75. # Check element table
  76. table = result.get("case_elements_table", {})
  77. if table:
  78. print(f" Element table: {table.get('field_count', 0)} fields")
  79. return result
  80. except Exception as e:
  81. print(f" ERROR: {e}")
  82. return None
  83. def test_portrait(case_id):
  84. print(f"\n=== 3. Get Portrait (case {case_id}) ===")
  85. try:
  86. r = requests.get(f"{BASE}/api/cases/{case_id}/portrait", timeout=30)
  87. result = r.json()
  88. print(f" Status: {r.status_code}")
  89. if "legal_score" in result:
  90. print(f" Legal Score: {result['legal_score']}/100")
  91. if "fact_score" in result:
  92. print(f" Fact Score: {result['fact_score']}/100")
  93. if "risk_score" in result:
  94. print(f" Risk Score: {result['risk_score']}/100")
  95. if "risk_level" in result:
  96. print(f" Risk Level: {result['risk_level']}")
  97. if "tags" in result:
  98. print(f" Tags: {result['tags'][:5] if isinstance(result['tags'], list) else result['tags']}")
  99. if "keywords" in result:
  100. kws = result["keywords"]
  101. if isinstance(kws, list):
  102. print(f" Keywords (top 10): {[kw['word'] for kw in kws[:10]]}")
  103. return result
  104. except Exception as e:
  105. print(f" ERROR: {e}")
  106. return None
  107. def test_similar(case_id):
  108. print(f"\n=== 4. Similar Cases (case {case_id}) ===")
  109. try:
  110. r = requests.post(
  111. f"{BASE}/api/cases/{case_id}/similar",
  112. json={"top_k": 3},
  113. timeout=30,
  114. )
  115. result = r.json()
  116. print(f" Status: {r.status_code}")
  117. if isinstance(result, list):
  118. print(f" Found {len(result)} similar cases")
  119. for i, case in enumerate(result[:3]):
  120. sim = case.get("similarity", case.get("score", 0))
  121. name = case.get("case_name", case.get("name", "unknown"))
  122. print(f" [{i+1}] {name} (similarity: {sim})")
  123. elif isinstance(result, dict):
  124. cases = result.get("cases", result.get("results", []))
  125. print(f" Found {len(cases)} similar cases")
  126. return result
  127. except Exception as e:
  128. print(f" ERROR: {e}")
  129. return None
  130. def test_nlp_service():
  131. print("\n=== 5. NLP Service Direct Test ===")
  132. text = (
  133. "申请人:张明,男,汉族,1990年5月15日出生。"
  134. "被申请人:北京恒达科技有限公司,住所北京市海淀区中关村大街1号。"
  135. "请求事项:1、请求支付拖欠工资8000元。"
  136. "2、请求支付违法解除劳动合同赔偿金24000元。"
  137. "事实与理由:申请人于2020年3月1日入职被申请人处,担任软件工程师,"
  138. "月工资12000元。2023年6月被申请人无故辞退申请人。"
  139. "依据《劳动合同法》第四十七条、第八十七条规定。"
  140. )
  141. try:
  142. r = requests.post(f"{NLP}/extract", json={"text": text}, timeout=60)
  143. result = r.json()
  144. print(f" Status: {r.status_code}")
  145. cause = result.get("case_cause", {}).get("type", "N/A")
  146. print(f" Case Cause: {cause}")
  147. parties = result.get("parties", {})
  148. print(f" Applicant: {parties.get('applicant_name', 'N/A')}")
  149. print(f" Respondent: {parties.get('respondent_name', 'N/A')}")
  150. facts = result.get("facts", {})
  151. print(f" Entry Date: {facts.get('entry_date', 'N/A')}")
  152. print(f" Month Salary: {facts.get('month_salary', 'N/A')}")
  153. return result
  154. except Exception as e:
  155. print(f" ERROR: {e}")
  156. return None
  157. def main():
  158. print("=" * 60)
  159. print("LABOR ARBITRATION SYSTEM - E2E TEST")
  160. print("=" * 60)
  161. if not test_health():
  162. print("\n[FAIL] Services not running!")
  163. print("Start them first:")
  164. print(" cd backend && uvicorn app.main:app --port 8000")
  165. print(" cd nlp-service && uvicorn app.main:app --port 8001")
  166. sys.exit(1)
  167. case_id = test_upload()
  168. if case_id:
  169. test_elements(case_id)
  170. test_portrait(case_id)
  171. test_similar(case_id)
  172. test_nlp_service()
  173. print("\n" + "=" * 60)
  174. print("E2E TEST COMPLETE")
  175. print("=" * 60)
  176. print(f"Frontend: http://localhost:5173")
  177. print(f"Backend: http://localhost:8000")
  178. print(f"NLP: http://localhost:8001")
  179. print(f"API Docs: http://localhost:8000/docs")
  180. if __name__ == "__main__":
  181. main()