#983 replace uniitetest with pytest for test_relativedelta

Open jpurviance
Showing 1 of 1 files from the diff.

@@ -4,695 +4,653 @@
Loading
4 4
5 5
import calendar
6 6
from datetime import datetime, date, timedelta
7 -
import unittest
8 7
9 8
import pytest
10 9
11 10
from dateutil.relativedelta import relativedelta, MO, TU, WE, FR, SU
12 11
12 +
NOW = datetime(2003, 9, 17, 20, 54, 47, 282310)
13 +
TODAY = date(2003, 9, 17)
13 14
14 -
class RelativeDeltaTest(unittest.TestCase):
15 -
    now = datetime(2003, 9, 17, 20, 54, 47, 282310)
16 -
    today = date(2003, 9, 17)
17 15
18 -
    def testInheritance(self):
19 -
        # Ensure that relativedelta is inheritance-friendly.
20 -
        class rdChildClass(relativedelta):
21 -
            pass
16 +
def test_relative_delta_inheritance():
17 +
    # Ensure that relativedelta is inheritance-friendly.
18 +
    class rdChildClass(relativedelta):
19 +
        pass
22 20
23 -
        ccRD = rdChildClass(years=1, months=1, days=1, leapdays=1, weeks=1,
24 -
                            hours=1, minutes=1, seconds=1, microseconds=1)
21 +
    ccRD = rdChildClass(years=1, months=1, days=1, leapdays=1, weeks=1,
22 +
                        hours=1, minutes=1, seconds=1, microseconds=1)
25 23
26 -
        rd = relativedelta(years=1, months=1, days=1, leapdays=1, weeks=1,
27 -
                           hours=1, minutes=1, seconds=1, microseconds=1)
28 -
29 -
        self.assertEqual(type(ccRD + rd), type(ccRD),
30 -
                         msg='Addition does not inherit type.')
31 -
32 -
        self.assertEqual(type(ccRD - rd), type(ccRD),
33 -
                         msg='Subtraction does not inherit type.')
24 +
    rd = relativedelta(years=1, months=1, days=1, leapdays=1, weeks=1,
25 +
                       hours=1, minutes=1, seconds=1, microseconds=1)
34 26
35 -
        self.assertEqual(type(-ccRD), type(ccRD),
36 -
                         msg='Negation does not inherit type.')
27 +
    assert type(ccRD + rd) == type(ccRD), 'Addition does not inherit type.'
37 28
38 -
        self.assertEqual(type(ccRD * 5.0), type(ccRD),
39 -
                         msg='Multiplication does not inherit type.')
40 -
41 -
        self.assertEqual(type(ccRD / 5.0), type(ccRD),
42 -
                         msg='Division does not inherit type.')
43 -
44 -
    def testMonthEndMonthBeginning(self):
45 -
        self.assertEqual(relativedelta(datetime(2003, 1, 31, 23, 59, 59),
46 -
                                       datetime(2003, 3, 1, 0, 0, 0)),
47 -
                         relativedelta(months=-1, seconds=-1))
48 -
49 -
        self.assertEqual(relativedelta(datetime(2003, 3, 1, 0, 0, 0),
50 -
                                       datetime(2003, 1, 31, 23, 59, 59)),
51 -
                         relativedelta(months=1, seconds=1))
52 -
53 -
    def testMonthEndMonthBeginningLeapYear(self):
54 -
        self.assertEqual(relativedelta(datetime(2012, 1, 31, 23, 59, 59),
55 -
                                       datetime(2012, 3, 1, 0, 0, 0)),
56 -
                         relativedelta(months=-1, seconds=-1))
57 -
58 -
        self.assertEqual(relativedelta(datetime(2003, 3, 1, 0, 0, 0),
59 -
                                       datetime(2003, 1, 31, 23, 59, 59)),
60 -
                         relativedelta(months=1, seconds=1))
61 -
62 -
    def testNextMonth(self):
63 -
        self.assertEqual(self.now+relativedelta(months=+1),
64 -
                         datetime(2003, 10, 17, 20, 54, 47, 282310))
65 -
66 -
    def testNextMonthPlusOneWeek(self):
67 -
        self.assertEqual(self.now+relativedelta(months=+1, weeks=+1),
68 -
                         datetime(2003, 10, 24, 20, 54, 47, 282310))
69 -
70 -
    def testNextMonthPlusOneWeek10am(self):
71 -
        self.assertEqual(self.today +
72 -
                         relativedelta(months=+1, weeks=+1, hour=10),
73 -
                         datetime(2003, 10, 24, 10, 0))
74 -
75 -
    def testNextMonthPlusOneWeek10amDiff(self):
76 -
        self.assertEqual(relativedelta(datetime(2003, 10, 24, 10, 0),
77 -
                                       self.today),
78 -
                         relativedelta(months=+1, days=+7, hours=+10))
79 -
80 -
    def testOneMonthBeforeOneYear(self):
81 -
        self.assertEqual(self.now+relativedelta(years=+1, months=-1),
82 -
                         datetime(2004, 8, 17, 20, 54, 47, 282310))
83 -
84 -
    def testMonthsOfDiffNumOfDays(self):
85 -
        self.assertEqual(date(2003, 1, 27)+relativedelta(months=+1),
86 -
                         date(2003, 2, 27))
87 -
        self.assertEqual(date(2003, 1, 31)+relativedelta(months=+1),
88 -
                         date(2003, 2, 28))
89 -
        self.assertEqual(date(2003, 1, 31)+relativedelta(months=+2),
90 -
                         date(2003, 3, 31))
91 -
92 -
    def testMonthsOfDiffNumOfDaysWithYears(self):
93 -
        self.assertEqual(date(2000, 2, 28)+relativedelta(years=+1),
94 -
                         date(2001, 2, 28))
95 -
        self.assertEqual(date(2000, 2, 29)+relativedelta(years=+1),
96 -
                         date(2001, 2, 28))
97 -
98 -
        self.assertEqual(date(1999, 2, 28)+relativedelta(years=+1),
99 -
                         date(2000, 2, 28))
100 -
        self.assertEqual(date(1999, 3, 1)+relativedelta(years=+1),
101 -
                         date(2000, 3, 1))
102 -
        self.assertEqual(date(1999, 3, 1)+relativedelta(years=+1),
103 -
                         date(2000, 3, 1))
104 -
105 -
        self.assertEqual(date(2001, 2, 28)+relativedelta(years=-1),
106 -
                         date(2000, 2, 28))
107 -
        self.assertEqual(date(2001, 3, 1)+relativedelta(years=-1),
108 -
                         date(2000, 3, 1))
109 -
110 -
    def testNextFriday(self):
111 -
        self.assertEqual(self.today+relativedelta(weekday=FR),
112 -
                         date(2003, 9, 19))
113 -
114 -
    def testNextFridayInt(self):
115 -
        self.assertEqual(self.today+relativedelta(weekday=calendar.FRIDAY),
116 -
                         date(2003, 9, 19))
117 -
118 -
    def testLastFridayInThisMonth(self):
119 -
        self.assertEqual(self.today+relativedelta(day=31, weekday=FR(-1)),
120 -
                         date(2003, 9, 26))
121 -
122 -
    def testNextWednesdayIsToday(self):
123 -
        self.assertEqual(self.today+relativedelta(weekday=WE),
124 -
                         date(2003, 9, 17))
125 -
126 -
    def testNextWenesdayNotToday(self):
127 -
        self.assertEqual(self.today+relativedelta(days=+1, weekday=WE),
128 -
                         date(2003, 9, 24))
129 -
130 -
    def testAddMoreThan12Months(self):
131 -
        self.assertEqual(date(2003, 12, 1) + relativedelta(months=+13),
132 -
                         date(2005, 1, 1))
133 -
134 -
    def testAddNegativeMonths(self):
135 -
        self.assertEqual(date(2003, 1, 1) + relativedelta(months=-2),
136 -
                         date(2002, 11, 1))
137 -
138 -
    def test15thISOYearWeek(self):
139 -
        self.assertEqual(date(2003, 1, 1) +
140 -
                         relativedelta(day=4, weeks=+14, weekday=MO(-1)),
141 -
                         date(2003, 4, 7))
142 -
143 -
    def testMillenniumAge(self):
144 -
        self.assertEqual(relativedelta(self.now, date(2001, 1, 1)),
145 -
                         relativedelta(years=+2, months=+8, days=+16,
146 -
                                       hours=+20, minutes=+54, seconds=+47,
147 -
                                       microseconds=+282310))
148 -
149 -
    def testJohnAge(self):
150 -
        self.assertEqual(relativedelta(self.now,
151 -
                                       datetime(1978, 4, 5, 12, 0)),
152 -
                         relativedelta(years=+25, months=+5, days=+12,
153 -
                                       hours=+8, minutes=+54, seconds=+47,
154 -
                                       microseconds=+282310))
155 -
156 -
    def testJohnAgeWithDate(self):
157 -
        self.assertEqual(relativedelta(self.today,
158 -
                                       datetime(1978, 4, 5, 12, 0)),
159 -
                         relativedelta(years=+25, months=+5, days=+11,
160 -
                                       hours=+12))
161 -
162 -
    def testYearDay(self):
163 -
        self.assertEqual(date(2003, 1, 1)+relativedelta(yearday=260),
164 -
                         date(2003, 9, 17))
165 -
        self.assertEqual(date(2002, 1, 1)+relativedelta(yearday=260),
166 -
                         date(2002, 9, 17))
167 -
        self.assertEqual(date(2000, 1, 1)+relativedelta(yearday=260),
168 -
                         date(2000, 9, 16))
169 -
        self.assertEqual(self.today+relativedelta(yearday=261),
170 -
                         date(2003, 9, 18))
171 -
172 -
    def testYearDayBug(self):
173 -
        # Tests a problem reported by Adam Ryan.
174 -
        self.assertEqual(date(2010, 1, 1)+relativedelta(yearday=15),
175 -
                         date(2010, 1, 15))
176 -
177 -
    def testNonLeapYearDay(self):
178 -
        self.assertEqual(date(2003, 1, 1)+relativedelta(nlyearday=260),
179 -
                         date(2003, 9, 17))
180 -
        self.assertEqual(date(2002, 1, 1)+relativedelta(nlyearday=260),
181 -
                         date(2002, 9, 17))
182 -
        self.assertEqual(date(2000, 1, 1)+relativedelta(nlyearday=260),
183 -
                         date(2000, 9, 17))
184 -
        self.assertEqual(self.today+relativedelta(yearday=261),
185 -
                         date(2003, 9, 18))
186 -
187 -
    def testAddition(self):
188 -
        self.assertEqual(relativedelta(days=10) +
189 -
                         relativedelta(years=1, months=2, days=3, hours=4,
190 -
                                       minutes=5, microseconds=6),
191 -
                         relativedelta(years=1, months=2, days=13, hours=4,
192 -
                                       minutes=5, microseconds=6))
193 -
194 -
    def testAbsoluteAddition(self):
195 -
        self.assertEqual(relativedelta() + relativedelta(day=0, hour=0),
196 -
                         relativedelta(day=0, hour=0))
197 -
        self.assertEqual(relativedelta(day=0, hour=0) + relativedelta(),
198 -
                         relativedelta(day=0, hour=0))
199 -
200 -
    def testAdditionToDatetime(self):
201 -
        self.assertEqual(datetime(2000, 1, 1) + relativedelta(days=1),
202 -
                         datetime(2000, 1, 2))
203 -
204 -
    def testRightAdditionToDatetime(self):
205 -
        self.assertEqual(relativedelta(days=1) + datetime(2000, 1, 1),
206 -
                         datetime(2000, 1, 2))
207 -
208 -
    def testAdditionInvalidType(self):
209 -
        with self.assertRaises(TypeError):
210 -
            relativedelta(days=3) + 9
211 -
212 -
    def testAdditionUnsupportedType(self):
213 -
        # For unsupported types that define their own comparators, etc.
214 -
        self.assertIs(relativedelta(days=1) + NotAValue, NotAValue)
215 -
216 -
    def testAdditionFloatValue(self):
217 -
        self.assertEqual(datetime(2000, 1, 1) + relativedelta(days=float(1)),
218 -
                         datetime(2000, 1, 2))
219 -
        self.assertEqual(datetime(2000, 1, 1) + relativedelta(months=float(1)),
220 -
                         datetime(2000, 2, 1))
221 -
        self.assertEqual(datetime(2000, 1, 1) + relativedelta(years=float(1)),
222 -
                         datetime(2001, 1, 1))
223 -
224 -
    def testAdditionFloatFractionals(self):
225 -
        self.assertEqual(datetime(2000, 1, 1, 0) +
226 -
                         relativedelta(days=float(0.5)),
227 -
                         datetime(2000, 1, 1, 12))
228 -
        self.assertEqual(datetime(2000, 1, 1, 0, 0) +
229 -
                         relativedelta(hours=float(0.5)),
230 -
                         datetime(2000, 1, 1, 0, 30))
231 -
        self.assertEqual(datetime(2000, 1, 1, 0, 0, 0) +
232 -
                         relativedelta(minutes=float(0.5)),
233 -
                         datetime(2000, 1, 1, 0, 0, 30))
234 -
        self.assertEqual(datetime(2000, 1, 1, 0, 0, 0, 0) +
235 -
                         relativedelta(seconds=float(0.5)),
236 -
                         datetime(2000, 1, 1, 0, 0, 0, 500000))
237 -
        self.assertEqual(datetime(2000, 1, 1, 0, 0, 0, 0) +
238 -
                         relativedelta(microseconds=float(500000.25)),
239 -
                         datetime(2000, 1, 1, 0, 0, 0, 500000))
240 -
241 -
    def testSubtraction(self):
242 -
        self.assertEqual(relativedelta(days=10) -
243 -
                         relativedelta(years=1, months=2, days=3, hours=4,
244 -
                                       minutes=5, microseconds=6),
245 -
                         relativedelta(years=-1, months=-2, days=7, hours=-4,
246 -
                                       minutes=-5, microseconds=-6))
247 -
248 -
    def testRightSubtractionFromDatetime(self):
249 -
        self.assertEqual(datetime(2000, 1, 2) - relativedelta(days=1),
250 -
                         datetime(2000, 1, 1))
251 -
252 -
    def testSubractionWithDatetime(self):
253 -
        self.assertRaises(TypeError, lambda x, y: x - y,
254 -
                          (relativedelta(days=1), datetime(2000, 1, 1)))
255 -
256 -
    def testSubtractionInvalidType(self):
257 -
        with self.assertRaises(TypeError):
258 -
            relativedelta(hours=12) - 14
259 -
260 -
    def testSubtractionUnsupportedType(self):
261 -
        self.assertIs(relativedelta(days=1) + NotAValue, NotAValue)
262 -
263 -
    def testMultiplication(self):
264 -
        self.assertEqual(datetime(2000, 1, 1) + relativedelta(days=1) * 28,
265 -
                         datetime(2000, 1, 29))
266 -
        self.assertEqual(datetime(2000, 1, 1) + 28 * relativedelta(days=1),
267 -
                         datetime(2000, 1, 29))
268 -
269 -
    def testMultiplicationUnsupportedType(self):
270 -
        self.assertIs(relativedelta(days=1) * NotAValue, NotAValue)
271 -
272 -
    def testDivision(self):
273 -
        self.assertEqual(datetime(2000, 1, 1) + relativedelta(days=28) / 28,
274 -
                         datetime(2000, 1, 2))
275 -
276 -
    def testDivisionUnsupportedType(self):
277 -
        self.assertIs(relativedelta(days=1) / NotAValue, NotAValue)
278 -
279 -
    def testBoolean(self):
280 -
        self.assertFalse(relativedelta(days=0))
281 -
        self.assertTrue(relativedelta(days=1))
282 -
283 -
    def testAbsoluteValueNegative(self):
284 -
        rd_base = relativedelta(years=-1, months=-5, days=-2, hours=-3,
285 -
                                minutes=-5, seconds=-2, microseconds=-12)
286 -
        rd_expected = relativedelta(years=1, months=5, days=2, hours=3,
287 -
                                    minutes=5, seconds=2, microseconds=12)
288 -
        self.assertEqual(abs(rd_base), rd_expected)
289 -
290 -
    def testAbsoluteValuePositive(self):
291 -
        rd_base = relativedelta(years=1, months=5, days=2, hours=3,
292 -
                                minutes=5, seconds=2, microseconds=12)
293 -
        rd_expected = rd_base
294 -
295 -
        self.assertEqual(abs(rd_base), rd_expected)
296 -
297 -
    def testComparison(self):
298 -
        d1 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1,
299 -
                           minutes=1, seconds=1, microseconds=1)
300 -
        d2 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1,
301 -
                           minutes=1, seconds=1, microseconds=1)
302 -
        d3 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1,
303 -
                           minutes=1, seconds=1, microseconds=2)
304 -
305 -
        self.assertEqual(d1, d2)
306 -
        self.assertNotEqual(d1, d3)
307 -
308 -
    def testInequalityTypeMismatch(self):
309 -
        # Different type
310 -
        self.assertFalse(relativedelta(year=1) == 19)
311 -
312 -
    def testInequalityUnsupportedType(self):
313 -
        self.assertIs(relativedelta(hours=3) == NotAValue, NotAValue)
314 -
315 -
    def testInequalityWeekdays(self):
316 -
        # Different weekdays
317 -
        no_wday = relativedelta(year=1997, month=4)
318 -
        wday_mo_1 = relativedelta(year=1997, month=4, weekday=MO(+1))
319 -
        wday_mo_2 = relativedelta(year=1997, month=4, weekday=MO(+2))
320 -
        wday_tu = relativedelta(year=1997, month=4, weekday=TU)
321 -
322 -
        self.assertTrue(wday_mo_1 == wday_mo_1)
323 -
324 -
        self.assertFalse(no_wday == wday_mo_1)
325 -
        self.assertFalse(wday_mo_1 == no_wday)
326 -
327 -
        self.assertFalse(wday_mo_1 == wday_mo_2)
328 -
        self.assertFalse(wday_mo_2 == wday_mo_1)
329 -
330 -
        self.assertFalse(wday_mo_1 == wday_tu)
331 -
        self.assertFalse(wday_tu == wday_mo_1)
332 -
333 -
    def testMonthOverflow(self):
334 -
        self.assertEqual(relativedelta(months=273),
335 -
                         relativedelta(years=22, months=9))
336 -
337 -
    def testWeeks(self):
338 -
        # Test that the weeks property is working properly.
339 -
        rd = relativedelta(years=4, months=2, weeks=8, days=6)
340 -
        self.assertEqual((rd.weeks, rd.days), (8, 8 * 7 + 6))
341 -
342 -
        rd.weeks = 3
343 -
        self.assertEqual((rd.weeks, rd.days), (3, 3 * 7 + 6))
29 +
    assert type(ccRD - rd) == type(ccRD), 'Subtraction does not inherit type.'
344 30
345 -
    def testRelativeDeltaRepr(self):
346 -
        self.assertEqual(repr(relativedelta(years=1, months=-1, days=15)),
347 -
                         'relativedelta(years=+1, months=-1, days=+15)')
31 +
    assert type(-ccRD) == type(ccRD), 'Negation does not inherit type.'
348 32
349 -
        self.assertEqual(repr(relativedelta(months=14, seconds=-25)),
350 -
                         'relativedelta(years=+1, months=+2, seconds=-25)')
33 +
    assert type(ccRD * 5.0) == type(ccRD), 'Multiplication does not inherit type.'
351 34
352 -
        self.assertEqual(repr(relativedelta(month=3, hour=3, weekday=SU(3))),
353 -
                         'relativedelta(month=3, weekday=SU(+3), hour=3)')
35 +
    assert type(ccRD / 5.0) == type(ccRD), 'Division does not inherit type.'
354 36
355 -
    def testRelativeDeltaFractionalYear(self):
356 -
        with self.assertRaises(ValueError):
357 -
            relativedelta(years=1.5)
358 37
359 -
    def testRelativeDeltaFractionalMonth(self):
360 -
        with self.assertRaises(ValueError):
38 +
def test_relative_delta_month_end_month_beginning():
39 +
    assert relativedelta(datetime(2003, 1, 31, 23, 59, 59), datetime(2003, 3, 1, 0, 0, 0)) == relativedelta(months=-1,
40 +
                                                                                                            seconds=-1)
41 +
42 +
    assert relativedelta(datetime(2003, 3, 1, 0, 0, 0), datetime(2003, 1, 31, 23, 59, 59)) == relativedelta(months=1,
43 +
                                                                                                            seconds=1)
44 +
45 +
46 +
def test_relative_delta_month_end_month_beginning_leap_year():
47 +
    assert relativedelta(datetime(2012, 1, 31, 23, 59, 59), datetime(2012, 3, 1, 0, 0, 0)) == relativedelta(months=-1,
48 +
                                                                                                            seconds=-1)
49 +
50 +
    assert relativedelta(datetime(2003, 3, 1, 0, 0, 0), datetime(2003, 1, 31, 23, 59, 59)) == relativedelta(months=1,
51 +
                                                                                                            seconds=1)
52 +
53 +
54 +
def test_relative_delta_next_month():
55 +
    assert NOW + relativedelta(months=+1) == datetime(2003, 10, 17, 20, 54, 47, 282310)
56 +
57 +
58 +
def test_relative_delta_next_month_plus_one_week():
59 +
    assert NOW + relativedelta(months=+1, weeks=+1) == datetime(2003, 10, 24, 20, 54, 47, 282310)
60 +
61 +
62 +
def test_relative_delta_next_month_plus_one_week_10am():
63 +
    assert TODAY + relativedelta(months=+1, weeks=+1, hour=10) == datetime(2003, 10, 24, 10, 0)
64 +
65 +
66 +
def test_relative_delta_next_month_plus_one_week_10am_diff():
67 +
    assert relativedelta(datetime(2003, 10, 24, 10, 0), TODAY) == relativedelta(months=+1, days=+7, hours=+10)
68 +
69 +
70 +
def test_relative_delta_one_month_before_one_year():
71 +
    assert NOW + relativedelta(years=+1, months=-1) == datetime(2004, 8, 17, 20, 54, 47, 282310)
72 +
73 +
74 +
def test_relative_delta_months_of_diff_num_of_days():
75 +
    assert date(2003, 1, 27) + relativedelta(months=+1) == date(2003, 2, 27)
76 +
    assert date(2003, 1, 31) + relativedelta(months=+1) == date(2003, 2, 28)
77 +
    assert date(2003, 1, 31) + relativedelta(months=+2) == date(2003, 3, 31)
78 +
79 +
80 +
def test_relative_delta_months_of_diff_num_of_days_with_years():
81 +
    assert date(2000, 2, 28) + relativedelta(years=+1) == date(2001, 2, 28)
82 +
    assert date(2000, 2, 29) + relativedelta(years=+1) == date(2001, 2, 28)
83 +
84 +
    assert date(1999, 2, 28) + relativedelta(years=+1) == date(2000, 2, 28)
85 +
    assert date(1999, 3, 1) + relativedelta(years=+1) == date(2000, 3, 1)
86 +
    assert date(1999, 3, 1) + relativedelta(years=+1) == date(2000, 3, 1)
87 +
88 +
    assert date(2001, 2, 28) + relativedelta(years=-1) == date(2000, 2, 28)
89 +
    assert date(2001, 3, 1) + relativedelta(years=-1) == date(2000, 3, 1)
90 +
91 +
92 +
def test_relative_delta_next_friday():
93 +
    assert TODAY + relativedelta(weekday=FR) == date(2003, 9, 19)
94 +
95 +
96 +
def test_relative_delta_next_friday_int():
97 +
    assert TODAY + relativedelta(weekday=calendar.FRIDAY) == date(2003, 9, 19)
98 +
99 +
100 +
def test_relative_delta_last_friday_in_this_month():
101 +
    assert TODAY + relativedelta(day=31, weekday=FR(-1)) == date(2003, 9, 26)
102 +
103 +
104 +
def test_relative_delta_next_wednesday_is_today():
105 +
    assert TODAY + relativedelta(weekday=WE) == date(2003, 9, 17)
106 +
107 +
108 +
def test_relative_delta_next_wenesday_not_today():
109 +
    assert TODAY + relativedelta(days=+1, weekday=WE) == date(2003, 9, 24)
110 +
111 +
112 +
def test_relative_delta_add_more_than12_months():
113 +
    assert date(2003, 12, 1) + relativedelta(months=+13) == date(2005, 1, 1)
114 +
115 +
116 +
def test_relative_delta_add_negative_months():
117 +
    assert date(2003, 1, 1) + relativedelta(months=-2) == date(2002, 11, 1)
118 +
119 +
120 +
def test_relative_delta_15th_iso_year_week():
121 +
    assert date(2003, 1, 1) + relativedelta(day=4, weeks=+14, weekday=MO(-1)) == date(2003, 4, 7)
122 +
123 +
124 +
def test_relative_delta_millennium_age():
125 +
    assert relativedelta(NOW, date(2001, 1, 1)) == relativedelta(years=+2, months=+8, days=+16,
126 +
                                                                 hours=+20, minutes=+54, seconds=+47,
127 +
                                                                 microseconds=+282310)
128 +
129 +
130 +
def test_relative_delta_john_age():
131 +
    assert relativedelta(NOW, datetime(1978, 4, 5, 12, 0)) == relativedelta(years=+25, months=+5, days=+12,
132 +
                                                                            hours=+8, minutes=+54, seconds=+47,
133 +
                                                                            microseconds=+282310)
134 +
135 +
136 +
def test_relative_delta_john_age_with_date():
137 +
    assert relativedelta(TODAY, datetime(1978, 4, 5, 12, 0)) == relativedelta(years=+25, months=+5, days=+11, hours=+12)
138 +
139 +
140 +
def test_relative_delta_year_day():
141 +
    assert date(2003, 1, 1)+relativedelta(yearday=260) == date(2003, 9, 17)
142 +
    assert date(2002, 1, 1)+relativedelta(yearday=260) == date(2002, 9, 17)
143 +
    assert date(2000, 1, 1)+relativedelta(yearday=260) == date(2000, 9, 16)
144 +
    assert TODAY + relativedelta(yearday=261) == date(2003, 9, 18)
145 +
146 +
147 +
def test_relative_delta_year_day_bug():
148 +
    # Tests a problem reported by Adam Ryan.
149 +
    assert date(2010, 1, 1)+relativedelta(yearday=15) == date(2010, 1, 15)
150 +
151 +
152 +
def test_relative_delta_non_leap_year_day():
153 +
    assert date(2003, 1, 1)+relativedelta(nlyearday=260) == date(2003, 9, 17)
154 +
    assert date(2002, 1, 1)+relativedelta(nlyearday=260) == date(2002, 9, 17)
155 +
    assert date(2000, 1, 1)+relativedelta(nlyearday=260) == date(2000, 9, 17)
156 +
    assert TODAY + relativedelta(yearday=261) == date(2003, 9, 18)
157 +
158 +
159 +
def test_relative_delta_addition():
160 +
    assert relativedelta(days=10) + relativedelta(years=1, months=2, days=3, hours=4, minutes=5, microseconds=6) == \
161 +
           relativedelta(years=1, months=2, days=13, hours=4, minutes=5, microseconds=6)
162 +
163 +
164 +
def test_relative_delta_absolute_addition():
165 +
    assert relativedelta() + relativedelta(day=0, hour=0) == relativedelta(day=0, hour=0)
166 +
    assert relativedelta(day=0, hour=0) + relativedelta() == relativedelta(day=0, hour=0)
167 +
168 +
169 +
def test_relative_delta_addition_to_datetime():
170 +
    assert datetime(2000, 1, 1) + relativedelta(days=1) == datetime(2000, 1, 2)
171 +
172 +
173 +
def test_relative_delta_right_addition_to_datetime():
174 +
    assert relativedelta(days=1) + datetime(2000, 1, 1) == datetime(2000, 1, 2)
175 +
176 +
177 +
def test_relative_delta_addition_invalid_type():
178 +
    with pytest.raises(TypeError):
179 +
        relativedelta(days=3) + 9
180 +
181 +
182 +
def test_relative_delta_addition_unsupported_type():
183 +
    # For unsupported types that define their own comparators, etc.
184 +
    assert relativedelta(days=1) + NotAValue is NotAValue
185 +
186 +
187 +
def test_relative_delta_addition_float_value():
188 +
    assert datetime(2000, 1, 1) + relativedelta(days=float(1)) == datetime(2000, 1, 2)
189 +
    assert datetime(2000, 1, 1) + relativedelta(months=float(1)) == datetime(2000, 2, 1)
190 +
    assert datetime(2000, 1, 1) + relativedelta(years=float(1)) == datetime(2001, 1, 1)
191 +
192 +
193 +
def test_relative_delta_addition_float_fractionals():
194 +
    assert datetime(2000, 1, 1, 0) + relativedelta(days=float(0.5)) == datetime(2000, 1, 1, 12)
195 +
    assert datetime(2000, 1, 1, 0, 0) + relativedelta(hours=float(0.5)) == datetime(2000, 1, 1, 0, 30)
196 +
    assert datetime(2000, 1, 1, 0, 0, 0) + relativedelta(minutes=float(0.5)) == datetime(2000, 1, 1, 0, 0, 30)
197 +
    assert datetime(2000, 1, 1, 0, 0, 0, 0) + relativedelta(seconds=float(0.5)) == datetime(2000, 1, 1, 0, 0, 0, 500000)
198 +
199 +
    assert datetime(2000, 1, 1, 0, 0, 0, 0) + relativedelta(microseconds=float(500000.25)) == \
200 +
           datetime(2000, 1, 1, 0, 0, 0, 500000)
201 +
202 +
203 +
def test_relative_delta_subtraction():
204 +
    assert relativedelta(days=10) - relativedelta(years=1, months=2, days=3, hours=4, minutes=5, microseconds=6) == \
205 +
           relativedelta(years=-1, months=-2, days=7, hours=-4, minutes=-5, microseconds=-6)
206 +
207 +
208 +
def test_relative_delta_right_subtraction_from_datetime():
209 +
    assert datetime(2000, 1, 2) - relativedelta(days=1) == datetime(2000, 1, 1)
210 +
211 +
212 +
def test_relative_delta_subraction_with_datetime():
213 +
    with pytest.raises(TypeError):
214 +
        relativedelta(days=1) -  datetime(2000, 1, 1)
215 +
216 +
217 +
def test_relative_delta_subtraction_invalid_type():
218 +
    with pytest.raises(TypeError):
219 +
        relativedelta(hours=12) - 14
220 +
221 +
222 +
def test_relative_delta_subtraction_unsupported_type():
223 +
    assert relativedelta(days=1) + NotAValue is  NotAValue
224 +
225 +
226 +
def test_relative_delta_multiplication():
227 +
    assert datetime(2000, 1, 1) + relativedelta(days=1) * 28 == datetime(2000, 1, 29)
228 +
    assert datetime(2000, 1, 1) + 28 * relativedelta(days=1) == datetime(2000, 1, 29)
229 +
230 +
231 +
def test_relative_delta_multiplication_unsupported_type():
232 +
    assert relativedelta(days=1) * NotAValue is NotAValue
233 +
234 +
235 +
def test_relative_delta_division():
236 +
    assert datetime(2000, 1, 1) + relativedelta(days=28) / 28 == datetime(2000, 1, 2)
237 +
238 +
239 +
def test_relative_delta_division_unsupported_type():
240 +
    assert relativedelta(days=1) / NotAValue is NotAValue
241 +
242 +
243 +
def test_relative_delta_boolean():
244 +
    assert not relativedelta(days=0)
245 +
    assert relativedelta(days=1)
246 +
247 +
248 +
def test_relative_delta_absolute_value_negative():
249 +
    rd_base = relativedelta(years=-1, months=-5, days=-2, hours=-3, minutes=-5, seconds=-2, microseconds=-12)
250 +
    rd_expected = relativedelta(years=1, months=5, days=2, hours=3, minutes=5, seconds=2, microseconds=12)
251 +
    assert abs(rd_base) == rd_expected
252 +
253 +
254 +
def test_relative_delta_absolute_value_positive():
255 +
    rd_base = relativedelta(years=1, months=5, days=2, hours=3, minutes=5, seconds=2, microseconds=12)
256 +
    rd_expected = rd_base
257 +
258 +
    assert abs(rd_base) == rd_expected
259 +
260 +
261 +
def test_relative_delta_comparison():
262 +
    d1 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1,
263 +
                        minutes=1, seconds=1, microseconds=1)
264 +
    d2 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1,
265 +
                        minutes=1, seconds=1, microseconds=1)
266 +
    d3 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1,
267 +
                        minutes=1, seconds=1, microseconds=2)
268 +
269 +
    assert d1 == d2
270 +
    assert d1 != d3
271 +
272 +
273 +
def test_relative_delta_inequality_type_mismatch():
274 +
    # Different type
275 +
    assert not relativedelta(year=1) == 19
276 +
277 +
278 +
def test_relative_delta_inequality_unsupported_type():
279 +
    assert (relativedelta(hours=3) == NotAValue) is NotAValue
280 +
281 +
282 +
def test_relative_delta_inequality_weekdays():
283 +
    # Different weekdays
284 +
    no_wday = relativedelta(year=1997, month=4)
285 +
    wday_mo_1 = relativedelta(year=1997, month=4, weekday=MO(+1))
286 +
    wday_mo_2 = relativedelta(year=1997, month=4, weekday=MO(+2))
287 +
    wday_tu = relativedelta(year=1997, month=4, weekday=TU)
288 +
289 +
    assert wday_mo_1 == wday_mo_1
290 +
291 +
    assert not no_wday == wday_mo_1
292 +
    assert not wday_mo_1 == no_wday
293 +
294 +
    assert not wday_mo_1 == wday_mo_2
295 +
    assert not wday_mo_2 == wday_mo_1
296 +
297 +
    assert not wday_mo_1 == wday_tu
298 +
    assert not wday_tu == wday_mo_1
299 +
300 +
301 +
def test_relative_delta_month_overflow():
302 +
    assert relativedelta(months=273) == relativedelta(years=22, months=9)
303 +
304 +
305 +
def test_relative_delta_weeks():
306 +
     # Test that the weeks property is working properly.
307 +
    rd = relativedelta(years=4, months=2, weeks=8, days=6)
308 +
    assert (rd.weeks, rd.days) == (8, 8 * 7 + 6)
309 +
310 +
    rd.weeks = 3
311 +
    assert (rd.weeks, rd.days) == (3, 3 * 7 + 6)
312 +
313 +
314 +
def test_relative_delta_relative_delta_repr():
315 +
    assert repr(relativedelta(years=1, months=-1, days=15)) == 'relativedelta(years=+1, months=-1, days=+15)'
316 +
317 +
    assert repr(relativedelta(months=14, seconds=-25)) == 'relativedelta(years=+1, months=+2, seconds=-25)'
318 +
319 +
    assert repr(relativedelta(month=3, hour=3, weekday=SU(3))) == 'relativedelta(month=3, weekday=SU(+3), hour=3)'
320 +
321 +
322 +
def test_relative_delta_relative_delta_fractional_year():
323 +
    with pytest.raises(ValueError):
324 +
        relativedelta(years=1.5)
325 +
326 +
327 +
def test_relative_delta_relative_delta_fractional_month():
328 +
     with pytest.raises(ValueError):
361 329
            relativedelta(months=1.5)
362 330
363 -
    def testRelativeDeltaInvalidDatetimeObject(self):
364 -
        with self.assertRaises(TypeError):
365 -
            relativedelta(dt1='2018-01-01', dt2='2018-01-02')
366 -
367 -
        with self.assertRaises(TypeError):
368 -
            relativedelta(dt1=datetime(2018, 1, 1), dt2='2018-01-02')
369 -
370 -
        with self.assertRaises(TypeError):
371 -
            relativedelta(dt1='2018-01-01', dt2=datetime(2018, 1, 2))
372 -
373 -
    def testRelativeDeltaFractionalAbsolutes(self):
374 -
        # Fractional absolute values will soon be unsupported,
375 -
        # check for the deprecation warning.
376 -
        with pytest.warns(DeprecationWarning):
377 -
            relativedelta(year=2.86)
378 -
379 -
        with pytest.warns(DeprecationWarning):
380 -
            relativedelta(month=1.29)
381 -
382 -
        with pytest.warns(DeprecationWarning):
383 -
            relativedelta(day=0.44)
384 331
385 -
        with pytest.warns(DeprecationWarning):
386 -
            relativedelta(hour=23.98)
387 -
388 -
        with pytest.warns(DeprecationWarning):
389 -
            relativedelta(minute=45.21)
332 +
def test_relative_delta_relative_delta_invalid_datetime_object():
333 +
    with pytest.raises(TypeError):
334 +
        relativedelta(dt1='2018-01-01', dt2='2018-01-02')
335 +
336 +
    with pytest.raises(TypeError):
337 +
        relativedelta(dt1=datetime(2018, 1, 1), dt2='2018-01-02')
338 +
339 +
    with pytest.raises(TypeError):
340 +
        relativedelta(dt1='2018-01-01', dt2=datetime(2018, 1, 2))
341 +
342 +
343 +
def test_relative_delta_relative_delta_fractional_absolutes():
344 +
    # Fractional absolute values will soon be unsupported,
345 +
    # check for the deprecation warning.
346 +
    with pytest.warns(DeprecationWarning):
347 +
        relativedelta(year=2.86)
348 +
349 +
    with pytest.warns(DeprecationWarning):
350 +
        relativedelta(month=1.29)
351 +
352 +
    with pytest.warns(DeprecationWarning):
353 +
        relativedelta(day=0.44)
354 +
355 +
    with pytest.warns(DeprecationWarning):
356 +
        relativedelta(hour=23.98)
357 +
358 +
    with pytest.warns(DeprecationWarning):
359 +
        relativedelta(minute=45.21)
360 +
361 +
    with pytest.warns(DeprecationWarning):
362 +
        relativedelta(second=13.2)
363 +
364 +
    with pytest.warns(DeprecationWarning):
365 +
        relativedelta(microsecond=157221.93)
366 +
367 +
368 +
def test_relative_delta_relative_delta_fractional_repr():
369 +
    rd = relativedelta(years=3, months=-2, days=1.25)
370 +
371 +
    assert repr(rd) == 'relativedelta(years=+3, months=-2, days=+1.25)'
372 +
373 +
    rd = relativedelta(hours=0.5, seconds=9.22)
374 +
    assert repr(rd) == 'relativedelta(hours=+0.5, seconds=+9.22)'
375 +
376 +
377 +
def test_relative_delta_relative_delta_fractional_weeks():
378 +
    # Equivalent to days=8, hours=18
379 +
    rd = relativedelta(weeks=1.25)
380 +
    d1 = datetime(2009, 9, 3, 0, 0)
381 +
    assert d1 + rd == datetime(2009, 9, 11, 18)
382 +
383 +
384 +
def test_relative_delta_relative_delta_fractional_days():
385 +
    rd1 = relativedelta(days=1.48)
386 +
387 +
    d1 = datetime(2009, 9, 3, 0, 0)
388 +
    assert d1 + rd1 ==  datetime(2009, 9, 4, 11, 31, 12)
389 +
390 +
    rd2 = relativedelta(days=1.5)
391 +
    assert d1 + rd2 == datetime(2009, 9, 4, 12, 0, 0)
392 +
393 +
394 +
def test_relative_delta_relative_delta_fractional_hours():
395 +
    rd = relativedelta(days=1, hours=12.5)
396 +
    d1 = datetime(2009, 9, 3, 0, 0)
397 +
    assert d1 + rd == datetime(2009, 9, 4, 12, 30, 0)
398 +
399 +
400 +
def test_relative_delta_relative_delta_fractional_minutes():
401 +
    rd = relativedelta(hours=1, minutes=30.5)
402 +
    d1 = datetime(2009, 9, 3, 0, 0)
403 +
    assert d1 + rd == datetime(2009, 9, 3, 1, 30, 30)
404 +
405 +
406 +
def test_relative_delta_relative_delta_fractional_seconds():
407 +
    rd = relativedelta(hours=5, minutes=30, seconds=30.5)
408 +
    d1 = datetime(2009, 9, 3, 0, 0)
409 +
    assert d1 + rd == datetime(2009, 9, 3, 5, 30, 30, 500000)
410 +
411 +
412 +
def test_relative_delta_relative_delta_fractional_positive_overflow():
413 +
    # Equivalent to (days=1, hours=14)
414 +
    rd1 = relativedelta(days=1.5, hours=2)
415 +
    d1 = datetime(2009, 9, 3, 0, 0)
416 +
    assert d1 + rd1 == datetime(2009, 9, 4, 14, 0, 0)
417 +
418 +
    # Equivalent to (days=1, hours=14, minutes=45)
419 +
    rd2 = relativedelta(days=1.5, hours=2.5, minutes=15)
420 +
    d1 = datetime(2009, 9, 3, 0, 0)
421 +
    assert d1 + rd2 == datetime(2009, 9, 4, 14, 45)
422 +
423 +
    # Carry back up - equivalent to (days=2, hours=2, minutes=0, seconds=1)
424 +
    rd3 = relativedelta(days=1.5, hours=13, minutes=59.5, seconds=31)
425 +
    assert d1 + rd3 == datetime(2009, 9, 5, 2, 0, 1)
426 +
427 +
428 +
def test_relative_delta_relative_delta_fractional_negative_days():
429 +
    # Equivalent to (days=-1, hours=-1)
430 +
    rd1 = relativedelta(days=-1.5, hours=11)
431 +
    d1 = datetime(2009, 9, 3, 12, 0)
432 +
    assert d1 + rd1 == datetime(2009, 9, 2, 11, 0, 0)
433 +
434 +
    # Equivalent to (days=-1, hours=-9)
435 +
    rd2 = relativedelta(days=-1.25, hours=-3)
436 +
    assert d1 + rd2 == datetime(2009, 9, 2, 3)
437 +
438 +
439 +
def test_relative_delta_relative_delta_normalize_fractional_days():
440 +
    # Equivalent to (days=2, hours=18)
441 +
    rd1 = relativedelta(days=2.75)
442 +
443 +
    assert rd1.normalized() == relativedelta(days=2, hours=18)
444 +
445 +
    # Equivalent to (days=1, hours=11, minutes=31, seconds=12)
446 +
    rd2 = relativedelta(days=1.48)
447 +
448 +
    assert rd2.normalized() == relativedelta(days=1, hours=11, minutes=31, seconds=12)
449 +
450 +
451 +
def test_relative_delta_relative_delta_normalize_fractional_days2():
452 +
    # Equivalent to (hours=1, minutes=30)
453 +
    rd1 = relativedelta(hours=1.5)
454 +
455 +
    assert rd1.normalized(), relativedelta(hours=1, minutes=30)
456 +
457 +
    # Equivalent to (hours=3, minutes=17, seconds=5, microseconds=100)
458 +
    rd2 = relativedelta(hours=3.28472225)
459 +
460 +
    assert rd2.normalized() == relativedelta(hours=3, minutes=17, seconds=5, microseconds=100)
461 +
462 +
463 +
def test_relative_delta_relative_delta_normalize_fractional_minutes():
464 +
    # Equivalent to (minutes=15, seconds=36)
465 +
    rd1 = relativedelta(minutes=15.6)
466 +
467 +
    assert rd1.normalized() == relativedelta(minutes=15, seconds=36)
468 +
469 +
    # Equivalent to (minutes=25, seconds=20, microseconds=25000)
470 +
    rd2 = relativedelta(minutes=25.33375)
471 +
472 +
    assert rd2.normalized() == relativedelta(minutes=25, seconds=20, microseconds=25000)
473 +
474 +
def test_relative_delta_relative_delta_normalize_fractional_seconds():
475 +
    # Equivalent to (seconds=45, microseconds=25000)
476 +
    rd1 = relativedelta(seconds=45.025)
477 +
    assert rd1.normalized() == relativedelta(seconds=45, microseconds=25000)
478 +
390 479
391 -
        with pytest.warns(DeprecationWarning):
392 -
            relativedelta(second=13.2)
480 +
def test_relative_delta_relative_delta_fractional_positive_overflow2():
481 +
    # Equivalent to (days=1, hours=14)
482 +
    rd1 = relativedelta(days=1.5, hours=2)
483 +
    assert rd1.normalized() == relativedelta(days=1, hours=14)
393 484
394 -
        with pytest.warns(DeprecationWarning):
395 -
            relativedelta(microsecond=157221.93)
485 +
    # Equivalent to (days=1, hours=14, minutes=45)
486 +
    rd2 = relativedelta(days=1.5, hours=2.5, minutes=15)
487 +
    assert rd2.normalized() == relativedelta(days=1, hours=14, minutes=45)
396 488
397 -
    def testRelativeDeltaFractionalRepr(self):
398 -
        rd = relativedelta(years=3, months=-2, days=1.25)
489 +
    # Carry back up - equivalent to:
490 +
    # (days=2, hours=2, minutes=0, seconds=2, microseconds=3)
491 +
    rd3 = relativedelta(days=1.5, hours=13, minutes=59.50045, seconds=31.473, microseconds=500003)
492 +
    assert rd3.normalized() == relativedelta(days=2, hours=2, minutes=0, seconds=2, microseconds=3)
493 +
494 +
495 +
def test_relative_delta_relative_delta_fractional_negative_overflow():
496 +
    # Equivalent to (days=-1)
497 +
    rd1 = relativedelta(days=-0.5, hours=-12)
498 +
    assert rd1.normalized() == relativedelta(days=-1)
499 +
500 +
    # Equivalent to (days=-1)
501 +
    rd2 = relativedelta(days=-1.5, hours=12)
502 +
    assert rd2.normalized() == relativedelta(days=-1)
399 503
400 -
        self.assertEqual(repr(rd),
401 -
                         'relativedelta(years=+3, months=-2, days=+1.25)')
504 +
    # Equivalent to (days=-1, hours=-14, minutes=-45)
505 +
    rd3 = relativedelta(days=-1.5, hours=-2.5, minutes=-15)
506 +
    assert rd3.normalized() == relativedelta(days=-1, hours=-14, minutes=-45)
507 +
508 +
    # Equivalent to (days=-1, hours=-14, minutes=+15)
509 +
    rd4 = relativedelta(days=-1.5, hours=-2.5, minutes=45)
510 +
    assert rd4.normalized() == relativedelta(days=-1, hours=-14, minutes=+15)
511 +
512 +
    # Carry back up - equivalent to:
513 +
    # (days=-2, hours=-2, minutes=0, seconds=-2, microseconds=-3)
514 +
    rd3 = relativedelta(days=-1.5, hours=-13, minutes=-59.50045, seconds=-31.473, microseconds=-500003)
515 +
    assert rd3.normalized() == relativedelta(days=-2, hours=-2, minutes=0, seconds=-2, microseconds=-3)
402 516
403 -
        rd = relativedelta(hours=0.5, seconds=9.22)
404 -
        self.assertEqual(repr(rd),
405 -
                         'relativedelta(hours=+0.5, seconds=+9.22)')
406 517
407 -
    def testRelativeDeltaFractionalWeeks(self):
408 -
        # Equivalent to days=8, hours=18
409 -
        rd = relativedelta(weeks=1.25)
410 -
        d1 = datetime(2009, 9, 3, 0, 0)
411 -
        self.assertEqual(d1 + rd,
412 -
                         datetime(2009, 9, 11, 18))
518 +
def test_relative_delta_invalid_year_day():
519 +
    with pytest.raises (ValueError):
520 +
        relativedelta(yearday=367)
413 521
414 -
    def testRelativeDeltaFractionalDays(self):
415 -
        rd1 = relativedelta(days=1.48)
416 522
417 -
        d1 = datetime(2009, 9, 3, 0, 0)
418 -
        self.assertEqual(d1 + rd1,
419 -
                         datetime(2009, 9, 4, 11, 31, 12))
523 +
def test_relative_delta_add_timedelta_to_unpopulated_relativedelta():
524 +
    td = timedelta(
525 +
        days=1,
526 +
        seconds=1,
527 +
        microseconds=1,
528 +
        milliseconds=1,
529 +
        minutes=1,
530 +
        hours=1,
531 +
        weeks=1
532 +
    )
420 533
421 -
        rd2 = relativedelta(days=1.5)
422 -
        self.assertEqual(d1 + rd2,
423 -
                         datetime(2009, 9, 4, 12, 0, 0))
534 +
    expected = relativedelta(
535 +
        weeks=1,
536 +
        days=1,
537 +
        hours=1,
538 +
        minutes=1,
539 +
        seconds=1,
540 +
        microseconds=1001
541 +
    )
424 542
425 -
    def testRelativeDeltaFractionalHours(self):
426 -
        rd = relativedelta(days=1, hours=12.5)
427 -
        d1 = datetime(2009, 9, 3, 0, 0)
428 -
        self.assertEqual(d1 + rd,
429 -
                         datetime(2009, 9, 4, 12, 30, 0))
430 -
431 -
    def testRelativeDeltaFractionalMinutes(self):
432 -
        rd = relativedelta(hours=1, minutes=30.5)
433 -
        d1 = datetime(2009, 9, 3, 0, 0)
434 -
        self.assertEqual(d1 + rd,
435 -
                         datetime(2009, 9, 3, 1, 30, 30))
436 -
437 -
    def testRelativeDeltaFractionalSeconds(self):
438 -
        rd = relativedelta(hours=5, minutes=30, seconds=30.5)
439 -
        d1 = datetime(2009, 9, 3, 0, 0)
440 -
        self.assertEqual(d1 + rd,
441 -
                         datetime(2009, 9, 3, 5, 30, 30, 500000))
442 -
443 -
    def testRelativeDeltaFractionalPositiveOverflow(self):
444 -
        # Equivalent to (days=1, hours=14)
445 -
        rd1 = relativedelta(days=1.5, hours=2)
446 -
        d1 = datetime(2009, 9, 3, 0, 0)
447 -
        self.assertEqual(d1 + rd1,
448 -
                         datetime(2009, 9, 4, 14, 0, 0))
449 -
450 -
        # Equivalent to (days=1, hours=14, minutes=45)
451 -
        rd2 = relativedelta(days=1.5, hours=2.5, minutes=15)
452 -
        d1 = datetime(2009, 9, 3, 0, 0)
453 -
        self.assertEqual(d1 + rd2,
454 -
                         datetime(2009, 9, 4, 14, 45))
455 -
456 -
        # Carry back up - equivalent to (days=2, hours=2, minutes=0, seconds=1)
457 -
        rd3 = relativedelta(days=1.5, hours=13, minutes=59.5, seconds=31)
458 -
        self.assertEqual(d1 + rd3,
459 -
                         datetime(2009, 9, 5, 2, 0, 1))
460 -
461 -
    def testRelativeDeltaFractionalNegativeDays(self):
462 -
        # Equivalent to (days=-1, hours=-1)
463 -
        rd1 = relativedelta(days=-1.5, hours=11)
464 -
        d1 = datetime(2009, 9, 3, 12, 0)
465 -
        self.assertEqual(d1 + rd1,
466 -
                         datetime(2009, 9, 2, 11, 0, 0))
467 -
468 -
        # Equivalent to (days=-1, hours=-9)
469 -
        rd2 = relativedelta(days=-1.25, hours=-3)
470 -
        self.assertEqual(d1 + rd2,
471 -
            datetime(2009, 9, 2, 3))
472 -
473 -
    def testRelativeDeltaNormalizeFractionalDays(self):
474 -
        # Equivalent to (days=2, hours=18)
475 -
        rd1 = relativedelta(days=2.75)
476 -
477 -
        self.assertEqual(rd1.normalized(), relativedelta(days=2, hours=18))
478 -
479 -
        # Equivalent to (days=1, hours=11, minutes=31, seconds=12)
480 -
        rd2 = relativedelta(days=1.48)
481 -
482 -
        self.assertEqual(rd2.normalized(),
483 -
            relativedelta(days=1, hours=11, minutes=31, seconds=12))
484 -
485 -
    def testRelativeDeltaNormalizeFractionalDays2(self):
486 -
        # Equivalent to (hours=1, minutes=30)
487 -
        rd1 = relativedelta(hours=1.5)
488 -
489 -
        self.assertEqual(rd1.normalized(), relativedelta(hours=1, minutes=30))
490 -
491 -
        # Equivalent to (hours=3, minutes=17, seconds=5, microseconds=100)
492 -
        rd2 = relativedelta(hours=3.28472225)
493 -
494 -
        self.assertEqual(rd2.normalized(),
495 -
            relativedelta(hours=3, minutes=17, seconds=5, microseconds=100))
496 -
497 -
    def testRelativeDeltaNormalizeFractionalMinutes(self):
498 -
        # Equivalent to (minutes=15, seconds=36)
499 -
        rd1 = relativedelta(minutes=15.6)
500 -
501 -
        self.assertEqual(rd1.normalized(),
502 -
            relativedelta(minutes=15, seconds=36))
503 -
504 -
        # Equivalent to (minutes=25, seconds=20, microseconds=25000)
505 -
        rd2 = relativedelta(minutes=25.33375)
506 -
507 -
        self.assertEqual(rd2.normalized(),
508 -
            relativedelta(minutes=25, seconds=20, microseconds=25000))
509 -
510 -
    def testRelativeDeltaNormalizeFractionalSeconds(self):
511 -
        # Equivalent to (seconds=45, microseconds=25000)
512 -
        rd1 = relativedelta(seconds=45.025)
513 -
        self.assertEqual(rd1.normalized(),
514 -
            relativedelta(seconds=45, microseconds=25000))
515 -
516 -
    def testRelativeDeltaFractionalPositiveOverflow2(self):
517 -
        # Equivalent to (days=1, hours=14)
518 -
        rd1 = relativedelta(days=1.5, hours=2)
519 -
        self.assertEqual(rd1.normalized(),
520 -
            relativedelta(days=1, hours=14))
521 -
522 -
        # Equivalent to (days=1, hours=14, minutes=45)
523 -
        rd2 = relativedelta(days=1.5, hours=2.5, minutes=15)
524 -
        self.assertEqual(rd2.normalized(),
525 -
            relativedelta(days=1, hours=14, minutes=45))
526 -
527 -
        # Carry back up - equivalent to:
528 -
        # (days=2, hours=2, minutes=0, seconds=2, microseconds=3)
529 -
        rd3 = relativedelta(days=1.5, hours=13, minutes=59.50045,
530 -
                            seconds=31.473, microseconds=500003)
531 -
        self.assertEqual(rd3.normalized(),
532 -
            relativedelta(days=2, hours=2, minutes=0,
533 -
                          seconds=2, microseconds=3))
534 -
535 -
    def testRelativeDeltaFractionalNegativeOverflow(self):
536 -
        # Equivalent to (days=-1)
537 -
        rd1 = relativedelta(days=-0.5, hours=-12)
538 -
        self.assertEqual(rd1.normalized(),
539 -
            relativedelta(days=-1))
540 -
541 -
        # Equivalent to (days=-1)
542 -
        rd2 = relativedelta(days=-1.5, hours=12)
543 -
        self.assertEqual(rd2.normalized(),
544 -
            relativedelta(days=-1))
545 -
546 -
        # Equivalent to (days=-1, hours=-14, minutes=-45)
547 -
        rd3 = relativedelta(days=-1.5, hours=-2.5, minutes=-15)
548 -
        self.assertEqual(rd3.normalized(),
549 -
            relativedelta(days=-1, hours=-14, minutes=-45))
550 -
551 -
        # Equivalent to (days=-1, hours=-14, minutes=+15)
552 -
        rd4 = relativedelta(days=-1.5, hours=-2.5, minutes=45)
553 -
        self.assertEqual(rd4.normalized(),
554 -
            relativedelta(days=-1, hours=-14, minutes=+15))
555 -
556 -
        # Carry back up - equivalent to:
557 -
        # (days=-2, hours=-2, minutes=0, seconds=-2, microseconds=-3)
558 -
        rd3 = relativedelta(days=-1.5, hours=-13, minutes=-59.50045,
559 -
                            seconds=-31.473, microseconds=-500003)
560 -
        self.assertEqual(rd3.normalized(),
561 -
            relativedelta(days=-2, hours=-2, minutes=0,
562 -
                          seconds=-2, microseconds=-3))
563 -
564 -
    def testInvalidYearDay(self):
565 -
        with self.assertRaises(ValueError):
566 -
            relativedelta(yearday=367)
567 -
568 -
    def testAddTimedeltaToUnpopulatedRelativedelta(self):
569 -
        td = timedelta(
570 -
            days=1,
571 -
            seconds=1,
572 -
            microseconds=1,
573 -
            milliseconds=1,
574 -
            minutes=1,
575 -
            hours=1,
576 -
            weeks=1
577 -
        )
578 -
579 -
        expected = relativedelta(
580 -
            weeks=1,
581 -
            days=1,
582 -
            hours=1,
583 -
            minutes=1,
584 -
            seconds=1,
585 -
            microseconds=1001
586 -
        )
587 -
588 -
        self.assertEqual(expected, relativedelta() + td)
589 -
590 -
    def testAddTimedeltaToPopulatedRelativeDelta(self):
591 -
        td = timedelta(
592 -
            days=1,
593 -
            seconds=1,
594 -
            microseconds=1,
595 -
            milliseconds=1,
596 -
            minutes=1,
597 -
            hours=1,
598 -
            weeks=1
599 -
        )
600 -
601 -
        rd = relativedelta(
602 -
            year=1,
603 -
            month=1,
604 -
            day=1,
605 -
            hour=1,
606 -
            minute=1,
607 -
            second=1,
608 -
            microsecond=1,
609 -
            years=1,
610 -
            months=1,
611 -
            days=1,
612 -
            weeks=1,
613 -
            hours=1,
614 -
            minutes=1,
615 -
            seconds=1,
616 -
            microseconds=1
617 -
        )
618 -
619 -
        expected = relativedelta(
620 -
            year=1,
621 -
            month=1,
622 -
            day=1,
623 -
            hour=1,
624 -
            minute=1,
625 -
            second=1,
626 -
            microsecond=1,
627 -
            years=1,
628 -
            months=1,
629 -
            weeks=2,
630 -
            days=2,
631 -
            hours=2,
632 -
            minutes=2,
633 -
            seconds=2,
634 -
            microseconds=1002,
635 -
        )
636 -
637 -
        self.assertEqual(expected, rd + td)
638 -
639 -
    def testHashable(self):
640 -
        try:
641 -
            {relativedelta(minute=1): 'test'}
642 -
        except:
643 -
            self.fail("relativedelta() failed to hash!")
644 -
645 -
646 -
class RelativeDeltaWeeksPropertyGetterTest(unittest.TestCase):
647 -
    """Test the weeks property getter"""
648 -
649 -
    def test_one_day(self):
650 -
        rd = relativedelta(days=1)
651 -
        self.assertEqual(rd.days, 1)
652 -
        self.assertEqual(rd.weeks, 0)
653 -
654 -
    def test_minus_one_day(self):
655 -
        rd = relativedelta(days=-1)
656 -
        self.assertEqual(rd.days, -1)
657 -
        self.assertEqual(rd.weeks, 0)
658 -
659 -
    def test_height_days(self):
660 -
        rd = relativedelta(days=8)
661 -
        self.assertEqual(rd.days, 8)
662 -
        self.assertEqual(rd.weeks, 1)
663 -
664 -
    def test_minus_height_days(self):
665 -
        rd = relativedelta(days=-8)
666 -
        self.assertEqual(rd.days, -8)
667 -
        self.assertEqual(rd.weeks, -1)
668 -
669 -
670 -
class RelativeDeltaWeeksPropertySetterTest(unittest.TestCase):
671 -
    """Test the weeks setter which makes a "smart" update of the days attribute"""
672 -
673 -
    def test_one_day_set_one_week(self):
674 -
        rd = relativedelta(days=1)
675 -
        rd.weeks = 1  # add 7 days
676 -
        self.assertEqual(rd.days, 8)
677 -
        self.assertEqual(rd.weeks, 1)
678 -
679 -
    def test_minus_one_day_set_one_week(self):
680 -
        rd = relativedelta(days=-1)
681 -
        rd.weeks = 1  # add 7 days
682 -
        self.assertEqual(rd.days, 6)
683 -
        self.assertEqual(rd.weeks, 0)
684 -
685 -
    def test_height_days_set_minus_one_week(self):
686 -
        rd = relativedelta(days=8)
687 -
        rd.weeks = -1  # change from 1 week, 1 day to -1 week, 1 day
688 -
        self.assertEqual(rd.days, -6)
689 -
        self.assertEqual(rd.weeks, 0)
690 -
691 -
    def test_minus_height_days_set_minus_one_week(self):
692 -
        rd = relativedelta(days=-8)
693 -
        rd.weeks = -1  # does not change anything
694 -
        self.assertEqual(rd.days, -8)
695 -
        self.assertEqual(rd.weeks, -1)
543 +
    assert expected ==  relativedelta() + td
696 544
697 545
546 +
def test_relative_delta_add_timedelta_to_populated_relative_delta():
547 +
    td = timedelta(
548 +
        days=1,
549 +
        seconds=1,
550 +
        microseconds=1,
551 +
        milliseconds=1,
552 +
        minutes=1,
553 +
        hours=1,
554 +
        weeks=1
555 +
    )
556 +
557 +
    rd = relativedelta(
558 +
        year=1,
559 +
        month=1,
560 +
        day=1,
561 +
        hour=1,
562 +
        minute=1,
563 +
        second=1,
564 +
        microsecond=1,
565 +
        years=1,
566 +
        months=1,
567 +
        days=1,
568 +
        weeks=1,
569 +
        hours=1,
570 +
        minutes=1,
571 +
        seconds=1,
572 +
        microseconds=1
573 +
    )
574 +
575 +
    expected = relativedelta(
576 +
        year=1,
577 +
        month=1,
578 +
        day=1,
579 +
        hour=1,
580 +
        minute=1,
581 +
        second=1,
582 +
        microsecond=1,
583 +
        years=1,
584 +
        months=1,
585 +
        weeks=2,
586 +
        days=2,
587 +
        hours=2,
588 +
        minutes=2,
589 +
        seconds=2,
590 +
        microseconds=1002,
591 +
    )
592 +
593 +
    assert expected == rd + td
594 +
595 +
596 +
def test_relative_delta_hashable():
597 +
    try:
598 +
        {relativedelta(minute=1): 'test'}
599 +
    except:
600 +
        pytest.fail("relativedelta() failed to hash!")
601 +
602 +
603 +
# Test the weeks property getter
604 +
def test_relative_delta_weeks_getter_one_day():
605 +
    rd = relativedelta(days=1)
606 +
    assert rd.days == 1
607 +
    assert rd.weeks == 0
608 +
609 +
610 +
def test_relative_delta_weeks_getter_minus_on_day():
611 +
    rd = relativedelta(days=-1)
612 +
    assert rd.days == -1
613 +
    assert rd.weeks == 0
614 +
615 +
616 +
def test_relative_delta_weeks_getter_height_days():
617 +
    rd = relativedelta(days=8)
618 +
    assert rd.days == 8
619 +
    assert rd.weeks == 1
620 +
621 +
622 +
def test_relative_delta_weeks_getter_minus_height_days():
623 +
    rd = relativedelta(days=-8)
624 +
    assert rd.days == -8
625 +
    assert rd.weeks == -1
626 +
627 +
628 +
# Test the weeks setter which makes a "smart" update of the days attribute
629 +
def test_relative_delta_weeks_setter_one_day_set_one_week():
630 +
    rd = relativedelta(days=1)
631 +
    rd.weeks = 1  # add 7 days
632 +
    assert rd.days == 8
633 +
    assert rd.weeks == 1
634 +
635 +
636 +
def test_relative_delta_weeks_setter_minus_one_day_set_one_week():
637 +
    rd = relativedelta(days=-1)
638 +
    rd.weeks = 1  # add 7 days
639 +
    assert rd.days == 6
640 +
    assert rd.weeks == 0
641 +
642 +
643 +
def test_relative_delta_weeks_setter_height_days_set_minus_one_week():
644 +
    rd = relativedelta(days=8)
645 +
    rd.weeks = -1  # change from 1 week, 1 day to -1 week, 1 day
646 +
    assert rd.days == -6
647 +
    assert rd.weeks == 0
648 +
649 +
650 +
def test_relative_delta_weeks_setter_minus_height_days_set_minus_one_week():
651 +
    rd = relativedelta(days=-8)
652 +
    rd.weeks = -1  # does not change anything
653 +
    assert rd.days == -8
654 +
    assert rd.weeks == -1
655 +
698 656
# vim:ts=4:sw=4:et

Everything is accounted for!

No changes detected that need to be reviewed.
What changes does Codecov check for?
Lines, not adjusted in diff, that have changed coverage data.
Files that introduced coverage data that had none before.
Files that have missing coverage data that once were tracked.
Files Coverage
dateutil -<.01% 95.84%
Project Totals (31 files) 95.84%
Loading