Skip to content

Commit 777b763

Browse files
committed
Fix: Sorting of custom table columns
1 parent f8758f9 commit 777b763

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/pytest_html/basereport.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ def data(self):
7878
def set_data(self, key, value):
7979
self._data[key] = value
8080

81-
def add_test(self, test_data, report, remove_log=False):
81+
def add_test(self, test_data, report, row, remove_log=False):
82+
for sortable, value in row.sortables.items():
83+
test_data[sortable] = value
84+
8285
# regardless of pass or fail we must add teardown logging to "call"
8386
if report.when == "teardown" and not remove_log:
8487
self.update_test_log(report)
@@ -282,28 +285,24 @@ def pytest_runtest_logreport(self, report):
282285
}
283286

284287
test_id = report.nodeid
285-
table_html = Html()
286-
if report.when == "call":
287-
row_cells = Row()
288-
self._config.hook.pytest_html_results_table_row(
289-
report=report, cells=row_cells
290-
)
291-
if row_cells.html is None:
292-
return
293-
data["resultsTableRow"] = row_cells.html
294-
295-
self._config.hook.pytest_html_results_table_html(
296-
report=report, data=table_html
297-
)
298-
data["tableHtml"] = table_html.html["html"]
299-
else:
288+
if report.when != "call":
300289
test_id += f"::{report.when}"
301290
data["testId"] = test_id
302291

292+
row_cells = Row()
293+
self._config.hook.pytest_html_results_table_row(report=report, cells=row_cells)
294+
if row_cells.html is None:
295+
return
296+
data["resultsTableRow"] = row_cells.html
297+
298+
table_html = Html()
299+
self._config.hook.pytest_html_results_table_html(report=report, data=table_html)
300+
data["tableHtml"] = table_html.html["html"]
301+
303302
data["result"] = _process_outcome(report)
304303
data["extras"] = self._process_extras(report, test_id)
305304

306-
if self._report.add_test(data, report, table_html.replace_log):
305+
if self._report.add_test(data, report, row_cells, table_html.replace_log):
307306
self._generate_report()
308307

309308

src/pytest_html/table.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import warnings
23

34

@@ -32,6 +33,7 @@ class Cell(Table):
3233
def __init__(self):
3334
super().__init__()
3435
self._append_counter = 0
36+
self._sortables = dict()
3537

3638
def __setitem__(self, key, value):
3739
warnings.warn(
@@ -42,6 +44,10 @@ def __setitem__(self, key, value):
4244
)
4345
self.insert(key, value)
4446

47+
@property
48+
def sortables(self):
49+
return self._sortables
50+
4551
def append(self, item):
4652
# We need a way of separating inserts from appends in JS,
4753
# hence the "Z" prefix
@@ -58,7 +64,8 @@ def insert(self, index, html):
5864
DeprecationWarning,
5965
)
6066
html = str(html)
61-
html = html.replace("col", "data-column-type")
67+
html = html.replace("col=", "data-column-type=")
68+
self._extract_sortable(html)
6269
self._html[index] = html
6370

6471
def pop(self, *args):
@@ -67,6 +74,13 @@ def pop(self, *args):
6774
DeprecationWarning,
6875
)
6976

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

7185
class Header(Cell):
7286
pass

0 commit comments

Comments
 (0)