test(workbook): fix margin-rate recalculation fixture and row selection
Verify / verify (push) Failing after 2m22s
Verify / verify (push) Failing after 2m22s
Clear raw gross values for the current-period lookup rows, edit both matching rows instead of a baseline row, and assert recalculated detail and unchanged baseline totals.
This commit was merged in pull request #11.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from decimal import Decimal
|
||||
from io import BytesIO
|
||||
|
||||
import pytest
|
||||
@@ -93,14 +94,18 @@ def test_margin_rate_lookup_edit_changes_gross_via_detail_recalculation(workspac
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from finance.models import Fact
|
||||
|
||||
if not (os.environ.get("SOFFICE_BIN") or shutil.which("soffice")):
|
||||
pytest.skip("set SOFFICE_BIN to run the LibreOffice recalculation check")
|
||||
|
||||
ds, member, *_ = workspace
|
||||
# 去掉直接毛利,使毛利全部走 lookup 毛利率
|
||||
Fact.objects.filter(dataset=ds).update(gross=None)
|
||||
# 模拟缺失原始直接毛利的导入行,数据库毛利按原毛利率计算。
|
||||
gross_index = next(col["index"] for col in ds.mapping["columns"] if col["role"] == "gross")
|
||||
for fact in ds.facts.filter(
|
||||
period="2026-08", dimensions__department="儿科", dimensions__product="医疗服务"
|
||||
):
|
||||
fact.raw[gross_index] = ""
|
||||
fact.gross = fact.revenue * Decimal("0.3")
|
||||
fact.save(update_fields=["raw", "gross"])
|
||||
mapping = {**ds.mapping, "margin_rates": {"医疗服务": "0.3"}}
|
||||
ds.mapping = mapping
|
||||
ds.save(update_fields=["mapping"])
|
||||
@@ -118,10 +123,21 @@ def test_margin_rate_lookup_edit_changes_gross_via_detail_recalculation(workspac
|
||||
)
|
||||
|
||||
editable = load_workbook(workbook_path)
|
||||
# MarginRate 列在维度列之后:G 列(期间+2维度=3 列后第 4 个保留列)
|
||||
header_row = [cell.value for cell in editable["计算明细"][1]]
|
||||
rate_column = header_row.index("MarginRate") + 1
|
||||
editable["计算明细"].cell(row=2, column=rate_column).value = 0.5
|
||||
details = editable["计算明细"]
|
||||
columns = {cell.value: cell.column for cell in details[1]}
|
||||
lookup_rows = [
|
||||
row
|
||||
for row in range(2, details.max_row + 1)
|
||||
if details.cell(row, columns["Period"]).value == "2026-08"
|
||||
and details.cell(row, columns["department"]).value == "儿科"
|
||||
and details.cell(row, columns["product"]).value == "医疗服务"
|
||||
]
|
||||
assert len(lookup_rows) == 2
|
||||
for row in lookup_rows:
|
||||
assert details.cell(row, columns["GrossInput"]).value is None
|
||||
assert details.cell(row, columns["GrossSource"]).value == "lookup"
|
||||
assert details.cell(row, columns["MarginRate"]).value == pytest.approx(0.3)
|
||||
details.cell(row, columns["MarginRate"]).value = 0.5
|
||||
editable.save(workbook_path)
|
||||
|
||||
import subprocess
|
||||
@@ -145,6 +161,8 @@ def test_margin_rate_lookup_edit_changes_gross_via_detail_recalculation(workspac
|
||||
)
|
||||
assert result.returncode == 0, result.stderr or result.stdout
|
||||
values = load_workbook(output / workbook_path.name, data_only=True)
|
||||
details = values["计算明细"]
|
||||
assert [details.cell(row, columns["GrossCalc"]).value for row in lookup_rows] == pytest.approx([40, 10])
|
||||
summary = values["分析汇总"]
|
||||
gross_row = next(
|
||||
row
|
||||
@@ -153,3 +171,4 @@ def test_margin_rate_lookup_edit_changes_gross_via_detail_recalculation(workspac
|
||||
)
|
||||
# 2026-08 儿科收入 100,lookup 毛利率改为 0.5 后毛利应为 50
|
||||
assert summary.cell(gross_row, 3).value == pytest.approx(50)
|
||||
assert summary.cell(gross_row, 4).value == pytest.approx(90)
|
||||
|
||||
Reference in New Issue
Block a user