| 1 |
from decimal import Decimal |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from isoduration.parser import exceptions, parse_duration |
| 6 |
from isoduration.types import DateDuration, Duration, TimeDuration |
| 7 |
|
| 8 |
|
| 9 |
@pytest.mark.parametrize( |
| 10 |
"duration, date_duration, time_duration", |
| 11 |
( |
| 12 |
|
| 13 |
("P0D", DateDuration(), TimeDuration()), |
| 14 |
("PT0S", DateDuration(), TimeDuration()), |
| 15 |
|
| 16 |
( |
| 17 |
"P3Y6M4DT12H30M5S", |
| 18 |
DateDuration(years=3, months=6, days=4), |
| 19 |
TimeDuration(hours=12, minutes=30, seconds=5), |
| 20 |
), |
| 21 |
( |
| 22 |
"P18Y9M4DT11H9M8S", |
| 23 |
DateDuration(years=18, months=9, days=4), |
| 24 |
TimeDuration(hours=11, minutes=9, seconds=8), |
| 25 |
), |
| 26 |
|
| 27 |
("P4Y5M18D", DateDuration(years=4, months=5, days=18), TimeDuration()), |
| 28 |
|
| 29 |
("PT2H3M4S", DateDuration(), TimeDuration(hours=2, minutes=3, seconds=4)), |
| 30 |
|
| 31 |
("P4Y", DateDuration(years=4), TimeDuration()), |
| 32 |
("P2W", DateDuration(weeks=2), TimeDuration()), |
| 33 |
("P1M", DateDuration(months=1), TimeDuration()), |
| 34 |
("P6D", DateDuration(days=6), TimeDuration()), |
| 35 |
|
| 36 |
("PT2H", DateDuration(), TimeDuration(hours=2)), |
| 37 |
("PT36H", DateDuration(), TimeDuration(hours=36)), |
| 38 |
("PT1M", DateDuration(), TimeDuration(minutes=1)), |
| 39 |
("PT22S", DateDuration(), TimeDuration(seconds=22)), |
| 40 |
("PT3M4S", DateDuration(), TimeDuration(minutes=3, seconds=4)), |
| 41 |
("PT6H59S", DateDuration(), TimeDuration(hours=6, seconds=59)), |
| 42 |
|
| 43 |
( |
| 44 |
"P1DT2H3M4S", |
| 45 |
DateDuration(days=1), |
| 46 |
TimeDuration(hours=2, minutes=3, seconds=4), |
| 47 |
), |
| 48 |
( |
| 49 |
"P3WT2H59S", |
| 50 |
DateDuration(weeks=3), |
| 51 |
TimeDuration(hours=2, seconds=59), |
| 52 |
), |
| 53 |
("P1DT2H", DateDuration(days=1), TimeDuration(hours=2)), |
| 54 |
("P1DT12H", DateDuration(days=1), TimeDuration(hours=12)), |
| 55 |
("P23DT23H", DateDuration(days=23), TimeDuration(hours=23)), |
| 56 |
( |
| 57 |
"P1DT2H3M", |
| 58 |
DateDuration(days=1), |
| 59 |
TimeDuration(hours=2, minutes=3), |
| 60 |
), |
| 61 |
|
| 62 |
("P0.5Y", DateDuration(years=Decimal("0.5")), TimeDuration()), |
| 63 |
("P0,5Y", DateDuration(years=Decimal("0.5")), TimeDuration()), |
| 64 |
( |
| 65 |
"PT8H3.5S", |
| 66 |
DateDuration(), |
| 67 |
TimeDuration(hours=8, seconds=Decimal("3.5")), |
| 68 |
), |
| 69 |
( |
| 70 |
"PT8H3,5S", |
| 71 |
DateDuration(), |
| 72 |
TimeDuration(hours=8, seconds=Decimal("3.5")), |
| 73 |
), |
| 74 |
("PT2.3H", DateDuration(), TimeDuration(hours=Decimal("2.3"))), |
| 75 |
( |
| 76 |
"PT22.22S", |
| 77 |
DateDuration(), |
| 78 |
TimeDuration(seconds=Decimal("22.22")), |
| 79 |
), |
| 80 |
("P1.3Y", DateDuration(years=Decimal("1.3")), TimeDuration()), |
| 81 |
( |
| 82 |
"P1Y2.5M", |
| 83 |
DateDuration(years=Decimal("1"), months=Decimal("2.5")), |
| 84 |
TimeDuration(), |
| 85 |
), |
| 86 |
( |
| 87 |
"P1Y2M6.9D", |
| 88 |
DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6.9")), |
| 89 |
TimeDuration(), |
| 90 |
), |
| 91 |
("P14.2W", DateDuration(weeks=Decimal("14.2")), TimeDuration()), |
| 92 |
( |
| 93 |
"P1Y2M6DT6.18H", |
| 94 |
DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6")), |
| 95 |
TimeDuration(hours=Decimal("6.18")), |
| 96 |
), |
| 97 |
( |
| 98 |
"P1Y2M6DT6H18.11M", |
| 99 |
DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6")), |
| 100 |
TimeDuration(hours=Decimal("6"), minutes=Decimal("18.11")), |
| 101 |
), |
| 102 |
( |
| 103 |
"P1Y2M6DT6H18M11.42S", |
| 104 |
DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6")), |
| 105 |
TimeDuration( |
| 106 |
hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11.42") |
| 107 |
), |
| 108 |
), |
| 109 |
( |
| 110 |
"P2M6DT6H18M11.42S", |
| 111 |
DateDuration(months=Decimal("2"), days=Decimal("6")), |
| 112 |
TimeDuration( |
| 113 |
hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11.42") |
| 114 |
), |
| 115 |
), |
| 116 |
( |
| 117 |
"P1Y6DT6H18M11.42S", |
| 118 |
DateDuration(years=Decimal("1"), days=Decimal("6")), |
| 119 |
TimeDuration( |
| 120 |
hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11.42") |
| 121 |
), |
| 122 |
), |
| 123 |
( |
| 124 |
"P1Y2MT6H18M11.42S", |
| 125 |
DateDuration(years=Decimal("1"), months=Decimal("2")), |
| 126 |
TimeDuration( |
| 127 |
hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11.42") |
| 128 |
), |
| 129 |
), |
| 130 |
( |
| 131 |
"P1Y2M6DT18M11.42S", |
| 132 |
DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6")), |
| 133 |
TimeDuration(minutes=Decimal("18"), seconds=Decimal("11.42")), |
| 134 |
), |
| 135 |
( |
| 136 |
"P1Y2M6DT6H11.42S", |
| 137 |
DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6")), |
| 138 |
TimeDuration(hours=Decimal("6"), seconds=Decimal("11.42")), |
| 139 |
), |
| 140 |
( |
| 141 |
"P6DT6H11.42S", |
| 142 |
DateDuration(days=Decimal("6")), |
| 143 |
TimeDuration(hours=Decimal("6"), seconds=Decimal("11.42")), |
| 144 |
), |
| 145 |
( |
| 146 |
"P1Y6DT18.11M", |
| 147 |
DateDuration(years=Decimal("1"), days=Decimal("6")), |
| 148 |
TimeDuration(minutes=Decimal("18.11")), |
| 149 |
), |
| 150 |
|
| 151 |
( |
| 152 |
"PT1e3S", |
| 153 |
DateDuration(), |
| 154 |
TimeDuration(seconds=Decimal("1000")), |
| 155 |
), |
| 156 |
( |
| 157 |
"PT1e-3S", |
| 158 |
DateDuration(), |
| 159 |
TimeDuration(seconds=Decimal("0.001")), |
| 160 |
), |
| 161 |
( |
| 162 |
"P1E11Y", |
| 163 |
DateDuration(years=Decimal("1e11")), |
| 164 |
TimeDuration(), |
| 165 |
), |
| 166 |
( |
| 167 |
"P1E+11Y", |
| 168 |
DateDuration(years=Decimal("1e11")), |
| 169 |
TimeDuration(), |
| 170 |
), |
| 171 |
( |
| 172 |
"P-1E11Y", |
| 173 |
DateDuration(years=Decimal("-1e11")), |
| 174 |
TimeDuration(), |
| 175 |
), |
| 176 |
( |
| 177 |
"PT1.03e2H", |
| 178 |
DateDuration(), |
| 179 |
TimeDuration(hours=Decimal("103")), |
| 180 |
), |
| 181 |
( |
| 182 |
"-PT1.03e2H", |
| 183 |
DateDuration(), |
| 184 |
TimeDuration(hours=Decimal("-103")), |
| 185 |
), |
| 186 |
( |
| 187 |
"P10,8E23W", |
| 188 |
DateDuration(weeks=Decimal("10.8e23")), |
| 189 |
TimeDuration(), |
| 190 |
), |
| 191 |
|
| 192 |
("PT000022.22", DateDuration(), TimeDuration()), |
| 193 |
( |
| 194 |
"PT000022.22H", |
| 195 |
DateDuration(), |
| 196 |
TimeDuration(hours=Decimal("22.22")), |
| 197 |
), |
| 198 |
|
| 199 |
("+P11D", DateDuration(days=11), TimeDuration()), |
| 200 |
("-P2Y", DateDuration(years=-2), TimeDuration()), |
| 201 |
("-P2W", DateDuration(weeks=-2), TimeDuration()), |
| 202 |
("-P2.2W", DateDuration(weeks=Decimal("-2.2")), TimeDuration()), |
| 203 |
( |
| 204 |
"-P3Y6M4DT12H30M5S", |
| 205 |
DateDuration(years=-3, months=-6, days=-4), |
| 206 |
TimeDuration(hours=-12, minutes=-30, seconds=-5), |
| 207 |
), |
| 208 |
( |
| 209 |
"-P1DT2H3M4S", |
| 210 |
DateDuration(days=-1), |
| 211 |
TimeDuration(hours=-2, minutes=-3, seconds=-4), |
| 212 |
), |
| 213 |
("P-20M", DateDuration(months=-20), TimeDuration()), |
| 214 |
("PT-6H3M", DateDuration(), TimeDuration(hours=-6, minutes=3)), |
| 215 |
("-PT-6H3M", DateDuration(), TimeDuration(hours=6, minutes=-3)), |
| 216 |
("-PT-6H+3M", DateDuration(), TimeDuration(hours=6, minutes=-3)), |
| 217 |
|
| 218 |
("P390D", DateDuration(days=390), TimeDuration()), |
| 219 |
("P20M", DateDuration(months=20), TimeDuration()), |
| 220 |
("P1000W", DateDuration(weeks=1000), TimeDuration()), |
| 221 |
("PT72H", DateDuration(), TimeDuration(hours=72)), |
| 222 |
("PT1000000M", DateDuration(), TimeDuration(minutes=1000000)), |
| 223 |
|
| 224 |
( |
| 225 |
"P0018-09-04T11:09:08", |
| 226 |
DateDuration(years=18, months=9, days=4), |
| 227 |
TimeDuration(hours=11, minutes=9, seconds=8), |
| 228 |
), |
| 229 |
( |
| 230 |
"P0003-06-04T12:30:00", |
| 231 |
DateDuration(years=3, months=6, days=4), |
| 232 |
TimeDuration(hours=12, minutes=30, seconds=0), |
| 233 |
), |
| 234 |
( |
| 235 |
"P00030604T123005", |
| 236 |
DateDuration(years=3, months=6, days=4), |
| 237 |
TimeDuration(hours=12, minutes=30, seconds=5), |
| 238 |
), |
| 239 |
|
| 240 |
( |
| 241 |
"-P0003-06-04T12:30:05", |
| 242 |
DateDuration(years=-3, months=-6, days=-4), |
| 243 |
TimeDuration(hours=-12, minutes=-30, seconds=-5), |
| 244 |
), |
| 245 |
( |
| 246 |
"-P00030604T123005", |
| 247 |
DateDuration(years=-3, months=-6, days=-4), |
| 248 |
TimeDuration(hours=-12, minutes=-30, seconds=-5), |
| 249 |
), |
| 250 |
), |
| 251 |
) |
| 252 |
def test_parse_duration(duration, date_duration, time_duration): |
| 253 |
assert parse_duration(duration) == Duration(date_duration, time_duration) |
| 254 |
|
| 255 |
|
| 256 |
@pytest.mark.parametrize( |
| 257 |
"duration, exception, error_msg", |
| 258 |
( |
| 259 |
|
| 260 |
("", exceptions.EmptyDuration, r"No duration information provided"), |
| 261 |
("P", exceptions.EmptyDuration, r"No duration information provided"), |
| 262 |
("ajaksdf", exceptions.EmptyDuration, r"No prefix provided"), |
| 263 |
("202hghf01217gg", exceptions.EmptyDuration, r"No prefix provided"), |
| 264 |
("2015-2", exceptions.EmptyDuration, r"No prefix provided"), |
| 265 |
("2015-01-01", exceptions.EmptyDuration, r"No prefix provided"), |
| 266 |
("2015-", exceptions.EmptyDuration, r"No prefix provided"), |
| 267 |
("2015-garbage", exceptions.EmptyDuration, r"No prefix provided"), |
| 268 |
("0003-06-04T12:30:00", exceptions.EmptyDuration, r"No prefix provided"), |
| 269 |
("00030604T123005", exceptions.EmptyDuration, r"No prefix provided"), |
| 270 |
("16.263772,-61.2329", exceptions.EmptyDuration, r"No prefix provided"), |
| 271 |
("1000", exceptions.EmptyDuration, r"No prefix provided"), |
| 272 |
("1Y2M", exceptions.EmptyDuration, r"No prefix provided"), |
| 273 |
|
| 274 |
( |
| 275 |
"P2M1Y", |
| 276 |
exceptions.IncorrectDesignator, |
| 277 |
r"Wrong date designator, or designator in the wrong order: Y", |
| 278 |
), |
| 279 |
( |
| 280 |
"P1D2H", |
| 281 |
exceptions.IncorrectDesignator, |
| 282 |
r"Wrong date designator, or designator in the wrong order: H", |
| 283 |
), |
| 284 |
( |
| 285 |
"P4W2D", |
| 286 |
exceptions.IncorrectDesignator, |
| 287 |
r"Wrong date designator, or designator in the wrong order: D", |
| 288 |
), |
| 289 |
( |
| 290 |
"P4Y2W", |
| 291 |
exceptions.IncorrectDesignator, |
| 292 |
r"Week is incompatible with any other date designator", |
| 293 |
), |
| 294 |
( |
| 295 |
"P36w", |
| 296 |
exceptions.IncorrectDesignator, |
| 297 |
r"Wrong date designator, or designator in the wrong order: w", |
| 298 |
), |
| 299 |
( |
| 300 |
"PT3S6M", |
| 301 |
exceptions.IncorrectDesignator, |
| 302 |
r"Wrong time designator, or designator in the wrong order: M", |
| 303 |
), |
| 304 |
( |
| 305 |
"PT36W", |
| 306 |
exceptions.IncorrectDesignator, |
| 307 |
r"Wrong time designator, or designator in the wrong order: W", |
| 308 |
), |
| 309 |
( |
| 310 |
"PT11ℵ", |
| 311 |
exceptions.IncorrectDesignator, |
| 312 |
r"Wrong time designator, or designator in the wrong order: ℵ", |
| 313 |
), |
| 314 |
( |
| 315 |
"PT6H8M43S8S", |
| 316 |
exceptions.IncorrectDesignator, |
| 317 |
r"Wrong time designator, or designator in the wrong order: S", |
| 318 |
), |
| 319 |
|
| 320 |
("PT", exceptions.NoTime, r"Wanted time, no time provided"), |
| 321 |
("P20MT", exceptions.NoTime, r"Wanted time, no time provided"), |
| 322 |
|
| 323 |
("P12'3W", exceptions.UnknownToken, r"Token not recognizable: '"), |
| 324 |
("P 8W", exceptions.UnknownToken, r"Token not recognizable: "), |
| 325 |
("PT(╯°□°)╯︵ ┻━┻", exceptions.UnknownToken, r"Token not recognizable: \("), |
| 326 |
("P∰", exceptions.UnknownToken, r"Token not recognizable: ∰"), |
| 327 |
("P💩", exceptions.UnknownToken, r"Token not recognizable: 💩"), |
| 328 |
("P0018/09/04T11:09:08", exceptions.UnknownToken, r"Token not recognizable: /"), |
| 329 |
|
| 330 |
( |
| 331 |
"P1YM5D", |
| 332 |
exceptions.UnparseableValue, |
| 333 |
r"Value could not be parsed as decimal: ", |
| 334 |
), |
| 335 |
( |
| 336 |
"PTS", |
| 337 |
exceptions.UnparseableValue, |
| 338 |
r"Value could not be parsed as decimal: ", |
| 339 |
), |
| 340 |
( |
| 341 |
"P12..80Y", |
| 342 |
exceptions.UnparseableValue, |
| 343 |
r"Value could not be parsed as decimal: 12..80", |
| 344 |
), |
| 345 |
( |
| 346 |
"PT11.0.42S", |
| 347 |
exceptions.UnparseableValue, |
| 348 |
r"Value could not be parsed as decimal: 11.0.42", |
| 349 |
), |
| 350 |
( |
| 351 |
"P1234T", |
| 352 |
exceptions.UnparseableValue, |
| 353 |
r"Value could not be parsed as datetime: 1234T", |
| 354 |
), |
| 355 |
( |
| 356 |
"P00030604T12300500", |
| 357 |
exceptions.UnparseableValue, |
| 358 |
r"Value could not be parsed as datetime: 00030604T12300500", |
| 359 |
), |
| 360 |
( |
| 361 |
"P00030604T12300", |
| 362 |
exceptions.UnparseableValue, |
| 363 |
r"Value could not be parsed as datetime: 00030604T12300", |
| 364 |
), |
| 365 |
( |
| 366 |
"P0003060T123005", |
| 367 |
exceptions.UnparseableValue, |
| 368 |
r"Value could not be parsed as datetime: 0003060T123005", |
| 369 |
), |
| 370 |
|
| 371 |
( |
| 372 |
"P1.5Y2M6DT6H18M11S", |
| 373 |
exceptions.InvalidFractional, |
| 374 |
r"Only the lowest order component can be fractional", |
| 375 |
), |
| 376 |
( |
| 377 |
"P1Y2.4M6DT6H18M11S", |
| 378 |
exceptions.InvalidFractional, |
| 379 |
r"Only the lowest order component can be fractional", |
| 380 |
), |
| 381 |
( |
| 382 |
"P1Y2M6.3DT6H18M11S", |
| 383 |
exceptions.InvalidFractional, |
| 384 |
r"Only the lowest order component can be fractional", |
| 385 |
), |
| 386 |
( |
| 387 |
"P1Y2M6DT6.2H18M11S", |
| 388 |
exceptions.InvalidFractional, |
| 389 |
r"Only the lowest order component can be fractional", |
| 390 |
), |
| 391 |
( |
| 392 |
"P1Y2M6DT6H18.1M11S", |
| 393 |
exceptions.InvalidFractional, |
| 394 |
r"Only the lowest order component can be fractional", |
| 395 |
), |
| 396 |
( |
| 397 |
"P1.5Y2M6DT6H18M11.42S", |
| 398 |
exceptions.InvalidFractional, |
| 399 |
r"Only the lowest order component can be fractional", |
| 400 |
), |
| 401 |
( |
| 402 |
"P1Y2.4M6DT6H18M11.42S", |
| 403 |
exceptions.InvalidFractional, |
| 404 |
r"Only the lowest order component can be fractional", |
| 405 |
), |
| 406 |
( |
| 407 |
"P1Y2M6.3DT6H18M11.42S", |
| 408 |
exceptions.InvalidFractional, |
| 409 |
r"Only the lowest order component can be fractional", |
| 410 |
), |
| 411 |
( |
| 412 |
"P1Y2M6DT6.2H18M11.42S", |
| 413 |
exceptions.InvalidFractional, |
| 414 |
r"Only the lowest order component can be fractional", |
| 415 |
), |
| 416 |
( |
| 417 |
"P1Y2M6DT6H18.1M11.42S", |
| 418 |
exceptions.InvalidFractional, |
| 419 |
r"Only the lowest order component can be fractional", |
| 420 |
), |
| 421 |
( |
| 422 |
"P1Y2.003M6DT6H18.3M11.42S", |
| 423 |
exceptions.InvalidFractional, |
| 424 |
r"Only the lowest order component can be fractional", |
| 425 |
), |
| 426 |
( |
| 427 |
"P1Y2.003M6.1DT6H18M11S", |
| 428 |
exceptions.InvalidFractional, |
| 429 |
r"Only the lowest order component can be fractional", |
| 430 |
), |
| 431 |
( |
| 432 |
"P1Y2.003M6DT6H18M11.42S", |
| 433 |
exceptions.InvalidFractional, |
| 434 |
r"Only the lowest order component can be fractional", |
| 435 |
), |
| 436 |
( |
| 437 |
"P1.5YT11.42S", |
| 438 |
exceptions.InvalidFractional, |
| 439 |
r"Only the lowest order component can be fractional", |
| 440 |
), |
| 441 |
( |
| 442 |
"P1.5YT11S", |
| 443 |
exceptions.InvalidFractional, |
| 444 |
r"Only the lowest order component can be fractional", |
| 445 |
), |
| 446 |
( |
| 447 |
"P7Y1.5DT11S", |
| 448 |
exceptions.InvalidFractional, |
| 449 |
r"Only the lowest order component can be fractional", |
| 450 |
), |
| 451 |
( |
| 452 |
"PT3.5H11S", |
| 453 |
exceptions.InvalidFractional, |
| 454 |
r"Only the lowest order component can be fractional", |
| 455 |
), |
| 456 |
), |
| 457 |
) |
| 458 |
def test_parse_duration_errors(duration, exception, error_msg): |
| 459 |
with pytest.raises(exception) as exc: |
| 460 |
parse_duration(duration) |
| 461 |
|
| 462 |
assert exc.match(error_msg) |