compare_setting_batch.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # -*- coding: utf-8 -*-
  2. """设置类接口实时对比(配送模板 / 物流公司 / 支付设置 / 支付模板 / 打印机)。
  3. 用法:python compare_setting_batch.py [用例名...]
  4. """
  5. import json
  6. import os
  7. import re
  8. import sys
  9. from pathlib import Path
  10. import requests
  11. PHP_BASE = os.environ.get("YOSHOP_PHP_BASE", "https://shop.qinys.cn/index.php?s=")
  12. JAVA_BASE = "http://127.0.0.1:18080"
  13. TOKEN = "testsession123"
  14. SESSION = requests.Session()
  15. SESSION.trust_env = False
  16. CASES = [
  17. ("delivery_detail_10002", "/store/setting.delivery/detail&deliveryId=10002",
  18. "/store/setting.delivery/detail?deliveryId=10002"),
  19. ("delivery_detail_10001", "/store/setting.delivery/detail&deliveryId=10001",
  20. "/store/setting.delivery/detail?deliveryId=10001"),
  21. ("delivery_detail_missing", "/store/setting.delivery/detail&deliveryId=99999",
  22. "/store/setting.delivery/detail?deliveryId=99999"),
  23. ("delivery_all", "/store/setting.delivery/all", "/store/setting.delivery/all"),
  24. ("delivery_list", "/store/setting.delivery/list&page=1", "/store/setting.delivery/list?page=1"),
  25. ("express_all", "/store/setting.express/all", "/store/setting.express/all"),
  26. ("express_list", "/store/setting.express/list&page=1", "/store/setting.express/list?page=1"),
  27. ("printer_all", "/store/setting.printer/all", "/store/setting.printer/all"),
  28. ("printer_list", "/store/setting.printer/list&page=1", "/store/setting.printer/list?page=1"),
  29. ("payment_options", "/store/setting.payment/options", "/store/setting.payment/options"),
  30. ("payment_template_list", "/store/setting.payment.template/list", "/store/setting.payment.template/list"),
  31. ("payment_template_all", "/store/setting.payment.template/all", "/store/setting.payment.template/all"),
  32. ("payment_template_detail_missing", "/store/setting.payment.template/detail&templateId=99999",
  33. "/store/setting.payment.template/detail?templateId=99999"),
  34. ]
  35. def php_token() -> str:
  36. if os.environ.get("YOSHOP_TOKEN"):
  37. return os.environ["YOSHOP_TOKEN"]
  38. src = Path(r"F:\import goods\upload_goods.py").read_text(encoding="utf-8")
  39. m = re.search(r'TOKEN\s*=\s*"([^"]+)"', src)
  40. return m.group(1) if m else TOKEN
  41. def diff(a, b, path="$", out=None, limit=25):
  42. out = out if out is not None else []
  43. if len(out) >= limit:
  44. return out
  45. if type(a) != type(b):
  46. if isinstance(a, (int, float)) and isinstance(b, (int, float)) \
  47. and not isinstance(a, bool) and not isinstance(b, bool):
  48. if float(a) != float(b):
  49. out.append(f"{path}: php={a!r} java={b!r}")
  50. return out
  51. out.append(f"{path}: 类型不同 php={type(a).__name__} java={type(b).__name__} (php={a!r} java={b!r})")
  52. return out
  53. if isinstance(a, dict):
  54. for k in a:
  55. if k not in b:
  56. out.append(f"{path}.{k}: Java 缺失 (php={a[k]!r})")
  57. else:
  58. diff(a[k], b[k], f"{path}.{k}", out, limit)
  59. for k in b:
  60. if k not in a:
  61. out.append(f"{path}.{k}: Java 多出 (java={b[k]!r})")
  62. return out
  63. if isinstance(a, list):
  64. if len(a) != len(b):
  65. out.append(f"{path}: 长度不同 php={len(a)} java={len(b)}")
  66. for i in range(min(len(a), len(b))):
  67. diff(a[i], b[i], f"{path}[{i}]", out, limit)
  68. return out
  69. if a != b:
  70. out.append(f"{path}: php={a!r} java={b!r}")
  71. return out
  72. def main():
  73. only = set(sys.argv[1:])
  74. token = php_token()
  75. ok = fail = 0
  76. for name, php_path, java_path in CASES:
  77. if only and name not in only:
  78. continue
  79. try:
  80. php = SESSION.get(PHP_BASE + php_path, headers={"Access-Token": token}, timeout=60).json()
  81. except Exception as e:
  82. print(f"[skip] {name} 拉取 PHP 失败: {e}")
  83. continue
  84. try:
  85. java = SESSION.get(JAVA_BASE + java_path, headers={"Access-Token": TOKEN}, timeout=60).json()
  86. except Exception as e:
  87. print(f"[FAIL] {name} 拉取 Java 失败: {e}")
  88. fail += 1
  89. continue
  90. if php.get("status") != java.get("status"):
  91. print(f"[DIFF] {name} status php={php.get('status')} java={java.get('status')} "
  92. f"msg php={php.get('message')!r} java={java.get('message')!r}")
  93. fail += 1
  94. continue
  95. diffs = diff(php.get("data"), java.get("data"))
  96. if diffs:
  97. fail += 1
  98. print(f"[DIFF] {name} ({len(diffs)}+ 处)")
  99. for d in diffs:
  100. print(" ", d)
  101. else:
  102. ok += 1
  103. print(f"[PASS] {name}")
  104. print(f"\n通过 {ok} / 差异 {fail}")
  105. if __name__ == "__main__":
  106. main()