regress_goods.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. """商品接口参数组合回归:PHP 线上 vs Java 本地,逐字段对比"""
  3. import json
  4. import re
  5. import sys
  6. from pathlib import Path
  7. import requests
  8. UPLOAD = Path(r"F:\import goods\upload_goods.py")
  9. token = re.search(r'TOKEN\s*=\s*"([^"]+)"', UPLOAD.read_text(encoding="utf-8")).group(1)
  10. PHP = "https://shop.qinys.cn/index.php?s="
  11. JAVA = "http://localhost:18080"
  12. LIST_CASES = [
  13. {"page": 1, "listRows": 15},
  14. {"page": 1, "goodsName": "榴莲"},
  15. {"page": 1, "status": 10},
  16. {"page": 1, "listType": "on_sale"},
  17. {"page": 1, "listType": "off_sale"},
  18. {"page": 1, "listType": "sold_out"},
  19. {"page": 1, "sortType": "sales"},
  20. {"page": 2},
  21. {"page": 1, "categoryId": 10001},
  22. {"page": 1, "goodsNo": "DL-TEST"},
  23. {"page": 1, "sortType": "price"},
  24. {"page": 1, "sortType": "price", "sortPrice": "true"},
  25. ]
  26. DETAIL_CASES = [{"goodsId": 10010}, {"goodsId": 10003}, {"goodsId": 999999}]
  27. def php(path, data):
  28. return requests.post(PHP + path, headers={"Access-Token": token}, data=data, timeout=30).json()
  29. def java(path, data):
  30. return requests.post(JAVA + path, data=data, timeout=30).json()
  31. def diff(a, b, path="", out=None):
  32. out = [] if out is None else out
  33. if type(a) != type(b):
  34. out.append(f"{path}: 类型 PHP={type(a).__name__}({str(a)[:60]}) JAVA={type(b).__name__}({str(b)[:60]})")
  35. return out
  36. if isinstance(a, dict):
  37. for k in sorted(set(a) | set(b)):
  38. if k not in a:
  39. out.append(f"{path}.{k}: Java 多出")
  40. elif k not in b:
  41. out.append(f"{path}.{k}: Java 缺失")
  42. else:
  43. diff(a[k], b[k], f"{path}.{k}", out)
  44. elif isinstance(a, list):
  45. if len(a) != len(b):
  46. out.append(f"{path}: 长度 PHP={len(a)} JAVA={len(b)}")
  47. for i in range(min(len(a), len(b))):
  48. diff(a[i], b[i], f"{path}[{i}]", out)
  49. elif a != b:
  50. out.append(f"{path}: PHP={str(a)[:60]!r} JAVA={str(b)[:60]!r}")
  51. return out
  52. total_bad = 0
  53. for case in LIST_CASES:
  54. p = php("/store/goods/list", case)
  55. j = java("/store/goods/list", case)
  56. d = diff(p, j)
  57. total_bad += len(d)
  58. print(("PASS " if not d else "DIFF ") + "list " + json.dumps(case, ensure_ascii=False),
  59. "" if not d else f"-> {len(d)} 处,例如 {d[0][:110]}")
  60. for case in DETAIL_CASES:
  61. p = php("/store/goods/detail", case)
  62. j = java("/store/goods/detail", case)
  63. d = diff(p, j)
  64. total_bad += len(d)
  65. print(("PASS " if not d else "DIFF ") + "detail " + json.dumps(case, ensure_ascii=False),
  66. "" if not d else f"-> {len(d)} 处,例如 {d[0][:110]}")
  67. print("\n总差异:", total_bad)
  68. sys.exit(0 if total_bad == 0 else 1)