[Django] #31476: Django Tutorial 5 - Testing create_question issue - Assetion Failure

3 views
Skip to first unread message

Django

unread,
Apr 17, 2020, 12:49:45 PM4/17/20
to django-...@googlegroups.com
#31476: Django Tutorial 5 - Testing create_question issue - Assetion Failure
-------------------------------------+-------------------------------------
Reporter: adavie1 | Owner: nobody
Type: Bug | Status: new
Component: | Version: 3.0
Documentation |
Severity: Normal | Keywords: tutorial, testing
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Firstly, thanks for a great tutorial! I'm really enjoying learning Django.

Secondly, the code in the Testing Tutorial (#6):

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

is redundant? This works as well:

{{{
create_question("Past question.", -30)
}}}

My error is this:

{{{
AssertionError: Lists differ: [] != ['<Question: Past question>']

Second list contains 1 additional elements.
First extra element 0:
'<Question: Past question>'

- []
+ ['<Question: Past question>']

}}}

For future question, I'm getting this:

{{{
AssertionError: Lists differ: ['<Question: Future question>'] != []

First list contains 1 additional elements.
First extra element 0:
'<Question: Future question>'

- ['<Question: Future question>']
+ []
}}}


This is the code for test_past_question:

{{{
def test_past_question(self):
"""
Questions with a published_date in the past.
"""
create_question('Past question', days=-30)

resp = self.client.get(reverse('polls:index'))
self.assertQuerysetEqual(
resp.context['latest'],
['<Question: Past question>']
)
}}}


and my create_question method:

{{{
def create_question(text, days):
"""
Create a Question with text and days from now. -ve in the past, +ve in
the future.
"""
time = timezone.now() - datetime.timedelta(days=days)
q = Question.objects.create(question_text=text, published_date=time)
# pr = "create_question(" + text + ", " + str(days) + ")"
# print(pr, q)
return q

}}}

I've been knocking my head over this for the last few days.
Any help much appreciated.

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

Django

unread,
Apr 17, 2020, 12:52:34 PM4/17/20
to django-...@googlegroups.com
#31476: Django Tutorial 5 - Testing create_question issue - Assertion Failure
-----------------------------------+--------------------------------------
Reporter: Anthony Davie | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 3.0
Severity: Normal | Resolution:
Keywords: tutorial, testing | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

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

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

Django

unread,
Apr 17, 2020, 2:00:04 PM4/17/20
to django-...@googlegroups.com
#31476: Django Tutorial 5 - Testing create_question issue - Assertion Failure
-----------------------------------+--------------------------------------
Reporter: Anthony Davie | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 3.0
Severity: Normal | Resolution: invalid
Keywords: tutorial, testing | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

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

* status: new => closed
* resolution: => invalid
* easy: 0 => 1


Comment:

Hello Anthony,

I think you just have a typo in your `create_question` function : yours
has `time = timezone.now() - datetime.timedelta(days=days)` whereas the
tutorial has `time = timezone.now() + datetime.timedelta(days=days)`. That
explains why your past and future are in the wrong place ;)

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

Django

unread,
Oct 8, 2022, 9:44:48 AM10/8/22
to django-...@googlegroups.com
#31476: Django Tutorial 5 - Testing create_question issue - Assertion Failure
-----------------------------------+--------------------------------------
Reporter: Anthony Davie | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 3.0
Severity: Normal | Resolution: invalid
Keywords: tutorial, testing | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by dasbairagya):

this is my case but no luck
{{{
def create_question(question_text, days):
"""
Create a question with the given `question_text` and published the
given number of `days` offset to now (negative for questions published
in the past, positive for questions that have yet to be published).


"""
time = timezone.now() + datetime.timedelta(days=days)

return Question.objects.create(question_text=question_text,
pub_date=time)
}}}


please have a look here
https://code.djangoproject.com/ticket/32816#comment:2

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

Reply all
Reply to author
Forward
0 new messages