Skip to content

Commit

Permalink
Squashed commit:
Browse files Browse the repository at this point in the history
    - Adding my name o the contributors file.

    - Updating the datetime deserialize function as well to keep both Date and DateTime in sync with each other.  Also added test case for datetime deserialization.

    - Adding test cases to show the issue before development.
  • Loading branch information
Keith committed Jul 30, 2020
1 parent c91a8e6 commit 765fd20
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,4 @@ Contributors
- Manuel Vázquez, 2018/11/22
- Kirill Kuzminykh, 2019/01/27
- Damian Dimmich, 2020/07/19
- Keith M Franklin, 2020/07/30
4 changes: 2 additions & 2 deletions src/colander/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1882,7 +1882,7 @@ def deserialize(self, node, cstruct):
result = iso8601.parse_date(
cstruct, default_timezone=self.default_tzinfo
)
except (ValueError, iso8601.ParseError) as e:
except (ValueError, TypeError, iso8601.ParseError) as e:
raise Invalid(
node, _(self.err_template, mapping={'val': cstruct, 'err': e})
)
Expand Down Expand Up @@ -1960,7 +1960,7 @@ def deserialize(self, node, cstruct):
else:
result = iso8601.parse_date(cstruct)
result = result.date()
except iso8601.ParseError as e:
except (ValueError, TypeError, iso8601.ParseError) as e:
raise Invalid(
node, _(self.err_template, mapping={'val': cstruct, 'err': e})
)
Expand Down
30 changes: 30 additions & 0 deletions tests/test_colander.py
Original file line number Diff line number Diff line change
Expand Up @@ -2631,6 +2631,17 @@ def test_deserialize_none_tzinfo(self):
self.assertEqual(result.isoformat(), dt.isoformat())
self.assertEqual(result.tzinfo, None)

def test_deserialize_invalid_type(self):
from colander import Invalid

typ = self._makeOne(default_tzinfo=None, format='%d/%m/%Y')
node = DummySchemaNode(None)
self.assertRaises(Invalid, typ.deserialize, node, 10012001)

class Anon():
pass
self.assertRaises(Invalid, typ.deserialize, node, Anon())


class TestDate(unittest.TestCase):
def _makeOne(self, *arg, **kw):
Expand Down Expand Up @@ -2745,6 +2756,25 @@ def test_deserialize_date_with_custom_format(self):
result = typ.deserialize(node, formatted)
self.assertEqual(result, date)

def test_deserialize_date_with_custom_format_invalid_type(self):
from colander import Invalid

typ = self._makeOne(format='%d/%m/%Y')
node = DummySchemaNode(None)
self.assertRaises(Invalid, typ.deserialize, node, 123)

class Anon():
pass
self.assertRaises(Invalid, typ.deserialize, node, Anon())

def test_deserialize_date_with_incorrect_format(self):
from colander import Invalid

typ = self._makeOne(format='%d/%m/%Y')
node = DummySchemaNode(None)
self.assertRaises(Invalid, typ.deserialize, node, "2001-01-01")
self.assertRaises(Invalid, typ.deserialize, node, "01012001")


class TestTime(unittest.TestCase):
def _makeOne(self, *arg, **kw):
Expand Down

0 comments on commit 765fd20

Please sign in to comment.