mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-16 04:11:58 +00:00
分析入口的测试补真算子:test_analysis_e2e 加 agent 调波动基线(本周明显超出常规波动、历史新低,本期完不完整按现查的表范围判)、趋势、差异检验(逐行明细,华东的单笔金额高于华南)、可比性检查(两份查询各一周)各一次的端到端,台账、交回模型的那一段、复算逐字段相同都核;分析卡以合并查询为输入跑一次(输入写明 sqlite 方言、工件就是合并查询的快照)。test_analysis_card 的下钻样例 DRILL 原来 by 和 period 是同一列、真算子下会报错:shop 加渠道列(各地区两周的合计不变),DRILL 改成读华东按渠道的那次查询(drill_graph),另用真算子跑一遍下钻(闭合、主要来自线上、parent 是上一项的结论工件、复算一致);夹具改用 scratch_source。过时的说明照实改:替身的用途、口径卡可比性挂钩里「还没有算子」的说法
This commit is contained in:
@@ -477,7 +477,7 @@ def _rendered(value: Any, definition: dict[str, Any], display: dict[str, Any]) -
|
||||
# 可比性检查(docs/design/analysis-round/00-总方案.md §3.4 第 3 条)
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
#: 可比性检查没做成(探查失败、算子出错、还没有算子……)时那条日志的 code
|
||||
#: 可比性检查没做成(快照读不出来、探查失败、算子出错、算不了……)时那条日志的 code
|
||||
COMPARABILITY_SKIPPED = "metric_comparability_skipped"
|
||||
#: 那条日志的说法
|
||||
COMPARABILITY_SKIPPED_TEXT = "指标「{name}」用到的两次查询没做成可比性检查:{why}。指标照常计算,两期能不能直接比请自行核对"
|
||||
@@ -508,7 +508,7 @@ async def _comparability(metric_id: str, name: str, inputs: list[dict[str, Any]]
|
||||
哪份是基期:不同的日期、期次里第一处两边写法相同、排得出先后的(真正的日期,或同一种写法的月、周、季、年),早的是基期;
|
||||
排不出的(按运行当天现算的界 date('now', '-7 days')),表达式里先出现的那份算本期(「(本周 - 上周) / 上周」的写法)。
|
||||
|
||||
做不成(快照读不出来、探查失败、算子出错、算不了、还没有算子)不挡口径卡:照实记一条日志(COMPARABILITY_SKIPPED),
|
||||
做不成(快照读不出来、探查失败、算子出错、算不了)不挡口径卡:照实记一条日志(COMPARABILITY_SKIPPED),
|
||||
这个指标不写 comparability。同一对快照只查一次。"""
|
||||
from app.engine.lineage import SNAPSHOT_VIAS
|
||||
|
||||
|
||||
+2
-1
@@ -1,4 +1,5 @@
|
||||
"""测试用的分析算子替身(第 1 波各包并行:W1-a 的 app.analysis.run 落地前,入口的测试不等它)。
|
||||
"""测试用的分析算子替身:入口(agent 分析工具、分析卡、口径卡的可比性挂钩)的测试用它断言入口交给算子的东西、算子交回后
|
||||
入口做的事,不受算子的判定细节牵连。接真算子的端到端见 tests/test_analysis_e2e*.py。
|
||||
|
||||
FakeOps 换掉模块上的 app.analysis.run(入口都按模块取 run),按请求交回 app.analysis.testing.fake_result 的假结论,
|
||||
并记下每次调用拿到的请求、按名字解析到的输入快照、阈值、运行期依赖、上一层结论——测试据此断言入口交进去的东西。
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
"""分析卡(节点类型 analysis,docs/design/analysis-round/00-总方案.md §3.2):画图时的校验、执行、产出的变量、受管规则、
|
||||
登记处(叫法、变量表、助手查看、目录影响面、升级提醒)。运行对比按项对齐见 tests/test_analysis_compare.py。
|
||||
|
||||
算子(app.analysis.run)换成按请求交回假结论的替身(tests/fixtures/analysis_ops.FakeOps)。库是虚构的零售库 shop。
|
||||
算子(app.analysis.run)换成按请求交回假结论的替身(tests/fixtures/analysis_ops.FakeOps);样例里的下钻那一项另用真算子
|
||||
跑一遍(替身不核参数)。库是虚构的零售库 shop:orders(region, week, amount, channel),华东、华南、华北两周。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -14,9 +15,8 @@ from sqlalchemy import select
|
||||
from app.analysis import contract
|
||||
from app.analysis.contract import CallError
|
||||
from app.core import artifact_store
|
||||
from app.data.engine import engines
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import DataSource, Run, RunEvent
|
||||
from app.db.models import Run, RunEvent
|
||||
from app.engine.runner import run_manager
|
||||
from app.engine.schema import GraphSpec, NodeType, validate_graph
|
||||
from tests.fixtures.analysis_ops import FakeOps
|
||||
@@ -24,16 +24,22 @@ from tests.fixtures.analysis_ops import FakeOps
|
||||
TOOL = "db_query__shop"
|
||||
WEEKLY = "SELECT week, SUM(amount) AS gmv FROM orders GROUP BY week ORDER BY week"
|
||||
REGION = "SELECT region, week, SUM(amount) AS gmv FROM orders GROUP BY region, week ORDER BY region, week"
|
||||
ROWS = [(1, "华东", "2026-W41", 820.0), (2, "华东", "2026-W42", 700.0), (3, "华南", "2026-W41", 700.0),
|
||||
(4, "华南", "2026-W42", 666.0), (5, "华北", "2026-W41", 600.0), (6, "华北", "2026-W42", 560.0)]
|
||||
#: 各地区两周的销售额:华东 820 → 700(线上 500 → 420、线下 320 → 280),华南 700 → 666,华北 600 → 560
|
||||
ROWS = [(1, "华东", "2026-W41", 500.0, "线上"), (2, "华东", "2026-W41", 320.0, "线下"), (3, "华东", "2026-W42", 420.0, "线上"),
|
||||
(4, "华东", "2026-W42", 280.0, "线下"), (5, "华南", "2026-W41", 700.0, "线上"), (6, "华南", "2026-W42", 666.0, "线上"),
|
||||
(7, "华北", "2026-W41", 600.0, "线下"), (8, "华北", "2026-W42", 560.0, "线下")]
|
||||
#: 华东按渠道(DRILL 的输入):两个渠道之和 820 → 700,和 WHERE 里华东那一段闭合
|
||||
EAST = ("SELECT channel, week, SUM(amount) AS gmv FROM orders WHERE region = '华东' GROUP BY channel, week "
|
||||
"ORDER BY channel, week")
|
||||
WOW = {"id": "wow", "op": "baseline", "from": "weekly", "params": {"period": "week", "measure": "gmv",
|
||||
"label": "销售额"}}
|
||||
WHERE = {"id": "where", "op": "decompose", "mode": "additive", "from": "by_region",
|
||||
"params": {"by": ["region"], "period": "week", "base": "2026-W41", "target": "2026-W42", "measure": "gmv",
|
||||
"headline": {"base": "weekly.r0.gmv", "target": "weekly.r1.gmv"}, "label": "销售额"},
|
||||
"hypothesis": "下降是不是集中在某个地区"}
|
||||
DRILL = {"id": "east", "op": "decompose", "mode": "additive", "from": "by_region",
|
||||
"params": {"by": ["week"], "period": "week", "base": "2026-W41", "target": "2026-W42", "measure": "gmv",
|
||||
#: 下钻:WHERE 里华东那一段按渠道再拆一层,读华东按渠道的那次查询(drill_graph 里的 east_q)
|
||||
DRILL = {"id": "east", "op": "decompose", "mode": "additive", "from": "east_q",
|
||||
"params": {"by": ["channel"], "period": "week", "base": "2026-W41", "target": "2026-W42", "measure": "gmv",
|
||||
"parent": {"of": "where", "key": ["华东"]}}}
|
||||
|
||||
|
||||
@@ -44,30 +50,20 @@ async def engine_up():
|
||||
await run_manager.shutdown()
|
||||
|
||||
|
||||
async def _shop(tmp_path, options=None):
|
||||
async def _shop(tmp_path, scratch_source, options=None):
|
||||
"""虚构零售库 shop(tests/conftest.py 的 scratch_source:同名的旧源连同数据目录、现查缓存先删掉,测试结束时再删)。"""
|
||||
path = tmp_path / "shop.db"
|
||||
db = sqlite3.connect(path)
|
||||
db.executescript("CREATE TABLE orders (id INTEGER PRIMARY KEY, region TEXT, week TEXT, amount REAL);")
|
||||
db.executemany("INSERT INTO orders VALUES (?, ?, ?, ?)", ROWS)
|
||||
db.executescript("CREATE TABLE orders (id INTEGER PRIMARY KEY, region TEXT, week TEXT, amount REAL, channel TEXT);")
|
||||
db.executemany("INSERT INTO orders VALUES (?, ?, ?, ?, ?)", ROWS)
|
||||
db.commit()
|
||||
db.close()
|
||||
async with SessionLocal() as session:
|
||||
row = (await session.execute(select(DataSource).where(DataSource.name == "shop"))).scalar_one_or_none()
|
||||
if row is None:
|
||||
row = DataSource(name="shop", kind="sqlite", database=str(path), readonly=True, enabled=True)
|
||||
session.add(row)
|
||||
row.database, row.kind, row.enabled, row.readonly, row.options = str(path), "sqlite", True, True, options or {}
|
||||
await session.commit()
|
||||
source_id = row.id
|
||||
await engines.invalidate(source_id)
|
||||
return source_id
|
||||
return await scratch_source("shop", path, options=options)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def shop(tmp_path):
|
||||
source_id = await _shop(tmp_path)
|
||||
yield
|
||||
await engines.invalidate(source_id)
|
||||
async def shop(tmp_path, scratch_source):
|
||||
return await _shop(tmp_path, scratch_source)
|
||||
|
||||
|
||||
def node(nid, ntype, **config):
|
||||
@@ -90,6 +86,12 @@ def card_graph(items=(WOW, WHERE), *, extra_nodes=(), extra_edges=(), **card):
|
||||
return {"nodes": nodes, "edges": edges}
|
||||
|
||||
|
||||
def drill_graph(items=(WOW, WHERE, DRILL), **card):
|
||||
"""带下钻那一项的分析卡:华东按渠道的那次查询(east_q)接在 by_region 后面、分析卡前面。"""
|
||||
return card_graph(items=items, extra_nodes=[node("east_q", "tool", tool=TOOL, args={"sql": EAST})],
|
||||
extra_edges=[edge("by_region", "east_q"), edge("east_q", "card")], **card)
|
||||
|
||||
|
||||
def card_issues(graph, level="error"):
|
||||
return [i for i in validate_graph(GraphSpec.model_validate(graph)).issues
|
||||
if i.node_id == "card" and (level is None or i.level == level)]
|
||||
@@ -139,7 +141,7 @@ def test_node_type_and_labels():
|
||||
|
||||
|
||||
def test_a_well_formed_card_validates():
|
||||
assert card_issues(card_graph(items=(WOW, WHERE, DRILL), overrides={"main_share": 0.6}), level=None) == []
|
||||
assert card_issues(drill_graph(overrides={"main_share": 0.6}), level=None) == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("items, field, code, why", [
|
||||
@@ -174,9 +176,9 @@ def test_a_well_formed_card_validates():
|
||||
"items[1].params.parent", "analysis.params_invalid", "没有「parent」"),
|
||||
])
|
||||
def test_item_problems(items, field, code, why):
|
||||
found = [i for i in card_issues(card_graph(items=items)) if i.field == field]
|
||||
found = [i for i in card_issues(drill_graph(items=items)) if i.field == field]
|
||||
assert found and found[0].code == code and why in found[0].message, [i.message for i in card_issues(
|
||||
card_graph(items=items))]
|
||||
drill_graph(items=items))]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("extra, why", [
|
||||
@@ -235,8 +237,7 @@ def test_two_snapshot_items_need_both_nodes():
|
||||
|
||||
async def test_the_card_runs_each_item_and_records_it(monkeypatch, shop):
|
||||
ops = FakeOps(monkeypatch, verdicts=["outside_band", "driver", "driver"], peek=["wow", "where", "east"])
|
||||
row = await run_graph(card_graph(items=(WOW, WHERE, DRILL), overrides={"main_share": 0.6}),
|
||||
options={contract.SETTINGS_KEY: {"k_warn": 2.5}})
|
||||
row = await run_graph(drill_graph(overrides={"main_share": 0.6}), options={contract.SETTINGS_KEY: {"k_warn": 2.5}})
|
||||
assert row.status == "succeeded", row.error
|
||||
|
||||
# 算子拿到的:请求、按上游节点 id 解析的快照(只认台账里的那几份)、阈值(缺省 ← 发起时的快照 ← 卡上的覆盖)
|
||||
@@ -280,6 +281,25 @@ async def test_the_card_runs_each_item_and_records_it(monkeypatch, shop):
|
||||
assert row.output["判定"] == results["wow"]["phrase"]
|
||||
|
||||
|
||||
async def test_the_drill_item_holds_up_with_the_real_operator(shop):
|
||||
"""上面的 DRILL 换真算子也成立(替身不核参数):华东按渠道拆,两个渠道之和和上一项里华东那一段闭合,主要来自线上;上一层
|
||||
按存下的工件认(parent 的哈希就是 where 那一项的结论工件),按封存的工件复算逐字段相同。"""
|
||||
import app.analysis as analysis_ops
|
||||
|
||||
row = await run_graph(drill_graph(overrides={"main_share": 0.6}))
|
||||
assert row.status == "succeeded", row.error
|
||||
_, output = await output_of(row.id, "card")
|
||||
results = output["results"]
|
||||
assert results["where"]["verdict"] == "driver" and "主要来自华东" in results["where"]["phrase"]
|
||||
east = artifact_store.load(results["east"]["artifact"])
|
||||
assert east["status"] == "ok" and east["verdict"] == {"code": "driver", "subjects": [["线上"]], "direction": "down"}
|
||||
assert east["values"]["base_total"] == "820" and east["values"]["target_total"] == "700"
|
||||
[parent] = [i for i in east["inputs"] if i.get("role") == "parent"]
|
||||
assert parent["artifact"] == results["where"]["artifact"]
|
||||
again = analysis_ops.recompute(east, artifact_store.load, load_result=artifact_store.load)
|
||||
assert contract.same(east, again)
|
||||
|
||||
|
||||
async def test_a_refused_item_does_not_fail_the_card(monkeypatch, shop):
|
||||
FakeOps(monkeypatch, statuses=["refused", "ok"])
|
||||
row = await run_graph(card_graph())
|
||||
@@ -320,15 +340,12 @@ async def test_inputs_that_are_not_query_snapshots_fail_clearly(monkeypatch, sho
|
||||
assert row.status == "failed" and "不是存成快照的查询结果" in row.error
|
||||
|
||||
|
||||
async def test_masked_columns_reach_the_operator(monkeypatch, tmp_path):
|
||||
source_id = await _shop(tmp_path, {"mask_columns": ["region"]})
|
||||
try:
|
||||
ops = FakeOps(monkeypatch)
|
||||
row = await run_graph(card_graph(items=[WHERE]))
|
||||
assert row.status == "succeeded", row.error
|
||||
assert "region" in ops.calls[0]["snapshots"]["by_region"]["masked"]
|
||||
finally:
|
||||
await engines.invalidate(source_id)
|
||||
async def test_masked_columns_reach_the_operator(monkeypatch, tmp_path, scratch_source):
|
||||
await _shop(tmp_path, scratch_source, {"mask_columns": ["region"]})
|
||||
ops = FakeOps(monkeypatch)
|
||||
row = await run_graph(card_graph(items=[WHERE]))
|
||||
assert row.status == "succeeded", row.error
|
||||
assert "region" in ops.calls[0]["snapshots"]["by_region"]["masked"]
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""分析入口接真算子跑一遍(不用替身):agent 分析工具(含下钻)、分析卡(波动基线 + 变化拆解)、口径卡的可比性挂钩、
|
||||
审批恢复时的重放。断言工件、台账、交回模型的那一段、节点产出,并用 analysis.recompute 按封存的工件复算、逐字段相同。
|
||||
"""分析入口接真算子跑一遍(不用替身):agent 的五个分析工具(变化拆解含下钻,波动基线、趋势、差异检验、可比性检查各一次)、
|
||||
分析卡(波动基线 + 变化拆解;输入是合并查询的一次)、口径卡的可比性挂钩、审批恢复时的重放。断言工件、台账、交回模型的那一段、
|
||||
节点产出,并用 analysis.recompute 按封存的工件复算、逐字段相同。
|
||||
|
||||
虚构零售库 shop:orders(day, region, channel, amount),2026-03-02 起 12 个整周,每周每个地区、渠道两笔(周三、周日),
|
||||
最后一周华东掉三成。模型换成脚本(MockChatModel._decide),不调用任何模型。
|
||||
@@ -25,6 +26,7 @@ from app.engine.runner import run_manager
|
||||
|
||||
TOOL = "db_query__shop"
|
||||
DECOMPOSE = contract.TOOL_NAMES["decompose"]
|
||||
BASELINE, TREND, DIFFER, COMPARABLE = (contract.TOOL_NAMES[op] for op in ("baseline", "trend", "differ", "comparable"))
|
||||
WEEK = "date(day, '-6 days', 'weekday 1')"
|
||||
BY_REGION = (f"SELECT region, {WEEK} AS week, SUM(amount) AS gmv FROM orders WHERE day >= '2026-05-11' "
|
||||
"GROUP BY region, week ORDER BY region, week")
|
||||
@@ -140,9 +142,9 @@ async def events(run_id, etype=None, node_id=None):
|
||||
return [e.data for e in (await session.execute(q.order_by(RunEvent.seq))).scalars()]
|
||||
|
||||
|
||||
def agent_graph(approval="never"):
|
||||
def agent_graph(approval="never", tools=(TOOL, DECOMPOSE)):
|
||||
return chain(node("start", "input"),
|
||||
node("bot", "agent", prompt="销售额为什么下降", tools=[TOOL, DECOMPOSE], approval=approval),
|
||||
node("bot", "agent", prompt="销售额为什么下降", tools=list(tools), approval=approval),
|
||||
node("out", "output", fields=[{"name": "answer", "value": "{{ nodes.bot.text }}"}]))
|
||||
|
||||
|
||||
@@ -201,6 +203,77 @@ async def test_an_agent_decomposes_and_drills_down_with_the_real_operator(monkey
|
||||
recomputes(a2)
|
||||
|
||||
|
||||
async def agent_analysis(monkeypatch, tool, calls):
|
||||
"""Agent 依次查库、最后调一次分析工具(真算子):交回 (那份结论, 交回模型的那一段的 JSON)。顺带断言台账记了这一条(带
|
||||
call_id)、交回模型的编号是「【分析 A1】」、判定语和结论一字不差,并按封存的工件复算、逐字段相同。"""
|
||||
script = Script(monkeypatch, calls)
|
||||
run = await run_manager.start(graph=agent_graph(tools=(TOOL, tool)), input_payload={})
|
||||
row = await wait(run.id)
|
||||
assert row.status == "succeeded", row.error
|
||||
ends = await events(row.id, "tool.end", "bot")
|
||||
artifact = ends[-1]["analysis_artifact"]
|
||||
stored = artifact_store.load(artifact)
|
||||
[finished] = await events(row.id, "node.finished", "bot")
|
||||
[entry] = [e for e in finished["evidence"] if e["kind"] == "analysis"]
|
||||
assert entry == contract.ledger_entry(stored, node_id="bot", exec_no=1, artifact=artifact,
|
||||
call_id=f"call_{len(calls) - 1}")
|
||||
assert set(entry["inputs"]) == {e["query_artifact"] for e in ends[:-1]}
|
||||
head, body = script.replies()[-1].split("\n", 1)
|
||||
assert head == "【分析 A1】" and json.loads(body)["phrase"] == stored["phrase"]
|
||||
recomputes(stored)
|
||||
return stored, json.loads(body)
|
||||
|
||||
|
||||
def _live(stored):
|
||||
"""结论里现查的表范围:按这次运行的数据源探 orders.day。"""
|
||||
return [(p["table"], p["column"], p["basis"], p["max"]) for p in stored["probes"]]
|
||||
|
||||
|
||||
async def test_an_agent_puts_the_week_into_its_history_with_the_real_operator(monkeypatch):
|
||||
stored, body = await agent_analysis(monkeypatch, BASELINE, [
|
||||
(TOOL, {"sql": WEEKLY}),
|
||||
(BASELINE, {"query": "Q1", "period": "week", "measure": "gmv", "label": "销售额", "hypothesis": "这周是不是异常"})])
|
||||
assert stored["op"] == "baseline" and stored["status"] == "ok" and stored["hypothesis"] == "这周是不是异常"
|
||||
assert stored["verdict"] == {"code": "far_outside_band", "direction": "down", "subjects": []}
|
||||
assert stored["phrase"].startswith("销售额本期变化 -10.6%,明显超出过去 10 周的常规波动范围")
|
||||
assert "record_low" in {c["code"] for c in stored["caveats"]} and body["verdict"] == "far_outside_band"
|
||||
assert _live(stored) == [("orders", "day", "live", "2026-05-24")] # 本期完不完整按现查的表范围判
|
||||
|
||||
|
||||
async def test_an_agent_asks_for_a_trend_with_the_real_operator(monkeypatch):
|
||||
stored, body = await agent_analysis(monkeypatch, TREND, [
|
||||
(TOOL, {"sql": WEEKLY}), (TREND, {"query": "Q1", "period": "week", "measure": "gmv", "label": "销售额"})])
|
||||
assert stored["op"] == "trend" and stored["status"] == "ok"
|
||||
assert stored["verdict"] == {"code": "no_trend", "direction": None, "subjects": []}
|
||||
assert stored["phrase"] == "过去 12 周销售额没有明显的上升或下降趋势" and stored["values"]["n"] == "12"
|
||||
assert _live(stored) == [("orders", "day", "live", "2026-05-24")] and body["values"] == stored["values"]
|
||||
|
||||
|
||||
async def test_an_agent_tests_a_difference_with_the_real_operator(monkeypatch):
|
||||
rows = "SELECT region, amount FROM orders WHERE region IN ('华东', '华南') ORDER BY region, day"
|
||||
stored, body = await agent_analysis(monkeypatch, DIFFER, [
|
||||
(TOOL, {"sql": rows}),
|
||||
(DIFFER, {"mode": "rows", "query": "Q1", "by": ["region"], "value": "amount", "label": "单笔金额"})])
|
||||
assert stored["op"] == "differ" and stored["mode"] == "rows" and stored["status"] == "ok"
|
||||
assert stored["verdict"] == {"code": "differs", "direction": "up", "subjects": [["华东"], ["华南"]]}
|
||||
assert stored["phrase"] == "华东的单笔金额(196.83)高于华南(152),差异站得住"
|
||||
assert stored["values"]["n_a"] == stored["values"]["n_b"] == "48" and stored["values"]["test"] == "welch"
|
||||
assert body["subjects"] == [["华东"], ["华南"]]
|
||||
|
||||
|
||||
async def test_an_agent_checks_two_weeks_are_comparable_with_the_real_operator(monkeypatch):
|
||||
stored, body = await agent_analysis(monkeypatch, COMPARABLE, [
|
||||
(TOOL, {"sql": _week_sql("2026-05-18", "2026-05-24")}), (TOOL, {"sql": _week_sql("2026-05-11", "2026-05-17")}),
|
||||
(COMPARABLE, {"base_query": "Q2", "target_query": "Q1", "measure": "gmv"})])
|
||||
assert stored["op"] == "comparable" and stored["status"] == "ok" and stored["phrase"] == "两期可比"
|
||||
assert stored["params"]["base_query"] == "Q2" and stored["params"]["target_query"] == "Q1"
|
||||
values = stored["values"]
|
||||
assert (values["base_start"], values["base_end"], values["target_start"], values["target_end"]) == (
|
||||
"2026-05-11", "2026-05-17", "2026-05-18", "2026-05-24")
|
||||
assert {values[k] for k in ("same_query", "window", "complete", "weekdays", "data")} == {"pass"}
|
||||
assert _live(stored) == [("orders", "day", "live", "2026-05-24")] and body["verdict"] == "comparable"
|
||||
|
||||
|
||||
async def test_replaying_after_approvals_does_not_recompute(monkeypatch):
|
||||
counted = Counting(monkeypatch)
|
||||
script = Script(monkeypatch, AGENT_CALLS)
|
||||
@@ -265,6 +338,31 @@ async def test_an_analysis_card_runs_the_real_operators():
|
||||
recomputes(decomposed)
|
||||
|
||||
|
||||
async def test_an_analysis_card_reads_a_merged_query():
|
||||
"""分析卡的输入是合并查询(merge 节点):按合并查询节点的做法取它的快照,输入写明 sqlite 方言(合并查询在 SQLite 里
|
||||
执行),真算子照常拆、复算逐字段相同。"""
|
||||
where = {"id": "where", "op": "decompose", "mode": "additive", "from": "merged",
|
||||
"params": {"by": ["region"], **TWO_WEEKS, "label": "销售额"}}
|
||||
graph = chain(node("start", "input"), node("by_region", "tool", tool=TOOL, args={"sql": BY_REGION}),
|
||||
node("merged", "merge", inputs={"a": "by_region"}, sql="SELECT region, week, gmv FROM a"),
|
||||
node("card", "analysis", items=[where]),
|
||||
node("out", "output", fields=[{"name": "r", "value": "{{ nodes.card.results.where.phrase }}"}]))
|
||||
run = await run_manager.start(graph=graph, input_payload={})
|
||||
row = await wait(run.id)
|
||||
assert row.status == "succeeded", row.error
|
||||
[finished] = await events(row.id, "node.finished", "card")
|
||||
results = artifact_store.load(finished["artifact"])["results"]
|
||||
stored = artifact_store.load(results["where"]["artifact"])
|
||||
assert stored["verdict"]["code"] == "driver" and stored["verdict"]["subjects"] == [["华东"]]
|
||||
assert row.output["r"] == stored["phrase"] and "主要来自华东" in stored["phrase"]
|
||||
[given] = stored["inputs"]
|
||||
[merged] = await events(row.id, "node.finished", "merged")
|
||||
assert (given["ref"], given["role"], given["dialect"]) == ("merged", "data", "sqlite")
|
||||
assert given["artifact"] == merged["evidence"][0]["artifact"]
|
||||
assert [(e["kind"], e["item"]) for e in finished["evidence"]] == [("analysis", "where")]
|
||||
recomputes(stored)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 口径卡的可比性挂钩:真 comparable
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
analysis.comparable_reasons 拆
|
||||
- SQL 结构不同(退款额 / 销售额)、两次是同一条 SQL → 不查、不记日志
|
||||
- 不同的字面量里没有日期、期次(只差地区、只差门店号)→ 是两段不是两期,不查、不记日志;日期和地区都不同的照查
|
||||
- 算子出错、算不了、算子没有实现(NotImplementedError)→ 不挡口径卡,记一条 metric_comparability_skipped
|
||||
- 算子出错(调用错误、意料之外的异常)、算不了 → 不挡口径卡,记一条 metric_comparability_skipped
|
||||
|
||||
这里的算子是替身(tests/fixtures/analysis_ops.FakeOps);真算子跑一遍的见 tests/test_analysis_e2e.py。
|
||||
"""
|
||||
@@ -192,8 +192,8 @@ def test_which_literals_are_periods(this, last, pairs, order):
|
||||
|
||||
|
||||
async def test_an_operator_that_is_not_there_does_not_block_the_card(monkeypatch):
|
||||
"""算子没有实现(合并前的骨架抛 NotImplementedError):口径卡照常算,照实记一条没做成。"""
|
||||
FakeOps(monkeypatch, errors=[NotImplementedError("analysis.run 还没有实现")])
|
||||
"""算子抛出意料之外的异常(这里是 NotImplementedError):口径卡照常算,照实记一条没做成。"""
|
||||
FakeOps(monkeypatch, errors=[NotImplementedError("这种写法还没有实现")])
|
||||
_, card, logs = await run_card(graph([{"id": "wow", "name": "销售额环比", "expression": WOW, "decimals": 4}]))
|
||||
wow = by_id(card)["wow"]
|
||||
assert "comparability" not in wow and wow["value"] == pytest.approx((450 + 300 - 900) / 900, abs=1e-4)
|
||||
|
||||
Reference in New Issue
Block a user