Pārlūkot izejas kodu

DatePeriod: overloaded comparison operator

Fabian Peter Hammerle 9 gadi atpakaļ
vecāks
revīzija
0509b06ac0
2 mainītis faili ar 41 papildinājumiem un 0 dzēšanām
  1. 5 0
      ioex/__init__.py
  2. 36 0
      tests/test_dateperiod.py

+ 5 - 0
ioex/__init__.py

@@ -105,3 +105,8 @@ class DatePeriod(object):
         attr = match.groupdict()
         self.start = dateutil.parser.parse(attr['start'])
         self.end = dateutil.parser.parse(attr['end'])
+
+    def __eq__(self, other):
+        return (type(self) == type(other)
+            and self.start == other.start
+            and self.end == other.end)

+ 36 - 0
tests/test_dateperiod.py

@@ -167,3 +167,39 @@ def test_dateperiod_set_isoformat_fail(iso):
     p = ioex.DatePeriod()
     with pytest.raises(ValueError):
         p.isoformat = iso
+
+@pytest.mark.parametrize(('a', 'b'), [
+    [
+        ioex.DatePeriod(
+            start = datetime.datetime(2016, 7, 24, 12, 21, 0),
+            end = datetime.datetime(2016, 7, 24, 12, 22, 13),
+            ),
+        ioex.DatePeriod(
+            start = datetime.datetime(2016, 7, 24, 12, 21, 0, 0),
+            end = datetime.datetime(2016, 7, 24, 12, 22, 13, 0),
+            ),
+        ],
+    [
+        ioex.DatePeriod(
+            start = pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 24, 12, 21, 0)),
+            end = datetime.datetime(2016, 7, 24, 12, 22, 13),
+            ),
+        ioex.DatePeriod(
+            start = pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 24, 12, 21, 0)),
+            end = datetime.datetime(2016, 7, 24, 12, 22, 13),
+            ),
+        ],
+    [
+        ioex.DatePeriod(
+            start = pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 24, 12, 21, 0)),
+            end = pytz.timezone('Europe/London').localize(datetime.datetime(2016, 7, 24, 12, 22, 13)),
+            ),
+        ioex.DatePeriod(
+            start = pytz.timezone('Europe/London').localize(datetime.datetime(2016, 7, 24, 11, 21, 0)),
+            end = pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 24, 13, 22, 13)),
+            ),
+        ],
+    ])
+def test_eq(a, b):
+    assert a == b
+    assert b == a