74 lines
2.6 KiB
Python
74 lines
2.6 KiB
Python
"""parse_export_sections для analytics export."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
from app.services.analytics.export_context import net_rub_display, net_rub_dominant_hint
|
|
from app.services.analytics.pdf_export import (
|
|
_COMPARISON_COL_WIDTHS,
|
|
_DETAIL_COL_PCT,
|
|
_DETAIL_COL_WIDTHS,
|
|
_USABLE_WIDTH,
|
|
_format_rub,
|
|
_format_rub_plain,
|
|
_widths_from_percentages,
|
|
)
|
|
from app.services.analytics.export_sections import (
|
|
export_filename_base,
|
|
parse_export_sections,
|
|
)
|
|
|
|
|
|
class ExportSectionsTests(unittest.TestCase):
|
|
def test_all_by_default(self) -> None:
|
|
self.assertEqual(
|
|
parse_export_sections(None),
|
|
frozenset({"summary", "comparison", "reports"}),
|
|
)
|
|
self.assertEqual(parse_export_sections("all"), parse_export_sections(None))
|
|
|
|
def test_single_section(self) -> None:
|
|
self.assertEqual(parse_export_sections("summary"), frozenset({"summary"}))
|
|
|
|
def test_filename_all(self) -> None:
|
|
name = export_filename_base(
|
|
date_from="2026-06-01",
|
|
date_to="2026-06-07",
|
|
sections=frozenset({"summary", "comparison", "reports"}),
|
|
)
|
|
self.assertEqual(name, "otchety_2026-06-01_2026-06-07")
|
|
|
|
def test_filename_summary(self) -> None:
|
|
name = export_filename_base(
|
|
date_from="2026-06-01",
|
|
date_to="2026-06-07",
|
|
sections=frozenset({"summary"}),
|
|
)
|
|
self.assertEqual(name, "itogi_2026-06-01_2026-06-07")
|
|
|
|
def test_net_rub_display_without_minus(self) -> None:
|
|
self.assertEqual(net_rub_display({"netRub": -23480.99}), 23480.99)
|
|
self.assertEqual(net_rub_display({"netRub": 1500.0}), 1500.0)
|
|
|
|
def test_net_rub_dominant_hint(self) -> None:
|
|
self.assertIn("надоев", net_rub_dominant_hint({"dominantIssue": "underload"}))
|
|
self.assertIn("перерасход", net_rub_dominant_hint({"dominantIssue": "overload"}))
|
|
|
|
def test_pdf_rub_format_without_sign_or_ruble_symbol(self) -> None:
|
|
self.assertEqual(_format_rub(-23480.99), "23 481 руб.")
|
|
self.assertNotIn("₽", _format_rub(1500))
|
|
self.assertEqual(_format_rub_plain(-10154), "10 154")
|
|
|
|
def test_pdf_table_widths_fit_landscape_page(self) -> None:
|
|
comparison_total = sum(_COMPARISON_COL_WIDTHS)
|
|
detail_total = sum(_DETAIL_COL_WIDTHS)
|
|
self.assertLessEqual(comparison_total, _USABLE_WIDTH + 0.1)
|
|
self.assertLessEqual(detail_total, _USABLE_WIDTH + 0.1)
|
|
self.assertEqual(sum(_DETAIL_COL_PCT), 100)
|
|
self.assertAlmostEqual(detail_total, _USABLE_WIDTH, places=1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|