| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
"""This tests the ISO 8601 parsing and data model functionality.""" |
| 17 |
|
| 18 |
import copy |
| 19 |
import datetime |
| 20 |
from itertools import chain |
| 21 |
import unittest |
| 22 |
import pytest |
| 23 |
|
| 24 |
from metomi.isodatetime import data |
| 25 |
from metomi.isodatetime import dumpers |
| 26 |
from metomi.isodatetime import parsers |
| 27 |
from metomi.isodatetime import parser_spec |
| 28 |
from metomi.isodatetime.exceptions import ( |
| 29 |
ISO8601SyntaxError, TimePointDumperBoundsError, BadInputError) |
| 30 |
|
| 31 |
|
| 32 |
def get_timedurationparser_tests(): |
| 33 |
"""Yield tests for the duration parser.""" |
| 34 |
test_expressions = { |
| 35 |
"P3Y": {"years": 3}, |
| 36 |
"P90Y": {"years": 90}, |
| 37 |
"P1Y2M": {"years": 1, "months": 2}, |
| 38 |
"P20Y2M": {"years": 20, "months": 2}, |
| 39 |
"P2M": {"months": 2}, |
| 40 |
"P52M": {"months": 52}, |
| 41 |
"P20Y10M2D": {"years": 20, "months": 10, "days": 2}, |
| 42 |
"P1Y3D": {"years": 1, "days": 3}, |
| 43 |
"P4M1D": {"months": 4, "days": 1}, |
| 44 |
"P3Y404D": {"years": 3, "days": 404}, |
| 45 |
"P30Y2D": {"years": 30, "days": 2}, |
| 46 |
"PT6H": {"hours": 6}, |
| 47 |
"PT1034H": {"hours": 1034}, |
| 48 |
"P3YT4H2M": {"years": 3, "hours": 4, "minutes": 2}, |
| 49 |
"P30Y2DT10S": {"years": 30, "days": 2, "seconds": 10}, |
| 50 |
"PT2S": {"seconds": 2}, |
| 51 |
"PT2.5S": {"seconds": 2.5}, |
| 52 |
"PT2,5S": {"seconds": 2.5}, |
| 53 |
"PT5.5023H": {"hours": 5.5023}, |
| 54 |
"PT5,5023H": {"hours": 5.5023}, |
| 55 |
"P5W": {"weeks": 5}, |
| 56 |
"P100W": {"weeks": 100}, |
| 57 |
"P0004-03-02T01": {"years": 4, "months": 3, "days": 2, |
| 58 |
"hours": 1}, |
| 59 |
"P0004": {"years": 4}, |
| 60 |
"P0004-03-00": {"years": 4, "months": 3}, |
| 61 |
"P0004-00-78": {"years": 4, "days": 78}, |
| 62 |
"P0004-078": {"years": 4, "days": 78}, |
| 63 |
"P0004-078T10,5": {"years": 4, "days": 78, "hours": 10.5}, |
| 64 |
"P00000020T133702": {"days": 20, "hours": 13, "minutes": 37, |
| 65 |
"seconds": 2}, |
| 66 |
"-P3YT4H2M": {"years": -3, "hours": -4, "minutes": -2}, |
| 67 |
"-PT5M": {"minutes": -5}, |
| 68 |
"-P7Y": {"years": -7, "hours": 0} |
| 69 |
} |
| 70 |
for expression, ctrl_result in test_expressions.items(): |
| 71 |
ctrl_data = str(data.Duration(**ctrl_result)) |
| 72 |
yield expression, ctrl_data |
| 73 |
|
| 74 |
|
| 75 |
def get_timedurationdumper_tests(): |
| 76 |
"""Yield tests for the duration dumper.""" |
| 77 |
test_expressions = { |
| 78 |
"P3Y": {"years": 3}, |
| 79 |
"P90Y": {"years": 90}, |
| 80 |
"P1Y2M": {"years": 1, "months": 2}, |
| 81 |
"P20Y2M": {"years": 20, "months": 2}, |
| 82 |
"P2M": {"months": 2}, |
| 83 |
"P52M": {"months": 52}, |
| 84 |
"P20Y10M2D": {"years": 20, "months": 10, "days": 2}, |
| 85 |
"P1Y3D": {"years": 1, "days": 3}, |
| 86 |
"P4M1D": {"months": 4, "days": 1}, |
| 87 |
"P3Y404D": {"years": 3, "days": 404}, |
| 88 |
"P30Y2D": {"years": 30, "days": 2}, |
| 89 |
"PT6H": {"hours": 6}, |
| 90 |
"PT1034H": {"hours": 1034}, |
| 91 |
"P3YT4H2M": {"years": 3, "hours": 4, "minutes": 2}, |
| 92 |
"P30Y2DT10S": {"years": 30, "days": 2, "seconds": 10}, |
| 93 |
"PT2S": {"seconds": 2}, |
| 94 |
"PT2,5S": {"seconds": 2.5}, |
| 95 |
"PT5,5023H": {"hours": 5.5023}, |
| 96 |
"P5W": {"weeks": 5}, |
| 97 |
"P100W": {"weeks": 100}, |
| 98 |
"-P3YT4H2M": {"years": -3, "hours": -4, "minutes": -2}, |
| 99 |
"-PT5M": {"minutes": -5}, |
| 100 |
"-P7Y": {"years": -7, "hours": 0}, |
| 101 |
"PT1H": {"seconds": 3600, "standardize": True}, |
| 102 |
"P1DT5M": {"minutes": 1445, "standardize": True}, |
| 103 |
"PT59S": {"seconds": 59, "standardize": True}, |
| 104 |
"PT1H4M56S": {"minutes": 10, "seconds": 3296, "standardize": True}, |
| 105 |
} |
| 106 |
for expression, ctrl_result in test_expressions.items(): |
| 107 |
yield expression, ctrl_result |
| 108 |
|
| 109 |
|
| 110 |
def get_timepoint_dumper_tests(): |
| 111 |
"""Yield tests for custom timepoint dumps.""" |
| 112 |
return [ |
| 113 |
( |
| 114 |
{"year": 44, "month_of_year": 1, "day_of_month": 4, |
| 115 |
"hour_of_day": 5, "minute_of_hour": 1, "second_of_minute": 2, |
| 116 |
"time_zone_hour": 0, "time_zone_minute": 0}, |
| 117 |
[("CCYY-MMDDThhmmZ", "0044-0104T0501Z"), |
| 118 |
("YYDDDThh:mm:ss", "44004T05:01:02"), |
| 119 |
("WwwD", "W011"), |
| 120 |
("CCDDDThh*ss-0600", "00003T23*02-0600"), |
| 121 |
("+XCCYY-MM-DDThh:mm:ss-11:45", |
| 122 |
"+000044-01-03T17:16:02-11:45"), |
| 123 |
("+XCCYYMM-DDThh-01:00", "+00004401-04T04-01:00"), |
| 124 |
("+XCCYYMM-DDThh+13:00", "+00004401-04T18+13:00"), |
| 125 |
("+XCCYYMM-DDThh-0100", "+00004401-04T04-0100"), |
| 126 |
("+XCCYYMM-DDThh+1300", "+00004401-04T18+1300"), |
| 127 |
("+XCCYYMMDDThh-0100", "+0000440104T04-0100"), |
| 128 |
("+XCCYYMMDDThh+13", "+0000440104T18+13"), |
| 129 |
("+XCCYYMMDDThh+hhmm", "+0000440104T05+0000"), |
| 130 |
("+XCCYY-MM-DDThh:mm:ss+hh:mm", |
| 131 |
"+000044-01-04T05:01:02+00:00"), |
| 132 |
("DD/MM/CCYY is a silly format", "04/01/0044 is a silly format"), |
| 133 |
("ThhZ", "T05Z"), |
| 134 |
("%Y-%m-%dT%H:%M", "0044-01-04T05:01"), |
| 135 |
("CCYYMMDD00.T+3", "0044010400.T+3"), |
| 136 |
("T+3", "T+3")] |
| 137 |
), |
| 138 |
( |
| 139 |
{"year": 500200, "month_of_year": 7, "day_of_month": 28, |
| 140 |
"num_expanded_year_digits": 2, "hour_of_day": 0, |
| 141 |
"hour_of_day_decimal": 0.4356, "time_zone_hour": -8, |
| 142 |
"time_zone_minute": -30}, |
| 143 |
[("+XCCYY-MMDDThhmmZ", "+500200-0728T0856Z"), |
| 144 |
("+XCCYYDDDThh:mm:ss", "+500200209T00:26:08"), |
| 145 |
("WwwD", "W311"), |
| 146 |
("+XCCDDDThh*ss-0600", "+5002209T02*08-0600"), |
| 147 |
("+XCCYY-MM-DDThh:mm:ss-11:45", |
| 148 |
"+500200-07-27T21:11:08-11:45"), |
| 149 |
("+XCCYYMM-DDThhmm-01:00", "+50020007-28T0756-01:00"), |
| 150 |
("+XCCYYMM-DDThhmm+13:00", "+50020007-28T2156+13:00"), |
| 151 |
("+XCCYYMM-DDThhmm-0100", "+50020007-28T0756-0100"), |
| 152 |
("+XCCYYMM-DDThhmm+1300", "+50020007-28T2156+1300"), |
| 153 |
("+XCCYYMMDDThhmm-0100", "+5002000728T0756-0100"), |
| 154 |
("+XCCYYMMDDThhmm+13", "+5002000728T2156+13"), |
| 155 |
("+XCCYYMMDDThh+hhmm", "+5002000728T00-0830"), |
| 156 |
("+XCCYYWwwDThhmm+hh", "+500200W311T0026-08"), |
| 157 |
("+XCCYYDDDThhmm+hh", "+500200209T0026-08"), |
| 158 |
("+XCCYY-MM-DDThh:mm:ss+hh:mm", |
| 159 |
"+500200-07-28T00:26:08-08:30"), |
| 160 |
("+XCCYY-MM-DDThh:mm:ssZ", "+500200-07-28T08:56:08Z"), |
| 161 |
("DD/MM/+XCCYY is a silly format", |
| 162 |
"28/07/+500200 is a silly format"), |
| 163 |
("ThhmmZ", "T0856Z"), |
| 164 |
("%m-%dT%H:%M", "07-28T00:26"), |
| 165 |
("+XCCYY-MM-DDThh,ii", "+500200-07-28T00,4356")] |
| 166 |
), |
| 167 |
( |
| 168 |
{"year": -56, "day_of_year": 318, "num_expanded_year_digits": 2, |
| 169 |
"hour_of_day": 5, "minute_of_hour": 1, "time_zone_hour": 6}, |
| 170 |
[("+XCCYY-MMDDThhmmZ", "-000056-1112T2301Z"), |
| 171 |
("+XCCYYDDDThh:mm:ss", "-000056318T05:01:00"), |
| 172 |
("WwwD", "W461"), |
| 173 |
("+XCCDDDThh*ss-0600", "-0000317T17*00-0600"), |
| 174 |
("+XCCYY-MM-DDThh:mm:ss-11:45", |
| 175 |
"-000056-11-12T11:16:00-11:45"), |
| 176 |
("+XCCYYMM-DDThhmm-01:00", "-00005611-12T2201-01:00"), |
| 177 |
("+XCCYYMM-DDThhmm+13:00", "-00005611-13T1201+13:00"), |
| 178 |
("+XCCYYMM-DDThhmm-0100", "-00005611-12T2201-0100"), |
| 179 |
("+XCCYYMM-DDThhmm+1300", "-00005611-13T1201+1300"), |
| 180 |
("+XCCYYMMDDThhmm-0100", "-0000561112T2201-0100"), |
| 181 |
("+XCCYYMMDDThhmm+13", "-0000561113T1201+13"), |
| 182 |
("+XCCYYMMDDThh+hhmm", "-0000561113T05+0600"), |
| 183 |
("+XCCYYWwwDThhmm+hh", "-000056W461T0501+06"), |
| 184 |
("+XCCYYDDDThhmm+hh", "-000056318T0501+06"), |
| 185 |
("+XCCYY-MM-DDThh:mm:ss+hh:mm", |
| 186 |
"-000056-11-13T05:01:00+06:00"), |
| 187 |
("+XCCYY-MM-DDThh:mm:ssZ", "-000056-11-12T23:01:00Z"), |
| 188 |
("DD/MM/+XCCYY is a silly format", |
| 189 |
"13/11/-000056 is a silly format"), |
| 190 |
("ThhmmZ", "T2301Z"), |
| 191 |
("%m-%dT%H:%M", "11-13T05:01")] |
| 192 |
), |
| 193 |
( |
| 194 |
{"year": 1000, "week_of_year": 1, "day_of_week": 1, |
| 195 |
"time_zone_hour": 0}, |
| 196 |
[("CCYY-MMDDThhmmZ", "0999-1230T0000Z"), |
| 197 |
("CCYY-DDDThhmmZ", "0999-364T0000Z"), |
| 198 |
("CCYY-Www-DThhmm+0200", "1000-W01-1T0200+0200"), |
| 199 |
("CCYY-Www-DThhmm-0200", "0999-W52-7T2200-0200"), |
| 200 |
("%Y-%m-%dT%H:%M", "0999-12-30T00:00")] |
| 201 |
), |
| 202 |
( |
| 203 |
{"year": 999, "day_of_year": 364, "time_zone_hour": 0}, |
| 204 |
[("CCYY-MMDDThhmmZ", "0999-1230T0000Z"), |
| 205 |
("CCYY-DDDThhmmZ", "0999-364T0000Z"), |
| 206 |
("CCYY-Www-DThhmm+0200", "1000-W01-1T0200+0200"), |
| 207 |
("CCYY-Www-DThhmm-0200", "0999-W52-7T2200-0200"), |
| 208 |
("%Y-%m-%dT%H:%M", "0999-12-30T00:00")] |
| 209 |
), |
| 210 |
( |
| 211 |
{"year": 2027, "month_of_year": 12, "day_of_month": 31, |
| 212 |
"minute_of_hour": 59, "minute_of_hour_decimal": 0.99999999}, |
| 213 |
[("CCYY-MM-DDThh:mm,nnZ", "2027-12-31T00:59,999999Z"), |
| 214 |
("CCYY-MM-DDThh:mm.nnZ", "2027-12-31T00:59.999999Z"), |
| 215 |
("Thh:mm:ss,tt", "T00:59:59,999999")] |
| 216 |
) |
| 217 |
] |
| 218 |
|
| 219 |
|
| 220 |
def get_timepointdumper_failure_tests(): |
| 221 |
"""Yield tests that raise exceptions for custom time point dumps.""" |
| 222 |
bounds_error = TimePointDumperBoundsError |
| 223 |
return [ |
| 224 |
( |
| 225 |
{"year": 10000, "month_of_year": 1, "day_of_month": 4, |
| 226 |
"time_zone_hour": 0, "time_zone_minute": 0}, |
| 227 |
[("CCYY-MMDDThhmmZ", bounds_error, 0), |
| 228 |
("%Y-%m-%dT%H:%M", bounds_error, 0)] |
| 229 |
), |
| 230 |
( |
| 231 |
{"year": -10000, "month_of_year": 1, "day_of_month": 4, |
| 232 |
"time_zone_hour": 0, "time_zone_minute": 0}, |
| 233 |
[("CCYY-MMDDThhmmZ", bounds_error, 0), |
| 234 |
("%Y-%m-%dT%H:%M", bounds_error, 0)] |
| 235 |
), |
| 236 |
( |
| 237 |
{"year": 10000, "month_of_year": 1, "day_of_month": 4, |
| 238 |
"time_zone_hour": 0, "time_zone_minute": 0}, |
| 239 |
[("CCYY-MMDDThhmmZ", bounds_error, 2)] |
| 240 |
), |
| 241 |
( |
| 242 |
{"year": -10000, "month_of_year": 1, "day_of_month": 4, |
| 243 |
"time_zone_hour": 0, "time_zone_minute": 0}, |
| 244 |
[("CCYY-MMDDThhmmZ", bounds_error, 2)] |
| 245 |
), |
| 246 |
( |
| 247 |
{"year": 1000000, "month_of_year": 1, "day_of_month": 4, |
| 248 |
"time_zone_hour": 0, "time_zone_minute": 0}, |
| 249 |
[("+XCCYY-MMDDThhmmZ", bounds_error, 2)] |
| 250 |
), |
| 251 |
( |
| 252 |
{"year": -1000000, "month_of_year": 1, "day_of_month": 4, |
| 253 |
"time_zone_hour": 0, "time_zone_minute": 0}, |
| 254 |
[("+XCCYY-MMDDThhmmZ", bounds_error, 2)] |
| 255 |
) |
| 256 |
] |
| 257 |
|
| 258 |
|
| 259 |
def get_timepointparser_tests(allow_only_basic=False, |
| 260 |
allow_truncated=False, |
| 261 |
skip_time_zones=False): |
| 262 |
"""Yield tests for the time point parser.""" |
| 263 |
|
| 264 |
test_date_map = { |
| 265 |
"basic": { |
| 266 |
"complete": { |
| 267 |
"00440104": {"year": 44, "month_of_year": 1, |
| 268 |
"day_of_month": 4}, |
| 269 |
"+5002000830": {"year": 500200, "month_of_year": 8, |
| 270 |
"day_of_month": 30, |
| 271 |
"num_expanded_year_digits": 2}, |
| 272 |
"-0000561113": {"year": -56, "month_of_year": 11, |
| 273 |
"day_of_month": 13, |
| 274 |
"num_expanded_year_digits": 2}, |
| 275 |
"-1000240210": {"year": -100024, "month_of_year": 2, |
| 276 |
"day_of_month": 10, |
| 277 |
"num_expanded_year_digits": 2}, |
| 278 |
"1967056": {"year": 1967, "day_of_year": 56}, |
| 279 |
"+123456078": {"year": 123456, "day_of_year": 78, |
| 280 |
"num_expanded_year_digits": 2}, |
| 281 |
"-004560134": {"year": -4560, "day_of_year": 134, |
| 282 |
"num_expanded_year_digits": 2}, |
| 283 |
"1001W011": {"year": 1001, "week_of_year": 1, |
| 284 |
"day_of_week": 1}, |
| 285 |
"+000001W457": {"year": 1, "week_of_year": 45, |
| 286 |
"day_of_week": 7, |
| 287 |
"num_expanded_year_digits": 2}, |
| 288 |
"-010001W053": {"year": -10001, "week_of_year": 5, |
| 289 |
"day_of_week": 3, |
| 290 |
"num_expanded_year_digits": 2} |
| 291 |
}, |
| 292 |
"reduced": { |
| 293 |
"4401-03": {"year": 4401, "month_of_year": 3}, |
| 294 |
"1982": {"year": 1982}, |
| 295 |
"19": {"year": 1900}, |
| 296 |
"+056789-01": {"year": 56789, "month_of_year": 1, |
| 297 |
"num_expanded_year_digits": 2}, |
| 298 |
"-000001-12": {"year": -1, "month_of_year": 12, |
| 299 |
"num_expanded_year_digits": 2}, |
| 300 |
"-789123": {"year": -789123, "num_expanded_year_digits": 2}, |
| 301 |
"+450001": {"year": 450001, "num_expanded_year_digits": 2}, |
| 302 |
|
| 303 |
|
| 304 |
"+5678": {"year": 567800, "num_expanded_year_digits": 2}, |
| 305 |
"1765W04": {"year": 1765, "week_of_year": 4}, |
| 306 |
"+001765W44": {"year": 1765, "week_of_year": 44, |
| 307 |
"num_expanded_year_digits": 2}, |
| 308 |
"-123321W50": {"year": -123321, "week_of_year": 50, |
| 309 |
"num_expanded_year_digits": 2} |
| 310 |
}, |
| 311 |
"truncated": { |
| 312 |
"-9001": {"year": 90, "month_of_year": 1, |
| 313 |
"truncated": True, |
| 314 |
"truncated_property": "year_of_century"}, |
| 315 |
"960328": {"year": 96, "month_of_year": 3, |
| 316 |
"day_of_month": 28, |
| 317 |
"truncated": True, |
| 318 |
"truncated_property": "year_of_century"}, |
| 319 |
"-90": {"year": 90, "truncated": True, |
| 320 |
"truncated_property": "year_of_century"}, |
| 321 |
"--0501": {"month_of_year": 5, "day_of_month": 1, |
| 322 |
"truncated": True}, |
| 323 |
"--12": {"month_of_year": 12, "truncated": True}, |
| 324 |
"---30": {"day_of_month": 30, "truncated": True}, |
| 325 |
"98354": {"year": 98, "day_of_year": 354, "truncated": True, |
| 326 |
"truncated_property": "year_of_century"}, |
| 327 |
"-034": {"day_of_year": 34, "truncated": True}, |
| 328 |
"00W031": {"year": 0, "week_of_year": 3, "day_of_week": 1, |
| 329 |
"truncated": True, |
| 330 |
"truncated_property": "year_of_century"}, |
| 331 |
"99W34": {"year": 99, "week_of_year": 34, "truncated": True, |
| 332 |
"truncated_property": "year_of_century"}, |
| 333 |
"-1W02": {"year": 1, "week_of_year": 2, |
| 334 |
"truncated": True, |
| 335 |
"truncated_property": "year_of_decade"}, |
| 336 |
"-W031": {"week_of_year": 3, "day_of_week": 1, |
| 337 |
"truncated": True}, |
| 338 |
"-W32": {"week_of_year": 32, "truncated": True}, |
| 339 |
"-W-1": {"day_of_week": 1, "truncated": True} |
| 340 |
} |
| 341 |
}, |
| 342 |
"extended": { |
| 343 |
"complete": { |
| 344 |
"0044-01-04": {"year": 44, "month_of_year": 1, |
| 345 |
"day_of_month": 4}, |
| 346 |
"+500200-08-30": {"year": 500200, "month_of_year": 8, |
| 347 |
"day_of_month": 30, |
| 348 |
"num_expanded_year_digits": 2}, |
| 349 |
"-000056-11-13": {"year": -56, "month_of_year": 11, |
| 350 |
"day_of_month": 13, |
| 351 |
"num_expanded_year_digits": 2}, |
| 352 |
"-100024-02-10": {"year": -100024, "month_of_year": 2, |
| 353 |
"day_of_month": 10, |
| 354 |
"num_expanded_year_digits": 2}, |
| 355 |
"1967-056": {"year": 1967, "day_of_year": 56}, |
| 356 |
"+123456-078": {"year": 123456, "day_of_year": 78, |
| 357 |
"num_expanded_year_digits": 2}, |
| 358 |
"-004560-134": {"year": -4560, "day_of_year": 134, |
| 359 |
"num_expanded_year_digits": 2}, |
| 360 |
"1001-W01-1": {"year": 1001, "week_of_year": 1, |
| 361 |
"day_of_week": 1}, |
| 362 |
"+000001-W45-7": {"year": 1, "week_of_year": 45, |
| 363 |
"day_of_week": 7, |
| 364 |
"num_expanded_year_digits": 2}, |
| 365 |
"-010001-W05-3": {"year": -10001, "week_of_year": 5, |
| 366 |
"day_of_week": 3, |
| 367 |
"num_expanded_year_digits": 2} |
| 368 |
}, |
| 369 |
"reduced": { |
| 370 |
"4401-03": {"year": 4401, "month_of_year": 3}, |
| 371 |
"1982": {"year": 1982}, |
| 372 |
"19": {"year": 1900}, |
| 373 |
"+056789-01": {"year": 56789, "month_of_year": 1, |
| 374 |
"num_expanded_year_digits": 2}, |
| 375 |
"-000001-12": {"year": -1, "month_of_year": 12, |
| 376 |
"num_expanded_year_digits": 2}, |
| 377 |
"-789123": {"year": -789123, "num_expanded_year_digits": 2}, |
| 378 |
"+450001": {"year": 450001, "num_expanded_year_digits": 2}, |
| 379 |
|
| 380 |
|
| 381 |
"+5678": {"year": 567800, "num_expanded_year_digits": 2}, |
| 382 |
"1765-W04": {"year": 1765, "week_of_year": 4}, |
| 383 |
"+001765-W44": {"year": 1765, "week_of_year": 44, |
| 384 |
"num_expanded_year_digits": 2}, |
| 385 |
"-123321-W50": {"year": -123321, "week_of_year": 50, |
| 386 |
"num_expanded_year_digits": 2} |
| 387 |
}, |
| 388 |
"truncated": { |
| 389 |
"-9001": {"year": 90, "month_of_year": 1, |
| 390 |
"truncated": True, |
| 391 |
"truncated_property": "year_of_century"}, |
| 392 |
"96-03-28": {"year": 96, "month_of_year": 3, |
| 393 |
"day_of_month": 28, |
| 394 |
"truncated": True, |
| 395 |
"truncated_property": "year_of_century"}, |
| 396 |
"-90": {"year": 90, "truncated": True, |
| 397 |
"truncated_property": "year_of_century"}, |
| 398 |
"--05-01": {"month_of_year": 5, "day_of_month": 1, |
| 399 |
"truncated": True}, |
| 400 |
"--12": {"month_of_year": 12, "truncated": True}, |
| 401 |
"---30": {"day_of_month": 30, "truncated": True}, |
| 402 |
"98-354": {"year": 98, "day_of_year": 354, "truncated": True, |
| 403 |
"truncated_property": "year_of_century"}, |
| 404 |
"-034": {"day_of_year": 34, "truncated": True}, |
| 405 |
"00-W03-1": {"year": 0, "week_of_year": 3, "day_of_week": 1, |
| 406 |
"truncated": True, |
| 407 |
"truncated_property": "year_of_century"}, |
| 408 |
"99-W34": {"year": 99, "week_of_year": 34, "truncated": True, |
| 409 |
"truncated_property": "year_of_century"}, |
| 410 |
"-1-W02": {"year": 1, "week_of_year": 2, |
| 411 |
"truncated": True, |
| 412 |
"truncated_property": "year_of_decade"}, |
| 413 |
"-W03-1": {"week_of_year": 3, "day_of_week": 1, |
| 414 |
"truncated": True}, |
| 415 |
"-W32": {"week_of_year": 32, "truncated": True}, |
| 416 |
"-W-1": {"day_of_week": 1, "truncated": True} |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
test_time_map = { |
| 421 |
"basic": { |
| 422 |
"complete": { |
| 423 |
"050102": {"hour_of_day": 5, "minute_of_hour": 1, |
| 424 |
"second_of_minute": 2}, |
| 425 |
"235902,345": {"hour_of_day": 23, "minute_of_hour": 59, |
| 426 |
"second_of_minute": 2, |
| 427 |
"second_of_minute_decimal": 0.345}, |
| 428 |
"235902.345": {"hour_of_day": 23, "minute_of_hour": 59, |
| 429 |
"second_of_minute": 2, |
| 430 |
"second_of_minute_decimal": 0.345}, |
| 431 |
"1201,4": {"hour_of_day": 12, "minute_of_hour": 1, |
| 432 |
"minute_of_hour_decimal": 0.4}, |
| 433 |
"1201.4": {"hour_of_day": 12, "minute_of_hour": 1, |
| 434 |
"minute_of_hour_decimal": 0.4}, |
| 435 |
"00,4356": {"hour_of_day": 0, |
| 436 |
"hour_of_day_decimal": 0.4356}, |
| 437 |
"00.4356": {"hour_of_day": 0, |
| 438 |
"hour_of_day_decimal": 0.4356} |
| 439 |
}, |
| 440 |
"reduced": { |
| 441 |
"0203": {"hour_of_day": 2, "minute_of_hour": 3}, |
| 442 |
"17": {"hour_of_day": 17} |
| 443 |
}, |
| 444 |
"truncated": { |
| 445 |
"-5612": {"minute_of_hour": 56, "second_of_minute": 12, |
| 446 |
"truncated": True}, |
| 447 |
"-12": {"minute_of_hour": 12, "truncated": True}, |
| 448 |
"--45": {"second_of_minute": 45, "truncated": True}, |
| 449 |
"-1234,45": {"minute_of_hour": 12, "second_of_minute": 34, |
| 450 |
"second_of_minute_decimal": 0.45, |
| 451 |
"truncated": True}, |
| 452 |
"-1234.45": {"minute_of_hour": 12, "second_of_minute": 34, |
| 453 |
"second_of_minute_decimal": 0.45, |
| 454 |
"truncated": True}, |
| 455 |
"-34,2": {"minute_of_hour": 34, "minute_of_hour_decimal": 0.2, |
| 456 |
"truncated": True}, |
| 457 |
"-34.2": {"minute_of_hour": 34, "minute_of_hour_decimal": 0.2, |
| 458 |
"truncated": True}, |
| 459 |
"--59,99": {"second_of_minute": 59, |
| 460 |
"second_of_minute_decimal": 0.99, |
| 461 |
"truncated": True}, |
| 462 |
"--59.99": {"second_of_minute": 59, |
| 463 |
"second_of_minute_decimal": 0.99, |
| 464 |
"truncated": True} |
| 465 |
} |
| 466 |
}, |
| 467 |
"extended": { |
| 468 |
"complete": { |
| 469 |
"05:01:02": {"hour_of_day": 5, "minute_of_hour": 1, |
| 470 |
"second_of_minute": 2}, |
| 471 |
"23:59:02,345": {"hour_of_day": 23, "minute_of_hour": 59, |
| 472 |
"second_of_minute": 2, |
| 473 |
"second_of_minute_decimal": 0.345}, |
| 474 |
"23:59:02.345": {"hour_of_day": 23, "minute_of_hour": 59, |
| 475 |
"second_of_minute": 2, |
| 476 |
"second_of_minute_decimal": 0.345}, |
| 477 |
"12:01,4": {"hour_of_day": 12, "minute_of_hour": 1, |
| 478 |
"minute_of_hour_decimal": 0.4}, |
| 479 |
"12:01.4": {"hour_of_day": 12, "minute_of_hour": 1, |
| 480 |
"minute_of_hour_decimal": 0.4}, |
| 481 |
"00,4356": {"hour_of_day": 0, "hour_of_day_decimal": 0.4356}, |
| 482 |
"00.4356": {"hour_of_day": 0, "hour_of_day_decimal": 0.4356} |
| 483 |
}, |
| 484 |
"reduced": { |
| 485 |
"02:03": {"hour_of_day": 2, "minute_of_hour": 3}, |
| 486 |
"17": {"hour_of_day": 17} |
| 487 |
}, |
| 488 |
"truncated": { |
| 489 |
"-56:12": {"minute_of_hour": 56, "second_of_minute": 12, |
| 490 |
"truncated": True}, |
| 491 |
"-12": {"minute_of_hour": 12, "truncated": True}, |
| 492 |
"--45": {"second_of_minute": 45, "truncated": True}, |
| 493 |
"-12:34,45": {"minute_of_hour": 12, "second_of_minute": 34, |
| 494 |
"second_of_minute_decimal": 0.45, |
| 495 |
"truncated": True}, |
| 496 |
"-12:34.45": {"minute_of_hour": 12, "second_of_minute": 34, |
| 497 |
"second_of_minute_decimal": 0.45, |
| 498 |
"truncated": True}, |
| 499 |
"-34,2": {"minute_of_hour": 34, "minute_of_hour_decimal": 0.2, |
| 500 |
"truncated": True}, |
| 501 |
"-34.2": {"minute_of_hour": 34, "minute_of_hour_decimal": 0.2, |
| 502 |
"truncated": True}, |
| 503 |
"--59,99": {"second_of_minute": 59, |
| 504 |
"second_of_minute_decimal": 0.99, |
| 505 |
"truncated": True}, |
| 506 |
"--59.99": {"second_of_minute": 59, |
| 507 |
"second_of_minute_decimal": 0.99, |
| 508 |
"truncated": True} |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
test_time_zone_map = { |
| 513 |
"basic": { |
| 514 |
"Z": {"time_zone_hour": 0, "time_zone_minute": 0}, |
| 515 |
"+01": {"time_zone_hour": 1}, |
| 516 |
"-05": {"time_zone_hour": -5}, |
| 517 |
"+2301": {"time_zone_hour": 23, "time_zone_minute": 1}, |
| 518 |
"-1230": {"time_zone_hour": -12, "time_zone_minute": -30} |
| 519 |
}, |
| 520 |
"extended": { |
| 521 |
"Z": {"time_zone_hour": 0, "time_zone_minute": 0}, |
| 522 |
"+01": {"time_zone_hour": 1}, |
| 523 |
"-05": {"time_zone_hour": -5}, |
| 524 |
"+23:01": {"time_zone_hour": 23, "time_zone_minute": 1}, |
| 525 |
"-12:30": {"time_zone_hour": -12, "time_zone_minute": -30} |
| 526 |
} |
| 527 |
} |
| 528 |
format_ok_keys = ["basic", "extended"] |
| 529 |
if allow_only_basic: |
| 530 |
format_ok_keys = ["basic"] |
| 531 |
date_combo_ok_keys = ["complete"] |
| 532 |
if allow_truncated: |
| 533 |
date_combo_ok_keys = ["complete", "truncated"] |
| 534 |
time_combo_ok_keys = ["complete", "reduced"] |
| 535 |
time_designator = parser_spec.TIME_DESIGNATOR |
| 536 |
for format_type in format_ok_keys: |
| 537 |
date_format_tests = test_date_map[format_type] |
| 538 |
time_format_tests = test_time_map[format_type] |
| 539 |
time_zone_format_tests = test_time_zone_map[format_type] |
| 540 |
for date_key in date_format_tests: |
| 541 |
if not allow_truncated and date_key == "truncated": |
| 542 |
continue |
| 543 |
for date_expr, info in date_format_tests[date_key].items(): |
| 544 |
yield date_expr, info |
| 545 |
for date_key in date_combo_ok_keys: |
| 546 |
date_tests = date_format_tests[date_key] |
| 547 |
|
| 548 |
for date_expr, info in date_tests.items(): |
| 549 |
for time_key in time_combo_ok_keys: |
| 550 |
time_items = time_format_tests[time_key].items() |
| 551 |
for time_expr, time_info in time_items: |
| 552 |
combo_expr = ( |
| 553 |
date_expr + |
| 554 |
time_designator + |
| 555 |
time_expr |
| 556 |
) |
| 557 |
combo_info = {} |
| 558 |
for key, value in chain( |
| 559 |
info.items(), time_info.items()): |
| 560 |
combo_info[key] = value |
| 561 |
yield combo_expr, combo_info |
| 562 |
if skip_time_zones: |
| 563 |
continue |
| 564 |
time_zone_items = time_zone_format_tests.items() |
| 565 |
for time_zone_expr, time_zone_info in time_zone_items: |
| 566 |
tz_expr = combo_expr + time_zone_expr |
| 567 |
tz_info = {} |
| 568 |
for key, value in \ |
| 569 |
chain(combo_info.items(), |
| 570 |
time_zone_info.items()): |
| 571 |
tz_info[key] = value |
| 572 |
yield tz_expr, tz_info |
| 573 |
if not allow_truncated: |
| 574 |
continue |
| 575 |
for time_key in time_format_tests: |
| 576 |
time_tests = time_format_tests[time_key] |
| 577 |
for time_expr, time_info in time_tests.items(): |
| 578 |
combo_expr = ( |
| 579 |
time_designator + |
| 580 |
time_expr |
| 581 |
) |
| 582 |
|
| 583 |
combo_info = {"truncated": True} |
| 584 |
for key, value in time_info.items(): |
| 585 |
combo_info[key] = value |
| 586 |
yield combo_expr, combo_info |
| 587 |
if skip_time_zones: |
| 588 |
continue |
| 589 |
time_zone_items = time_zone_format_tests.items() |
| 590 |
for time_zone_expr, time_zone_info in time_zone_items: |
| 591 |
tz_expr = combo_expr + time_zone_expr |
| 592 |
tz_info = {} |
| 593 |
for key, value in \ |
| 594 |
chain(combo_info.items(), |
| 595 |
time_zone_info.items()): |
| 596 |
tz_info[key] = value |
| 597 |
yield tz_expr, tz_info |
| 598 |
|
| 599 |
|
| 600 |
def get_truncated_property_tests(): |
| 601 |
"""Tests for largest truncated and smallest missing property names.""" |
| 602 |
test_timepoints = { |
| 603 |
"-9001": {"year": 90, |
| 604 |
"month_of_year": 1, |
| 605 |
"largest_truncated_property_name": "year_of_century", |
| 606 |
"smallest_missing_property_name": "century"}, |
| 607 |
"20960328": {"year": 96, |
| 608 |
"month_of_year": 3, |
| 609 |
"day_of_month": 28, |
| 610 |
"largest_truncated_property_name": None, |
| 611 |
"smallest_missing_property_name": None}, |
| 612 |
"-90": {"year": 90, |
| 613 |
"largest_truncated_property_name": "year_of_century", |
| 614 |
"smallest_missing_property_name": "century"}, |
| 615 |
"--0501": {"month_of_year": 5, "day_of_month": 1, |
| 616 |
"largest_truncated_property_name": "month_of_year", |
| 617 |
"smallest_missing_property_name": "year_of_century"}, |
| 618 |
"--12": {"month_of_year": 12, |
| 619 |
"largest_truncated_property_name": "month_of_year", |
| 620 |
"smallest_missing_property_name": "year_of_century"}, |
| 621 |
"---30": {"day_of_month": 30, |
| 622 |
"largest_truncated_property_name": "day_of_month", |
| 623 |
"smallest_missing_property_name": "month_of_year"}, |
| 624 |
"98354": {"year": 98, |
| 625 |
"day_of_year": 354, |
| 626 |
"largest_truncated_property_name": "year_of_century", |
| 627 |
"smallest_missing_property_name": "century"}, |
| 628 |
"-034": {"day_of_year": 34, |
| 629 |
"largest_truncated_property_name": "day_of_year", |
| 630 |
"smallest_missing_property_name": "year_of_century"}, |
| 631 |
"00W031": {"year": 0, |
| 632 |
"week_of_year": 3, |
| 633 |
"day_of_week": 1, |
| 634 |
"largest_truncated_property_name": "year_of_century", |
| 635 |
"smallest_missing_property_name": "century"}, |
| 636 |
"99W34": {"year": 99, |
| 637 |
"week_of_year": 34, |
| 638 |
"largest_truncated_property_name": "year_of_century", |
| 639 |
"smallest_missing_property_name": "century"}, |
| 640 |
"-1W02": {"year": 1, |
| 641 |
"week_of_year": 2, |
| 642 |
"largest_truncated_property_name": "year_of_decade", |
| 643 |
"smallest_missing_property_name": "decade_of_century"}, |
| 644 |
"-W031": {"week_of_year": 3, |
| 645 |
"day_of_week": 1, |
| 646 |
"largest_truncated_property_name": "week_of_year", |
| 647 |
"smallest_missing_property_name": "year_of_century"}, |
| 648 |
"-W32": {"week_of_year": 32, |
| 649 |
"largest_truncated_property_name": "week_of_year", |
| 650 |
"smallest_missing_property_name": "year_of_century"}, |
| 651 |
"-W-1": {"day_of_week": 1, |
| 652 |
"largest_truncated_property_name": "day_of_week", |
| 653 |
"smallest_missing_property_name": "week_of_year"}, |
| 654 |
"T04:30": {"hour_of_day": 4, |
| 655 |
"minute_of_hour": 30, |
| 656 |
"largest_truncated_property_name": "hour_of_day", |
| 657 |
"smallest_missing_property_name": "day_of_month"}, |
| 658 |
"T19": {"hour_of_day": 19, |
| 659 |
"largest_truncated_property_name": "hour_of_day", |
| 660 |
"smallest_missing_property_name": "day_of_month"}, |
| 661 |
"T-56:12": {"minute_of_hour": 56, |
| 662 |
"second_of_minute": 12, |
| 663 |
"largest_truncated_property_name": "minute_of_hour", |
| 664 |
"smallest_missing_property_name": "hour_of_day"}, |
| 665 |
"T-12": {"minute_of_hour": 12, |
| 666 |
"largest_truncated_property_name": "minute_of_hour", |
| 667 |
"smallest_missing_property_name": "hour_of_day"}, |
| 668 |
"T--45": {"second_of_minute": 45, |
| 669 |
"largest_truncated_property_name": "second_of_minute", |
| 670 |
"smallest_missing_property_name": "minute_of_hour"}, |
| 671 |
"T-12:34.45": {"minute_of_hour": 12, |
| 672 |
"second_of_minute": 34, |
| 673 |
"second_of_minute_decimal": 0.45, |
| 674 |
"largest_truncated_property_name": "minute_of_hour", |
| 675 |
"smallest_missing_property_name": "hour_of_day"}, |
| 676 |
"T-34,2": {"minute_of_hour": 34, |
| 677 |
"minute_of_hour_decimal": 0.2, |
| 678 |
"largest_truncated_property_name": "minute_of_hour", |
| 679 |
"smallest_missing_property_name": "hour_of_day"}, |
| 680 |
"T--59.99": {"second_of_minute": 59, |
| 681 |
"second_of_minute_decimal": 0.99, |
| 682 |
"largest_truncated_property_name": "second_of_minute", |
| 683 |
"smallest_missing_property_name": "minute_of_hour"}} |
| 684 |
return test_timepoints |
| 685 |
|
| 686 |
|
| 687 |
def get_timerecurrence_expansion_tests(): |
| 688 |
"""Return test expansion expressions for data.TimeRecurrence. |
| 689 |
|
| 690 |
If no. of repetitions is unbounded, will test the first three. |
| 691 |
""" |
| 692 |
return [ |
| 693 |
("R5/2020-01-01T00:00:00Z/2020-01-05T00:00:00Z", |
| 694 |
["2020-01-01T00:00:00Z", "2020-01-05T00:00:00Z", |
| 695 |
"2020-01-09T00:00:00Z", "2020-01-13T00:00:00Z", |
| 696 |
"2020-01-17T00:00:00Z"]), |
| 697 |
("R3/1001-W01-1T00:00:00Z/1002-W52-6T00:00:00-05:30", |
| 698 |
["1001-W01-1T00:00:00Z", "1002-W52-6T05:30:00Z", |
| 699 |
"1005-W01-4T11:00:00Z"]), |
| 700 |
("R3/P700D/1957-W01-1T06,5Z", |
| 701 |
["1953-W10-1T06,5Z", "1955-W05-1T06,5Z", "1957-W01-1T06,5Z"]), |
| 702 |
("R3/P5DT2,5S/1001-W11-1T00:30:02,5-02:00", |
| 703 |
["1001-W09-5T00:29:57,5-02:00", "1001-W10-3T00:30:00-02:00", |
| 704 |
"1001-W11-1T00:30:02,5-02:00"]), |
| 705 |
("R/+000001W457T060000Z/P4M1D", |
| 706 |
["+000001-W45-7T06:00:00Z", "+000002-W11-2T06:00:00Z", |
| 707 |
"+000002-W28-6T06:00:00Z"]), |
| 708 |
("R/P4M1DT6M/+002302-002T06:00:00-00:30", |
| 709 |
["+002302-002T06:00:00-00:30", "+002301-244T05:54:00-00:30", |
| 710 |
"+002301-120T05:48:00-00:30"]), |
| 711 |
("R/P30Y2DT15H/-099994-02-12T17:00:00-02:30", |
| 712 |
["-099994-02-12T17:00:00-02:30", "-100024-02-10T02:00:00-02:30", |
| 713 |
"-100054-02-07T11:00:00-02:30"]), |
| 714 |
("R/-100024-02-10T17:00:00-12:30/PT5.5H", |
| 715 |
["-100024-02-10T17:00:00-12:30", "-100024-02-10T22:30:00-12:30", |
| 716 |
"-100024-02-11T04:00:00-12:30"]) |
| 717 |
] |
| 718 |
|
| 719 |
|
| 720 |
def get_timerecurrence_comparison_tests(): |
| 721 |
"""Yield tests for executing the '==' operator and hash() on |
| 722 |
TimeRecurrences.""" |
| 723 |
return [ |
| 724 |
("R5/2020-036T00Z/PT15M", "R5/2020-036T00Z/PT15M", True), |
| 725 |
*[("R5/2020-036T00Z/PT15M", rhs, False) for rhs in ( |
| 726 |
"R4/2020-036T00Z/PT15M", |
| 727 |
"R5/2020-036T01Z/PT15M", |
| 728 |
"R5/2020-036T00Z/PT14M")], |
| 729 |
|
| 730 |
("R4/2020-01-01T00Z/P1D", "R4/P1D/2020-01-04T00Z", True), |
| 731 |
("R/2020-01-01T00Z/P1D", "R/P1D/2020-01-04T00Z", False), |
| 732 |
|
| 733 |
("R5/2020-001T00Z/2020-005T00Z", "R5/2020-001T00Z/P4D", True), |
| 734 |
("R/2020-02-07T09Z/2020-02-07T10Z", "R/2020-02-07T09Z/PT1H", True), |
| 735 |
|
| 736 |
("R5/2020-001T00Z/2020-005T00Z", "R5/P4D/2020-005T00Z", False), |
| 737 |
("R5/2020-001T00Z/2020-005T00Z", "R5/P4D/2020-017T00Z", True), |
| 738 |
("R/2020-02-07T09Z/2020-02-07T10Z", "R/PT1H/2020-02-07T10Z", False), |
| 739 |
|
| 740 |
("R3/2020-05-01T16Z/PT3H", "R3/2020-W18-5T18:30+02:30/PT180M", True), |
| 741 |
|
| 742 |
("R27/2020-10-08T00Z/2020-10-08T04+04", |
| 743 |
"R1/2020-10-08T00Z/2020-10-08T00Z", True) |
| 744 |
] |
| 745 |
|
| 746 |
|
| 747 |
def get_timerecurrence_expansion_tests_for_alt_calendar(calendar_mode): |
| 748 |
"""Return alternate calendar tests for data.TimeRecurrence.""" |
| 749 |
if calendar_mode == "360": |
| 750 |
return get_timerecurrence_expansion_tests_360() |
| 751 |
if calendar_mode == "365": |
| 752 |
return get_timerecurrence_expansion_tests_365() |
| 753 |
if calendar_mode == "366": |
| 754 |
return get_timerecurrence_expansion_tests_366() |
| 755 |
|
| 756 |
|
| 757 |
def get_timerecurrence_expansion_tests_360(): |
| 758 |
"""Return test expansion expressions for data.TimeRecurrence.""" |
| 759 |
return [ |
| 760 |
("R13/1984-01-30T00Z/P1M", |
| 761 |
["1984-01-30T00:00:00Z", "1984-02-30T00:00:00Z", |
| 762 |
"1984-03-30T00:00:00Z", "1984-04-30T00:00:00Z", |
| 763 |
"1984-05-30T00:00:00Z", "1984-06-30T00:00:00Z", |
| 764 |
"1984-07-30T00:00:00Z", "1984-08-30T00:00:00Z", |
| 765 |
"1984-09-30T00:00:00Z", "1984-10-30T00:00:00Z", |
| 766 |
"1984-11-30T00:00:00Z", "1984-12-30T00:00:00Z", |
| 767 |
"1985-01-30T00:00:00Z"]), |
| 768 |
("R2/1984-01-30T00Z/P1D", |
| 769 |
["1984-01-30T00:00:00Z", "1984-02-01T00:00:00Z"]), |
| 770 |
("R2/P1D/1984-02-01T00Z", |
| 771 |
["1984-01-30T00:00:00Z", "1984-02-01T00:00:00Z"]), |
| 772 |
("R2/P1D/1984-01-01T00Z", |
| 773 |
["1983-12-30T00:00:00Z", "1984-01-01T00:00:00Z"]), |
| 774 |
("R2/1983-12-30T00Z/P1D", |
| 775 |
["1983-12-30T00:00:00Z", "1984-01-01T00:00:00Z"]), |
| 776 |
("R2/P1D/2005-01-01T00Z", |
| 777 |
["2004-12-30T00:00:00Z", "2005-01-01T00:00:00Z"]), |
| 778 |
("R2/2003-12-30T00Z/P1D", |
| 779 |
["2003-12-30T00:00:00Z", "2004-01-01T00:00:00Z"]), |
| 780 |
("R2/P1D/2004-01-01T00Z", |
| 781 |
["2003-12-30T00:00:00Z", "2004-01-01T00:00:00Z"]), |
| 782 |
("R2/2004-12-30T00Z/P1D", |
| 783 |
["2004-12-30T00:00:00Z", "2005-01-01T00:00:00Z"]), |
| 784 |
("R3/P1Y/2005-02-30T00Z", |
| 785 |
["2003-02-30T00:00:00Z", "2004-02-30T00:00:00Z", |
| 786 |
"2005-02-30T00:00:00Z"]), |
| 787 |
("R3/2003-02-30T00Z/P1Y", |
| 788 |
["2003-02-30T00:00:00Z", "2004-02-30T00:00:00Z", |
| 789 |
"2005-02-30T00:00:00Z"]), |
| 790 |
] |
| 791 |
|
| 792 |
|
| 793 |
def get_timerecurrence_expansion_tests_365(): |
| 794 |
"""Return test expansion expressions for data.TimeRecurrence.""" |
| 795 |
return [ |
| 796 |
("R13/1984-01-30T00Z/P1M", |
| 797 |
["1984-01-30T00:00:00Z", "1984-02-28T00:00:00Z", |
| 798 |
"1984-03-28T00:00:00Z", "1984-04-28T00:00:00Z", |
| 799 |
"1984-05-28T00:00:00Z", "1984-06-28T00:00:00Z", |
| 800 |
"1984-07-28T00:00:00Z", "1984-08-28T00:00:00Z", |
| 801 |
"1984-09-28T00:00:00Z", "1984-10-28T00:00:00Z", |
| 802 |
"1984-11-28T00:00:00Z", "1984-12-28T00:00:00Z", |
| 803 |
"1985-01-28T00:00:00Z"]), |
| 804 |
("R13/1985-01-30T00Z/P1M", |
| 805 |
["1985-01-30T00:00:00Z", "1985-02-28T00:00:00Z", |
| 806 |
"1985-03-28T00:00:00Z", "1985-04-28T00:00:00Z", |
| 807 |
"1985-05-28T00:00:00Z", "1985-06-28T00:00:00Z", |
| 808 |
"1985-07-28T00:00:00Z", "1985-08-28T00:00:00Z", |
| 809 |
"1985-09-28T00:00:00Z", "1985-10-28T00:00:00Z", |
| 810 |
"1985-11-28T00:00:00Z", "1985-12-28T00:00:00Z", |
| 811 |
"1986-01-28T00:00:00Z"]), |
| 812 |
("R2/1984-01-30T00Z/P1D", |
| 813 |
["1984-01-30T00:00:00Z", "1984-01-31T00:00:00Z"]), |
| 814 |
("R2/P1D/1984-02-01T00Z", |
| 815 |
["1984-01-31T00:00:00Z", "1984-02-01T00:00:00Z"]), |
| 816 |
("R2/P1D/1984-01-01T00Z", |
| 817 |
["1983-12-31T00:00:00Z", "1984-01-01T00:00:00Z"]), |
| 818 |
("R2/1983-12-30T00Z/P1D", |
| 819 |
["1983-12-30T00:00:00Z", "1983-12-31T00:00:00Z"]), |
| 820 |
("R2/2000-02-28T00Z/P1Y1D", |
| 821 |
["2000-02-28T00:00:00Z", "2001-03-01T00:00:00Z"]), |
| 822 |
("R2/2001-02-28T00Z/P1Y1D", |
| 823 |
["2001-02-28T00:00:00Z", "2002-03-01T00:00:00Z"]), |
| 824 |
] |
| 825 |
|
| 826 |
|
| 827 |
def get_timerecurrence_expansion_tests_366(): |
| 828 |
"""Return test expansion expressions for data.TimeRecurrence.""" |
| 829 |
return [ |
| 830 |
("R13/1984-01-30T00Z/P1M", |
| 831 |
["1984-01-30T00:00:00Z", "1984-02-29T00:00:00Z", |
| 832 |
"1984-03-29T00:00:00Z", "1984-04-29T00:00:00Z", |
| 833 |
"1984-05-29T00:00:00Z", "1984-06-29T00:00:00Z", |
| 834 |
"1984-07-29T00:00:00Z", "1984-08-29T00:00:00Z", |
| 835 |
"1984-09-29T00:00:00Z", "1984-10-29T00:00:00Z", |
| 836 |
"1984-11-29T00:00:00Z", "1984-12-29T00:00:00Z", |
| 837 |
"1985-01-29T00:00:00Z"]), |
| 838 |
("R13/1985-01-30T00Z/P1M", |
| 839 |
["1985-01-30T00:00:00Z", "1985-02-29T00:00:00Z", |
| 840 |
"1985-03-29T00:00:00Z", "1985-04-29T00:00:00Z", |
| 841 |
"1985-05-29T00:00:00Z", "1985-06-29T00:00:00Z", |
| 842 |
"1985-07-29T00:00:00Z", "1985-08-29T00:00:00Z", |
| 843 |
"1985-09-29T00:00:00Z", "1985-10-29T00:00:00Z", |
| 844 |
"1985-11-29T00:00:00Z", "1985-12-29T00:00:00Z", |
| 845 |
"1986-01-29T00:00:00Z"]), |
| 846 |
("R2/1984-01-30T00Z/P1D", |
| 847 |
["1984-01-30T00:00:00Z", "1984-01-31T00:00:00Z"]), |
| 848 |
("R2/P1D/1984-02-01T00Z", |
| 849 |
["1984-01-31T00:00:00Z", "1984-02-01T00:00:00Z"]), |
| 850 |
("R2/P1D/1984-01-01T00Z", |
| 851 |
["1983-12-31T00:00:00Z", "1984-01-01T00:00:00Z"]), |
| 852 |
("R2/1983-12-30T00Z/P1D", |
| 853 |
["1983-12-30T00:00:00Z", "1983-12-31T00:00:00Z"]), |
| 854 |
("R2/1999-02-28T00Z/P1Y1D", |
| 855 |
["1999-02-28T00:00:00Z", "2000-02-29T00:00:00Z"]), |
| 856 |
("R2/2000-02-28T00Z/P1Y1D", |
| 857 |
["2000-02-28T00:00:00Z", "2001-02-29T00:00:00Z"]), |
| 858 |
("R2/2001-02-28T00Z/P1Y1D", |
| 859 |
["2001-02-28T00:00:00Z", "2002-02-29T00:00:00Z"]), |
| 860 |
] |
| 861 |
|
| 862 |
|
| 863 |
def get_timerecurrence_membership_tests(): |
| 864 |
"""Return test membership expressions for data.TimeRecurrence.""" |
| 865 |
return [ |
| 866 |
("R5/2020-01-01T00:00:00Z/2020-01-05T00:00:00Z", |
| 867 |
[("2020-01-02T00:00:00Z", False), |
| 868 |
("2020-01-17T00:00:00Z", True), |
| 869 |
("2020-01-21T00:00:00Z", False)]), |
| 870 |
("R3/1001-W01-1T00:00:00Z/1002-W52-6T00:00:00-05:30", |
| 871 |
[("1001-W01-1T00:00:00Z", True), |
| 872 |
("1000-12-29T00:00:00Z", True), |
| 873 |
("0901-07-08T12:45:00Z", False), |
| 874 |
("1001-W01-2T00:00:00Z", False), |
| 875 |
("1001-W53-3T14:45:00Z", False), |
| 876 |
("1002-W52-6T05:30:00Z", True), |
| 877 |
("1002-W52-6T03:30:00-02:00", True), |
| 878 |
("1002-W52-6T07:30:00+02:00", True), |
| 879 |
("1005-W01-4T11:00:00Z", True), |
| 880 |
("10030101T00Z", False)]), |
| 881 |
("R3/P700D/1957-W01-1T06,5Z", |
| 882 |
[("1953-W10-1T06,5Z", True), |
| 883 |
("1953-03-02T06,5Z", True), |
| 884 |
("1952-03-02T06,5Z", False), |
| 885 |
("1955-W05-1T06,5Z", True), |
| 886 |
("1957-W01-1T06,5Z", True), |
| 887 |
("1956-366T06,5Z", True), |
| 888 |
("1956-356T04,5Z", False)]), |
| 889 |
] |
| 890 |
|
| 891 |
|
| 892 |
def get_timerecurrence_first_after_tests(): |
| 893 |
"""Return test get_first_after expressions for data.TimeRecurrence""" |
| 894 |
return [ |
| 895 |
("R5/2020-01-01T00:00:00Z/2020-01-05T00:00:00Z", |
| 896 |
[("2019-01-01T00:00:00Z", "2020-01-01T00:00:00Z"), |
| 897 |
("2020-01-02T00:00:00Z", "2020-01-05T00:00:00Z"), |
| 898 |
("2020-01-15T00:00:00Z", "2020-01-17T00:00:00Z"), |
| 899 |
("2021-01-01T00:00:00Z", "None")]), |
| 900 |
("R3/P700D/1961-W01-1T06,5Z", |
| 901 |
[("1950-01-01T00:00:00Z", "1957-W10-1T06,5Z"), |
| 902 |
("1958-01-01T00:00:00Z", "1959-02-02T06:30:00Z"), |
| 903 |
("2021-01-01T00:00:00Z", "None")]), |
| 904 |
("R/2000-01-01T00:00:00Z/P1Y", |
| 905 |
[("2001-01-01T00:00:00Z", "2002-01-01T00:00:00Z"), |
| 906 |
("2002-01-01T00:00:00Z", "2003-01-01T00:00:00Z")]), |
| 907 |
("R1/2000-01-01T00:00:00Z/P1Y", |
| 908 |
[("2000-01-01T00:00:00Z", "None")]) |
| 909 |
] |
| 910 |
|
| 911 |
|
| 912 |
def get_timerecurrenceparser_tests(): |
| 913 |
"""Yield tests for the time recurrence parser.""" |
| 914 |
test_points = ["-100024-02-10T17:00:00-12:30", |
| 915 |
"+000001-W45-7T06Z", "1001W011", |
| 916 |
"1955W051T06,5Z", "1999-06-01", |
| 917 |
"1967-056", "+5002000830T235902,345", |
| 918 |
"1765-W04"] |
| 919 |
for reps in [None, 1, 2, 3, 10]: |
| 920 |
if reps is None: |
| 921 |
reps_string = "" |
| 922 |
else: |
| 923 |
reps_string = str(reps) |
| 924 |
point_parser = parsers.TimePointParser() |
| 925 |
duration_parser = parsers.DurationParser() |
| 926 |
for point_expr in test_points: |
| 927 |
duration_tests = get_timedurationparser_tests() |
| 928 |
start_point = point_parser.parse(point_expr) |
| 929 |
for duration_expr, _ in duration_tests: |
| 930 |
if duration_expr.startswith("-P"): |
| 931 |
|
| 932 |
continue |
| 933 |
duration = duration_parser.parse(duration_expr) |
| 934 |
end_point = start_point + duration |
| 935 |
expr_1 = ("R" + reps_string + "/" + str(start_point) + |
| 936 |
"/" + str(end_point)) |
| 937 |
yield expr_1, {"repetitions": reps, "start_point": start_point, |
| 938 |
"end_point": end_point} |
| 939 |
expr_3 = ("R" + reps_string + "/" + str(start_point) + |
| 940 |
"/" + str(duration)) |
| 941 |
yield expr_3, {"repetitions": reps, "start_point": start_point, |
| 942 |
"duration": duration} |
| 943 |
expr_4 = ("R" + reps_string + "/" + str(duration) + "/" + |
| 944 |
str(end_point)) |
| 945 |
yield expr_4, {"repetitions": reps, "duration": duration, |
| 946 |
"end_point": end_point} |
| 947 |
|
| 948 |
|
| 949 |
def get_local_time_zone_hours_minutes(): |
| 950 |
"""Provide an independent method of getting the local time zone.""" |
| 951 |
utc_offset = datetime.datetime.now() - datetime.datetime.utcnow() |
| 952 |
|
| 953 |
|
| 954 |
utc_offset_seconds = utc_offset.seconds + 86400 * utc_offset.days |
| 955 |
utc_offset_hours = (utc_offset_seconds + 1800) // 3600 |
| 956 |
utc_offset_minutes = ( |
| 957 |
((utc_offset_seconds - 3600 * utc_offset_hours) + 30) // 60 |
| 958 |
) |
| 959 |
return utc_offset_hours, utc_offset_minutes |
| 960 |
|
| 961 |
|
| 962 |
class TestSuite(unittest.TestCase): |
| 963 |
"""Test the functionality of parsers and data model manipulation.""" |
| 964 |
|
| 965 |
def test_largest_truncated_property_name(self): |
| 966 |
"""Test the largest truncated property name.""" |
| 967 |
|
| 968 |
parser = parsers.TimePointParser( |
| 969 |
allow_truncated=True) |
| 970 |
|
| 971 |
truncated_property_tests = get_truncated_property_tests() |
| 972 |
for expression in truncated_property_tests.keys(): |
| 973 |
try: |
| 974 |
test_data = parser.parse(expression) |
| 975 |
except ISO8601SyntaxError as syn_exc: |
| 976 |
raise ValueError("Parsing failed for {0}: {1}".format( |
| 977 |
expression, syn_exc)) |
| 978 |
|
| 979 |
self.assertEqual( |
| 980 |
test_data.get_largest_truncated_property_name(), |
| 981 |
truncated_property_tests[expression] |
| 982 |
["largest_truncated_property_name"], |
| 983 |
msg=expression) |
| 984 |
|
| 985 |
def test_smallest_missing_property_name(self): |
| 986 |
"""Test the smallest missing property name.""" |
| 987 |
|
| 988 |
parser = parsers.TimePointParser( |
| 989 |
allow_truncated=True) |
| 990 |
|
| 991 |
truncated_property_tests = get_truncated_property_tests() |
| 992 |
for expression in truncated_property_tests.keys(): |
| 993 |
try: |
| 994 |
test_data = parser.parse(expression) |
| 995 |
except ISO8601SyntaxError as syn_exc: |
| 996 |
raise ValueError("Parsing failed for {0}: {1}".format( |
| 997 |
expression, syn_exc)) |
| 998 |
|
| 999 |
self.assertEqual( |
| 1000 |
test_data.get_smallest_missing_property_name(), |
| 1001 |
truncated_property_tests[expression] |
| 1002 |
["smallest_missing_property_name"], |
| 1003 |
msg=expression) |
| 1004 |
|
| 1005 |
def test_timeduration_parser(self): |
| 1006 |
"""Test the duration parsing.""" |
| 1007 |
parser = parsers.DurationParser() |
| 1008 |
for expression, ctrl_result in get_timedurationparser_tests(): |
| 1009 |
try: |
| 1010 |
test_result = str(parser.parse(expression)) |
| 1011 |
except ISO8601SyntaxError: |
| 1012 |
raise ValueError( |
| 1013 |
"DurationParser test failed to parse '%s'" % |
| 1014 |
expression |
| 1015 |
) |
| 1016 |
self.assertEqual(test_result, ctrl_result, expression) |
| 1017 |
|
| 1018 |
def test_timeduration_dumper(self): |
| 1019 |
"""Test the duration dumping.""" |
| 1020 |
for ctrl_expression, test_props in get_timedurationdumper_tests(): |
| 1021 |
duration = data.Duration(**test_props) |
| 1022 |
test_expression = str(duration) |
| 1023 |
self.assertEqual(test_expression, ctrl_expression, |
| 1024 |
str(test_props)) |
| 1025 |
|
| 1026 |
def test_timepoint_time_zone(self): |
| 1027 |
"""Test the time zone handling of timepoint instances.""" |
| 1028 |
year, month_of_year, day_of_month = (2000, 1, 1) |
| 1029 |
utc_offset_hours, utc_offset_minutes = ( |
| 1030 |
get_local_time_zone_hours_minutes() |
| 1031 |
) |
| 1032 |
for hour_of_day in range(24): |
| 1033 |
for minute_of_hour in [0, 30]: |
| 1034 |
point = data.TimePoint(year=year, month_of_year=month_of_year, |
| 1035 |
day_of_month=day_of_month, |
| 1036 |
hour_of_day=hour_of_day, |
| 1037 |
minute_of_hour=minute_of_hour) |
| 1038 |
test_dates = [ |
| 1039 |
point.to_utc(), |
| 1040 |
point.to_local_time_zone(), |
| 1041 |
point.to_time_zone(data.TimeZone(hours=-13, minutes=-45)), |
| 1042 |
point.to_time_zone(data.TimeZone(hours=8, minutes=30)) |
| 1043 |
] |
| 1044 |
self.assertEqual(test_dates[0].time_zone.hours, 0, |
| 1045 |
test_dates[0]) |
| 1046 |
self.assertEqual(test_dates[0].time_zone.minutes, 0, |
| 1047 |
test_dates[0]) |
| 1048 |
|
| 1049 |
self.assertEqual(test_dates[1].time_zone.hours, |
| 1050 |
utc_offset_hours, test_dates[1]) |
| 1051 |
self.assertEqual(test_dates[1].time_zone.minutes, |
| 1052 |
utc_offset_minutes, test_dates[1]) |
| 1053 |
|
| 1054 |
for i_test_date in list(test_dates): |
| 1055 |
i_test_date_str = str(i_test_date) |
| 1056 |
date_no_tz = i_test_date._copy() |
| 1057 |
date_no_tz._time_zone = data.TimeZone(hours=0, minutes=0) |
| 1058 |
if (i_test_date.time_zone.hours >= 0 or |
| 1059 |
i_test_date.time_zone.minutes >= 0): |
| 1060 |
utc_offset = date_no_tz - i_test_date |
| 1061 |
else: |
| 1062 |
utc_offset = (i_test_date - date_no_tz) * -1 |
| 1063 |
self.assertEqual(utc_offset.hours, |
| 1064 |
i_test_date.time_zone.hours, |
| 1065 |
i_test_date_str + " utc offset (hrs)") |
| 1066 |
self.assertEqual(utc_offset.minutes, |
| 1067 |
i_test_date.time_zone.minutes, |
| 1068 |
i_test_date_str + " utc offset (mins)") |
| 1069 |
for j_test_date in list(test_dates): |
| 1070 |
j_test_date_str = str(j_test_date) |
| 1071 |
self.assertEqual( |
| 1072 |
i_test_date, j_test_date, |
| 1073 |
i_test_date_str + " == " + j_test_date_str) |
| 1074 |
duration = j_test_date - i_test_date |
| 1075 |
self.assertEqual( |
| 1076 |
duration, data.Duration(days=0), |
| 1077 |
i_test_date_str + " - " + j_test_date_str) |
| 1078 |
|
| 1079 |
|
| 1080 |
def test_timepoint_dumper(self): |
| 1081 |
"""Test the dumping of TimePoint instances.""" |
| 1082 |
parser = parsers.TimePointParser(allow_truncated=True, |
| 1083 |
default_to_unknown_time_zone=True) |
| 1084 |
dumper = dumpers.TimePointDumper() |
| 1085 |
for expression, timepoint_kwargs in get_timepointparser_tests( |
| 1086 |
allow_truncated=True): |
| 1087 |
ctrl_timepoint = data.TimePoint(**timepoint_kwargs) |
| 1088 |
try: |
| 1089 |
test_timepoint = parser.parse(str(ctrl_timepoint)) |
| 1090 |
except ISO8601SyntaxError as syn_exc: |
| 1091 |
raise ValueError( |
| 1092 |
"Parsing failed for the dump of {0}: {1}".format( |
| 1093 |
expression, syn_exc)) |
| 1094 |
self.assertEqual(test_timepoint, |
| 1095 |
ctrl_timepoint, expression) |
| 1096 |
for timepoint_kwargs, format_results in ( |
| 1097 |
get_timepoint_dumper_tests()): |
| 1098 |
ctrl_timepoint = data.TimePoint(**timepoint_kwargs) |
| 1099 |
for format_, ctrl_data in format_results: |
| 1100 |
test_data = dumper.dump(ctrl_timepoint, format_) |
| 1101 |
self.assertEqual(test_data, ctrl_data, format_) |
| 1102 |
for timepoint_kwargs, format_exception_results in ( |
| 1103 |
get_timepointdumper_failure_tests()): |
| 1104 |
ctrl_timepoint = data.TimePoint(**timepoint_kwargs) |
| 1105 |
for format_, ctrl_exception, num_expanded_year_digits in ( |
| 1106 |
format_exception_results): |
| 1107 |
dumper = dumpers.TimePointDumper( |
| 1108 |
num_expanded_year_digits=num_expanded_year_digits) |
| 1109 |
self.assertRaises(ctrl_exception, dumper.dump, |
| 1110 |
ctrl_timepoint, format_) |
| 1111 |
|
| 1112 |
def test_timepoint_dumper_bounds_error_message(self): |
| 1113 |
"""Test the exception text contains the information expected""" |
| 1114 |
the_error = TimePointDumperBoundsError("TimePoint1", "year", |
| 1115 |
10, 20) |
| 1116 |
the_string = the_error.__str__() |
| 1117 |
|
| 1118 |
self.assertTrue("TimePoint1" in the_string, |
| 1119 |
"Failed to find TimePoint1 in {}".format(the_string)) |
| 1120 |
self.assertTrue("year" in the_string, |
| 1121 |
"Failed to find TimePoint1 in {}".format(the_string)) |
| 1122 |
self.assertTrue("10" in the_string, |
| 1123 |
"Failed to find TimePoint1 in {}".format(the_string)) |
| 1124 |
self.assertTrue("20" in the_string, |
| 1125 |
"Failed to find TimePoint1 in {}".format(the_string)) |
| 1126 |
|
| 1127 |
get_test_timepoint_dumper_get_time_zone = [ |
| 1128 |
["+250:00", None], |
| 1129 |
["+25:00", (25, 0)], |
| 1130 |
["+12:00", (12, 0)], |
| 1131 |
["+12:45", (12, 45)], |
| 1132 |
["+01:00", (1, 0)], |
| 1133 |
["Z", (0, 0)], |
| 1134 |
["-03:00", (-3, 0)], |
| 1135 |
["-03:30", (-3, -30)], |
| 1136 |
["-11:00", (-11, 0)], |
| 1137 |
["+00:00", (0, 0)], |
| 1138 |
["-00:00", (0, 0)] |
| 1139 |
] |
| 1140 |
|
| 1141 |
def test_timepoint_dumper_get_time_zone(self): |
| 1142 |
"""Test the time zone returned by TimerPointDumper.get_time_zone""" |
| 1143 |
dumper = dumpers.TimePointDumper(num_expanded_year_digits=2) |
| 1144 |
for value, expected in self.get_test_timepoint_dumper_get_time_zone: |
| 1145 |
tz = dumper.get_time_zone(value) |
| 1146 |
self.assertEqual(expected, tz) |
| 1147 |
|
| 1148 |
def test_timepoint_dumper_after_copy(self): |
| 1149 |
"""Test that printing the TimePoint attributes works after it has |
| 1150 |
been copied, see issue #102 for more information""" |
| 1151 |
time_point = data.TimePoint(year=2000, truncated=True, |
| 1152 |
truncated_dump_format='CCYY') |
| 1153 |
the_copy = time_point._copy() |
| 1154 |
self.assertEqual(str(time_point), str(the_copy)) |
| 1155 |
|
| 1156 |
def test_timepoint_parser(self): |
| 1157 |
"""Test the parsing of date/time expressions.""" |
| 1158 |
|
| 1159 |
|
| 1160 |
parser = parsers.TimePointParser( |
| 1161 |
allow_truncated=True, |
| 1162 |
default_to_unknown_time_zone=True) |
| 1163 |
for expression, timepoint_kwargs in get_timepointparser_tests( |
| 1164 |
allow_truncated=True): |
| 1165 |
timepoint_kwargs = copy.deepcopy(timepoint_kwargs) |
| 1166 |
try: |
| 1167 |
test_data = str(parser.parse(expression)) |
| 1168 |
except ISO8601SyntaxError as syn_exc: |
| 1169 |
raise ValueError("Parsing failed for {0}: {1}".format( |
| 1170 |
expression, syn_exc)) |
| 1171 |
ctrl_data = str(data.TimePoint(**timepoint_kwargs)) |
| 1172 |
self.assertEqual(test_data, ctrl_data, expression) |
| 1173 |
ctrl_data = expression |
| 1174 |
test_data = str(parser.parse(expression, dump_as_parsed=True)) |
| 1175 |
self.assertEqual(test_data, ctrl_data, expression) |
| 1176 |
|
| 1177 |
|
| 1178 |
utc_offset_hours, utc_offset_minutes = ( |
| 1179 |
get_local_time_zone_hours_minutes() |
| 1180 |
) |
| 1181 |
parser = parsers.TimePointParser(allow_truncated=True) |
| 1182 |
for expression, timepoint_kwargs in get_timepointparser_tests( |
| 1183 |
allow_truncated=True, skip_time_zones=True): |
| 1184 |
timepoint_kwargs = copy.deepcopy(timepoint_kwargs) |
| 1185 |
try: |
| 1186 |
test_timepoint = parser.parse(expression) |
| 1187 |
except ISO8601SyntaxError as syn_exc: |
| 1188 |
raise ValueError("Parsing failed for {0}: {1}".format( |
| 1189 |
expression, syn_exc)) |
| 1190 |
test_data = (test_timepoint.time_zone.hours, |
| 1191 |
test_timepoint.time_zone.minutes) |
| 1192 |
ctrl_data = (utc_offset_hours, utc_offset_minutes) |
| 1193 |
self.assertEqual(test_data, ctrl_data, |
| 1194 |
"Local time zone for " + expression) |
| 1195 |
|
| 1196 |
|
| 1197 |
utc_offset_hours, utc_offset_minutes = ( |
| 1198 |
get_local_time_zone_hours_minutes() |
| 1199 |
) |
| 1200 |
given_utc_offset_hours = -2 |
| 1201 |
if given_utc_offset_hours == utc_offset_hours: |
| 1202 |
|
| 1203 |
given_utc_offset_hours = -3 |
| 1204 |
given_utc_offset_minutes = -15 |
| 1205 |
given_time_zone_hours_minutes = ( |
| 1206 |
given_utc_offset_hours, given_utc_offset_minutes) |
| 1207 |
parser = parsers.TimePointParser( |
| 1208 |
allow_truncated=True, |
| 1209 |
assumed_time_zone=given_time_zone_hours_minutes |
| 1210 |
) |
| 1211 |
for expression, timepoint_kwargs in get_timepointparser_tests( |
| 1212 |
allow_truncated=True, skip_time_zones=True): |
| 1213 |
timepoint_kwargs = copy.deepcopy(timepoint_kwargs) |
| 1214 |
try: |
| 1215 |
test_timepoint = parser.parse(expression) |
| 1216 |
except ISO8601SyntaxError as syn_exc: |
| 1217 |
raise ValueError("Parsing failed for {0}: {1}".format( |
| 1218 |
expression, syn_exc)) |
| 1219 |
test_data = (test_timepoint.time_zone.hours, |
| 1220 |
test_timepoint.time_zone.minutes) |
| 1221 |
ctrl_data = given_time_zone_hours_minutes |
| 1222 |
self.assertEqual(test_data, ctrl_data, |
| 1223 |
"A given time zone for " + expression) |
| 1224 |
|
| 1225 |
|
| 1226 |
parser = parsers.TimePointParser( |
| 1227 |
allow_truncated=True, |
| 1228 |
assumed_time_zone=(0, 0) |
| 1229 |
) |
| 1230 |
for expression, timepoint_kwargs in get_timepointparser_tests( |
| 1231 |
allow_truncated=True, skip_time_zones=True): |
| 1232 |
timepoint_kwargs = copy.deepcopy(timepoint_kwargs) |
| 1233 |
try: |
| 1234 |
test_timepoint = parser.parse(expression) |
| 1235 |
except ISO8601SyntaxError as syn_exc: |
| 1236 |
raise ValueError("Parsing failed for {0}: {1}".format( |
| 1237 |
expression, syn_exc)) |
| 1238 |
test_data = (test_timepoint.time_zone.hours, |
| 1239 |
test_timepoint.time_zone.minutes) |
| 1240 |
ctrl_data = (0, 0) |
| 1241 |
self.assertEqual(test_data, ctrl_data, |
| 1242 |
"UTC for " + expression) |
| 1243 |
|
| 1244 |
def test_timepoint_strftime_strptime(self): |
| 1245 |
"""Test the strftime/strptime for date/time expressions.""" |
| 1246 |
parser = parsers.TimePointParser(assumed_time_zone=(0, 0)) |
| 1247 |
strftime_string = "%d :?foobar++(%F%H %j:%m %M?foobar :%S++(%X %Y:" |
| 1248 |
strptime_strings = [ |
| 1249 |
"%d :?foo++(%H :%m %M?foo :%S++( %Y:", |
| 1250 |
"%j :?foo %H++( %M?foo :%S++( %Y:", |
| 1251 |
"?foo%s :++(" |
| 1252 |
] |
| 1253 |
ctrl_date = datetime.datetime(2002, 3, 1, 12, 30, 2, |
| 1254 |
tzinfo=datetime.timezone.utc) |
| 1255 |
test_date = data.TimePoint( |
| 1256 |
year=ctrl_date.year, |
| 1257 |
month_of_year=ctrl_date.month, |
| 1258 |
day_of_month=ctrl_date.day, |
| 1259 |
hour_of_day=ctrl_date.hour, |
| 1260 |
minute_of_hour=ctrl_date.minute, |
| 1261 |
second_of_minute=ctrl_date.second |
| 1262 |
) |
| 1263 |
|
| 1264 |
|
| 1265 |
for test_date in [test_date, test_date.to_week_date(), |
| 1266 |
test_date.to_ordinal_date()]: |
| 1267 |
|
| 1268 |
ctrl_data = ctrl_date.strftime(strftime_string) |
| 1269 |
test_data = test_date.strftime(strftime_string) |
| 1270 |
self.assertEqual(test_data, ctrl_data, strftime_string) |
| 1271 |
|
| 1272 |
|
| 1273 |
for strptime_string in strptime_strings: |
| 1274 |
|
| 1275 |
if "%s" in strptime_string: |
| 1276 |
unix_time = ctrl_date.timestamp() |
| 1277 |
|
| 1278 |
ctrl_dump = strptime_string % int(unix_time) |
| 1279 |
ctrl_data = ctrl_date |
| 1280 |
else: |
| 1281 |
ctrl_dump = ctrl_date.strftime(strptime_string) |
| 1282 |
ctrl_data = datetime.datetime.strptime( |
| 1283 |
ctrl_dump, strptime_string) |
| 1284 |
test_dump = test_date.strftime(strptime_string) |
| 1285 |
test_data = parser.strptime(test_dump, strptime_string) |
| 1286 |
test_data = test_data.to_utc() |
| 1287 |
|
| 1288 |
self.assertEqual(test_dump, ctrl_dump, strptime_string) |
| 1289 |
|
| 1290 |
ctrl_data = ( |
| 1291 |
ctrl_data.year, ctrl_data.month, ctrl_data.day, |
| 1292 |
ctrl_data.hour, ctrl_data.minute, ctrl_data.second |
| 1293 |
) |
| 1294 |
test_data = tuple(list(test_data.get_calendar_date()) + |
| 1295 |
list(test_data.get_hour_minute_second())) |
| 1296 |
self.assertEqual(test_data, ctrl_data, |
| 1297 |
test_dump + "\n" + strptime_string) |
| 1298 |
|
| 1299 |
|
| 1300 |
for sign in [1, -1]: |
| 1301 |
for hour in range(0, 24): |
| 1302 |
for minute in range(0, 59): |
| 1303 |
if hour == 0 and minute == 0 and sign == -1: |
| 1304 |
|
| 1305 |
continue |
| 1306 |
test_date = data.TimePoint( |
| 1307 |
year=ctrl_date.year, |
| 1308 |
month_of_year=ctrl_date.month, |
| 1309 |
day_of_month=ctrl_date.day, |
| 1310 |
hour_of_day=ctrl_date.hour, |
| 1311 |
minute_of_hour=ctrl_date.minute, |
| 1312 |
second_of_minute=ctrl_date.second, |
| 1313 |
time_zone_hour=sign * hour, |
| 1314 |
time_zone_minute=sign * minute |
| 1315 |
) |
| 1316 |
ctrl_string = "-" if sign == -1 else "+" |
| 1317 |
ctrl_string += "%02d%02d" % (hour, minute) |
| 1318 |
self.assertEqual(test_date.strftime("%z"), |
| 1319 |
ctrl_string, |
| 1320 |
"%z for " + str(test_date)) |
| 1321 |
|
| 1322 |
def test_timerecurrence_alt_calendars(self): |
| 1323 |
"""Test recurring date/time series for alternate calendars.""" |
| 1324 |
for calendar_mode in ["360", "365", "366"]: |
| 1325 |
data.CALENDAR.set_mode(calendar_mode + "day") |
| 1326 |
self.assertEqual( |
| 1327 |
data.CALENDAR.mode, |
| 1328 |
getattr(data.Calendar, "MODE_%s" % calendar_mode) |
| 1329 |
) |
| 1330 |
parser = parsers.TimeRecurrenceParser() |
| 1331 |
tests = get_timerecurrence_expansion_tests_for_alt_calendar( |
| 1332 |
calendar_mode) |
| 1333 |
for expression, ctrl_results in tests: |
| 1334 |
try: |
| 1335 |
test_recurrence = parser.parse(expression) |
| 1336 |
except ISO8601SyntaxError: |
| 1337 |
raise ValueError( |
| 1338 |
"TimeRecurrenceParser test failed to parse '%s'" % |
| 1339 |
expression |
| 1340 |
) |
| 1341 |
test_results = [] |
| 1342 |
for time_point in test_recurrence: |
| 1343 |
test_results.append(str(time_point)) |
| 1344 |
self.assertEqual(test_results, ctrl_results, |
| 1345 |
expression + "(%s)" % calendar_mode) |
| 1346 |
data.CALENDAR.set_mode() |
| 1347 |
self.assertEqual(data.CALENDAR.mode, |
| 1348 |
data.Calendar.MODE_GREGORIAN) |
| 1349 |
|
| 1350 |
def test_timerecurrence(self): |
| 1351 |
"""Test the recurring date/time series data model.""" |
| 1352 |
parser = parsers.TimeRecurrenceParser() |
| 1353 |
for expression, ctrl_results in get_timerecurrence_expansion_tests(): |
| 1354 |
try: |
| 1355 |
test_recurrence = parser.parse(expression) |
| 1356 |
except ISO8601SyntaxError: |
| 1357 |
raise ValueError( |
| 1358 |
"TimeRecurrenceParser test failed to parse '%s'" % |
| 1359 |
expression |
| 1360 |
) |
| 1361 |
test_results = [] |
| 1362 |
reps = test_recurrence.repetitions or 3 |
| 1363 |
for i, time_point in enumerate(test_recurrence): |
| 1364 |
if i >= reps: |
| 1365 |
break |
| 1366 |
test_results.append(str(time_point)) |
| 1367 |
self.assertEqual(test_results, ctrl_results, expression) |
| 1368 |
if test_recurrence.start_point is None: |
| 1369 |
forward_method = test_recurrence.get_prev |
| 1370 |
backward_method = test_recurrence.get_next |
| 1371 |
else: |
| 1372 |
forward_method = test_recurrence.get_next |
| 1373 |
backward_method = test_recurrence.get_prev |
| 1374 |
test_points = [test_recurrence[0]] |
| 1375 |
for i in range(1, reps): |
| 1376 |
test_points.append(forward_method(test_points[-1])) |
| 1377 |
test_results = [str(point) for point in test_points] |
| 1378 |
self.assertEqual(test_results, ctrl_results, expression) |
| 1379 |
|
| 1380 |
test_points = [test_recurrence[reps - 1]] |
| 1381 |
for i in range(0, reps): |
| 1382 |
test_points.append(backward_method(test_points[-1])) |
| 1383 |
self.assertEqual(test_points[reps], None, expression) |
| 1384 |
|
| 1385 |
test_points.pop(-1) |
| 1386 |
test_points.reverse() |
| 1387 |
test_results = [str(point) for point in test_points] |
| 1388 |
self.assertEqual(test_results, ctrl_results, expression) |
| 1389 |
|
| 1390 |
for expression, results in get_timerecurrence_membership_tests(): |
| 1391 |
try: |
| 1392 |
test_recurrence = parser.parse(expression) |
| 1393 |
except ISO8601SyntaxError: |
| 1394 |
raise ValueError( |
| 1395 |
"TimeRecurrenceParser test failed to parse '%s'" % |
| 1396 |
expression |
| 1397 |
) |
| 1398 |
for timepoint_expression, ctrl_is_member in results: |
| 1399 |
timepoint = parsers.parse_timepoint_expression( |
| 1400 |
timepoint_expression) |
| 1401 |
test_is_member = test_recurrence.get_is_valid(timepoint) |
| 1402 |
self.assertEqual(test_is_member, ctrl_is_member, |
| 1403 |
timepoint_expression + " in " + expression) |
| 1404 |
for expression, results in get_timerecurrence_first_after_tests(): |
| 1405 |
try: |
| 1406 |
test_recurrence = parser.parse(expression) |
| 1407 |
except ISO8601SyntaxError: |
| 1408 |
raise ValueError( |
| 1409 |
"TimeRecurrenceParser test failed to parse '%s'" % |
| 1410 |
expression |
| 1411 |
) |
| 1412 |
for timepoint_expression, ctrl_result in results: |
| 1413 |
timepoint = parsers.parse_timepoint_expression( |
| 1414 |
timepoint_expression) |
| 1415 |
test_result = str(test_recurrence.get_first_after(timepoint)) |
| 1416 |
self.assertEqual(test_result, ctrl_result, |
| 1417 |
ctrl_result + " is first in " + expression |
| 1418 |
+ " after " + timepoint_expression) |
| 1419 |
|
| 1420 |
def test_timerecurrence_parser(self): |
| 1421 |
"""Test the recurring date/time series parsing.""" |
| 1422 |
parser = parsers.TimeRecurrenceParser() |
| 1423 |
for expression, test_info in get_timerecurrenceparser_tests(): |
| 1424 |
try: |
| 1425 |
test_data = str(parser.parse(expression)) |
| 1426 |
except ISO8601SyntaxError: |
| 1427 |
raise ValueError("Parsing failed for %s" % expression) |
| 1428 |
ctrl_data = str(data.TimeRecurrence(**test_info)) |
| 1429 |
self.assertEqual(test_data, ctrl_data, expression) |
| 1430 |
|
| 1431 |
def test_timerecurrence_comparison(self): |
| 1432 |
"""Test the '==' operator and hash() on TimeRecurrences.""" |
| 1433 |
parser = parsers.TimeRecurrenceParser() |
| 1434 |
tests = get_timerecurrence_comparison_tests() |
| 1435 |
for lhs_str, rhs_str, expected in tests: |
| 1436 |
lhs = parser.parse(lhs_str) |
| 1437 |
rhs = parser.parse(rhs_str) |
| 1438 |
test = lhs == rhs |
| 1439 |
assert test is expected, "{0} == {1}".format(lhs_str, rhs_str) |
| 1440 |
test = rhs == lhs |
| 1441 |
assert test is expected, "{0} == {1}".format(rhs_str, lhs_str) |
| 1442 |
test = lhs != rhs |
| 1443 |
assert test is not expected, "{0} != {1}".format(lhs_str, rhs_str) |
| 1444 |
test = hash(lhs) == hash(rhs) |
| 1445 |
assert test is expected, ( |
| 1446 |
"hash of {0} == hash of {1}".format(lhs_str, rhs_str)) |
| 1447 |
|
| 1448 |
if lhs.repetitions is not None and rhs.repetitions is not None: |
| 1449 |
|
| 1450 |
test = list(lhs) == list(rhs) |
| 1451 |
assert test is expected |
| 1452 |
test_recurrence = parser.parse(tests[0][0]) |
| 1453 |
for var in [7, 'foo', (1, 2), data.Duration(days=1)]: |
| 1454 |
self.assertFalse(test_recurrence == var) |
| 1455 |
|
| 1456 |
def test_timerecurrence_add(self): |
| 1457 |
"""Test adding/subtracting Duration to/from TimeRecurrence""" |
| 1458 |
rep = 4 |
| 1459 |
start_pt = data.TimePoint(year=2020, month_of_year=3, day_of_month=13) |
| 1460 |
second_pt = data.TimePoint(year=2020, month_of_year=3, day_of_month=20) |
| 1461 |
end_pt = data.TimePoint(year=2020, month_of_year=4, day_of_month=3) |
| 1462 |
dur = data.Duration(days=7) |
| 1463 |
offset = data.Duration(hours=9, minutes=59) |
| 1464 |
|
| 1465 |
recurrence_fmt1 = data.TimeRecurrence( |
| 1466 |
repetitions=rep, start_point=start_pt, end_point=second_pt) |
| 1467 |
recurrence_fmt3 = data.TimeRecurrence( |
| 1468 |
repetitions=rep, start_point=start_pt, duration=dur) |
| 1469 |
recurrence_fmt4 = data.TimeRecurrence( |
| 1470 |
repetitions=rep, duration=dur, end_point=end_pt) |
| 1471 |
|
| 1472 |
assert recurrence_fmt1 == recurrence_fmt3 == recurrence_fmt4 |
| 1473 |
|
| 1474 |
new_start_pt = start_pt + offset |
| 1475 |
expected = data.TimeRecurrence( |
| 1476 |
repetitions=rep, start_point=new_start_pt, duration=dur) |
| 1477 |
for recurrence in (recurrence_fmt1, recurrence_fmt3, recurrence_fmt4): |
| 1478 |
assert recurrence + offset == expected |
| 1479 |
assert offset + recurrence == expected |
| 1480 |
|
| 1481 |
new_start_pt = start_pt - offset |
| 1482 |
expected = data.TimeRecurrence( |
| 1483 |
repetitions=rep, start_point=new_start_pt, duration=dur) |
| 1484 |
for recurrence in (recurrence_fmt1, recurrence_fmt3, recurrence_fmt4): |
| 1485 |
assert recurrence - offset == expected |
| 1486 |
|
| 1487 |
with pytest.raises(TypeError): |
| 1488 |
recurrence_fmt3 + data.TimePoint(year=2049, month_of_year=2) |
| 1489 |
|
| 1490 |
def test_invalid_timerecurrence(self): |
| 1491 |
"""Test that init'ing a TimeRecurrence with bad inputs raises error.""" |
| 1492 |
start_pt = data.TimePoint(year=2020, day_of_month=9) |
| 1493 |
end_pt = data.TimePoint(year=2020, day_of_month=30) |
| 1494 |
dur = data.Duration(hours=36) |
| 1495 |
_tests = [{"repetitions": 0, "start_point": start_pt, "duration": dur}] |
| 1496 |
for reps in (4, None): |
| 1497 |
_tests += [ |
| 1498 |
{"repetitions": reps, "start_point": start_pt, |
| 1499 |
"end_point": end_pt, "duration": dur}, |
| 1500 |
{"repetitions": reps, "start_point": start_pt}, |
| 1501 |
{"repetitions": reps, "end_point": end_pt}, |
| 1502 |
{"repetitions": reps, "duration": dur}, |
| 1503 |
{"repetitions": reps, "start_point": start_pt, |
| 1504 |
"duration": data.Duration(minutes=-20)}, |
| 1505 |
{"repetitions": reps, "start_point": end_pt, |
| 1506 |
"end_point": start_pt} |
| 1507 |
] |
| 1508 |
for kwargs in _tests: |
| 1509 |
with pytest.raises(BadInputError): |
| 1510 |
data.TimeRecurrence(**kwargs) |
| 1511 |
|
| 1512 |
def test_timepoint_dump_format(self): |
| 1513 |
"""Test the timepoint format dump when values are programmatically |
| 1514 |
set to None""" |
| 1515 |
|
| 1516 |
t = data.TimePoint(year="1984") |
| 1517 |
|
| 1518 |
t._month_of_year = None |
| 1519 |
t._day_of_year = None |
| 1520 |
t._week_of_year = None |
| 1521 |
with self.assertRaises(RuntimeError): |
| 1522 |
self.assertEqual("1984-01-01T00:00:00Z", str(t)) |
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
if __name__ == '__main__': |
| 1527 |
unittest.main() |