{{{
>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.
--
Ticket URL: <https://code.djangoproject.com/ticket/28361#comment:1>
* stage: Unreviewed => Ready for checkin
Comment:
When merging 268a646353c6fa9e5fc3730e13b386ddabb018ef, I thought this
could be a problem.
--
Ticket URL: <https://code.djangoproject.com/ticket/28361#comment:2>
Comment (by benjaoming):
@martonbognar - what happens if you run:
{{{
while True:
datetime.now()
}}}
How often does it change? Might reveal some kind of precision level of
said Windows system.
--
Ticket URL: <https://code.djangoproject.com/ticket/28361#comment:3>
Comment (by marton bognar):
Replying to [comment:3 benjaoming]:
> @martonbognar - what happens if you run:
>
>
> {{{
> while True:
> datetime.now()
> }}}
>
> How often does it change? Might reveal some kind of precision level of
said Windows system.
Here's an excerpt of the output:
https://gist.github.com/martonbognar/4de49f322573fc162c1594efd24efe8c
It does not seem very deterministic to me.
--
Ticket URL: <https://code.djangoproject.com/ticket/28361#comment:4>
Comment (by marton bognar):
Replying to [comment:2 Tim Graham]:
> When merging 268a646353c6fa9e5fc3730e13b386ddabb018ef, I thought this
could be a problem.
So what's the next step? Sorry, I'm new to django issue tracking, should I
create a pull request with my proposed changes or will someone more
qualified take care of the ticket?
--
Ticket URL: <https://code.djangoproject.com/ticket/28361#comment:5>
Comment (by Tim Graham):
You may create a pull request.
--
Ticket URL: <https://code.djangoproject.com/ticket/28361#comment:6>
* owner: nobody => marton bognar
* status: new => assigned
Comment:
Thanks, I submitted a pull request, hope it is alright:
https://github.com/django/django/pull/8711
--
Ticket URL: <https://code.djangoproject.com/ticket/28361#comment:7>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"82741605206382d0afd879c4bce174ef3d6e8aea" 82741605]:
{{{
#!CommitTicketReference repository=""
revision="82741605206382d0afd879c4bce174ef3d6e8aea"
Fixed #28361 -- Fixed possible time-related failure in
was_published_recently() tutorial test.
Regression in 268a646353c6fa9e5fc3730e13b386ddabb018ef.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28361#comment:8>
Comment (by Tim Graham <timograham@…>):
In [changeset:"5b450b84e14f42302f58ec1f15a67a368e64d85c" 5b450b8]:
{{{
#!CommitTicketReference repository=""
revision="5b450b84e14f42302f58ec1f15a67a368e64d85c"
[1.11.x] Fixed #28361 -- Fixed possible time-related failure in
was_published_recently() tutorial test.
Regression in 268a646353c6fa9e5fc3730e13b386ddabb018ef.
Backport of 82741605206382d0afd879c4bce174ef3d6e8aea from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28361#comment:9>