pytest-html icon indicating copy to clipboard operation
pytest-html copied to clipboard

Numeric column sorting disappeared in v3

Open lukjak opened this issue 5 years ago • 11 comments

Hello,

I've noticed that in v3 the ability to sort columns in report by numeric value has been removed. ("Numeric" class is no more detected and relevant key sorting function is removed). Can I restore this capability from v2? Or is there any reason it's not in v3 anymore?

lukjak avatar Dec 12 '20 16:12 lukjak

Hey @lukjak thank you for filing this issue!. Could you attach a screenshot with what you're describing? It'll be easier to investigate

gnikonorov avatar Dec 12 '20 16:12 gnikonorov

If I understand correctly, you're referring to 'numeric' class. That was removed in https://github.com/pytest-dev/pytest-html/commit/b57ca0bcf25e77bc95f1216eed51c7d235cb270e. The reason it was removed is because it was only used to sort values in the 'Duration' column. That column used to be reported as a double value of seconds+milliseconds but is now reported as a formatted time string ( see this link ).

All sorting works exactly as before for report columns with this change. Is there a bug you're seeing?

gnikonorov avatar Dec 12 '20 17:12 gnikonorov

Hello

I observed it for a custom column (added "officially" via hooks) - so there is no issue in the default setup. It's used for test results ordering - so numbers are the most convenient.

lukjak avatar Dec 12 '20 18:12 lukjak

Duration fails in v3x:

vvvDuration (highest first)
--
8.89
8.72
5.64
35.34
3.95
236.57
2.97
17.91
14.42

and the other way:


^^^Duration (lowest first)
--
14.42
17.91
2.97
236.57
3.95
35.34
5.64
8.72
8.89

fenchu avatar Feb 16 '21 07:02 fenchu

You can patch this in main.js by adding a simple check for number and float and then subtract to find who is largest:

function numOrFloat(x) {
    let regexPattern = /^-?[0-9\.]+$/;
    if (regexPattern.test(x)) {
        return true;
    }
    return false;
}

function sort(items, keyFunc, reversed) {
    const sortArray = items.map(function(item, i) {
        return [keyFunc(item), i];
    });

    sortArray.sort(function(a, b) {
        const keyA = a[0];
        const keyB = b[0];

        if (keyA == keyB) return 0;


        if (reversed) {
            if (numOrFloat(keyA) && numOrFloat(keyB)) {
                return (keyB-keyA)>0 ? 1 : -1;
            }
            return keyA < keyB ? 1 : -1;
        } else {
            if (numOrFloat(keyA) && numOrFloat(keyB)) {
                return (keyA-keyB)>0 ? 1 : -1;
            }
            return keyA > keyB ? 1 : -1;
        }
    });

fenchu avatar Feb 16 '21 09:02 fenchu

As a user, I also would expect that the Duration sort would be by the numeric value -- this makes it harder to seek tests of overly long duration.

image

mnaumann-plenty avatar May 05 '21 21:05 mnaumann-plenty

I thought it was a great idea to have a column with individual test durations, so that I could sort by that column and see which tests were consuming most time and maybe needed to be optimised/tweaked. Until I found that sorting was not numerical...

gscelsi avatar Aug 04 '21 23:08 gscelsi

I would assume (safely, I feel) that the consensus among users is that the sort here should clearly be numeric and not as a string (time-specific or otherwise). The value itself (either in fact or in visual representation) is not a time value. If anything, it would be a timedelta which would sort properly.

mnaumann-plenty avatar Aug 10 '21 16:08 mnaumann-plenty

If PR #464 solves the issue, that would be fantastic. Is anyone looking into its efficacy?

SoCalLongboard avatar Dec 15 '21 23:12 SoCalLongboard

I agree, will make sure to correct this in the next major release.

BeyondEvil avatar Jan 14 '22 19:01 BeyondEvil

Thank you!

mnaumann-plenty avatar Jan 14 '22 19:01 mnaumann-plenty