Opened 5 years ago

Closed 5 years ago

Last modified 2 years ago

#31476 closed Bug (invalid)

Django Tutorial 5 - Testing create_question issue - Assertion Failure

Reported by: Anthony Davie Owned by: nobody
Component: Documentation Version: 3.0
Severity: Normal Keywords: tutorial, testing
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

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.

Change History (3)

comment:1 by Anthony Davie, 5 years ago

Summary: Django Tutorial 5 - Testing create_question issue - Assetion FailureDjango Tutorial 5 - Testing create_question issue - Assertion Failure

comment:2 by Martin Lehoux, 5 years ago

Easy pickings: set
Resolution: invalid
Status: newclosed

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 ;)

comment:3 by Gopal Dasbariagya, 2 years ago

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

Note: See TracTickets for help on using tickets.
Back to Top