# -*- coding: utf-8 -*- """四档匹配引擎分层测试:精确 / 次精确(含名称护栏)/ 字集 / 模糊, 加 自过滤、x_enabled 排除、期次距离排序。临时库跑,不碰生产数据。""" import os import sqlite3 import sys _APP = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) _CORE = os.path.join(_APP, "core") if _CORE not in sys.path: sys.path.insert(0, _CORE) import import_job as job # noqa: E402 import match # noqa: E402 import pytest # noqa: E402 BASE = 2026 * 12 + 7 # 24319 @pytest.fixture() def hist(tmp_path): con = sqlite3.connect(str(tmp_path / "h.db")) con.row_factory = sqlite3.Row # match 引擎按列名取值(dict(x)) con.executescript(open(os.path.join(_APP, "store", "schema.sql"), encoding="utf-8").read()) yield con con.close() def mk_batch(con, name, period=BASE, src=None, enabled=1): cur = con.execute("""INSERT INTO batches(kind, Name, FileKind, BulidUnit, x_province, x_city, x_district, x_period_start_m, x_period_end_m, x_enabled, x_src_hash, x_parser_ver, is_mock) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)""", ("project", name, 1, "建unit", "广东省", "珠海市", "香洲区", period, period, enabled, src or name + ".cos4", "test", 0)) return cur.lastrowid def mk_bill(con, bid, num, name, attr="", unit="台", price=100): num9 = job.norm(num)[:9] con.execute("""INSERT INTO bills(batch_id, Number, Name, Attr, Unit, Price, x_name_norm, x_unit_norm, x_hash_exact, x_hash_semi, x_setkey, x_seq) VALUES(?,?,?,?,?,?,?,?,?,?,?,1)""", (bid, num, name, attr, unit, price, job.norm(name), job.norm(unit), job.md5(num9, name, unit), job.md5(num9, unit), match.bill_setkey(name, unit))) def qrow(num, name, attr="", unit="台"): num9 = job.norm(num)[:9] return {"Number": num, "Name": name, "Attr": attr, "Unit": unit, "x_hash_exact": job.md5(num9, name, unit), "x_hash_semi": job.md5(num9, unit)} def run(con, row, **kw): kw.setdefault("limit", 50) return match.match_bill(con, row, (BASE, BASE), [], [], [], **kw) def test_tier_exact(hist): b = mk_batch(hist, "历史B1") mk_bill(hist, b, "030507008001", "高清网络摄像机", "1、400万", "台", 1500) hist.commit() res = run(hist, qrow("030507008001004", "高清网络摄像机", unit="台")) assert res and res[0]["tier"] == "精确" # 编码后 3 位顺序码不参与(决策 27) assert res[0]["name"] == "高清网络摄像机" and res[0]["proj"] == "历史B1" assert res[0]["sim"] == 1.0 def test_tier_semi_with_name_guard(hist): b = mk_batch(hist, "历史B2") mk_bill(hist, b, "030507008001", "高清网络摄像机(防爆型)", unit="台") # 同码同单位、名近似 mk_bill(hist, b, "030507008002", "彩色交换机", unit="台") # 同码同单位、名无关 hist.commit() res = run(hist, qrow("030507008001009", "高清网络摄像机")) tiers = {r["name"]: r["tier"] for r in res} assert tiers.get("高清网络摄像机(防爆型)") == "次精确" # Jaccard ≥ 0.2 → 放行 assert "彩色交换机" not in tiers # 护栏 < 0.2 → 剔除 def test_tier_setkey_anagram(hist): b = mk_batch(hist, "历史B3", period=2025 * 12 + 3) mk_bill(hist, b, "010101001001", "混凝土钢筋", unit="t") hist.commit() res = run(hist, qrow("020202002002", "钢筋混凝土", unit="t")) assert any(r["tier"] == "字集" and r["name"] == "混凝土钢筋" for r in res) def test_tier_fuzzy(hist): b = mk_batch(hist, "历史B4") mk_bill(hist, b, "040404004004", "网络摄像机立杆基础", unit="座") hist.commit() res = run(hist, qrow("050505005005", "网络摄像机立杆", unit="座")) assert res and res[0]["tier"] == "模糊" and res[0]["sim"] >= 0.6 def test_disabled_batch_excluded(hist): b1 = mk_batch(hist, "禁用批次", enabled=0) mk_bill(hist, b1, "030507008001", "高清网络摄像机") hist.commit() assert run(hist, qrow("030507008001009", "高清网络摄像机")) == [] def test_self_filter_by_src_and_name(hist): b = mk_batch(hist, "被审工程自己", src="self.cos4") mk_bill(hist, b, "030507008001", "高清网络摄像机") hist.commit() row = qrow("030507008001009", "高清网络摄像机") assert run(hist, row, self_src="self.cos4") == [] # 同一文件 assert run(hist, row, self_name="被审工程自己") == [] # 同名项目 assert len(run(hist, row)) == 1 # 不带自过滤 → 命中 def test_period_distance_sort_and_window(hist): b_new = mk_batch(hist, "同期批次", period=BASE) mk_bill(hist, b_new, "030507008001", "高清网络摄像机", price=1500) b_old = mk_batch(hist, "远期批次", period=2024 * 12 + 1) mk_bill(hist, b_old, "030507008001", "高清网络摄像机", price=900) hist.commit() res = run(hist, qrow("030507008001009", "高清网络摄像机")) assert res[0]["proj"] == "同期批次" and res[0]["dist"] == 0 assert res[1]["proj"] == "远期批次" and res[1]["dist"] == 30 # 期次窗口:止期收窄到 2024 → 同期批次被滤掉 res2 = run(hist, qrow("030507008001009", "高清网络摄像机"), pfrom=2024 * 12 + 1, pto=2024 * 12 + 6) assert [r["proj"] for r in res2] == ["远期批次"]