88 lines
3.8 KiB
Python
88 lines
3.8 KiB
Python
from decimal import Decimal
|
|
|
|
import openpyxl
|
|
import pytest
|
|
from copy import copy
|
|
from openpyxl.styles import PatternFill, Font, Alignment
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from app.database.database import Base
|
|
from app.importer.excel_importer import import_excel_data, parse_excel_data, preview_excel_data
|
|
from app.models.project import Project
|
|
from app.models.boq import BoqDocument, System, Section, WorkItem
|
|
|
|
|
|
@pytest.fixture
|
|
def workbook_file(tmp_path):
|
|
path = tmp_path / "boq.xlsx"
|
|
wb = openpyxl.Workbook()
|
|
ws = wb.active
|
|
ws.title = "Ведомость ОВ"
|
|
ws.append(["title"])
|
|
ws.append([None]); ws.append([None]); ws.append([None])
|
|
ws.append(["№ п/п", "Наименование", "Модель/Артикул", "Производитель", "Ед. изм.", "Кол-во", "Цена за ед.", "Сумма"])
|
|
ws.merge_cells("A6:H6")
|
|
ws["A6"] = "Система один"
|
|
ws["A6"].fill = PatternFill("solid", fgColor="FFB4C6E7")
|
|
ws["A6"].font = Font(size=11)
|
|
ws.append(["Раздел один"])
|
|
ws.append([1, "Работа 1", "M1", "Изготовитель", "шт", 2, 10, 20])
|
|
ws.append(["Итого по разделу", None, None, None, None, None, None, 20])
|
|
ws.append([None] * 8)
|
|
ws.merge_cells("A11:H11")
|
|
ws["A11"] = "Система два"
|
|
ws["A11"].fill = PatternFill("solid", fgColor="FFB4C6E7")
|
|
ws["A11"].font = Font(size=11)
|
|
ws.append(["Раздел два"])
|
|
ws.append([2, "Работа 2", None, None, "м", 3, 5, 15])
|
|
ws.append(["ИТОГО ПО ВЕДОМОСТИ", None, None, None, None, None, None, 35])
|
|
wb.save(path)
|
|
return path
|
|
|
|
|
|
def test_parser_and_preview_share_counts_and_totals(workbook_file):
|
|
parsed = parse_excel_data(str(workbook_file), "Ведомость ОВ", 5, 6)
|
|
preview = preview_excel_data(str(workbook_file), "Ведомость ОВ", 5, 6)
|
|
assert (parsed["systems_count"], parsed["sections_count"], parsed["work_items_count"]) == (2, 2, 2)
|
|
assert parsed["total_amount"] == Decimal("35")
|
|
assert preview["difference"] == Decimal("0")
|
|
assert not parsed["errors"]
|
|
|
|
|
|
def test_import_persists_hierarchy_and_is_atomic(workbook_file):
|
|
engine = create_engine("sqlite:///:memory:")
|
|
Base.metadata.create_all(engine)
|
|
session = sessionmaker(bind=engine)()
|
|
project = Project(name="Test")
|
|
session.add(project); session.commit()
|
|
result = import_excel_data(session, str(workbook_file), "Ведомость ОВ", 5, 6, project.id)
|
|
assert (result["systems"], result["sections"], result["work_items"]) == (2, 2, 2)
|
|
items = session.query(WorkItem).all()
|
|
assert all(item.system_id and item.section_id for item in items)
|
|
assert session.query(BoqDocument).count() == 1
|
|
session.close()
|
|
|
|
|
|
def test_bad_numeric_values_report_row_number(workbook_file):
|
|
wb = openpyxl.load_workbook(workbook_file)
|
|
wb["Ведомость ОВ"]["F8"] = "many"
|
|
wb.save(workbook_file)
|
|
parsed = parse_excel_data(str(workbook_file), "Ведомость ОВ", 5, 6)
|
|
assert any("Строка 8" in message and "Кол-во" in message for message in parsed["errors"])
|
|
|
|
|
|
def test_project_workbook_matches_expected_totals():
|
|
path = next((__import__("pathlib").Path(__file__).resolve().parent.parent).glob("*.xlsx"), None)
|
|
if path is None:
|
|
pytest.skip("Project workbook is not present in the workspace")
|
|
wb = openpyxl.load_workbook(path, read_only=True)
|
|
try:
|
|
sheet_name = wb.sheetnames[0]
|
|
finally:
|
|
wb.close()
|
|
preview = preview_excel_data(str(path), sheet_name, 5, 6)
|
|
assert (preview["systems_count"], preview["sections_count"], preview["work_items_count"]) == (11, 61, 463)
|
|
assert preview["total_amount"] == Decimal("18600000.00")
|
|
assert preview["difference"] == Decimal("0.00")
|