﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32816	QuerysetEqual Test error on the polls app.	Hassan	nobody	"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."	Bug	closed	Documentation	3.2	Normal	invalid	Polls, Test, QuerysetEqual		Unreviewed	0	0	0	0	0	0
