Commit f941f5fc authored by Jérome Perrin's avatar Jérome Perrin

test_tidrange

parent cbbc1adb
......@@ -20,14 +20,14 @@
import os
import time
import datetime
from pytest import raises, fixture
import pytest
import pytz
from freezegun import freeze_time
from ZODB.TimeStamp import TimeStamp
from zodbtools.util import parse_tidrange, TidRangeInvalid
from zodbtools.util import parse_tidrange, parse_tid, ashex, TidRangeInvalid
@fixture
@pytest.fixture
def europe_paris_timezone():
"""Pytest's fixture to run this test with Europe/Paris as default timezone.
"""
......@@ -40,12 +40,13 @@ def europe_paris_timezone():
os.environ["TZ"] = initial_tz
time.tzset()
@fixture
def fake_time_2001_01_01():
"""Pytest's fixture to run this test as if now() was 2001/01/01 at midnight UTC.
# TODO: merge these fixtures in one
@pytest.fixture
def fake_time():
"""Pytest's fixture to run this test as if now() was 2009-08-30T19:20:00Z
"""
reference_time = datetime.datetime(2001, 1, 1, 0, 0, 0, 0, pytz.utc)
reference_time = datetime.datetime(2009, 8, 30, 19, 20, 0, 0, pytz.utc)\
.astimezone(pytz.timezone("Europe/Paris"))
with freeze_time(reference_time):
yield
......@@ -66,12 +67,13 @@ def test_tidrange_tid():
assert (None, None) == parse_tidrange("..")
with raises(TidRangeInvalid) as exc:
with pytest.raises(TidRangeInvalid) as exc:
parse_tidrange("invalid")
assert exc.value.args == ("invalid",)
def test_tidrange_date(europe_paris_timezone, fake_time_2001_01_01):
# XXX do we need this
def test_tidrange_date(europe_paris_timezone, fake_time):
# dates in UTC
assert (
b"\x03\xc4\x85v\x00\x00\x00\x00",
......@@ -83,19 +85,16 @@ def test_tidrange_date(europe_paris_timezone, fake_time_2001_01_01):
"2018-01-01 10:30:00 UTC.."
)
# dates in local timezone
assert (
b"\x03\xc4\x85:\x00\x00\x00\x00",
b"\x03\xc4\x88d\x00\x00\x00\x00",
) == parse_tidrange("2018-01-01 10:30:00..2018-01-02")
# dates in natural language (also in local timezone)
assert (
b"\x03\xc4\x85:\x00\x00\x00\x00",
b"\x03\xc4\x88d\x00\x00\x00\x00",
) == parse_tidrange("le 1er janvier 2018 à 10h30..2018年1月2日")
# or relative dates
assert (b"\x039\x8e\x7f\x00\x00\x00\x00", None) == parse_tidrange("1 minute ago..")
assert (None, None) != parse_tidrange("1 month ago..yesterday")
import os
test_parameters = []
with open(os.path.join(os.path.dirname(__file__), "testdata", "tidrange-formats.txt")) as f:
for line in f:
line = line.strip()
if line and not line.startswith('#'):
test_parameters.append(line.split(" ", 2))
@pytest.mark.parametrize("reference_time,reference_tid,input_time", test_parameters)
def test_parse_tid(europe_paris_timezone, fake_time, reference_time, reference_tid, input_time):
print ("parsing", input_time, "expecting", reference_time)
assert reference_tid == ashex(parse_tid(input_time))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment