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

105 lines
4.8 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 -*-
"""决策 245(2026-11-01 主人定):后台「打开项目」Excel 通道 + 单价类型落库。
口径:①后台弹框与工作台同款,Excel 走同一套模板/校验,唯一区别=写 history.db 批次;
②/ api/admin/import_project 支持整项目模板(path)与目录树行内 Excel(tree);
③批次 FileKind=单价类型;④同名/单单位工程模板按既定口径拒绝。
"""
import base64
import importlib.util
import os
import sqlite3
from api_harness import PERIOD_202607, get, post, server # noqa: F401
_mi_spec = importlib.util.spec_from_file_location(
"manual_import_src245", os.path.join(os.path.dirname(__file__), "..", "core", "manual_import.py"))
mi = importlib.util.module_from_spec(_mi_spec)
_mi_spec.loader.exec_module(mi)
def _save(server, fname, data):
p = str(server["src_dir"] / fname)
with open(p, "wb") as f:
f.write(data)
return p
def _upload(server, path):
data = base64.b64encode(open(path, "rb").read()).decode("ascii")
r = post(server, "/api/admin/upload", {"name": os.path.basename(path), "data": data})
assert r.get("ok") is not False and r.get("path"), r
return r["path"]
def _body(name, up=None, **kw):
b = {"path": up or "", "name": name, "province": "广东省", "city": "珠海市", "district": "香洲区",
"period_from": PERIOD_202607, "period_to": PERIOD_202607,
"bulid_unit": "某某建设单位", "file_kind": 3}
b.update(kw)
return b
def test_admin_excel_project_template(server):
p = _save(server, "整项目模板245.xlsx", mi.template_project_bytes())
up = _upload(server, p)
r = post(server, "/api/admin/import_project", _body("Excel项目245", up))
assert r["ok"] is True, r
bid = r["batch_id"]
con = sqlite3.connect(server["db"])
try:
row = con.execute("SELECT Name, FileKind, BulidUnit, x_province, x_period_start_m "
"FROM batches WHERE id=?", (bid,)).fetchone()
secs = con.execute("SELECT COUNT(*) FROM sections WHERE batch_id=?", (bid,)).fetchone()[0]
uws = con.execute("SELECT COUNT(*) FROM unit_works WHERE batch_id=?", (bid,)).fetchone()[0]
n_item = con.execute("SELECT COUNT(*) FROM bills WHERE batch_id=? AND Kind IS NULL",
(bid,)).fetchone()[0]
n_prelim = con.execute("SELECT COUNT(*) FROM bills WHERE batch_id=? AND Kind='prelim'",
(bid,)).fetchone()[0]
finally:
con.close()
assert row == ("Excel项目245", 3, "某某建设单位", "广东省", PERIOD_202607), row
assert secs == 1 and uws == 2, (secs, uws)
assert n_item > 0 and n_prelim > 0, (n_item, n_prelim)
# 重复同名(不同文件)→ 拒绝(决策 242 后台口径);同文件先被哈希拦
p2 = _save(server, "整项目模板245_2.xlsx", mi.template_project_bytes() + b"0")
up2 = _upload(server, p2)
r2 = post(server, "/api/admin/import_project", _body("Excel项目245", up2))
assert r2["ok"] is False and "名字重复" in r2["msg"], r2
r3 = post(server, "/api/admin/import_project", _body("Excel项目245", up))
assert r3["ok"] is False and "重复导入" in r3["msg"], r3
def test_admin_excel_single_template_rejected(server):
p = _save(server, "单位模板245.xlsx", mi.template_bytes())
up = _upload(server, p)
r = post(server, "/api/admin/import_project", _body("单位模板工程245", up))
assert r["ok"] is False and "单位工程模板" in r["msg"], r
def test_admin_excel_tree_mode(server):
"""目录树 + 行内单位工程 Excel(同一份模板拆成两个单位工程)——与整项目模板同款落库。"""
p = _save(server, "单位树245.xlsx", mi.template_bytes())
up = _upload(server, p)
r = post(server, "/api/admin/import_project", _body(
"Excel树245", None,
tree=[{"name": "单项甲", "units": [{"name": "单位甲", "file": up}]},
{"name": "单项乙", "units": [{"name": "单位乙", "file": up}]}]))
assert r["ok"] is True, r
con = sqlite3.connect(server["db"])
try:
secs = con.execute("SELECT COUNT(*) FROM sections WHERE batch_id=?", (r["batch_id"],)).fetchone()[0]
uws = con.execute("SELECT COUNT(*) FROM unit_works WHERE batch_id=?", (r["batch_id"],)).fetchone()[0]
finally:
con.close()
assert secs == 2 and uws == 2, (secs, uws)
def test_admin_excel_requires_common_fields(server):
p = _save(server, "整项目模板245b.xlsx", mi.template_project_bytes())
up = _upload(server, p)
for kw, msg in (({"file_kind": None}, "单价类型"), ({"bulid_unit": ""}, "建设单位"),
({"district": ""}, "地区")):
r = post(server, "/api/admin/import_project", _body("缺字段245" + msg, up, **kw))
assert r["ok"] is False and msg in r["msg"], (kw, r)