Debian Codegraph @

metomi/isodatetime/tests/test_time_point.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # ----------------------------------------------------------------------------
3 # Copyright (C) British Crown (Met Office) & Contributors.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # ----------------------------------------------------------------------------
18
19 """This tests the ISO 8601 parsing and data model functionality."""
20
21 import datetime
22 import pytest
23 import random
24
25 from metomi.isodatetime.data import TimePoint, Duration, get_days_since_1_ad
26
27
28 def daterange(start_date, end_date):
29 """https://stackoverflow.com/a/1060330"""
30 for n in range(1 + int((end_date - start_date).days)):
31 yield start_date + datetime.timedelta(n)
32
33
34 test_duration_attributes = [
35 ("weeks", 110),
36 ("days", 770),
37 ("hours", 770 * 24),
38 ("minutes", 770 * 24 * 60),
39 ("seconds", 770 * 24 * 60 * 60)
40 ]
41
42
43 @pytest.mark.slow
44 @pytest.mark.parametrize('century', range(18, 24))
45 def test_timepoint(century):
46 """Test the time point data model (takes a while).
47
48 For a range of years (e.g. 1800 to 2400) it iterates through each
49 year, then creates another range with the days in this year. Finally
50 performs a series of tests, failing if any operation results in
51 an error."""
52 for test_year in range(century * 100, (century + 1) * 100, 1):
53 _test_timepoint(test_year)
54
55
56 def _test_timepoint(test_year):
57 my_date = datetime.datetime(test_year, 1, 1)
58 stop_date = datetime.datetime(test_year + 1, 1, 1)
59 for date in daterange(my_date, stop_date):
60 ctrl_data = date.isocalendar()
61 test_date = TimePoint(
62 year=date.year,
63 month_of_year=date.month,
64 day_of_month=date.day
65 )
66 test_week_date = test_date.to_week_date()
67 test_data = test_week_date.get_week_date()
68 assert test_data == ctrl_data
69 ctrl_data = (date.year, date.month, date.day)
70 test_data = test_week_date.get_calendar_date()
71 assert test_data == ctrl_data
72 ctrl_data = date.toordinal()
73 year, day_of_year = test_date.get_ordinal_date()
74 test_data = day_of_year
75 test_data += get_days_since_1_ad(year - 1)
76 assert test_data == ctrl_data
77 for attribute, attr_max in test_duration_attributes:
78 kwargs = {attribute: random.randrange(0, attr_max)}
79 ctrl_data = date + datetime.timedelta(**kwargs)
80 ctrl_data = ctrl_data.year, ctrl_data.month, ctrl_data.day
81 test_data = (
82 (test_date + Duration(**kwargs)).get_calendar_date())
83 assert test_data == ctrl_data
84 ctrl_data = date - datetime.timedelta(**kwargs)
85 ctrl_data = ctrl_data.year, ctrl_data.month, ctrl_data.day
86 # TBD: the subtraction is quite slow. Much slower than other
87 # operations. Could be related to the fact it converts the
88 # value in kwargs to negative multiplying by -1 (i.e. from
89 # __sub__ to __mul__), and also adds it to the date (i.e.
90 # __add__). Profiling the tests, the __sub__ operation used in
91 # the next line will appear amongst the top of time consuming
92 # operations.
93 test_data = (
94 (test_date - Duration(**kwargs)).get_calendar_date())
95 assert test_data == ctrl_data
96 kwargs = {}
97 for attribute, attr_max in test_duration_attributes:
98 kwargs[attribute] = random.randrange(0, attr_max)
99 test_date_minus = test_date - Duration(**kwargs)
100 test_data = test_date - test_date_minus
101 ctrl_data = Duration(**kwargs)
102 assert test_data == ctrl_data
103 test_data = test_date_minus + (test_date - test_date_minus)
104 ctrl_data = test_date
105 assert test_data == ctrl_data
106 test_data = (test_date_minus + Duration(**kwargs))
107 ctrl_data = test_date
108 assert test_data == ctrl_data
109 ctrl_data = (
110 date +
111 datetime.timedelta(minutes=450) +
112 datetime.timedelta(hours=5) -
113 datetime.timedelta(seconds=500, weeks=5))
114 ctrl_data = [
115 (ctrl_data.year, ctrl_data.month, ctrl_data.day),
116 (ctrl_data.hour, ctrl_data.minute, ctrl_data.second)]
117 test_data = (
118 test_date + Duration(minutes=450) +
119 Duration(hours=5) -
120 Duration(weeks=5, seconds=500)
121 )
122 test_data = [
123 test_data.get_calendar_date(),
124 test_data.get_hour_minute_second()]
125 assert test_data == ctrl_data