Debian Codegraph @

tests/unit/test_time_parser.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 """Test Cylc recurring date/time syntax parsing."""
18
19 import pytest
20
21 from cylc.flow.time_parser import (
22 CylcTimeParser,
23 UTC_UTC_OFFSET_HOURS_MINUTES,
24 )
25
26
27 @pytest.fixture
28 def parsers():
29 _start_point = "19991226T0930Z"
30 # Note: the following timezone will be Z-ified *after* truncation
31 # or offsets are applied.
32 _end_point = "20010506T1200+0200"
33 return {
34 0: CylcTimeParser(
35 _start_point, _end_point,
36 CylcTimeParser.initiate_parsers(
37 assumed_time_zone=UTC_UTC_OFFSET_HOURS_MINUTES
38 )
39 ),
40 2: CylcTimeParser(
41 _start_point, _end_point,
42 CylcTimeParser.initiate_parsers(
43 num_expanded_year_digits=2,
44 assumed_time_zone=UTC_UTC_OFFSET_HOURS_MINUTES
45 )
46 )
47 }
48
49
50 @pytest.mark.parametrize(
51 'expression, num_expanded_year_digits, ctrl_data, ctrl_results',
52 [
53 (
54 "R5/T06/04T1230-0300",
55 0,
56 "R5/19991227T0600Z/20010604T1530Z",
57 ["19991227T0600Z", "20010604T1530Z",
58 "20021112T0100Z", "20040420T1030Z",
59 "20050927T2000Z"]
60 ),
61 (
62 "R2/-0450000101T04Z/+0020630405T2200-05",
63 2,
64 "R2/-0450000101T0400Z/+0020630406T0300Z",
65 ["-0450000101T0400Z", "+0020630406T0300Z"]
66 ),
67 (
68 "R10/T12+P10D/-P1Y",
69 0,
70 "R10/20000105T1200Z/20000506T1000Z",
71 ["20000105T1200Z", "20000506T1000Z",
72 "20000905T0800Z", "20010105T0600Z",
73 "20010507T0400Z", "20010906T0200Z",
74 "20020106T0000Z", "20020507T2200Z",
75 "20020906T2000Z", "20030106T1800Z"]
76 )
77 ]
78 )
79 def test_first_recurrence_format(
80 expression, num_expanded_year_digits, ctrl_data, ctrl_results,
81 parsers
82 ):
83 """Test the first ISO 8601 recurrence format."""
84 parser = parsers[num_expanded_year_digits]
85 recurrence = parser.parse_recurrence(expression)[0]
86 test_data = str(recurrence)
87 assert test_data == ctrl_data
88 test_results = [str(res) for res in recurrence]
89 assert test_results == ctrl_results
90
91
92 def test_third_recurrence_format(parsers):
93 """Test the third ISO 8601 recurrence format."""
94 tests = [("T06", "R/19991227T0600Z/P1D"),
95 ("T1230", "R/19991226T1230Z/P1D"),
96 ("04T00", "R/20000104T0000Z/P1M"),
97 ("01T06/PT2H", "R/20000101T0600Z/PT2H"),
98 ("0501T/P4Y3M", "R/20000501T0000Z/P4Y3M"),
99 ("P1YT5H", "R/19991226T0930Z/P1YT5H",
100 ["19991226T0930Z", "20001226T1430Z"]),
101 ("PT5M", "R/19991226T0930Z/PT5M"),
102 ("R/T-40", "R/19991226T0940Z/PT1H"),
103 ("R/150401T", "R/20150401T0000Z/P100Y"),
104 ("R5/T04-0300", "R5/19991227T0700Z/P1D"),
105 ("R2/T23Z/", "R2/19991226T2300Z/P1D"),
106 ("R/19991226T2359Z/P1W", "R/19991226T2359Z/P1W"),
107 ("R/-P1W/P1W", "R/19991219T0930Z/P1W"),
108 ("R/+P1D/P1Y", "R/19991227T0930Z/P1Y"),
109 ("R/T06-P1D/PT1H", "R/19991226T0600Z/PT1H"),
110 ("R/T12/PT10,5H", "R/19991226T1200Z/PT10,5H"),
111 ("R/T12/PT10.5H", "R/19991226T1200Z/PT10,5H"),
112 ("R5/+P10Y5M4D/PT2H", "R5/20100529T0930Z/PT2H"),
113 ("R30/-P200D/P10D", "R30/19990609T0930Z/P10D"),
114 ("R10/T06/PT2H", "R10/19991227T0600Z/PT2H",
115 ["19991227T0600Z", "19991227T0800Z", "19991227T1000Z",
116 "19991227T1200Z", "19991227T1400Z", "19991227T1600Z",
117 "19991227T1800Z", "19991227T2000Z", "19991227T2200Z",
118 "19991228T0000Z"]),
119 ("R//P1Y", "R/19991226T0930Z/P1Y",
120 ["19991226T0930Z", "20001226T0930Z"]),
121 ("R5//P1D", "R5/19991226T0930Z/P1D",
122 ["19991226T0930Z", "19991227T0930Z",
123 "19991228T0930Z", "19991229T0930Z",
124 "19991230T0930Z"]),
125 ("R1", "R1/19991226T0930Z/P0Y",
126 ["19991226T0930Z"])]
127 for test in tests:
128 if len(test) == 2:
129 expression, ctrl_data = test
130 ctrl_results = None
131 else:
132 expression, ctrl_data, ctrl_results = test
133 recurrence = (parsers[0].parse_recurrence(expression))[0]
134 test_data = str(recurrence)
135 assert test_data == ctrl_data
136 if ctrl_results is None:
137 continue
138 test_results = []
139 for i, test_result in enumerate(recurrence):
140 if i > len(ctrl_results) - 1:
141 break
142 assert str(test_result) == ctrl_results[i]
143 test_results.append(str(test_result))
144 assert test_results == ctrl_results
145
146
147 def test_fourth_recurrence_format(parsers):
148 """Test the fourth ISO 8601 recurrence format."""
149 tests = [("PT6H/20000101T0500Z", "R25/19991226T0500Z/PT6H"),
150 ("P12D/+P2W", "R44/19991221T1000Z/P12D"),
151 ("R2/P1W/-P1M1D", "R2/P1W/20010405T1000Z"),
152 ("R3/P6D/T12+02", "R3/P6D/20010506T1000Z"),
153 ("R4/P6DT12H/01T00+02", "R4/P6DT12H/20010531T2200Z"),
154 ("R5/P1D/20010506T1200+0200", "R5/P1D/20010506T1000Z"),
155 ("R6/PT5M/+PT2M", "R6/PT5M/20010506T1002Z"),
156 ("R7/P20Y/-P20Y", "R7/P20Y/19810506T1000Z"),
157 ("R8/P3YT2H/T18-02", "R8/P3YT2H/20010506T2000Z"),
158 ("R9/PT3H/31T", "R9/PT3H/20010531T0000Z"),
159 ("R10/P1Y/", "R10/P1Y/20010506T1000Z"),
160 ("R3/P2Y/02T", "R3/P2Y/20010602T0000Z"),
161 ("R/P2Y", "R2/19990506T1000Z/P2Y"),
162 ("R48/PT2H", "R48/PT2H/20010506T1000Z"),
163 ("R100/P21Y/", "R100/P21Y/20010506T1000Z")]
164 for test in tests:
165 if len(test) == 2:
166 expression, ctrl_data = test
167 ctrl_results = None
168 else:
169 expression, ctrl_data, ctrl_results = test
170 recurrence = (parsers[0].parse_recurrence(
171 expression))[0]
172 test_data = str(recurrence)
173 assert test_data == ctrl_data
174 if ctrl_results is None:
175 continue
176 test_results = []
177 for i, test_result in enumerate(recurrence):
178 assert str(test_result) == ctrl_results[i]
179 test_results.append(str(test_result))
180 assert test_results == ctrl_results
181
182
183 def test_inter_cycle_timepoints(parsers):
184 """Test the inter-cycle point parsing."""
185 task_cycle_time = parsers[0].parse_timepoint("20000101T00Z")
186 tests = [("T06", "20000101T0600Z", 0),
187 ("-PT6H", "19991231T1800Z", 0),
188 ("+P5Y2M", "20050301T0000Z", 0),
189 ("0229T", "20000229T0000Z", 0),
190 ("+P54D", "20000224T0000Z", 0),
191 ("T12+P5W", "20000205T1200Z", 0),
192 ("-P1Y", "19990101T0000Z", 0),
193 ("-9999990101T00Z", "-9999990101T0000Z", 2),
194 ("20050601T2359+0200", "20050601T2159Z", 0)]
195 for expression, ctrl_data, num_expanded_year_digits in tests:
196 parser = parsers[num_expanded_year_digits]
197 test_data = str(parser.parse_timepoint(
198 expression,
199 context_point=task_cycle_time))
200 assert test_data == ctrl_data
201
202
203 def test_interval(parsers):
204 """Test the interval timepoint parsing (purposefully weak tests)."""
205 tests = ["PT6H2M", "PT6H", "P2Y5D",
206 "P5W", "PT12M2S", "PT65S",
207 "PT2M", "P1YT567,4M"]
208 for expression in tests:
209 test_data = str(parsers[0].parse_interval(expression))
210 assert test_data == expression