Bladeren bron

datetimeex.Period tests: replace isoformat setter method by class method from_iso() [breaking]

Fabian Peter Hammerle 8 jaren geleden
bovenliggende
commit
ba8c5d6b00
2 gewijzigde bestanden met toevoegingen van 19 en 15 verwijderingen
  1. 13 7
      ioex/datetimeex.py
  2. 6 8
      tests/datetimeex/test_period.py

+ 13 - 7
ioex/datetimeex.py

@@ -130,17 +130,23 @@ class Period(object):
             self.end.isoformat().replace('+00:00', 'Z'),
         )
 
-    @isoformat.setter
-    def isoformat(self, text):
-        match = re.search('^%s$' % self.__class__._timeperiod_iso_format, text)
+    @classmethod
+    def from_iso(cls, iso):
+        match = re.search(
+            '^{}$'.format(cls._timeperiod_iso_format),
+            iso,
+        )
         if not match:
             raise ValueError(
                 "given string '%s' does not match the supported pattern '%s'"
-                     % (text, self.__class__._timeperiod_iso_format)
+                     % (iso, cls._timeperiod_iso_format)
+            )
+        else:
+            attr = match.groupdict()
+            return cls(
+                start = dateutil.parser.parse(attr['start']),
+                end = dateutil.parser.parse(attr['end']),
             )
-        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)

+ 6 - 8
tests/datetimeex/test_period.py

@@ -110,7 +110,7 @@ def test_get_isoformat(start, end, iso):
     assert p.isoformat == iso
 
 
-@pytest.mark.parametrize(('start', 'end', 'iso'), [
+@pytest.mark.parametrize(('start', 'end', 'source_iso'), [
     [
         datetime.datetime(2016, 7, 24, 12, 21, 0),
         datetime.datetime(2016, 7, 24, 12, 22, 13),
@@ -156,20 +156,18 @@ def test_get_isoformat(start, end, iso):
         '2016-07-24T12:20:00.0255/2016-07-24T12:21:00.13Z',
     ],
 ])
-def test_set_isoformat(start, end, iso):
-    p = ioex.datetimeex.Period()
-    p.isoformat = iso
+def test_from_iso(start, end, source_iso):
+    p = ioex.datetimeex.Period.from_iso(source_iso)
     assert p.start == start
     assert p.end == end
 
 
-@pytest.mark.parametrize(('iso'), [
+@pytest.mark.parametrize(('source_iso'), [
     '2016-07-24T12:20:0<INVALID>0.0255/2016-07-24T12:21:00.13Z',
 ])
-def test_set_isoformat_fail(iso):
-    p = ioex.datetimeex.Period()
+def test_from_iso_fail(source_iso):
     with pytest.raises(ValueError):
-        p.isoformat = iso
+        ioex.datetimeex.Period.from_iso(source_iso)
 
 
 @pytest.mark.parametrize(('a', 'b'), [