Commits

Anonymous committed e09a273 Draft

enhance _get_default_value exception message as requested by aeric in #15; minor cleanup in _from_db_dict

  • Participants
  • Parent commits 4c0d01b

Comments (0)

Files changed (2)

dynamodb_mapper/model.py

     return schema_type()
 
 
-def _get_default_value(schema_type, default=None):
+def _get_default_value(schema_type, default=None, name=''):
     """Return a default value matching schema_type or default if provided:
 
       - For datetime.datetime, it's NOW.
        - default was callable and required arguments
        - default or its return value is not an instance of schema_type
 
-    :param schema_type class object to instanciate
-    :param default default value. May be a value or a callable (functions, class, ...) It must *NOT* require an any argument and it's type must match schema_type
+    :param schema_type: class object to instanciate
+    :param default: default value. May be a value or a callable (functions, class, ...) It must *NOT* require an any argument and it's type must match schema_type
+    :param name: field name to use in exception string if applicable
 
     """
     if default is not None:
             default = default()
         # Check default value consitency
         if not isinstance(default, schema_type):
-            raise TypeError("Expected default value of type {}, got: {}".format(schema_type, type(default)))
+            raise TypeError("Expected default value of type {}, got value: '{}' of type: {} for field: '{}'".format(schema_type, str(default), type(default), name))
         else:
             return default
 
                 default = kwargs.get(name)
             else:
                 default = defaults[name] if name in defaults else None
-            setattr(self, name, _get_default_value(type_, default))
+            setattr(self, name, _get_default_value(type_, default, name=name))
 
     @classmethod
     def _from_db_dict(cls, raw_data):
 
         for (name, type_) in cls.__schema__.iteritems():
             # Set the value if we got one from DynamoDB. Otherwise, stick with the default
-            value = _dynamodb_to_python(type_, raw_data.get(name))
-            if value is None:
-                value = _get_default_value(type_)
+            value = _dynamodb_to_python(type_, raw_data.get(name)) # de-serialize
+            value = _get_default_value(type_, value, name=name)    # match value
             setattr(instance, name, value)
 
         return instance

dynamodb_mapper/tests/test_model.py

         m_defaulter.return_value = u"weak"
         d = PlayerStrength()
 
-        m_defaulter.assert_called_with(unicode, u"weak")
+        m_defaulter.assert_called_with(unicode, u"weak", name='strength')
         self.assertEquals(d.strength, u"weak")
 
     def test_init_from_args(self):