Skip to content

Commit

Permalink
Merge pull request #106 from danifus/dtfixes
Browse files Browse the repository at this point in the history
Fix encoding datetime objects.
  • Loading branch information
brunato authored May 4, 2019
2 parents c20c9d8 + 633ac31 commit 376e208
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
41 changes: 41 additions & 0 deletions xmlschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,47 @@ def test_complex_elements(self):
expected=XMLSchemaValidationError, indent=0, cdata_prefix='#'
)

def test_encode_datetime(self):
xs = self.get_schema('<element name="dt" type="dateTime"/>')

dt = xs.decode('<ns:dt xmlns:ns="ns">2019-01-01T13:40:00</ns:dt>', datetime_types=True)
self.assertEqual(
etree_tostring(xs.encode(dt)),
'<ns:dt xmlns:ns="ns">2019-01-01T13:40:00</ns:dt>'
)

def test_encode_date(self):
xs = self.get_schema('<element name="dt" type="date"/>')
date = xs.decode('<ns:dt xmlns:ns="ns">2001-04-15</ns:dt>', datetime_types=True)
self.assertEqual(
etree_tostring(xs.encode(date)),
'<ns:dt xmlns:ns="ns">2001-04-15</ns:dt>'
)

def test_duration(self):
xs = self.get_schema('<element name="td" type="duration"/>')
duration = xs.decode('<ns:td xmlns:ns="ns">P5Y3MT60H30.001S</ns:td>', datetime_types=True)
self.assertEqual(
etree_tostring(xs.encode(duration)),
'<ns:td xmlns:ns="ns">P5Y3M2DT12H30.001S</ns:td>'
)

def test_gregorian_year(self):
xs = self.get_schema('<element name="td" type="gYear"/>')
gyear = xs.decode('<ns:td xmlns:ns="ns">2000</ns:td>', datetime_types=True)
self.assertEqual(
etree_tostring(xs.encode(gyear)),
'<ns:td xmlns:ns="ns">2000</ns:td>'
)

def test_gregorian_yearmonth(self):
xs = self.get_schema('<element name="td" type="gYearMonth"/>')
gyear_month = xs.decode('<ns:td xmlns:ns="ns">2000-12</ns:td>', datetime_types=True)
self.assertEqual(
etree_tostring(xs.encode(gyear_month)),
'<ns:td xmlns:ns="ns">2000-12</ns:td>'
)


class TestEncoding11(TestEncoding):
schema_class = XMLSchema11
Expand Down
8 changes: 4 additions & 4 deletions xmlschema/validators/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,28 +416,28 @@ def python_to_boolean(obj):
# --- Year related primitive types (year 0 not allowed) ---
{
'name': XSD_DATETIME,
'python_type': (unicode_type, str, datatypes.DateTime),
'python_type': (unicode_type, str, datatypes.DateTime10),
'admitted_facets': DATETIME_FACETS,
'facets': [COLLAPSE_WHITE_SPACE_ELEMENT],
'to_python': datatypes.DateTime10.fromstring,
}, # [-][Y*]YYYY-MM-DD[Thh:mm:ss]
{
'name': XSD_DATE,
'python_type': (unicode_type, str, datatypes.Date),
'python_type': (unicode_type, str, datatypes.Date10),
'admitted_facets': DATETIME_FACETS,
'facets': [COLLAPSE_WHITE_SPACE_ELEMENT],
'to_python': datatypes.Date10.fromstring,
}, # [-][Y*]YYYY-MM-DD
{
'name': XSD_GYEAR,
'python_type': (unicode_type, str, datatypes.GregorianYear),
'python_type': (unicode_type, str, datatypes.GregorianYear10),
'admitted_facets': DATETIME_FACETS,
'facets': [COLLAPSE_WHITE_SPACE_ELEMENT],
'to_python': datatypes.GregorianYear10.fromstring,
}, # [-][Y*]YYYY
{
'name': XSD_GYEAR_MONTH,
'python_type': (unicode_type, str, datatypes.GregorianYearMonth),
'python_type': (unicode_type, str, datatypes.GregorianYearMonth10),
'admitted_facets': DATETIME_FACETS,
'facets': [COLLAPSE_WHITE_SPACE_ELEMENT],
'to_python': datatypes.GregorianYearMonth10.fromstring,
Expand Down

0 comments on commit 376e208

Please sign in to comment.