Files
37duAutomation/libs/erpsdk/erpsdk/_selftest.py
T
puddy 94e73a29c5 feat(sdk): 新增 erpsdk 0.1.5 与 printkit 0.1.0
- erpsdk:金蝶云星空(K3Cloud)WebAPI 通用 SDK——账套/组织/AppSecret 登录、
  会话复用、单据查询与读写(Save/Submit/Audit/删除/下推/分配/工作流审批)、
  分组维护、即时库存、报表取数(分页),内置新旧两套 ERP 站点
- printkit:网络打印机直连(IPP over TLS,无驱动无弹窗)——内置 EPSON
  L6570/L565,PDF/图片格式化、Excel·WPS 表格按列过滤排版分页、
  Validate-Job 干跑校验、设备能力预检与参数降级
- 均按 SDK 形态供给:sdk.json / api.json / pyproject.toml / _selftest.py
2026-11-16 11:27:33 +08:00

90 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""erpsdk 自检:离线检查常驻;提供账号时顺带做一次真实登录 + 单据/报表取数。
用法:
python -m erpsdk._selftest
环境变量(可选,不提供则只做离线检查):
ERP_SYSTEM=new|old 默认 new
ERP_USERNAME / ERP_PASSWORD
ERP_REPORT_SCHEME 报表过滤方案 ID(提供才跑报表取数)
ERP_REPORT_ORG_ID 库存组织内码
"""
import os
import sys
def _offline():
"""离线检查:站点常量、模块可导入、关键方法齐全。"""
from .client import ErpClient, ErpError # noqa: F401
from .sites import SITES
ok = True
for key, site in SITES.items():
for f in ('host', 'acct'):
if not site.get(f):
print(f'[FAIL] 站点 {key} 缺少 {f}')
ok = False
for m in ('login', 'session_alive', 'bill_query', 'bill_query_all',
'query_metadata', 'report_query', 'logout'):
if not hasattr(ErpClient, m):
print(f'[FAIL] ErpClient 缺少方法 {m}')
ok = False
print('[OK] 离线检查通过:站点与接口方法齐全')
return ok
def _online(erp_key, user, pwd):
from .client import ErpClient
erp = ErpClient(erp_key, logger=print)
try:
erp.login(user, pwd)
except Exception as e:
print(f'[FAIL] 登录失败:{e}')
return False
print(f'[OK] 登录成功({erp.host} / 账套 {erp.acct})')
if not erp.session_alive():
print('[FAIL] 会话探活失败')
return False
print('[OK] 会话探活通过')
try:
rows = erp.bill_query('BD_MATERIAL', 'FNumber,FName', limit=2)
print(f'[OK] 单据查询:{len(rows)} 行,首行 {rows[0] if rows else "-"}')
except Exception as e:
# 权限不足属业务限制,不算 SDK 缺陷
if '权限' in str(e):
print(f'[SKIP] 单据查询无权限({e})')
else:
print(f'[FAIL] 单据查询失败:{e}')
return False
scheme = os.getenv('ERP_REPORT_SCHEME', '').strip()
org = os.getenv('ERP_REPORT_ORG_ID', '').strip()
if scheme and org:
try:
rep = erp.report_query('STK_StockSummaryRpt', scheme, '2026-11-01', '2026-11-13',
org_id=org, limit=5000, max_pages=1)
print(f'[OK] 报表取数:{len(rep)} 行')
except Exception as e:
print(f'[FAIL] 报表取数失败:{e}')
return False
else:
print('[SKIP] 未提供 ERP_REPORT_SCHEME / ERP_REPORT_ORG_ID,跳过报表取数')
return True
def main():
ok = _offline()
user = os.getenv('ERP_USERNAME', '').strip()
pwd = os.getenv('ERP_PASSWORD', '')
if not (user and pwd):
print('[SKIP] 未提供 ERP_USERNAME/ERP_PASSWORD,跳过在线检查')
return 0 if ok else 1
erp_key = (os.getenv('ERP_SYSTEM', 'new') or 'new').strip()
ok = ok and _online(erp_key, user, pwd)
print('自检结果:', 'PASS' if ok else 'FAIL')
return 0 if ok else 1
if __name__ == '__main__':
sys.exit(main())