| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
"""This tests the ISO 8601 data model functionality.""" |
| 20 |
|
| 21 |
import pytest |
| 22 |
import unittest |
| 23 |
|
| 24 |
from metomi.isodatetime import data |
| 25 |
from metomi.isodatetime.exceptions import BadInputError |
| 26 |
|
| 27 |
|
| 28 |
def get_timeduration_tests(): |
| 29 |
"""Yield tests for the duration class.""" |
| 30 |
tests = { |
| 31 |
"get_days_and_seconds": [ |
| 32 |
([], {"hours": 25}, (1, 3600)), |
| 33 |
([], {"seconds": 59}, (0, 59)), |
| 34 |
([], {"minutes": 10}, (0, 600)), |
| 35 |
([], {"days": 5, "minutes": 2}, (5, 120)), |
| 36 |
([], {"hours": 2, "minutes": 5, "seconds": 11.5}, (0, 7511.5)), |
| 37 |
([], {"hours": 23, "minutes": 1446}, (1, 83160)) |
| 38 |
], |
| 39 |
"get_seconds": [ |
| 40 |
([], {"hours": 25}, 90000), |
| 41 |
([], {"seconds": 59}, 59), |
| 42 |
([], {"minutes": 10}, 600), |
| 43 |
([], {"days": 5, "minutes": 2}, 432120), |
| 44 |
([], {"hours": 2, "minutes": 5, "seconds": 11.5}, 7511.5), |
| 45 |
([], {"hours": 23, "minutes": 1446}, 169560) |
| 46 |
] |
| 47 |
} |
| 48 |
for method, method_tests in tests.items(): |
| 49 |
for method_args, test_props, ctrl_results in method_tests: |
| 50 |
yield test_props, method, method_args, ctrl_results |
| 51 |
|
| 52 |
|
| 53 |
def get_duration_subtract_tests(): |
| 54 |
"""Yield tests for subtracting a duration from a timepoint.""" |
| 55 |
return [ |
| 56 |
{ |
| 57 |
"start": { |
| 58 |
"year": 2010, "day_of_year": 65, |
| 59 |
|
| 60 |
"hour_of_day": 12, "minute_of_hour": 0, "second_of_minute": 0, |
| 61 |
"time_zone_hour": 0, "time_zone_minute": 0 |
| 62 |
}, |
| 63 |
"duration": { |
| 64 |
"years": 6 |
| 65 |
}, |
| 66 |
"result": { |
| 67 |
"year": 2004, |
| 68 |
"month_of_year": 3, "day_of_month": 5, |
| 69 |
"hour_of_day": 12, "minute_of_hour": 0, "second_of_minute": 0, |
| 70 |
"time_zone_hour": 0, "time_zone_minute": 0 |
| 71 |
} |
| 72 |
}, |
| 73 |
{ |
| 74 |
"start": { |
| 75 |
"year": 2010, "week_of_year": 10, "day_of_week": 3, |
| 76 |
|
| 77 |
"hour_of_day": 12, "minute_of_hour": 0, "second_of_minute": 0, |
| 78 |
"time_zone_hour": 0, "time_zone_minute": 0 |
| 79 |
}, |
| 80 |
"duration": { |
| 81 |
"years": 6 |
| 82 |
}, |
| 83 |
"result": { |
| 84 |
"year": 2004, |
| 85 |
"month_of_year": 3, "day_of_month": 3, |
| 86 |
"hour_of_day": 12, "minute_of_hour": 0, "second_of_minute": 0, |
| 87 |
"time_zone_hour": 0, "time_zone_minute": 0 |
| 88 |
} |
| 89 |
}, |
| 90 |
] |
| 91 |
|
| 92 |
|
| 93 |
def get_duration_comparison_tests(): |
| 94 |
"""Yield tests for executing comparison operators on Durations. |
| 95 |
|
| 96 |
All True "==" tests will be carried out for "<=" & ">= too. Likewise |
| 97 |
all True "<" & "> tests will be carried out for "<=" & ">=" respectively. |
| 98 |
|
| 99 |
Tuple format --> test: |
| 100 |
(args1, args2, bool1 [, bool2]) --> |
| 101 |
Duration(**args1) <operator> Duration(**args2) is bool1 |
| 102 |
& the reverse: |
| 103 |
Duration(**args2) <operator> Duration(**args1) is bool2 if |
| 104 |
bool2 supplied else bool1 |
| 105 |
""" |
| 106 |
nominal_units = ["years", "months"] |
| 107 |
|
| 108 |
return { |
| 109 |
"==": [ |
| 110 |
|
| 111 |
*[({prop: 1}, {prop: 1}, True) for prop in nominal_units], |
| 112 |
*[(dur, dur, True) for dur in [ |
| 113 |
{"years": 1, "months": 1, "days": 1}]], |
| 114 |
*[({prop: 1}, {prop: 2}, False) for prop in nominal_units], |
| 115 |
|
| 116 |
({"years": 1}, {"months": 12}, False), |
| 117 |
*[({"years": 1}, {"days": i}, False) for i in [365, 366]], |
| 118 |
*[({"months": 1}, {"days": i}, False) for i in [28, 29, 30, 31]], |
| 119 |
({"months": 1, "days": 7}, {"weeks": 1}, False), |
| 120 |
|
| 121 |
({"weeks": 1}, {"days": 7}, True), |
| 122 |
({"weeks": 1}, {"hours": 7 * 24}, True), |
| 123 |
({"days": 1}, {"hours": 24}, True), |
| 124 |
({"days": 1}, {"seconds": 24 * 60 * 60}, True), |
| 125 |
({"hours": 1}, {"minutes": 60}, True), |
| 126 |
({"hours": 1}, {"minutes": 30, "seconds": 30 * 60}, True), |
| 127 |
({"hours": 1.5}, {"minutes": 90}, True) |
| 128 |
], |
| 129 |
"<": [ |
| 130 |
|
| 131 |
*[({prop: 1}, {prop: 1}, False) for prop in nominal_units], |
| 132 |
*[(dur, dur, False) for dur in [ |
| 133 |
{"years": 1, "months": 1, "days": 1}]], |
| 134 |
*[({prop: 1}, {prop: 2}, True, False) for prop in nominal_units], |
| 135 |
|
| 136 |
({"years": 1}, {"months": 12}, False, True), |
| 137 |
({"years": 1}, {"months": 12, "days": 10}, True, False), |
| 138 |
({"years": 1}, {"days": 364}, False, True), |
| 139 |
({"years": 1}, {"days": 365}, False), |
| 140 |
({"years": 1}, {"days": 366}, True, False), |
| 141 |
({"months": 1}, {"days": 29}, False, True), |
| 142 |
({"months": 1}, {"days": 30}, False), |
| 143 |
({"months": 1}, {"days": 31}, True, False), |
| 144 |
({"weeks": 1}, {"days": 6}, False, True), |
| 145 |
({"weeks": 1}, {"days": 7}, False), |
| 146 |
({"weeks": 1}, {"days": 8}, True, False), |
| 147 |
({"days": 1}, {"seconds": 24 * 60 * 60 - 1}, False, True), |
| 148 |
({"days": 1}, {"seconds": 24 * 60 * 60}, False), |
| 149 |
({"days": 1}, {"seconds": 24 * 60 * 60 + 1}, True, False), |
| 150 |
], |
| 151 |
"<=": [ |
| 152 |
({"years": 1}, {"days": 365}, True), |
| 153 |
({"months": 1}, {"days": 30}, True), |
| 154 |
], |
| 155 |
">": [ |
| 156 |
|
| 157 |
*[({prop: 1}, {prop: 1}, False) for prop in nominal_units], |
| 158 |
*[({prop: 2}, {prop: 1}, True, False) for prop in nominal_units], |
| 159 |
|
| 160 |
({"years": 1}, {"months": 12}, True, False), |
| 161 |
({"years": 1}, {"days": 364}, True, False), |
| 162 |
({"years": 1}, {"days": 365}, False), |
| 163 |
({"years": 1}, {"days": 366}, False, True), |
| 164 |
({"months": 1}, {"days": 29}, True, False), |
| 165 |
({"months": 1}, {"days": 30}, False), |
| 166 |
({"months": 1}, {"days": 31}, False, True), |
| 167 |
({"weeks": 1}, {"days": 6}, True, False), |
| 168 |
({"weeks": 1}, {"days": 7}, False), |
| 169 |
({"weeks": 1}, {"days": 8}, False, True), |
| 170 |
({"days": 1}, {"seconds": 24 * 60 * 60 - 1}, True, False), |
| 171 |
({"days": 1}, {"seconds": 24 * 60 * 60}, False), |
| 172 |
({"days": 1}, {"seconds": 24 * 60 * 60 + 1}, False, True), |
| 173 |
], |
| 174 |
">=": [ |
| 175 |
({"years": 1}, {"days": 365}, True), |
| 176 |
({"months": 1}, {"days": 30}, True), |
| 177 |
] |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
def get_timepoint_subtract_tests(): |
| 182 |
"""Yield tests for subtracting one timepoint from another.""" |
| 183 |
return [ |
| 184 |
( |
| 185 |
{"year": 44, "month_of_year": 1, "day_of_month": 4, |
| 186 |
"hour_of_day": 5, "minute_of_hour": 1, "second_of_minute": 2, |
| 187 |
"time_zone_hour": 0, "time_zone_minute": 0}, |
| 188 |
{"year": 41, "month_of_year": 12, "day_of_month": 2, |
| 189 |
"hour_of_day": 4, "minute_of_hour": 23, "second_of_minute": 1, |
| 190 |
"time_zone_hour": 3, "time_zone_minute": 20}, |
| 191 |
"P763DT3H58M1S" |
| 192 |
), |
| 193 |
( |
| 194 |
{"year": 41, "month_of_year": 12, "day_of_month": 2, |
| 195 |
"hour_of_day": 4, "minute_of_hour": 23, "second_of_minute": 1, |
| 196 |
"time_zone_hour": 3, "time_zone_minute": 20}, |
| 197 |
{"year": 44, "month_of_year": 1, "day_of_month": 4, |
| 198 |
"hour_of_day": 5, "minute_of_hour": 1, "second_of_minute": 2, |
| 199 |
"time_zone_hour": 0, "time_zone_minute": 0}, |
| 200 |
"-P763DT3H58M1S" |
| 201 |
), |
| 202 |
( |
| 203 |
{"year": 1991, "month_of_year": 6, "day_of_month": 3, |
| 204 |
"hour_of_day": 0, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 205 |
{"year": 1991, "month_of_year": 5, "day_of_month": 4, |
| 206 |
"hour_of_day": 5, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 207 |
"P29DT19H" |
| 208 |
), |
| 209 |
( |
| 210 |
{"year": 1969, "month_of_year": 7, "day_of_month": 20, |
| 211 |
"hour_of_day": 20, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 212 |
{"year": 1969, "month_of_year": 7, "day_of_month": 20, |
| 213 |
"hour_of_day": 19, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 214 |
"PT1H" |
| 215 |
), |
| 216 |
|
| 217 |
( |
| 218 |
{"year": 1969, "month_of_year": 7, "day_of_month": 20, |
| 219 |
"hour_of_day": 19, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 220 |
{"year": 1969, "month_of_year": 7, "day_of_month": 20, |
| 221 |
"hour_of_day": 20, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 222 |
"-PT1H" |
| 223 |
), |
| 224 |
( |
| 225 |
{"year": 1991, "month_of_year": 5, "day_of_month": 4, |
| 226 |
"hour_of_day": 5, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 227 |
{"year": 1991, "month_of_year": 6, "day_of_month": 3, |
| 228 |
"hour_of_day": 0, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 229 |
"-P29DT19H" |
| 230 |
), |
| 231 |
( |
| 232 |
{"year": 2014, "month_of_year": 1, "day_of_month": 1, |
| 233 |
"hour_of_day": 0, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 234 |
{"year": 2013, "month_of_year": 12, "day_of_month": 31, |
| 235 |
"hour_of_day": 23, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 236 |
"PT1H" |
| 237 |
), |
| 238 |
( |
| 239 |
{"year": 2013, "month_of_year": 12, "day_of_month": 31, |
| 240 |
"hour_of_day": 23, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 241 |
{"year": 2014, "month_of_year": 1, "day_of_month": 1, |
| 242 |
"hour_of_day": 0, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 243 |
"-PT1H" |
| 244 |
), |
| 245 |
( |
| 246 |
{"year": 2014, "month_of_year": 1, "day_of_month": 1, |
| 247 |
"hour_of_day": 0, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 248 |
{"year": 2013, "month_of_year": 12, "day_of_month": 1, |
| 249 |
"hour_of_day": 0, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 250 |
"P31D" |
| 251 |
), |
| 252 |
( |
| 253 |
{"year": 2013, "month_of_year": 12, "day_of_month": 1, |
| 254 |
"hour_of_day": 0, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 255 |
{"year": 2014, "month_of_year": 1, "day_of_month": 1, |
| 256 |
"hour_of_day": 0, "time_zone_hour": 0, "time_zone_minute": 0}, |
| 257 |
"-P31D" |
| 258 |
), |
| 259 |
( |
| 260 |
{"year": 44, "month_of_year": 1, "day_of_month": 4, |
| 261 |
"hour_of_day": 5, "minute_of_hour": 1, "second_of_minute": 2, |
| 262 |
"time_zone_hour": 0, "time_zone_minute": 0}, |
| 263 |
{"year": 41, "month_of_year": 12, "day_of_month": 2, |
| 264 |
"hour_of_day": 13, "minute_of_hour": 23, "second_of_minute": 1, |
| 265 |
"time_zone_hour": 3, "time_zone_minute": 20}, |
| 266 |
"P762DT18H58M1S" |
| 267 |
), |
| 268 |
( |
| 269 |
{"year": 41, "month_of_year": 12, "day_of_month": 2, |
| 270 |
"hour_of_day": 13, "minute_of_hour": 23, "second_of_minute": 1, |
| 271 |
"time_zone_hour": 3, "time_zone_minute": 20}, |
| 272 |
{"year": 44, "month_of_year": 1, "day_of_month": 4, |
| 273 |
"hour_of_day": 5, "minute_of_hour": 1, "second_of_minute": 2, |
| 274 |
"time_zone_hour": 0, "time_zone_minute": 0}, |
| 275 |
"-P762DT18H58M1S" |
| 276 |
), |
| 277 |
] |
| 278 |
|
| 279 |
|
| 280 |
def get_timepoint_comparison_tests(): |
| 281 |
"""Yield tests for executing comparison operators on TimePoints. |
| 282 |
|
| 283 |
All True "==" tests will be carried out for "<=" & ">= too. Likewise |
| 284 |
all True "<" & "> tests will be carried out for "<=" & ">=" respectively. |
| 285 |
|
| 286 |
Tuple format --> test: |
| 287 |
(args1, args2, bool1 [, bool2]) --> |
| 288 |
TimePoint(**args1) <operator> TimePoint(**args2) is bool1 |
| 289 |
& the reverse: |
| 290 |
TimePoint(**args2) <operator> TimePoint(**args1) is bool2 if |
| 291 |
bool2 supplied else bool1 |
| 292 |
""" |
| 293 |
base_YMD = {"year": 2020, "month_of_year": 3, "day_of_month": 14} |
| 294 |
trunc = {"truncated": True} |
| 295 |
return { |
| 296 |
"==": [ |
| 297 |
(base_YMD, base_YMD, True), |
| 298 |
({"year": 2020, "month_of_year": 2, "day_of_month": 5}, |
| 299 |
{"year": 2020, "day_of_year": 36}, |
| 300 |
True), |
| 301 |
({"year": 2019, "month_of_year": 12, "day_of_month": 30}, |
| 302 |
{"year": 2020, "week_of_year": 1, "day_of_week": 1}, |
| 303 |
True), |
| 304 |
({"year": 2019, "day_of_year": 364}, |
| 305 |
{"year": 2020, "week_of_year": 1, "day_of_week": 1}, |
| 306 |
True), |
| 307 |
({**base_YMD, "hour_of_day": 9, "time_zone_hour": 0}, |
| 308 |
{**base_YMD, "hour_of_day": 11, "minute_of_hour": 30, |
| 309 |
"time_zone_hour": 2, "time_zone_minute": 30}, |
| 310 |
True), |
| 311 |
({"month_of_year": 3, "day_of_month": 14, **trunc}, |
| 312 |
{"month_of_year": 3, "day_of_month": 14, **trunc}, |
| 313 |
True), |
| 314 |
|
| 315 |
({"month_of_year": 2, "day_of_month": 5, **trunc}, |
| 316 |
{"day_of_year": 36, **trunc}, |
| 317 |
False), |
| 318 |
({"month_of_year": 12, "day_of_month": 30, **trunc}, |
| 319 |
{"week_of_year": 1, "day_of_week": 1, **trunc}, |
| 320 |
False), |
| 321 |
({"day_of_year": 364, **trunc}, |
| 322 |
{"week_of_year": 1, "day_of_week": 1, **trunc}, |
| 323 |
False) |
| 324 |
|
| 325 |
|
| 326 |
], |
| 327 |
"<": [ |
| 328 |
(base_YMD, base_YMD, False), |
| 329 |
({"year": 2019}, {"year": 2020}, True, False), |
| 330 |
({"year": -1}, {"year": 1}, True, False), |
| 331 |
({"year": 2020, "month_of_year": 2}, |
| 332 |
{"year": 2020, "month_of_year": 3}, |
| 333 |
True, False), |
| 334 |
({"year": 2020, "month_of_year": 2, "day_of_month": 5}, |
| 335 |
{"year": 2020, "month_of_year": 2, "day_of_month": 6}, |
| 336 |
True, False), |
| 337 |
({**base_YMD, "hour_of_day": 9}, {**base_YMD, "hour_of_day": 10}, |
| 338 |
True, False), |
| 339 |
({**base_YMD, "hour_of_day": 9, "time_zone_hour": 0}, |
| 340 |
{**base_YMD, "hour_of_day": 7, "time_zone_hour": -3}, |
| 341 |
True, False), |
| 342 |
({"day_of_month": 3, **trunc}, {"day_of_month": 4, **trunc}, |
| 343 |
True, False), |
| 344 |
({"month_of_year": 1, "day_of_month": 3, **trunc}, |
| 345 |
{"month_of_year": 1, "day_of_month": 4, **trunc}, |
| 346 |
True, False) |
| 347 |
], |
| 348 |
">": [ |
| 349 |
(base_YMD, base_YMD, False), |
| 350 |
({"year": 2019}, {"year": 2020}, False, True), |
| 351 |
({"year": -1}, {"year": 1}, False, True), |
| 352 |
({"year": 2020, "month_of_year": 2}, |
| 353 |
{"year": 2020, "month_of_year": 3}, |
| 354 |
False, True), |
| 355 |
({"year": 2020, "month_of_year": 2, "day_of_month": 5}, |
| 356 |
{"year": 2020, "month_of_year": 2, "day_of_month": 6}, |
| 357 |
False, True), |
| 358 |
({**base_YMD, "hour_of_day": 9}, {**base_YMD, "hour_of_day": 10}, |
| 359 |
False, True), |
| 360 |
({**base_YMD, "hour_of_day": 9, "time_zone_hour": 0}, |
| 361 |
{**base_YMD, "hour_of_day": 7, "time_zone_hour": -3}, |
| 362 |
False, True), |
| 363 |
({"day_of_month": 3, **trunc}, {"day_of_month": 4, **trunc}, |
| 364 |
False, True), |
| 365 |
({"month_of_year": 1, "day_of_month": 3, **trunc}, |
| 366 |
{"month_of_year": 1, "day_of_month": 4, **trunc}, |
| 367 |
False, True) |
| 368 |
] |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
def get_timepoint_bounds_tests(): |
| 373 |
"""Yield tests for checking out of bounds TimePoints.""" |
| 374 |
return { |
| 375 |
"in_bounds": [ |
| 376 |
{"year": 2020, "month_of_year": 2, "day_of_month": 29}, |
| 377 |
{"truncated": True, "month_of_year": 2, "day_of_month": 29}, |
| 378 |
{"year": 2020, "week_of_year": 53}, |
| 379 |
{"truncated": True, "week_of_year": 53}, |
| 380 |
{"year": 2020, "day_of_year": 366}, |
| 381 |
{"truncated": True, "day_of_year": 366}, |
| 382 |
|
| 383 |
{"year": 2019, "hour_of_day": 24}, |
| 384 |
{"year": 2019, "time_zone_hour": 99}, |
| 385 |
{"year": 2019, "time_zone_hour": 0, "time_zone_minute": -1}, |
| 386 |
{"year": 2019, "time_zone_hour": 0, "time_zone_minute": 1}, |
| 387 |
{"year": 2019, "time_zone_hour": -1, "time_zone_minute": -1}, |
| 388 |
], |
| 389 |
"out_of_bounds": [ |
| 390 |
{"year": 2019, "month_of_year": 0}, |
| 391 |
{"year": 2019, "month_of_year": 13}, |
| 392 |
{"year": 2019, "month_of_year": 1, "day_of_month": 0}, |
| 393 |
{"year": 2019, "month_of_year": 1, "day_of_month": 32}, |
| 394 |
{"year": 2019, "month_of_year": 2, "day_of_month": 29}, |
| 395 |
{"truncated": True, "month_of_year": 1, "day_of_month": 32}, |
| 396 |
|
| 397 |
{"year": 2019, "week_of_year": 0}, |
| 398 |
{"year": 2019, "week_of_year": 53}, |
| 399 |
{"year": 2019, "week_of_year": 1, "day_of_week": 0}, |
| 400 |
{"year": 2019, "week_of_year": 1, "day_of_week": 8}, |
| 401 |
|
| 402 |
{"year": 2019, "day_of_year": 0}, |
| 403 |
{"year": 2019, "day_of_year": 366}, |
| 404 |
|
| 405 |
{"year": 2019, "hour_of_day": -1}, |
| 406 |
{"year": 2019, "hour_of_day": 25}, |
| 407 |
{"year": 2019, "hour_of_day": 10, "hour_of_day_decimal": -0.1}, |
| 408 |
{"year": 2019, "hour_of_day": 10, "hour_of_day_decimal": 1}, |
| 409 |
{"year": 2019, "hour_of_day": 24, "hour_of_day_decimal": 0.1}, |
| 410 |
|
| 411 |
{"year": 2019, "hour_of_day": 10, "minute_of_hour": -1}, |
| 412 |
{"year": 2019, "hour_of_day": 10, "minute_of_hour": 60}, |
| 413 |
{"year": 2019, "hour_of_day": 24, "minute_of_hour": 1}, |
| 414 |
{"year": 2019, "hour_of_day": 10, "minute_of_hour": 1, |
| 415 |
"minute_of_hour_decimal": -0.1}, |
| 416 |
{"year": 2019, "hour_of_day": 10, "minute_of_hour": 1, |
| 417 |
"minute_of_hour_decimal": 1}, |
| 418 |
|
| 419 |
{"year": 2019, "hour_of_day": 10, "minute_of_hour": 1, |
| 420 |
"second_of_minute": -1}, |
| 421 |
{"year": 2019, "hour_of_day": 10, "minute_of_hour": 1, |
| 422 |
"second_of_minute": 60}, |
| 423 |
{"year": 2019, "hour_of_day": 24, "minute_of_hour": 1, |
| 424 |
"second_of_minute": 1}, |
| 425 |
{"year": 2019, "hour_of_day": 10, "minute_of_hour": 1, |
| 426 |
"second_of_minute": 1, "second_of_minute_decimal": -0.1}, |
| 427 |
{"year": 2019, "hour_of_day": 10, "minute_of_hour": 1, |
| 428 |
"second_of_minute": 1, "second_of_minute_decimal": 1}, |
| 429 |
|
| 430 |
{"year": 2019, "time_zone_hour": -100}, |
| 431 |
{"year": 2019, "time_zone_hour": 100}, |
| 432 |
{"year": 2019, "time_zone_hour": 0, "time_zone_minute": -60}, |
| 433 |
{"year": 2019, "time_zone_hour": 1, "time_zone_minute": -1}, |
| 434 |
{"year": 2019, "time_zone_hour": 1, "time_zone_minute": 60}, |
| 435 |
{"year": 2019, "time_zone_hour": -1, "time_zone_minute": 1} |
| 436 |
] |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
def get_timepoint_conflicting_input_tests(): |
| 441 |
"""Yield tests for checking TimePoints initialized with incompatible |
| 442 |
inputs.""" |
| 443 |
return [ |
| 444 |
{"year": 2020, "day_of_year": 1, "month_of_year": 1}, |
| 445 |
{"year": 2020, "day_of_year": 1, "day_of_month": 1}, |
| 446 |
{"year": 2020, "day_of_year": 6, "week_of_year": 2}, |
| 447 |
{"year": 2020, "day_of_year": 1, "day_of_week": 3}, |
| 448 |
|
| 449 |
{"year": 2020, "month_of_year": 2, "week_of_year": 5}, |
| 450 |
{"year": 2020, "month_of_year": 2, "day_of_week": 6}, |
| 451 |
{"year": 2020, "day_of_month": 6, "week_of_year": 2}, |
| 452 |
{"year": 2020, "day_of_month": 1, "day_of_week": 3} |
| 453 |
] |
| 454 |
|
| 455 |
|
| 456 |
def run_comparison_tests(data_class, test_cases): |
| 457 |
""" |
| 458 |
Args: |
| 459 |
data_class: E.g. Duration or TimePoint |
| 460 |
test_cases (dict): Of the form {"==": [...], "<": [...], ...} |
| 461 |
""" |
| 462 |
for op in test_cases: |
| 463 |
for case in test_cases[op]: |
| 464 |
lhs = data_class(**case[0]) |
| 465 |
rhs = data_class(**case[1]) |
| 466 |
expected = {"forward": case[2], |
| 467 |
"reverse": case[3] if len(case) == 4 else case[2]} |
| 468 |
if op == "==": |
| 469 |
tests = [ |
| 470 |
{"op": "==", "forward": lhs == rhs, "reverse": rhs == lhs}] |
| 471 |
if True in expected.values(): |
| 472 |
tests.append({"op": "<=", "forward": lhs <= rhs, |
| 473 |
"reverse": rhs <= lhs}) |
| 474 |
tests.append({"op": ">=", "forward": lhs >= rhs, |
| 475 |
"reverse": rhs >= lhs}) |
| 476 |
if op == "<": |
| 477 |
tests = [ |
| 478 |
{"op": "<", "forward": lhs < rhs, "reverse": rhs < lhs}] |
| 479 |
if True in expected.values(): |
| 480 |
tests.append({"op": "<=", "forward": lhs <= rhs, |
| 481 |
"reverse": rhs <= lhs}) |
| 482 |
if op == "<=": |
| 483 |
tests = [ |
| 484 |
{"op": "<=", "forward": lhs <= rhs, "reverse": rhs <= lhs}] |
| 485 |
if op == ">": |
| 486 |
tests = [ |
| 487 |
{"op": ">", "forward": lhs > rhs, "reverse": rhs > lhs}] |
| 488 |
if True in expected.values(): |
| 489 |
tests.append({"op": ">=", "forward": lhs >= rhs, |
| 490 |
"reverse": rhs >= lhs}) |
| 491 |
if op == ">=": |
| 492 |
tests = [ |
| 493 |
{"op": ">=", "forward": lhs >= rhs, "reverse": rhs >= lhs}] |
| 494 |
|
| 495 |
for test in tests: |
| 496 |
assert test["forward"] is expected["forward"], ( |
| 497 |
"{0} {1} {2}".format(lhs, test["op"], rhs)) |
| 498 |
assert test["reverse"] is expected["reverse"], ( |
| 499 |
"{0} {1} {2}".format(rhs, test["op"], lhs)) |
| 500 |
|
| 501 |
if op == "==": |
| 502 |
test = lhs != rhs |
| 503 |
assert test is not expected["forward"], ( |
| 504 |
"{0} != {1}".format(lhs, rhs)) |
| 505 |
test = hash(lhs) == hash(rhs) |
| 506 |
assert test is expected["forward"], ( |
| 507 |
"hash of {0} == hash of {1}".format(rhs, lhs)) |
| 508 |
|
| 509 |
|
| 510 |
class TestDataModel(unittest.TestCase): |
| 511 |
"""Test the functionality of data model manipulation.""" |
| 512 |
|
| 513 |
def test_days_in_year_range(self): |
| 514 |
"""Test the summing-over-days-in-year-range shortcut code.""" |
| 515 |
for start_year in range(-401, 2): |
| 516 |
for end_year in range(start_year, 2): |
| 517 |
test_days = data.get_days_in_year_range( |
| 518 |
start_year, end_year) |
| 519 |
control_days = 0 |
| 520 |
for year in range(start_year, end_year + 1): |
| 521 |
control_days += data.get_days_in_year(year) |
| 522 |
self.assertEqual( |
| 523 |
control_days, test_days, "days in %s to %s" % ( |
| 524 |
start_year, end_year) |
| 525 |
) |
| 526 |
|
| 527 |
def test_timeduration(self): |
| 528 |
"""Test the Duration class methods.""" |
| 529 |
for test_props, method, method_args, ctrl_results in ( |
| 530 |
get_timeduration_tests()): |
| 531 |
duration = data.Duration(**test_props) |
| 532 |
duration_method = getattr(duration, method) |
| 533 |
test_results = duration_method(*method_args) |
| 534 |
self.assertEqual( |
| 535 |
test_results, ctrl_results, |
| 536 |
"%s -> %s(%s)" % (test_props, method, method_args) |
| 537 |
) |
| 538 |
|
| 539 |
def test_duration_float_args(self): |
| 540 |
"""Test that floats passed to Duration() init are handled correctly.""" |
| 541 |
for kwarg in ["years", "months", "weeks", "days"]: |
| 542 |
with self.assertRaises(BadInputError): |
| 543 |
data.Duration(**{kwarg: 1.5}) |
| 544 |
for kwarg, expected_secs in [("hours", 5400), ("minutes", 90), |
| 545 |
("seconds", 1.5)]: |
| 546 |
self.assertEqual(data.Duration(**{kwarg: 1.5}).get_seconds(), |
| 547 |
expected_secs) |
| 548 |
|
| 549 |
def test_duration_in_weeks(self): |
| 550 |
"""Test the Duration class when the week arg is supplied.""" |
| 551 |
dur = data.Duration(weeks=4) |
| 552 |
self.assertEqual(dur.get_is_in_weeks(), True) |
| 553 |
|
| 554 |
for kwarg, expected_days in [ |
| 555 |
("years", 365 + 28), ("months", 30 + 28), ("days", 1 + 28), |
| 556 |
("hours", 28), ("minutes", 28), ("seconds", 28)]: |
| 557 |
dur = data.Duration(weeks=4, **{kwarg: 1}) |
| 558 |
self.assertFalse(dur.get_is_in_weeks()) |
| 559 |
self.assertIsNone(dur.weeks) |
| 560 |
self.assertEqual(dur.get_days_and_seconds()[0], expected_days) |
| 561 |
|
| 562 |
def test_duration_to_weeks(self): |
| 563 |
"""Test converting Duration in days to Duration in weeks""" |
| 564 |
duration_in_days = data.Duration(days=365).to_weeks() |
| 565 |
duration_in_weeks = data.Duration(weeks=52) |
| 566 |
self.assertEqual(duration_in_days.weeks, duration_in_weeks.weeks) |
| 567 |
|
| 568 |
def test_duration_to_days(self): |
| 569 |
"""Test converting Duration in weeks to Duration in days""" |
| 570 |
dur = data.Duration(weeks=4) |
| 571 |
self.assertEqual(dur.to_days().days, 28) |
| 572 |
|
| 573 |
def test_duration_comparison(self): |
| 574 |
"""Test the Duration rich comparison methods and hashing.""" |
| 575 |
run_comparison_tests(data.Duration, get_duration_comparison_tests()) |
| 576 |
dur = data.Duration(days=1) |
| 577 |
for var in [7, 'foo', (1, 2), data.TimePoint(year=2000)]: |
| 578 |
self.assertFalse(dur == var) |
| 579 |
with self.assertRaises(TypeError): |
| 580 |
dur < var |
| 581 |
|
| 582 |
def test_timeduration_add_week(self): |
| 583 |
"""Test the Duration not in weeks add Duration in weeks.""" |
| 584 |
self.assertEqual( |
| 585 |
str(data.Duration(days=7) + data.Duration(weeks=1)), |
| 586 |
"P14D") |
| 587 |
|
| 588 |
def test_duration_floordiv(self): |
| 589 |
"""Test the existing dunder floordir, which will be removed when we |
| 590 |
move to Python 3""" |
| 591 |
duration = data.Duration(years=4, months=4, days=4, hours=4, |
| 592 |
minutes=4, seconds=4) |
| 593 |
expected = data.Duration(years=2, months=2, days=2, hours=2, |
| 594 |
minutes=2, seconds=2) |
| 595 |
duration //= 2 |
| 596 |
self.assertEqual(duration, expected) |
| 597 |
|
| 598 |
def test_duration_in_weeks_floordiv(self): |
| 599 |
"""Test the existing dunder floordir, which will be removed when we |
| 600 |
move to Python 3""" |
| 601 |
duration = data.Duration(weeks=4) |
| 602 |
duration //= 2 |
| 603 |
self.assertEqual(2, duration.weeks) |
| 604 |
|
| 605 |
def test_duration_subtract(self): |
| 606 |
"""Test subtracting a duration from a timepoint.""" |
| 607 |
for test in get_duration_subtract_tests(): |
| 608 |
start_point = data.TimePoint(**test["start"]) |
| 609 |
test_duration = data.Duration(**test["duration"]) |
| 610 |
end_point = data.TimePoint(**test["result"]) |
| 611 |
test_subtract = (start_point - test_duration).to_calendar_date() |
| 612 |
self.assertEqual(test_subtract, end_point, |
| 613 |
"%s - %s" % (start_point, test_duration)) |
| 614 |
|
| 615 |
def test_timepoint_comparison(self): |
| 616 |
"""Test the TimePoint rich comparison methods and hashing.""" |
| 617 |
run_comparison_tests(data.TimePoint, get_timepoint_comparison_tests()) |
| 618 |
point = data.TimePoint(year=2000) |
| 619 |
for var in [7, 'foo', (1, 2), data.Duration(days=1)]: |
| 620 |
self.assertFalse(point == var) |
| 621 |
with self.assertRaises(TypeError): |
| 622 |
point < var |
| 623 |
|
| 624 |
day_month_point = data.TimePoint(month_of_year=2, day_of_month=5, |
| 625 |
truncated=True) |
| 626 |
ordinal_point = data.TimePoint(day_of_year=36, truncated=True) |
| 627 |
with self.assertRaises(TypeError): |
| 628 |
day_month_point < ordinal_point |
| 629 |
|
| 630 |
def test_timepoint_plus_float_time_duration_day_of_month_type(self): |
| 631 |
"""Test (TimePoint + Duration).day_of_month is an int.""" |
| 632 |
time_point = data.TimePoint(year=2000) + data.Duration(seconds=1.0) |
| 633 |
self.assertEqual(type(time_point.day_of_month), int) |
| 634 |
|
| 635 |
def test_timepoint_subtract(self): |
| 636 |
"""Test subtracting one time point from another.""" |
| 637 |
for test_props1, test_props2, ctrl_string in ( |
| 638 |
get_timepoint_subtract_tests()): |
| 639 |
point1 = data.TimePoint(**test_props1) |
| 640 |
point2 = data.TimePoint(**test_props2) |
| 641 |
test_string = str(point1 - point2) |
| 642 |
self.assertEqual(test_string, ctrl_string, |
| 643 |
"%s - %s" % (point1, point2)) |
| 644 |
|
| 645 |
def test_timepoint_add_duration(self): |
| 646 |
"""Test adding a duration to a timepoint""" |
| 647 |
seconds_added = 5 |
| 648 |
timepoint = data.TimePoint(year=1900, month_of_year=1, day_of_month=1, |
| 649 |
hour_of_day=1, minute_of_hour=1) |
| 650 |
duration = data.Duration(seconds=seconds_added) |
| 651 |
t = timepoint + duration |
| 652 |
self.assertEqual(seconds_added, t.second_of_minute) |
| 653 |
|
| 654 |
def test_timepoint_add_duration_without_minute(self): |
| 655 |
"""Test adding a duration to a timepoint""" |
| 656 |
seconds_added = 5 |
| 657 |
timepoint = data.TimePoint(year=1900, month_of_year=1, day_of_month=1, |
| 658 |
hour_of_day=1) |
| 659 |
duration = data.Duration(seconds=seconds_added) |
| 660 |
t = timepoint + duration |
| 661 |
self.assertEqual(seconds_added, t.second_of_minute) |
| 662 |
|
| 663 |
def test_timepoint_bounds(self): |
| 664 |
"""Test out of bounds TimePoints""" |
| 665 |
tests = get_timepoint_bounds_tests() |
| 666 |
for kwargs in tests["in_bounds"]: |
| 667 |
data.TimePoint(**kwargs) |
| 668 |
for kwargs in tests["out_of_bounds"]: |
| 669 |
with self.assertRaises(BadInputError) as cm: |
| 670 |
data.TimePoint(**kwargs) |
| 671 |
assert "out of bounds" in str(cm.exception) |
| 672 |
|
| 673 |
def test_timepoint_conflicting_inputs(self): |
| 674 |
"""Test TimePoints initialized with incompatible inputs""" |
| 675 |
tests = get_timepoint_conflicting_input_tests() |
| 676 |
for kwargs in tests: |
| 677 |
with self.assertRaises(BadInputError) as cm: |
| 678 |
data.TimePoint(**kwargs) |
| 679 |
assert "Conflicting input" in str(cm.exception) |
| 680 |
|
| 681 |
|
| 682 |
def test_timepoint_without_year(): |
| 683 |
"""Test that TimePoints cannot be init'd without a year unless |
| 684 |
truncated""" |
| 685 |
for kwargs in [{}, {"month_of_year": 2}, {"hour_of_day": 9}]: |
| 686 |
with pytest.raises(BadInputError) as exc: |
| 687 |
data.TimePoint(**kwargs) |
| 688 |
assert "Missing input: year" in str(exc.value) |
| 689 |
|
| 690 |
data.TimePoint(truncated=True, month_of_year=2) |
| 691 |
|