[Django] #29308: Testing: assertQuerysetEqual's transform parameter not applied to the values parameter

16 views
Skip to first unread message

Django

unread,
Apr 9, 2018, 4:50:16 PM4/9/18
to django-...@googlegroups.com
#29308: Testing: assertQuerysetEqual's transform parameter not applied to the
values parameter
---------------------------------------------+------------------------
Reporter: Alexander Todorov | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 2.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29308>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 10, 2018, 1:05:45 PM4/10/18
to django-...@googlegroups.com
#29308: Clarify how assertQuerysetEqual()'s transform parameter works
--------------------------------------+------------------------------------

Reporter: Alexander Todorov | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted
* component: Testing framework => Documentation


Old description:

> 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.

New description:

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

--

Comment:

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()`.

--
Ticket URL: <https://code.djangoproject.com/ticket/29308#comment:1>

Django

unread,
Mar 1, 2020, 6:36:09 AM3/1/20
to django-...@googlegroups.com
#29308: Clarify how assertQuerysetEqual()'s transform parameter works
-------------------------------------+-------------------------------------
Reporter: Alexander Todorov | Owner: shubham
Type: | singh
Cleanup/optimization | Status: assigned

Component: Documentation | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by shubham singh ):

* cc: shubham singh (added)
* owner: nobody => shubham singh
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/29308#comment:2>

Django

unread,
Jul 4, 2020, 10:03:33 AM7/4/20
to django-...@googlegroups.com
#29308: Clarify how assertQuerysetEqual()'s transform parameter works
-------------------------------------+-------------------------------------
Reporter: Alexander Todorov | Owner: Jacob
Type: | Walls

Cleanup/optimization | Status: assigned
Component: Documentation | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* owner: shubham singh => Jacob Walls
* has_patch: 0 => 1


Comment:

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

[https://github.com/django/django/pull/13147 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/29308#comment:3>

Django

unread,
Jul 4, 2020, 10:10:51 AM7/4/20
to django-...@googlegroups.com
#29308: Clarify how assertQuerysetEqual()'s transform parameter works
-------------------------------------+-------------------------------------
Reporter: Alexander Todorov | Owner: Jacob
Type: | Walls
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0

-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* easy: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/29308#comment:4>

Django

unread,
Jul 4, 2020, 5:48:33 PM7/4/20
to django-...@googlegroups.com
#29308: Clarify how assertQuerysetEqual()'s transform parameter works
-------------------------------------+-------------------------------------
Reporter: Alexander Todorov | Owner: Jacob
Type: | Walls
Cleanup/optimization | Status: closed
Component: Documentation | Version: 2.0
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"659a73bc0a2df9be856e23fcfc6cc66d0d1a35fd" 659a73bc]:
{{{
#!CommitTicketReference repository=""
revision="659a73bc0a2df9be856e23fcfc6cc66d0d1a35fd"
Fixed #29308 -- Clarified how assertQuerysetEqual()'s transform works.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29308#comment:5>

Django

unread,
Jul 4, 2020, 5:49:14 PM7/4/20
to django-...@googlegroups.com
#29308: Clarify how assertQuerysetEqual()'s transform parameter works
-------------------------------------+-------------------------------------
Reporter: Alexander Todorov | Owner: Jacob
Type: | Walls
Cleanup/optimization | Status: closed
Component: Documentation | Version: 2.0
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"eae78a363b94640cd17ddd2b3133bf1a790bca72" eae78a3]:
{{{
#!CommitTicketReference repository=""
revision="eae78a363b94640cd17ddd2b3133bf1a790bca72"
[3.1.x] Fixed #29308 -- Clarified how assertQuerysetEqual()'s transform
works.

Backport of 659a73bc0a2df9be856e23fcfc6cc66d0d1a35fd from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29308#comment:6>

Django

unread,
Jul 8, 2020, 2:27:02 AM7/8/20
to django-...@googlegroups.com
#29308: Clarify how assertQuerysetEqual()'s transform parameter works
-------------------------------------+-------------------------------------
Reporter: Alexander Todorov | Owner: Jacob
Type: | Walls
Cleanup/optimization | Status: closed
Component: Documentation | Version: 2.0
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"9141841cca505b9f6b605834dc5adb3e5269fe72" 9141841c]:
{{{
#!CommitTicketReference repository=""
revision="9141841cca505b9f6b605834dc5adb3e5269fe72"
[3.0.x] Fixed #29308 -- Clarified how assertQuerysetEqual()'s transform
works.

Backport of 659a73bc0a2df9be856e23fcfc6cc66d0d1a35fd from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29308#comment:7>

Reply all
Reply to author
Forward
0 new messages