Skip to content

Commit

Permalink
Fix: Duration sorting (pytest-dev#691)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondEvil authored Jul 22, 2023
1 parent 2ce0aa6 commit ae54ee6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
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 comments on commit ae54ee6

Please sign in to comment.