Skip to content

Commit

Permalink
Fix: Sorting of custom table columns
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondEvil committed Apr 7, 2023
1 parent e4ad806 commit ec785f1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
33 changes: 16 additions & 17 deletions src/pytest_html/basereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ def data(self):
def set_data(self, key, value):
self._data[key] = value

def add_test(self, test_data, report, remove_log=False):
def add_test(self, test_data, report, row, remove_log=False):
for sortable, value in row.sortables.items():
test_data[sortable] = value

# regardless of pass or fail we must add teardown logging to "call"
if report.when == "teardown" and not remove_log:
self.update_test_log(report)
Expand Down Expand Up @@ -273,28 +276,24 @@ def pytest_runtest_logreport(self, report):
}

test_id = report.nodeid
table_html = Html()
if report.when == "call":
row_cells = Row()
self._config.hook.pytest_html_results_table_row(
report=report, cells=row_cells
)
if row_cells.html is None:
return
data["resultsTableRow"] = row_cells.html

self._config.hook.pytest_html_results_table_html(
report=report, data=table_html
)
data["tableHtml"] = table_html.html["html"]
else:
if report.when != "call":
test_id += f"::{report.when}"
data["testId"] = test_id

row_cells = Row()
self._config.hook.pytest_html_results_table_row(report=report, cells=row_cells)
if row_cells.html is None:
return
data["resultsTableRow"] = row_cells.html

table_html = Html()
self._config.hook.pytest_html_results_table_html(report=report, data=table_html)
data["tableHtml"] = table_html.html["html"]

data["result"] = _process_outcome(report)
data["extras"] = self._process_extras(report, test_id)

if self._report.add_test(data, report, table_html.replace_log):
if self._report.add_test(data, report, row_cells, table_html.replace_log):
self._generate_report()


Expand Down
19 changes: 18 additions & 1 deletion src/pytest_html/table.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import warnings


Expand Down Expand Up @@ -29,6 +30,14 @@ def append(self, html):


class Cell(Table):
def __init__(self):
super().__init__()
self._sortables = dict()

@property
def sortables(self):
return self._sortables

def __setitem__(self, key, value):
warnings.warn(
"list-type assignment is deprecated and support "
Expand All @@ -38,6 +47,13 @@ def __setitem__(self, key, value):
)
self.insert(key, value)

def _extract_sortable(self, html):
match = re.search(r'<td class="col-(\w+)">(.*?)</', html)
if match:
sortable = match.group(1)
value = match.group(2)
self._sortables[sortable] = value

def insert(self, index, html):
# backwards-compat
if not isinstance(html, str):
Expand All @@ -48,7 +64,8 @@ def insert(self, index, html):
DeprecationWarning,
)
html = str(html)
html = html.replace("col", "data-column-type")
html = html.replace("col=", "data-column-type=")
self._extract_sortable(html)
self._html[index] = html

def pop(self, *args):
Expand Down

0 comments on commit ec785f1

Please sign in to comment.