Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.

Conversation

@jwhitlock
Copy link
Contributor

Re-write all the feeds tests, to use pytest and fixtures, for full code coverage of kuma/wiki/feeds.py, and to avoid silly things like sleeping for a second to get a new timestamp.

Then, add some additional handling of our naive timestamps, to turn them into timezone-aware timestamps needed for the feeds. This avoid errors in the second half of the year when the US moves from Daylight Savings Time to standard time, and 1 AM happens twice. When these ambiguous times are encountered, they currently cause the feed to fail with an error. The change is to assume that it is the first 1 AM, avoiding the conversion error.

@jwhitlock jwhitlock requested a review from escattone January 9, 2018 17:11
Copy link
Contributor

@escattone escattone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow. Fixes the DST issue, modernizes the tests, and takes coverage on kuma/wiki/feeds.py from 93% to 100%. Thanks @jwhitlock!



def test_recent_documents_handles_ambiguous_time(root_doc, client):
"""The recent_documents feed handles times during DST transition."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super nit: recent documents rather than recent_documents?

Convert Document feed tests to use pytest and fixtures. Existing tests
were converted (except for the silly parts), and then additional tests
added to get complete coverage.
When a naive datetime is ambigious because of the transition from
daylight saving time to standard time, assume that the date is DST. This
avoids a month of feed errors in the second half of the year, until we
can transition to timezone-aware dates.
@jwhitlock jwhitlock force-pushed the force-aware-times-1410138 branch from d9090c4 to e1c7b05 Compare January 10, 2018 17:26
@codecov-io
Copy link

Codecov Report

Merging #4621 into master will increase coverage by 0.08%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4621      +/-   ##
==========================================
+ Coverage    95.2%   95.28%   +0.08%     
==========================================
  Files         262      262              
  Lines       23188    23287      +99     
  Branches     1673     1660      -13     
==========================================
+ Hits        22076    22190     +114     
+ Misses        893      886       -7     
+ Partials      219      211       -8
Impacted Files Coverage Δ
kuma/wiki/feeds.py 100% <100%> (+6.53%) ⬆️
kuma/wiki/tests/__init__.py 100% <100%> (ø) ⬆️
kuma/wiki/tests/test_feeds.py 100% <100%> (ø) ⬆️
kuma/search/views.py 86.66% <0%> (ø) ⬆️
kuma/users/apps.py 85.71% <0%> (+2.85%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b9f0ef3...e1c7b05. Read the comment docs.

@jwhitlock
Copy link
Contributor Author

Force-push to rebase and fix doc nit.

@escattone escattone self-assigned this Jan 10, 2018
if is_naive(raw_pubdate):
# USE_TZ is False, database has naive timestamps
timezone = get_current_timezone()
pubdate = timezone.localize(raw_pubdate, is_dst=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not find any documentation of this localize in documentation.
by supplying is_dst=True, are we assuming that its always Daylight Saving time?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@safwanrahman Yes, we're just going to assume it's always DST until we start storing timezone-aware datetimes.

Copy link
Contributor Author

@jwhitlock jwhitlock Jan 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@safwanrahman I had similar questions that were not answered by the docs I read. I assumed it was OK because other tests outside the magic hour passed. I'm investigating a little more now that you've asked it. Results to come...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

localize is provided by pytz, which has pretty good narrative documentation, but doesn't go into details about the functions.

The details are in the source, hosted on launchpad:

https://git.launchpad.net/pytz/tree/src/pytz/tzinfo.py#n244

The code looks forward a day and back a day, to see if the time is around a transition. This can result in 0, 1, or 2 possible timezones:

  • 0 cases: The time is an "impossible time", during the lost hour during the transition into daylight savings time. is_dst=None raises an error, is_dst=True uses the timezone 6 hours later, and is_dst=False uses the timezone 6 hours earlier.
  • 1 case: There is no ambiguity, the one timezone is used
  • 2 cases: The time is from the transition out of daylight savings time, when an hour is repeated. is_dst=None raises an error, is_dst=True uses the DST timezone, is_dst=False uses the standard timezone.

I'm not Python, so I could have read the code wrong. Here's some test code in the Django shell:

>>> from datetime import datetime as dt
>>> from django.utils.timezone import get_current_timezone
>>> for month in range(1,13):
...   test = dt(2017, month, 3, 1, 23)
...   print(get_current_timezone().localize(test))
...
2017-01-03 01:23:00-08:00
2017-02-03 01:23:00-08:00
2017-03-03 01:23:00-08:00
2017-04-03 01:23:00-07:00
2017-05-03 01:23:00-07:00
2017-06-03 01:23:00-07:00
2017-07-03 01:23:00-07:00
2017-08-03 01:23:00-07:00
2017-09-03 01:23:00-07:00
2017-10-03 01:23:00-07:00
2017-11-03 01:23:00-07:00
2017-12-03 01:23:00-08:00

Here in the Central time zone, the dates from April through November are Central Daylight Time (-07:00), and the other months are Central Standard Time (-8:00).

That was a fun question, thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants