From 50a6753ff8a02644ad9f1080e93e7f1262ec4386 Mon Sep 17 00:00:00 2001 From: shangfangjian Date: Mon, 21 Sep 2026 17:36:07 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E4=BF=AE=E6=AD=A3=20selectin=20?= =?UTF-8?q?=E5=AE=88=E5=8D=AB=E7=9A=84=E5=81=87=E9=98=B3=E6=80=A7(?= =?UTF-8?q?=E5=8F=98=E5=BC=82=E6=B5=8B=E8=AF=95=E5=8F=91=E7=8E=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原断言对「关系声明到下一注解」的整段(含注释)做 lazy="selectin" 子串匹配;而 Match.league 声明正下方的注释里恰好也写着该字样, 导致移除真实 kwarg 后测试依旧通过(变异测试确认)。 改为只匹配 relationship(...) 括号内内容,并加注释说明该假阳性。 验证: 干净代码通过 → 移除 league 的 lazy 后失败 → 还原后又通过。 全套 238 passed / 1 skipped。 --- tests/test_regressions.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/test_regressions.py b/tests/test_regressions.py index e26ee3f..4ff3601 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -63,15 +63,18 @@ class TestEagerLoadCoverage: assert m, "Match 类未找到" body = m.group(0) for rel in MATCH_RELATIONS: - # 关系声明可能跨多行(home_team/away_team 都是),因此按 - # 「从 `rel: Mapped` 到下一个单行注解声明之前」整段匹配。 + # 只取 relationship(...) 调用本身的括号内内容。 + # 注意: 不能把整段(含注释)做子串匹配 —— 关系声明下方的注释里 + # 恰好也写着 lazy="selectin",会导致「删掉真实 kwarg 但测试仍绿」 + # 的假阳性(已用变异测试证实: 移除 league 的 lazy 后断言依旧通过)。 m_rel = re.search( - rf"^[ \t]*{rel}: Mapped.*?(?=^[ \t]*\w+:[^\n]*Mapped|\Z)", + rf"^[ \t]*{rel}: Mapped.*?relationship\((.*?)\)[ \t]*$", body, re.M | re.S, ) - assert m_rel, f"Match.{rel} 未找到" - assert 'lazy="selectin"' in m_rel.group(0), ( - f"Match.{rel} 未声明 lazy='selectin' —— 兜底缺失 (P0-2)" + assert m_rel, f"Match.{rel} 未找到 relationship(...) 声明" + call_args = m_rel.group(1) + assert 'lazy="selectin"' in call_args, ( + f"Match.{rel} 的 relationship() 未声明 lazy='selectin' —— 兜底缺失 (P0-2)" ) def test_stats_relationship_is_lazy_select_by_design(self):