Skip to content

Commit

Permalink
More timezone tests for #254.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemon24 committed Oct 6, 2021
1 parent 8e3e8db commit 5083bef
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import py.path
import pytest
from utils import monkeypatch_tz
from utils import reload_module

from reader import make_reader as original_make_reader
Expand Down
10 changes: 3 additions & 7 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,9 @@ def test_parse_returns_etag_last_modified(


@pytest.mark.parametrize('tz', ['UTC', 'Europe/Helsinki'])
# timt.tzset() does not exist on Windows
# time.tzset() does not exist on Windows
@pytest.mark.skipif("os.name == 'nt'")
def test_parse_local_timezone(monkeypatch, request, parse, tz, data_dir):
def test_parse_local_timezone(monkeypatch_tz, request, parse, tz, data_dir):
"""parse() return the correct dates regardless of the local timezone."""

feed_path = data_dir.join('full.atom')
Expand All @@ -381,11 +381,7 @@ def test_parse_local_timezone(monkeypatch, request, parse, tz, data_dir):
expected = {'url_base': url_base, 'rel_base': rel_base}
exec(feed_path.new(ext='.atom.py').read(), expected)

import time

request.addfinalizer(time.tzset)
monkeypatch.setenv('TZ', tz)
time.tzset()
monkeypatch_tz(tz)
feed, _, _, _ = parse(str(feed_path))
assert feed.updated == expected['feed'].updated

Expand Down
18 changes: 12 additions & 6 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3122,7 +3122,7 @@ def test_entry_read_important_modified_gets_set_to_now(reader, flag):

@pytest.mark.parametrize('flag', ['read', 'important'])
@rename_argument('reader', 'reader_with_one_feed')
def test_entry_read_important_modified_argument(reader, flag):
def test_entry_read_important_modified_argument(reader, flag, monkeypatch_tz):
from datetime import datetime

reader.update_feeds()
Expand All @@ -3134,12 +3134,18 @@ def test_entry_read_important_modified_argument(reader, flag):
entry, modified=datetime(2010, 1, 1, tzinfo=timezone(timedelta(hours=-2)))
)
entry = next(reader.get_entries())
assert getattr(entry, f'{flag}_modified') == datetime(
2010, 1, 1, 2, tzinfo=timezone.utc
)

# TODO: test naive datetime handling (by mocking the system timezone?)
assert getattr(entry, f'{flag}_modified') == utc_datetime(2010, 1, 1, 2)

getattr(reader, f'mark_entry_as_{flag}')(entry, modified=None)
entry = next(reader.get_entries())
assert getattr(entry, f'{flag}_modified') is None

monkeypatch_tz('UTC')
getattr(reader, f'mark_entry_as_{flag}')(entry, modified=datetime(2010, 1, 1, 4))
entry = next(reader.get_entries())
assert getattr(entry, f'{flag}_modified') == utc_datetime(2010, 1, 1, 4)

monkeypatch_tz('Etc/GMT+6')
getattr(reader, f'mark_entry_as_{flag}')(entry, modified=datetime(2010, 1, 1))
entry = next(reader.get_entries())
assert getattr(entry, f'{flag}_modified') == utc_datetime(2010, 1, 1, 6)
23 changes: 23 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import importlib
import inspect
import os.path
import time
from datetime import datetime
from datetime import timezone
from urllib.parse import urlparse
Expand Down Expand Up @@ -75,6 +76,28 @@ def reload_module(monkeypatch):
reloader.undo()


class TZSetter:
def __init__(self, monkeypatch):
self.monkeypatch = monkeypatch

def __call__(self, tz):
self.monkeypatch.setenv('TZ', tz)
time.tzset()

def undo(self):
self.monkeypatch.undo()
time.tzset()


@pytest.fixture
def monkeypatch_tz(monkeypatch):
tzsetter = TZSetter(monkeypatch)
try:
yield tzsetter
finally:
tzsetter.undo()


# FIXME: explain what this is
# https://github.com/lemon24/reader/issues/233

Expand Down

0 comments on commit 5083bef

Please sign in to comment.