10BC0 Fix: Duration sorting by BeyondEvil · Pull Request #691 · pytest-dev/pytest-html · GitHub
[go: up one dir, main page]

Skip to content
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
2 changes: 1 addition & 1 deletion src/pytest_html/scripts/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const dom = {
header.querySelector('#results-table-head > tr').appendChild(t.content)
})

header.querySelector(`.sortable[data-column-type="${sortAttr}"]`).classList.add(sortAsc ? 'desc' : 'asc')
header.querySelector(`.sortable[data-column-type="${sortAttr}"]`)?.classList.add(sortAsc ? 'desc' : 'asc')

return header
},
Expand Down
33 changes: 31 additions & 2 deletions src/pytest_html/scripts/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ const genericSort = (list, key, ascending, customOrder) => {
return sorted
}

const durationSort = (list, ascending) => {
const parseDuration = (duration) => {
if (duration.includes(':')) {
// If it's in the format "HH:mm:ss"
const [hours, minutes, seconds] = duration.split(':').map(Number)
return (hours * 3600 + minutes * 60 + seconds) * 1000
} else {
// If it's in the format "nnn ms"
return parseInt(duration)
}
}
const sorted = list.sort((a, b) => parseDuration(a['duration']) - parseDuration(b['duration']))
if (ascending) {
sorted.reverse()
}
return sorted
}

const doInitSort = () => {
const type = storageModule.getSort()
const ascending = storageModule.getSortDirection()
Expand All @@ -32,7 +50,18 @@ const doInitSort = () => {
if (type?.toLowerCase() === 'original') {
manager.setRender(list)
} else {
const sortedList = genericSort(list, type, ascending, initialOrder)
let sortedList
switch (type) {
case 'duration':
sortedList = durationSort(list, ascending)
break
case 'result':
sortedList = genericSort(list, type, ascending, initialOrder)
break
default:
sortedList = genericSort(list, type, ascending)
break
}
manager.setRender(sortedList)
}
}
Expand All @@ -45,7 +74,7 @@ const doSort = (type) => {
storageModule.setSortDirection(ascending)
const list = manager.testSubset

const sortedList = genericSort(list, type, ascending)
const sortedList = type === 'duration' ? durationSort(list, ascending) : genericSort(list, type, ascending)
manager.setRender(sortedList)
}

Expand Down
2 changes: 1 addition & 1 deletion testing/unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('Sort tests', () => {

afterEach(() => [sortMock, sortDirectionMock, managerSpy].forEach((fn) => fn.restore()))
it('has no stored sort', () => {
sortMock = sinon.stub(storageModule, 'getSort').returns(null)
sortMock = sinon.stub(storageModule, 'getSort').returns('result')
sortDirectionMock = sinon.stub(storageModule, 'getSortDirection').returns(null)
managerSpy = sinon.spy(dataModule.manager, 'setRender')

Expand Down
0