Opened 3 years ago
Closed 2 years ago
#32816 closed Bug (invalid)
QuerysetEqual Test error on the polls app.
Reported by: | Hassan | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 3.2 |
Severity: | Normal | Keywords: | Polls, Test, QuerysetEqual |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hello.
I walk through the polls app tutorial ... I found an error in the code on part5 of the tutorial;
Code highlighting:
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:
Code highlighting:
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.
Change History (3)
comment:1 by , 3 years ago
Component: | Testing framework → Documentation |
---|---|
Has patch: | unset |
Resolution: | → invalid |
Status: | new → closed |
comment:2 by , 2 years ago
Resolution: | invalid |
---|---|
Status: | closed → new |
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.
comment:3 by , 2 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
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 improving your view. If you're having trouble understanding how Django works, see TicketClosingReasons/UseSupportChannels for ways to get help.
Tutorial works for me. It looks that you're using docs for Django 3.2+ with older version of Django.