Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#33609 closed Cleanup/optimization (wontfix)

Use assertCountEqual() in assertQuerysetEqual().

Reported by: David Owned by: nobody
Component: Testing framework Version: dev
Severity: Normal Keywords: queryseteuql
Cc: Nick Pope Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

As of now the assertQueryEqual method
ueses counters to compare unordered Querysets, in the standard unittest package there is already such method called
assertCountEqual, which also has better output-formatting capability.

self.assertCountEqual(items, values, msg=msg)

Using data from current test it will output:

Element counts were not equal:
First has 0, Second has 1:  'Extra Person'

Change History (4)

comment:1 by Mariusz Felisiak, 2 years ago

Resolution: wontfix
Status: newclosed
Summary: Use assertCountEqual in assertQuerysetEqualUse assertCountEqual() in assertQuerysetEqual().

This change would be backward incompatible because an output is completely different. Moreover assertCountEqual() doesn't support maxDiff (see #32469). I don't think it's worth changing.

in reply to:  1 comment:2 by David, 2 years ago

Replying to Mariusz Felisiak:

This change would be backward incompatible because an output is completely different. Moreover assertCountEqual() doesn't support maxDiff (see #32469). I don't think it's worth changing.

The output will be different but is not true that it does not support for maxDiff, since it is part of standard library and like other standard methods uses the same truncation logic, See source.

Having a custom implementation of assertCountEqual in assertQuerysetEqual would not be DRY. Also is much easier to understand assertion output with unittests method than with the current implementation.

PS: while testing if this could be used I have also checked that the input provided to AssertQuerysetEqualTests.test_maxdiff does not get truncated with assertCountEqual, because it just points out differences (which is a single line).

Last edited 2 years ago by David (previous) (diff)

comment:3 by David, 2 years ago

To be more concise: this is the output with current implementation

Count[330 chars]': 1, 'Joe Smith 18': 1, 'Joe Smith 19': 1}) != Count[330 chars]': 1, 'Joe Smith 18': 1, 'Joe Smith 19': 1, 'Extra Person': 1})
  Counter({'Joe Smith 0': 1,
           'Joe Smith 1': 1,
           'Joe Smith 2': 1,
           'Joe Smith 3': 1,
           'Joe Smith 4': 1,
           'Joe Smith 5': 1,
           'Joe Smith 6': 1,
           'Joe Smith 7': 1,
           'Joe Smith 8': 1,
           'Joe Smith 9': 1,
           'Joe Smith 10': 1,
           'Joe Smith 11': 1,
           'Joe Smith 12': 1,
           'Joe Smith 13': 1,
           'Joe Smith 14': 1,
           'Joe Smith 15': 1,
           'Joe Smith 16': 1,
           'Joe Smith 17': 1,
           'Joe Smith 18': 1,
-          'Joe Smith 19': 1})
?                           ^^

+          'Joe Smith 19': 1,
?                           ^

+          'Extra Person': 1})..

Versus this, which is the output for the same input with assertCountEqual

Element counts were not equal:
First has 0, Second has 1:  'Extra Person'

And last the proof for maxDiff (I had to put 20 extra elements to be sure it was generated):

# default maxDiff
Element counts were not equal:

Diff is 909 characters long. Set self.maxDiff to None to see it.

# maxDiff = None
Element counts were not equal:
First has 0, Second has 1:  'Extra Person 0'
First has 0, Second has 1:  'Extra Person 1'
First has 0, Second has 1:  'Extra Person 2'
First has 0, Second has 1:  'Extra Person 3'
First has 0, Second has 1:  'Extra Person 4'
First has 0, Second has 1:  'Extra Person 5'
First has 0, Second has 1:  'Extra Person 6'
First has 0, Second has 1:  'Extra Person 7'
First has 0, Second has 1:  'Extra Person 8'
First has 0, Second has 1:  'Extra Person 9'
First has 0, Second has 1:  'Extra Person 10'
First has 0, Second has 1:  'Extra Person 11'
First has 0, Second has 1:  'Extra Person 12'
First has 0, Second has 1:  'Extra Person 13'
First has 0, Second has 1:  'Extra Person 14'
First has 0, Second has 1:  'Extra Person 15'
First has 0, Second has 1:  'Extra Person 16'
First has 0, Second has 1:  'Extra Person 17'
First has 0, Second has 1:  'Extra Person 18'
First has 0, Second has 1:  'Extra Person 19'

comment:4 by Mariusz Felisiak, 2 years ago

Cc: Nick Pope added
Note: See TracTickets for help on using tickets.
Back to Top