fix(segments): 余量计算兼容 Asset 领域实体(metadata dict)

staging 真实接口验收发现:路由 repository 返回的是 Asset 领域实体而非
ORM AssetModel,区间记录在 metadata dict(repository 与 classification_result
JSON 互转);_read_meta 只读 classification_result 列导致 AttributeError,
余量计算全部走异常兜底,四字段在真实接口里恒为 None。

_read_meta 双形态兼容:实体 metadata 为 dict 时直接用;否则读
classification_result JSON 字符串(ORM 形态)。
测试:补充实体形态/ORM 形态对比用例(13913 passed)。
This commit is contained in:
CI Bot
2026-08-30 08:02:27 +08:00
parent 8c760d4a2d
commit 99cbff8abb
2 changed files with 59 additions and 11 deletions
+15 -4
View File
@@ -60,12 +60,23 @@ def _now_iso() -> str:
return datetime.now(timezone.utc).isoformat()
def _read_meta(model: AssetModel) -> dict:
"""从 AssetModel 读取 metadata dict(classification_result 列承载的 JSON)."""
if not model.classification_result:
def _read_meta(model) -> dict:
"""读取素材 metadata dict。
兼容两种对象:
- ORM ``AssetModel``:metadata 以 JSON 字符串存在 ``classification_result`` 列;
- 领域实体 ``Asset``(路由层 repository 返回):metadata 直接是 dict 属性
(repository 与 classification_result 互转,见 asset_repository.py)。
"""
# 领域实体:metadata 已是 dict
meta = getattr(model, "metadata", None)
if isinstance(meta, dict):
return meta
raw = getattr(model, "classification_result", None)
if not raw:
return {}
try:
data = json.loads(model.classification_result)
data = json.loads(raw) if isinstance(raw, str) else raw
return data if isinstance(data, dict) else {}
except Exception:
return {}