[Django] #32816: QuerysetEqual Test error on the polls app.

96 views
Skip to first unread message

Django

unread,
Jun 4, 2021, 3:20:19 AM6/4/21
to django-...@googlegroups.com
#32816: QuerysetEqual Test error on the polls app.
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
MesteRobot |
Type: Bug | Status: new
Component: Testing | Version: 3.2
framework | Keywords: Polls, Test,
Severity: Normal | QuerysetEqual
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hello.

I walk through the polls app tutorial ... I found an error in the code on
part5 of the tutorial;

{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
def test_past_question(self):
"""
Questions with a pub_date in the past are displayed on the
index page.
"""
question = create_question("Past question.", -30)
response = self.client.get(reverse("polls:index"))
self.assertQuerysetEqual(
response.context["latest_question_list"],
[question]
)

def test_future_question_and_past_question(self):
"""
Even if both past and future questions exist, only past questions
are displayed.
"""
question = create_question(question_text="Past question.",
days=-30)
create_question(question_text="Future question.", days=30)
response = self.client.get(reverse('polls:index'))
self.assertQuerysetEqual(
response.context['latest_question_list'],
[question],
)


def test_two_past_questions(self):
"""
The questions index page may display multiple questions.
"""
question1 = create_question(question_text="Past question 1.",
days=-30)
question2 = create_question(question_text="Past question 2.",
days=-5)
response = self.client.get(reverse('polls:index'))
self.assertQuerysetEqual(
response.context['latest_question_list'],
[question2, question1],
)
}}}
}}}


when i run the test i saw this error message:

{{{
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.F.FF...
======================================================================
FAIL: test_future_question_and_past_question
(polls.tests.QuestionIndexViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\Samples\Django\mysite-project\polls\tests.py", line 58, in
test_future_question_and_past_question
self.assertQuerysetEqual(
File "C:\Users\Mr.Robot\AppData\Roaming\Python\Python39\site-
packages\django\test\testcases.py", line 1052, in assertQuerysetEqual
return self.assertEqual(list(items), values, msg=msg)
AssertionError: Lists differ: ['<Question: Past question.>'] !=
[<Question: Past question.>]

First differing element 0:
'<Question: Past question.>'
<Question: Past question.>

- ['<Question: Past question.>']
? - -

+ [<Question: Past question.>]

======================================================================
FAIL: test_past_question (polls.tests.QuestionIndexViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\Samples\Django\mysite-project\polls\tests.py", line 43, in
test_past_question
self.assertQuerysetEqual(
File "C:\Users\Mr.Robot\AppData\Roaming\Python\Python39\site-
packages\django\test\testcases.py", line 1052, in assertQuerysetEqual
return self.assertEqual(list(items), values, msg=msg)
AssertionError: Lists differ: ['<Question: Past question.>'] !=
[<Question: Past question.>]

First differing element 0:
'<Question: Past question.>'
<Question: Past question.>

- ['<Question: Past question.>']
? - -

+ [<Question: Past question.>]

======================================================================
FAIL: test_two_past_question (polls.tests.QuestionIndexViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\Samples\Django\mysite-project\polls\tests.py", line 67, in
test_two_past_question
self.assertQuerysetEqual(
File "C:\Users\Mr.Robot\AppData\Roaming\Python\Python39\site-
packages\django\test\testcases.py", line 1052, in assertQuerysetEqual
return self.assertEqual(list(items), values, msg=msg)
AssertionError: Lists differ: ['<Question: Past question 2.>', '<Question:
Past question 1.>'] != [<Question: Past question 2.>, <Question: Past
question 1.>]

First differing element 0:
'<Question: Past question 2.>'
<Question: Past question 2.>

- ['<Question: Past question 2.>', '<Question: Past question 1.>']
? - - - -

+ [<Question: Past question 2.>, <Question: Past question 1.>]

----------------------------------------------------------------------
Ran 8 tests in 0.126s

FAILED (failures=3)
Destroying test database for alias 'default'...
}}}

So i search the web for solution and i found that on stackoverflow.com:
[https://stackoverflow.com/questions/51255851/djangos-assertquerysetequal-
method-failing-despite-the-two-query-sets-printin/66696842#66696842]

The code above needs to change this way to past the test:

{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
class QuestionIndexViewTest(TestCase):

def test_past_question(self):
question = create_question("Past question.", -30)
response = self.client.get(reverse("polls:index"))
self.assertQuerysetEqual(
response.context["latest_question_list"],
map(repr, [question]),
)

def test_future_question_and_past_question(self):
question = create_question(question_text="Past question.",
days=-30)
create_question(question_text="Future _question.", days=30)
response = self.client.get(reverse("polls:index"))
self.assertQuerysetEqual(
response.context["latest_question_list"],
map(repr, [question]),
)

def test_two_past_question(self):
question1 = create_question(question_text="Past question 1.",
days=-30)
question2 = create_question(question_text="Past question 2.",
days=-5)
response = self.client.get(reverse("polls:index"))
self.assertQuerysetEqual(
response.context["latest_question_list"],
map(repr, [question2, question1]),
)
}}}
}}}

This code needs to map the second queryset with single quotation mark to
pass the test.
please correct that or if i wrong help me for better solution.

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

Django

unread,
Jun 4, 2021, 3:34:50 AM6/4/21
to django-...@googlegroups.com
#32816: QuerysetEqual Test error on the polls app.
-------------------------------------+-------------------------------------
Reporter: MesteRobot | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 3.2
Severity: Normal | Resolution: invalid
Keywords: Polls, Test, | Triage Stage:
QuerysetEqual | Unreviewed
Has patch: 0 | Needs documentation: 0

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

* status: new => closed
* resolution: => invalid
* has_patch: 1 => 0
* component: Testing framework => Documentation


Comment:

Tutorial works for me. It looks that you're using docs for Django 3.2+
with older version of Django.

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

Django

unread,
Oct 8, 2022, 9:26:36 AM10/8/22
to django-...@googlegroups.com
#32816: QuerysetEqual Test error on the polls app.
-------------------------------------+-------------------------------------
Reporter: Hassan | Owner: nobody
Type: Bug | Status: new

Component: Documentation | Version: 3.2
Severity: Normal | Resolution:
Keywords: Polls, Test, | Triage Stage:
QuerysetEqual | Unreviewed
Has patch: 0 | Needs documentation: 0

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

* status: closed => new
* resolution: invalid =>


Comment:

Hello there I'm completely agree with @hasan please fix that for version 3


{{{


def test_past_question(self):
"""
Questions with a pub_date in the past are displayed on the
index page.
"""

question = create_question(question_text="Past question.",
days=-30)

response = self.client.get(reverse('polls:index'))

# print('####latest_question_list####',
response.context['latest_question_list'], end='\n')
# print('####question####', question, end='\n')

self.assertQuerysetEqual(response.context['latest_question_list'],
[question])

def test_future_question(self):
"""
Questions with a pub_date in the future aren't displayed on
the index page.
"""


create_question(question_text="Future question.", days=30)
response = self.client.get(reverse('polls:index'))

self.assertContains(response, "No polls are available.")
self.assertQuerysetEqual(response.context['latest_question_list'],
[])

def test_past_future_question(self):


question = create_question(question_text="Past question.",
days=-30)
create_question(question_text="Future question.", days=30)
response = self.client.get(reverse('polls:index'))

# print('RESPONSE=> ', response.context)
self.assertQuerysetEqual(response.context['latest_question_list'],
['<Question: Past question.>'])

}}}


the 'test_past_question' is passed but 'test_future_question' is not but
if I write
{{{
self.assertQuerysetEqual(response.context['latest_question_list'],
[question])

}}}
in 'test_future_question' it will again pass


so it is a genuine issue please don't ignore. Because this problem is
blocking to clear the hackerrank hands-on as well and problem is we can't
modify the test case file in hackerrank.

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

Django

unread,
Oct 8, 2022, 1:23:31 PM10/8/22
to django-...@googlegroups.com
#32816: QuerysetEqual Test error on the polls app.
-------------------------------------+-------------------------------------
Reporter: Hassan | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 3.2
Severity: Normal | Resolution: invalid

Keywords: Polls, Test, | Triage Stage:
QuerysetEqual | Unreviewed
Has patch: 0 | Needs documentation: 0

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

* status: new => closed
* resolution: => invalid


Comment:

Gopal, please don't re-open old tickets. First of all, your issue has
nothing to do with the problem reported in the ticket description.
Secondly, tutorial works for me. It looks that you missed point about
[https://docs.djangoproject.com/en/3.0/intro/tutorial05/#improving-our-
view improving your view]. If you're having trouble understanding how
Django works, see TicketClosingReasons/UseSupportChannels for ways to get
help.

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

Reply all
Reply to author
Forward
0 new messages