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

67 lines
3.2 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 -*-
"""决策 265 扩展(2026-11-04 主人定「数据只来源原数据,耗弹框不许重复」):
① `_lme_res` 同码多行只返回一条——此前采纳补子级跨批次把 12 个批次的同码组成全灌进会话,
弹框展开显示 60 行(机上人工×12 等),引擎数量也翻倍。
② `dedupe_lme_res` 去重键去掉 qty/seq——旧键让「同码不同 seq」的重复套躲过自愈。"""
import os
import sqlite3
import sys
_HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(os.path.dirname(_HERE), "core"))
sys.path.insert(0, os.path.join(os.path.dirname(_HERE), "server"))
import api_server as srv # noqa: E402
import session_job as sj # noqa: E402
SID = 1
def _mk_sc():
sc = sqlite3.connect(":memory:")
sc.row_factory = sqlite3.Row
c = sc.cursor()
c.execute("""CREATE TABLE s_lmes(id INTEGER PRIMARY KEY, session_id INT, Number TEXT,
Name TEXT, Specification TEXT, Unit TEXT, Kind INT, Quantity REAL,
NoTaxPrice REAL, TaxPrice REAL)""")
c.execute("""CREATE TABLE s_lme_proj(id INTEGER PRIMARY KEY, session_id INT, code TEXT,
name TEXT, spec TEXT, unit TEXT, kind INT, qty REAL, ntp REAL, taxrate REAL,
taxp REAL)""")
c.execute("""CREATE TABLE s_lme_res(id INTEGER PRIMARY KEY, session_id INT, p_code TEXT,
res_code TEXT, qty REAL, seq INTEGER, name TEXT)""")
c.execute("""CREATE TABLE s_lme_lib(id INTEGER PRIMARY KEY, session_id INT, code TEXT,
name TEXT, spec TEXT, unit TEXT, kind INT, ntp REAL, taxp REAL)""")
sc.commit()
return sc
def test_lme_res_same_code_returns_one():
"""同码 3 行(seq 0/1/2,正是跨批次灌入的形态)→ _lme_res 只返回 1 条。"""
sc = _mk_sc()
sc.execute("INSERT INTO s_lmes(session_id, Number, Name, Specification, Unit, Kind,"
" NoTaxPrice, TaxPrice) VALUES(1,'C1','机上人工','','工日',1,230.0,230.0)")
sc.executemany("INSERT INTO s_lme_res(session_id, p_code, res_code, qty, seq, name)"
" VALUES(1,'P1','C1',?,?,?)",
[(2.0, 0, '机上人工'), (2.0, 1, '机上人工'), (2.0, 2, '机上人工')])
sc.execute("INSERT INTO s_lme_res(session_id, p_code, res_code, qty, seq, name)"
" VALUES(1,'P1','C2',1.5,3,'柴油')")
sc.commit()
r = srv._lme_res(sc, srv.TBL["session"], SID, "P1")
assert [x[0] for x in r["rows"]] == ["C1", "C2"]
assert r["rows"][0][5] == 2.0
def test_dedupe_lme_res_key_drops_qty_seq():
"""同码不同 seq 的重复套(旧键删不掉)→ 自愈后只留 1 行;不同码不受影响。"""
sc = _mk_sc()
sc.executemany("INSERT INTO s_lme_res(session_id, p_code, res_code, qty, seq, name)"
" VALUES(1,'P1',?,?,?,?)",
[("C1", 2.0, 0, "机上人工"), ("C1", 2.0, 1, "机上人工"),
("C1", 2.0, 2, "机上人工"), ("C2", 1.5, 3, "柴油")])
sc.commit()
sj.dedupe_lme_res(sc)
n = sc.execute("SELECT COUNT(*) FROM s_lme_res WHERE session_id=1 AND p_code='P1'").fetchone()[0]
assert n == 2
assert sc.execute("SELECT COUNT(*) FROM s_lme_res WHERE res_code='C1'").fetchone()[0] == 1