Commits

Max Noel committed 6e56abe

* Added default value (now UTC) for datetime.datetime fields when building an instance with from_dict.

Comments (0)

Files changed (2)

dynamodb_mapper/model.py

     # This means a missing attribute for a _JSON_TYPES object is valid,
     # and results in an empty sequence. Is that a bad thing?
     if value is None:
+        if schema_type == datetime.datetime:
+            # Default value for unsupplied datetimes:
+            # the beginning of the UNIX epoch.
+            return datetime.datetime.now(tz=utc_tz)
         # Empty sets/strings are represented as missing attributes
         return schema_type()
 

dynamodb_mapper/tests/test_model.py

             _dynamodb_to_python(datetime.datetime,
                                 "2010-11-01T04:00:00.000000Z"))
 
+    @mock.patch("dynamodb_mapper.model.datetime")
+    def test_dynamodb_to_python_default(self, m_datetime):
+        # There's some fairly ugly mock/datetime juggling in that test, because
+        # datetime is written in C, so I can't simply overwrite its attributes
+        # or those of its members:
+        # the entire module must be mocked, and precise bits un-mocked as
+        # required. Joy.
+        m_datetime.timedelta = datetime.timedelta
+        m_now = datetime.datetime.now(tz=utc_tz)
+        m_datetime.datetime.now.return_value = m_now
+        self.assertEquals(
+            m_now,
+            _dynamodb_to_python(m_datetime.datetime, None))
+
     def test_dynamodb_to_python_datetime_notz(self):
         # Timezone info is mandatory
         self.assertRaises(