Commits

Jean-Tiare Le Bigot committed 3a9d195

fix regression: allow callable in __defaults__

  • Participants
  • Parent commits 084b90b

Comments (0)

Files changed (2)

File dynamodb_mapper/model.py

             if name in kwargs:
                 value = kwargs.get(name)
             elif name in defaults:
-                value = copy.deepcopy(defaults[name])
+                template = defaults[name]
+                if callable(template):
+                    value = template()
+                else:
+                    value = copy.deepcopy(template)
             else:
                 value = None
             setattr(self, name, value)

File dynamodb_mapper/tests/test_model.py

         "bad": set([1,2,3]),
     }
 
+def return_42():
+    return 42
+
+class SchemaCallableDefault(DynamoDBModel):
+    __table__ = "SchemaValidators"
+    __hash_key__ = "id"
+    __schema__ = {
+        "id": int,
+    }
+    __defaults__ = {"id": return_42}
+
+
 class TestUTC(unittest.TestCase):
     def test_utcoffset(self):
         self.assertEqual(datetime.timedelta(0), UTC().utcoffset(None))
         d = PlayerStrength()
         self.assertEqual(d.strength, u"weak")
 
+    def test_build_default_values_with_callable_defaults(self):
+        d = SchemaCallableDefault()
+        self.assertEqual(d.id, 42)
+
     def test_init_from_args(self):
         d = DoomEpisode(id=1, name=u"Knee-deep in the Dead")
         self.assertEqual({}, d._raw_data)