test: 修正 selectin 守卫的假阳性(变异测试发现)

原断言对「关系声明到下一注解」的整段(含注释)做 lazy="selectin"
子串匹配;而 Match.league 声明正下方的注释里恰好也写着该字样,
导致移除真实 kwarg 后测试依旧通过(变异测试确认)。

改为只匹配 relationship(...) 括号内内容,并加注释说明该假阳性。
验证: 干净代码通过 → 移除 league 的 lazy 后失败 → 还原后又通过。
全套 238 passed / 1 skipped。
This commit is contained in:
shangfangjian
2026-09-21 17:36:07 +08:00
parent 951faf31df
commit 50a6753ff8
+9 -6
View File
@@ -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):