Opened 6 years ago

Closed 4 years ago

Last modified 4 years ago

#29308 closed Cleanup/optimization (fixed)

Clarify how assertQuerysetEqual()'s transform parameter works

Reported by: Alexander Todorov Owned by: Jacob Walls
Component: Documentation Version: 2.0
Severity: Normal Keywords:
Cc: shubham singh Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Tim Graham)

According to:
https://docs.djangoproject.com/en/2.0/topics/testing/tools/#django.test.TransactionTestCase.assertQuerysetEqual

Asserts that a queryset qs returns a particular list of values values.

The comparison of the contents of qs and values is performed using the function transform;

The way I read this is that this assert method receives a queryset and a list parameters and compares their contents for equality. By default it will use repr to do that but the user can change this. The way the docs are written suggests that the transform function is applied to both the queryset and the values list. In other words in my tests I can write:

self.assertQuerysetEqual(some_query_taken_from_request_context, [test_object1, test_object2, etc])

The tutorial doesn't use the above but instead hard codes string representations in the tests:
https://docs.djangoproject.com/en/2.0/intro/tutorial05/#testing-our-new-view

IMO this is less optimal, makes test suites more fragile (dependent on repr()) and less intuitive b/c the expected value isn't a list of the actual objects but a list of their representation which may have been obtained elsewhere.

Note: this may sounds like an RFE but if we trust the docs it's a bug.

Note2: I can contribute a PR for this and update the existing test suite & docs. Just let me know how you feel about it.

Change History (7)

comment:1 by Tim Graham, 6 years ago

Component: Testing frameworkDocumentation
Description: modified (diff)
Summary: Testing: assertQuerysetEqual's transform parameter not applied to the values parameterClarify how assertQuerysetEqual()'s transform parameter works
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

As someone who knows how the transform parameter works, the documentation is unclear to me. Changing behavior would be backwards incompatible.

For the use case you mentioned, it would also be useful to recommend assertSequenceEqual(queryset, [obj1, obj2, ...]) and assertCountEqual() as these are simpler than assertQuerysetEqual().

comment:2 by shubham singh , 4 years ago

Cc: shubham singh added
Owner: changed from nobody to shubham singh
Status: newassigned

comment:3 by Jacob Walls, 4 years ago

Has patch: set
Owner: changed from shubham singh to Jacob Walls

Hi Shubham Singh, hope it's alright that I submitted a tiny patch for this.

PR

comment:4 by Jacob Walls, 4 years ago

Easy pickings: set

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 659a73bc:

Fixed #29308 -- Clarified how assertQuerysetEqual()'s transform works.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In eae78a3:

[3.1.x] Fixed #29308 -- Clarified how assertQuerysetEqual()'s transform works.

Backport of 659a73bc0a2df9be856e23fcfc6cc66d0d1a35fd from master

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 9141841c:

[3.0.x] Fixed #29308 -- Clarified how assertQuerysetEqual()'s transform works.

Backport of 659a73bc0a2df9be856e23fcfc6cc66d0d1a35fd from master

Note: See TracTickets for help on using tickets.
Back to Top