Просмотр исходного кода

feat(tests): add HTML parser for table cell extraction in report metrics tests

Manuel 1 неделя назад
Родитель
Сommit
b7c8f4902a
1 измененных файлов с 24 добавлено и 1 удалено
  1. 24 1
      tests/test_reports_metrics.py

+ 24 - 1
tests/test_reports_metrics.py

@@ -1,6 +1,7 @@
 """Focused behavioural coverage for the reports metrics service."""
 
 from datetime import date, datetime
+from html.parser import HTMLParser
 
 import pytest
 from flask import render_template
@@ -10,6 +11,26 @@ from routes import reports_routes
 from services.analytics.get_reports import get_important_metrics
 
 
+class _TableCellParser(HTMLParser):
+    def __init__(self):
+        super().__init__()
+        self.cells: list[str] = []
+        self._current_cell: list[str] | None = None
+
+    def handle_starttag(self, tag, attrs):
+        if tag == "td":
+            self._current_cell = []
+
+    def handle_data(self, data):
+        if self._current_cell is not None:
+            self._current_cell.append(data)
+
+    def handle_endtag(self, tag):
+        if tag == "td" and self._current_cell is not None:
+            self.cells.append("".join(self._current_cell).strip())
+            self._current_cell = None
+
+
 @pytest.fixture()
 def report_models(in_memory_engine):
     """Create isolated daily tables compatible with the reports service."""
@@ -279,4 +300,6 @@ def test_reports_pdf_includes_the_new_summary_and_destination_metrics(
 
     assert "Tasa de éxito HTTP:" in document
     assert "Top dominios de destino" in document
-    assert "example.com" in document
+    table_cells = _TableCellParser()
+    table_cells.feed(document)
+    assert any(cell == "example.com" for cell in table_cells.cells)