72 lines
3.0 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.

"""Ячейка Excel -> `Cell` пакета: текст по формату, значение, ссылка, сетка (план 4, задача 3)."""
import datetime
from decimal import Decimal
from gridbuilder import M, at, sheet_from_rows, with_formats
from arm_statements import Cell
from src.parser.statements.cells import excel_cell, grid_rows
def _balance(row: int, col: int, value: Decimal): # noqa: ANN202
rows: list[list[object]] = [[] for _ in range(row - 1)]
rows.append(at({col: value}))
return sheet_from_rows(rows, name="Баланс")
def test_excel_cell_by_number_format() -> None:
sheet = with_formats(_balance(27, 4, Decimal("106311116.46")), {(27, 4): "#,##0,"})
cell = excel_cell(sheet, 27, 4, set())
assert cell == Cell(text="106 311", value=Decimal("106311"), confidence=100.0, ref="Баланс!D27")
def test_unparsed_format_keeps_raw_and_is_recorded() -> None:
formats: set[str] = set()
sheet = with_formats(sheet_from_rows([[Decimal("0.15")]]), {(1, 1): "0.0%"})
cell = excel_cell(sheet, 1, 1, formats)
assert cell is not None
assert (cell.value, cell.text, formats) == (Decimal("0.15"), "0,15", {"0.0%"})
def test_general_and_no_format_give_raw_value() -> None:
cell = excel_cell(sheet_from_rows([[Decimal("7016006635.0")]]), 1, 1, set())
assert cell is not None
assert (cell.value, cell.text) == (Decimal("7016006635.0"), "7016006635")
def test_date_text_bool_and_merged() -> None:
sheet = sheet_from_rows(
[
[datetime.date(2025, 9, 30), "Итого", True],
[M("Шапка", 2), False],
]
)
date_cell = excel_cell(sheet, 1, 1, set())
assert date_cell is not None
assert (date_cell.text, date_cell.value) == ("30.09.2025", datetime.date(2025, 9, 30))
text_cell = excel_cell(sheet, 1, 2, set())
assert text_cell is not None
assert (text_cell.text, text_cell.value) == ("Итого", None)
yes, no = excel_cell(sheet, 1, 3, set()), excel_cell(sheet, 2, 3, set())
assert yes is not None and no is not None
assert [(yes.text, yes.value), (no.text, no.value)] == [("ИСТИНА", True), ("ЛОЖЬ", False)]
origin = excel_cell(sheet, 2, 1, set())
assert origin is not None
assert origin.ref == "Лист_1!A2"
assert excel_cell(sheet, 2, 2, set()) is None # не левая верхняя позиция merged
assert excel_cell(sheet, 5, 5, set()) is None # пустая
def test_grid_rows_dense_width() -> None:
sheet = sheet_from_rows([["a"], [None, "b", None, Decimal(5)], [], ["z"]])
width, count = 6, 3
rows = grid_rows(sheet, 1, count, width, set())
assert len(rows) == count
assert all(len(row) == width for row in rows)
assert rows[0][0] is not None and rows[0][0].text == "a"
assert rows[1][1] is not None and rows[1][1].ref == "Лист_1!B2"
assert rows[1][3] is not None and rows[1][3].value == Decimal(5)
assert rows[2] == (None,) * 6
assert rows[0][1:] == (None,) * 5