Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Sorting of custom table columns #634

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions src/pytest_html/basereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,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 @@ -282,28 +285,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
16 changes: 15 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 @@ -32,6 +33,7 @@ class Cell(Table):
def __init__(self):
super().__init__()
self._append_counter = 0
self._sortables = dict()

def __setitem__(self, key, value):
warnings.warn(
Expand All @@ -42,6 +44,10 @@ def __setitem__(self, key, value):
)
self.insert(key, value)

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

def append(self, item):
# We need a way of separating inserts from appends in JS,
# hence the "Z" prefix
Expand All @@ -58,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 All @@ -67,6 +74,13 @@ def pop(self, *args):
DeprecationWarning,
)

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


class Header(Cell):
pass
Expand Down