#28361: One of the tutorial test cases fail on certain systems
-----------------------------------------+-------------------------------
               Reporter:  marton bognar  |          Owner:  nobody
                   Type:  Bug            |         Status:  new
              Component:  Documentation  |        Version:  1.11
               Severity:  Normal         |       Keywords:  datetime test
           Triage Stage:  Unreviewed     |      Has patch:  1
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  1
                  UI/UX:  0              |
-----------------------------------------+-------------------------------
 On my Windows machine the test
 `test_was_published_recently_with_old_question` fails. I'm fairly sure I
 copied every piece of code correctly, but even on a theoretical level I'm
 not sure why it would succeed.

 {{{
 >python manage.py test polls
 Creating test database for alias 'default'...
 System check identified no issues (0 silenced).
 ......F.
 ======================================================================
 FAIL: test_was_published_recently_with_old_question
 (polls.tests.QuestionModelTests)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "[...]\polls\tests.py", line 81
 , in test_was_published_recently_with_old_question
     self.assertIs(old_question.was_published_recently(), False)
 AssertionError: True is not False

 ----------------------------------------------------------------------
 Ran 8 tests in 0.031s

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

 The test case in question:

 {{{
 time = timezone.now() - datetime.timedelta(days=1)
 old_question = Question(pub_date=time)
 self.assertIs(old_question.was_published_recently(), False)
 }}}

 The check used in the function is `now - datetime.timedelta(days=1) <=
 self.pub_date <= now`, and the test sets the `pub_date` to exactly one day
 before now. Can we be sure that time passes between creating the `time`
 variable and checking it against the question's `pub_date`? This certainly
 doesn't seem to be the case on my machine (hence the fail).

 I think a good and unambiguous solution would be to set `time` to 1 day
 and 1 second in the past. This still test the edge case, but it is
 guaranteed that the check will succeed, even with `<=`.

 I'm not quite sure what the difference is, but an interesting comparison
 is how python handles the following on this machine:

 {{{
 >>> datetime.now(), datetime.now(), datetime.now(), datetime.now(),
 datetime.now(), datetime.now()
 (datetime.datetime(2017, 7, 4, 17, 28, 20, 132149),
 datetime.datetime(2017, 7, 4, 17, 28, 20, 132149), datetime.datetime(2017,
 7, 4, 17, 28, 20, 132149), datetime.datetime(2017, 7, 4, 17, 28, 20,
 132149), datetime.datetime(2017, 7, 4, 17, 28, 20, 132149),
 datetime.datetime(2017, 7, 4, 17, 28, 20, 132149))
 }}}

 Whereas on my Linux machine every instance has a different value.

 System specs:
 Windows 8.1 (Version 6.3, Build 9600)
 Python 3.6.1
 Django 1.11.3

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/055.5b139a836c6e758585a4519488708414%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to