Files
pricing_review/CostReview/app/tests/test_stopwords.py

83 lines
4.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 -*-
"""屏蔽词排除防回归(2026-10-11 主人定,工作台历史对比筛选栏)。
口径:历史行 名称/项目特征(规格)/单位 含**任一**屏蔽词 → 整行不显示。
两条路径:比对(/api/search 结果后过滤,前端发 stop)+ 检索(/api/hist/search,
stop 进 match.hist_search 的 SQL WHERE —— 本文件钉死的就是这层的两条关键性质):
① 命中屏蔽词的行一条不剩,其余行原样保留;
② 排除发生在 LIMIT/OFFSET **之前** —— 分页每页行数不因屏蔽缩水(否则翻页会出现
「第 1 页 2 行、第 2 页 1 行」的假象,检索口径就错了)。
"""
import sqlite3
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "core"))
import match # noqa: E402
def _mk_db(rows):
con = sqlite3.connect(":memory:")
con.row_factory = sqlite3.Row
c = con.cursor()
c.execute("""CREATE TABLE batches (id INTEGER PRIMARY KEY, Name TEXT, kind TEXT,
BulidUnit TEXT, FileKind TEXT, x_province TEXT, x_city TEXT, x_district TEXT,
x_period_start_m INT, x_period_end_m INT, x_src_hash TEXT, x_enabled INT DEFAULT 1,
x_publisher TEXT)""")
c.execute("""CREATE TABLE bills (id INTEGER PRIMARY KEY, batch_id INT,
Name TEXT, Attr TEXT, Unit TEXT, Price REAL)""")
c.execute("INSERT INTO batches (id, Name, kind, x_period_start_m, x_period_end_m) "
"VALUES (1, '历史项目甲', 'project', 20260, 20261)")
for i, nm in enumerate(rows):
c.execute("INSERT INTO bills (batch_id, Name, Attr, Unit, Price) VALUES (1, ?, '', 'm3', ?)",
(nm, 100 + i))
con.commit()
return con
def test_stop_words_parse():
assert match.stop_words("拆除 泵送,恢复") == ["拆除", "泵送", "恢复"]
assert match.stop_words(" ") == []
assert match.stop_words(None) == []
def test_stop_excludes_rows():
con = _mk_db(["C30混凝土浇筑", "C30混凝土浇筑(拆除)", "钢筋制作安装"])
# 无屏蔽词:3 行全在
assert len(match.hist_search(con, [], [], [], None, None, "", limit=50)) == 3
# 屏蔽「拆除」:含词行消失,其余原样
rs = match.hist_search(con, [], [], [], None, None, "", limit=50, stop=["拆除"])
assert [r["name"] for r in rs] == ["C30混凝土浇筑", "钢筋制作安装"]
def test_stop_case_and_attr_hit():
# 大小写折叠(LED 挡掉 led灯带)+ 特征字段也算命中面
con = _mk_db(["灯带", "电缆敷设"])
con.execute("UPDATE bills SET Attr='含 led灯带 3米' WHERE Name='灯带'")
con.commit()
rs = match.hist_search(con, [], [], [], None, None, "", limit=50, stop=["LED"])
assert [r["name"] for r in rs] == ["电缆敷设"]
def test_stop_before_limit_pagination():
# 每页 2 条:4 行里 1 行被屏蔽 → 两页应是 2+1,且第 2 页拿到的是第 3 个未屏蔽行
con = _mk_db(["甲项清单", "乙项清单", "丙项(拆除)清单", "丁项清单"])
p1 = match.hist_search(con, [], [], [], None, None, "", limit=2, page=1, stop=["拆除"])
p2 = match.hist_search(con, [], [], [], None, None, "", limit=2, page=2, stop=["拆除"])
assert [r["name"] for r in p1] == ["甲项清单", "乙项清单"]
assert [r["name"] for r in p2] == ["丁项清单"]
def test_stop_with_keyword_combined():
con = _mk_db(["C30混凝土浇筑", "C30混凝土拆除", "C40混凝土浇筑"])
rs = match.hist_search(con, [], [], [], None, None, "混凝土", limit=50, stop=["拆除"])
assert [r["name"] for r in rs] == ["C30混凝土浇筑", "C40混凝土浇筑"]
def test_stop_word_order_sensitive():
"""屏蔽词按输入词的字符顺序整词命中(2026-10-11 主人定「按每组出现的词及词的顺序屏蔽」):
不拆字、不做排列——只挡「拆除」连写;倒序「除拆」、散字「拆…除」都不挡。"""
con = _mk_db(["拆除垃圾", "除拆垃圾", "拆模除外"])
rs = match.hist_search(con, [], [], [], None, None, "", limit=50, stop=["拆除"])
assert [r["name"] for r in rs] == ["除拆垃圾", "拆模除外"]