| 1 | from decimal import Decimal |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | from isoduration.parser.exceptions import InvalidFractional |
| 6 | from isoduration.parser.validation import validate_fractional |
| 7 | from isoduration.types import DateDuration, Duration, TimeDuration |
| 8 | |
| 9 | |
| 10 | @pytest.mark.parametrize( |
| 11 | "date_duration, time_duration", |
| 12 | ( |
| 13 | (DateDuration(), TimeDuration()), |
| 14 | (DateDuration(years=Decimal("1.3")), TimeDuration()), |
| 15 | (DateDuration(years=Decimal("1"), months=Decimal("2.5")), TimeDuration()), |
| 16 | ( |
| 17 | DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6.9")), |
| 18 | TimeDuration(), |
| 19 | ), |
| 20 | (DateDuration(weeks=Decimal("14.2")), TimeDuration()), |
| 21 | ( |
| 22 | DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6")), |
| 23 | TimeDuration(hours=Decimal("6.18")), |
| 24 | ), |
| 25 | ( |
| 26 | DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6")), |
| 27 | TimeDuration(hours=Decimal("6"), minutes=Decimal("18.11")), |
| 28 | ), |
| 29 | ( |
| 30 | DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6")), |
| 31 | TimeDuration( |
| 32 | hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11.42") |
| 33 | ), |
| 34 | ), |
| 35 | ( |
| 36 | DateDuration(months=Decimal("2"), days=Decimal("6")), |
| 37 | TimeDuration( |
| 38 | hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11.42") |
| 39 | ), |
| 40 | ), |
| 41 | ( |
| 42 | DateDuration(years=Decimal("1"), days=Decimal("6")), |
| 43 | TimeDuration( |
| 44 | hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11.42") |
| 45 | ), |
| 46 | ), |
| 47 | ( |
| 48 | DateDuration(years=Decimal("1"), months=Decimal("2")), |
| 49 | TimeDuration( |
| 50 | hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11.42") |
| 51 | ), |
| 52 | ), |
| 53 | ( |
| 54 | DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6")), |
| 55 | TimeDuration(minutes=Decimal("18"), seconds=Decimal("11.42")), |
| 56 | ), |
| 57 | ( |
| 58 | DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6")), |
| 59 | TimeDuration(hours=Decimal("6"), seconds=Decimal("11.42")), |
| 60 | ), |
| 61 | ( |
| 62 | DateDuration(days=Decimal("6")), |
| 63 | TimeDuration(hours=Decimal("6"), seconds=Decimal("11.42")), |
| 64 | ), |
| 65 | ( |
| 66 | DateDuration(years=Decimal("1"), days=Decimal("6")), |
| 67 | TimeDuration(minutes=Decimal("18.11")), |
| 68 | ), |
| 69 | ), |
| 70 | ) |
| 71 | def test_correct_fractional_duration(date_duration, time_duration): |
| 72 | validate_fractional(Duration(date_duration, time_duration)) |
| 73 | |
| 74 | |
| 75 | @pytest.mark.parametrize( |
| 76 | "date_duration, time_duration", |
| 77 | ( |
| 78 | ( |
| 79 | DateDuration(years=Decimal("1.5"), months=Decimal("2"), days=Decimal("6")), |
| 80 | TimeDuration( |
| 81 | hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11") |
| 82 | ), |
| 83 | ), |
| 84 | ( |
| 85 | DateDuration(years=Decimal("1"), months=Decimal("2.4"), days=Decimal("6")), |
| 86 | TimeDuration( |
| 87 | hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11") |
| 88 | ), |
| 89 | ), |
| 90 | ( |
| 91 | DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6.3")), |
| 92 | TimeDuration( |
| 93 | hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11") |
| 94 | ), |
| 95 | ), |
| 96 | ( |
| 97 | DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6")), |
| 98 | TimeDuration( |
| 99 | hours=Decimal("6.2"), minutes=Decimal("18"), seconds=Decimal("11") |
| 100 | ), |
| 101 | ), |
| 102 | ( |
| 103 | DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6")), |
| 104 | TimeDuration( |
| 105 | hours=Decimal("6"), minutes=Decimal("18.1"), seconds=Decimal("11") |
| 106 | ), |
| 107 | ), |
| 108 | ( |
| 109 | DateDuration(years=Decimal("1.5"), months=Decimal("2"), days=Decimal("6")), |
| 110 | TimeDuration( |
| 111 | hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11.42") |
| 112 | ), |
| 113 | ), |
| 114 | ( |
| 115 | DateDuration(years=Decimal("1"), months=Decimal("2.4"), days=Decimal("6")), |
| 116 | TimeDuration( |
| 117 | hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11.42") |
| 118 | ), |
| 119 | ), |
| 120 | ( |
| 121 | DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6.3")), |
| 122 | TimeDuration( |
| 123 | hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11.42") |
| 124 | ), |
| 125 | ), |
| 126 | ( |
| 127 | DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6")), |
| 128 | TimeDuration( |
| 129 | hours=Decimal("6.2"), minutes=Decimal("18"), seconds=Decimal("11.42") |
| 130 | ), |
| 131 | ), |
| 132 | ( |
| 133 | DateDuration(years=Decimal("1"), months=Decimal("2"), days=Decimal("6")), |
| 134 | TimeDuration( |
| 135 | hours=Decimal("6"), minutes=Decimal("18.1"), seconds=Decimal("11.42") |
| 136 | ), |
| 137 | ), |
| 138 | ( |
| 139 | DateDuration( |
| 140 | years=Decimal("1"), months=Decimal("2.003"), days=Decimal("6") |
| 141 | ), |
| 142 | TimeDuration( |
| 143 | hours=Decimal("6"), minutes=Decimal("18.3"), seconds=Decimal("11.42") |
| 144 | ), |
| 145 | ), |
| 146 | ( |
| 147 | DateDuration( |
| 148 | years=Decimal("1"), months=Decimal("2.003"), days=Decimal("6.1") |
| 149 | ), |
| 150 | TimeDuration( |
| 151 | hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11") |
| 152 | ), |
| 153 | ), |
| 154 | ( |
| 155 | DateDuration( |
| 156 | years=Decimal("1"), months=Decimal("2.003"), days=Decimal("6") |
| 157 | ), |
| 158 | TimeDuration( |
| 159 | hours=Decimal("6"), minutes=Decimal("18"), seconds=Decimal("11.42") |
| 160 | ), |
| 161 | ), |
| 162 | ( |
| 163 | DateDuration(years=Decimal("1.5")), |
| 164 | TimeDuration(seconds=Decimal("11.42")), |
| 165 | ), |
| 166 | ( |
| 167 | DateDuration(years=Decimal("1.5")), |
| 168 | TimeDuration(seconds=Decimal("11")), |
| 169 | ), |
| 170 | ( |
| 171 | DateDuration(years=Decimal("7"), days=Decimal("1.5")), |
| 172 | TimeDuration(seconds=Decimal("11")), |
| 173 | ), |
| 174 | ( |
| 175 | DateDuration(), |
| 176 | TimeDuration(hours=Decimal("3.5"), seconds=Decimal("11")), |
| 177 | ), |
| 178 | ), |
| 179 | ) |
| 180 | def test_incorrect_fractional_duration(date_duration, time_duration): |
| 181 | with pytest.raises(InvalidFractional): |
| 182 | validate_fractional(Duration(date_duration, time_duration)) |