فهرست منبع

added class DatePeriod

Fabian Peter Hammerle 9 سال پیش
والد
کامیت
1d5eb4c154
2فایلهای تغییر یافته به همراه82 افزوده شده و 0 حذف شده
  1. 27 0
      ioex/__init__.py
  2. 55 0
      tests/test_.py

+ 27 - 0
ioex/__init__.py

@@ -1,6 +1,7 @@
 import os
 import sys
 import locale
+import datetime
 import threading
 import contextlib
 
@@ -45,3 +46,29 @@ def int_input_with_default(prompt, default):
         return int(s)
     else:
         return None
+
+class DatePeriod(object):
+
+    def __init__(self, start = None, end = None):
+        self.start = start
+        self.end = end
+
+    @property
+    def start(self):
+        return self._start
+
+    @start.setter
+    def start(self, start):
+        if not (start is None or type(start) is datetime.datetime):
+            raise TypeError()
+        self._start = start
+
+    @property
+    def end(self):
+        return self._end
+
+    @end.setter
+    def end(self, end):
+        if not (end is None or type(end) is datetime.datetime):
+            raise TypeError()
+        self._end = end

+ 55 - 0
tests/test_.py

@@ -30,3 +30,58 @@ def test_setlocale_unsupported_inheritance():
 def test_setlocale_strtime(dt, dt_format, locale_code, expected_string):
     with ioex.setlocale(locale_code):
         assert dt.strftime(dt_format).decode('utf-8') == expected_string
+
+@pytest.mark.parametrize(('start', 'end'), [
+    [datetime.datetime(2016, 7, 24, 12, 21), datetime.datetime(2016, 7, 24, 12, 22)],
+    [None, datetime.datetime(2016, 7, 24, 12, 22)],
+    [datetime.datetime(2016, 7, 24, 12, 21), None],
+    [None, None],
+    ])
+def test_dateperiod_init(start, end):
+    p = ioex.DatePeriod(start = start, end = end)
+    assert p.start == start
+    assert p.end == end
+
+@pytest.mark.parametrize(('start', 'end'), [
+    [';-)', datetime.datetime(2016, 7, 24, 12, 22)],
+    [datetime.datetime(2016, 7, 24, 12, 22), ';-)'],
+    ])
+def test_dateperiod_init_fail(start, end):
+    with pytest.raises(TypeError):
+        ioex.DatePeriod(start = start, end = end)
+
+@pytest.mark.parametrize(('start'), [
+    datetime.datetime(2016, 7, 24, 12, 21),
+    None,
+    ])
+def test_dateperiod_set_start(start):
+    p = ioex.DatePeriod()
+    assert p.start is None
+    p.start = start
+    assert p.start == start
+
+@pytest.mark.parametrize(('start'), [
+    ':-/',
+    ])
+def test_dateperiod_set_start_fail(start):
+    p = ioex.DatePeriod()
+    with pytest.raises(TypeError):
+        p.start = start
+
+@pytest.mark.parametrize(('end'), [
+    datetime.datetime(2016, 7, 24, 12, 21),
+    None,
+    ])
+def test_dateperiod_set_end(end):
+    p = ioex.DatePeriod()
+    assert p.end is None
+    p.end = end
+    assert p.end == end
+
+@pytest.mark.parametrize(('end'), [
+    ':-/',
+    ])
+def test_dateperiod_set_end_fail(end):
+    p = ioex.DatePeriod()
+    with pytest.raises(TypeError):
+        p.end = end