|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from pendulum import Period, Pendulum |
| 4 | + |
| 5 | +from .. import AbstractTestCase |
| 6 | + |
| 7 | + |
| 8 | +class IntersectTestCase(AbstractTestCase): |
| 9 | + |
| 10 | + def test_intersect_included(self): |
| 11 | + start = Pendulum(2016, 8, 7) |
| 12 | + end = start.add(weeks=1) |
| 13 | + p1 = Period(start, end) |
| 14 | + intersection = p1.intersect(Period(start.add(days=2), start.add(days=4))) |
| 15 | + |
| 16 | + self.assertPendulum(intersection.start, 2016, 8, 9) |
| 17 | + self.assertPendulum(intersection.end, 2016, 8, 11) |
| 18 | + |
| 19 | + def test_intersect_overlap(self): |
| 20 | + start = Pendulum(2016, 8, 7) |
| 21 | + end = start.add(weeks=1) |
| 22 | + p1 = Period(start, end) |
| 23 | + intersection = p1.intersect(Period(start.add(days=-2), start.add(days=2))) |
| 24 | + |
| 25 | + self.assertPendulum(intersection.start, 2016, 8, 7) |
| 26 | + self.assertPendulum(intersection.end, 2016, 8, 9) |
| 27 | + |
| 28 | + def test_intersect_multiple(self): |
| 29 | + start = Pendulum(2016, 8, 7) |
| 30 | + end = start.add(weeks=1) |
| 31 | + p1 = Period(start, end) |
| 32 | + intersection = p1.intersect( |
| 33 | + Period(start.add(days=-2), start.add(days=2)), |
| 34 | + Period(start.add(days=1), start.add(days=2)) |
| 35 | + ) |
| 36 | + |
| 37 | + self.assertPendulum(intersection.start, 2016, 8, 8) |
| 38 | + self.assertPendulum(intersection.end, 2016, 8, 9) |
| 39 | + |
| 40 | + def test_intersect_excluded(self): |
| 41 | + start = Pendulum(2016, 8, 7) |
| 42 | + end = start.add(weeks=1) |
| 43 | + p1 = Period(start, end) |
| 44 | + intersection = p1.intersect( |
| 45 | + Period(start.add(days=-2), start.add(days=-1)) |
| 46 | + ) |
| 47 | + |
| 48 | + self.assertIsNone(intersection) |
| 49 | + |
| 50 | + def test_intersect_same(self): |
| 51 | + start = Pendulum(2016, 8, 7) |
| 52 | + end = start.add(weeks=1) |
| 53 | + p1 = Period(start, end) |
| 54 | + intersection = p1.intersect( |
| 55 | + Period(start.copy(), end.copy()) |
| 56 | + ) |
| 57 | + |
| 58 | + self.assertPendulum(intersection.start, 2016, 8, 7) |
| 59 | + self.assertPendulum(intersection.end, 2016, 8, 14) |
0 commit comments