Commits

jason kirtland committed 7509f0c

Fix missing parenting on some SparseDict assignments.

Comments (0)

Files changed (2)

flatland/schema/containers.py

                 raise TypeError('May not set unknown key %r on %s %r' %
                                 (key, type(self).__name__, self.name))
             elif isinstance(value, schema):
+                value.parent = self
                 dict.__setitem__(self, key, value)
                 return
-            dict.__setitem__(self, key, schema(value))
+            dict.__setitem__(self, key, schema(value, parent=self))
         elif isinstance(value, schema):
             value.parent = self
             dict.__setitem__(self, key, value)

tests/schema/test_dicts.py

 
     el = schema({u'x': 123, u'y': 456})
     assert el.validate()
+
+
+def test_sparsedict_flattening():
+    schema = SparseDict.named(u'top').\
+                        of(Integer.named(u'x'), Integer.named(u'y'))
+
+    els = [
+        schema({'x': 123, 'y': 456}),
+        schema(),
+        schema(),
+        schema(),
+        ]
+    els[1].set({'x': 123, 'y': 456})
+    els[2]['x'] = 123
+    els[2]['y'] = 456
+    els[3]['x'] = Integer(123)
+    els[3]['y'] = Integer(456)
+
+    wanted = [(u'top_x', u'123'), (u'top_y', u'456')]
+    for el in els:
+        got = sorted(el.flatten())
+        assert wanted == got