]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
oof, history on collections were wrong. fixed byroot_tree test as well
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 14 Dec 2007 23:28:10 +0000 (23:28 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 14 Dec 2007 23:28:10 +0000 (23:28 +0000)
examples/adjacencytree/byroot_tree.py
lib/sqlalchemy/orm/attributes.py
test/orm/attributes.py

index a67eeb77149ff966978ef4f518b3db3e47b15224..e57b11beec74742bb76561d650d00b80853c36b4 100644 (file)
@@ -53,7 +53,7 @@ class TreeNode(object):
             c._set_root(root)
 
     def append(self, node):
-        if isinstance(node, str):
+        if isinstance(node, basestring):
             node = TreeNode(node)
         node._set_root(self.root)
         self.children.set(node)
@@ -110,7 +110,7 @@ class TreeLoader(MapperExtension):
         else:
             if isnew or selectcontext.populate_existing:
                 key = mapper.identity_key_from_primary_key(instance.parent_id)
-                parentnode = selectcontext.identity_map[key]
+                parentnode = selectcontext.session.identity_map[key]
                 parentnode.children.set(instance)
         return False
 
index 01be8813f07460cbb8d73edabf57ab0559b68a29..4f10ecc2254d0065cb73c8663b0ebe90f84a246a 100644 (file)
@@ -431,7 +431,7 @@ class CollectionAttributeImpl(AttributeImpl):
     def fire_append_event(self, state, value, initiator):
         if self.key not in state.committed_state and self.key in state.dict:
             state.committed_state[self.key] = self.copy(state.dict[self.key])
-
+            
         state.modified = True
 
         if self.trackparent and value is not None:
@@ -957,12 +957,13 @@ def _create_history(attr, state, current):
     original = state.committed_state.get(attr.key, NEVER_SET)
 
     if hasattr(attr, 'get_collection'):
+        current = attr.get_collection(state, current)
         if original is NO_VALUE:
             return (list(current), [], [])
         elif original is NEVER_SET:
             return ([], list(current), [])
         else:
-            collection = util.OrderedIdentitySet(attr.get_collection(state, current))
+            collection = util.OrderedIdentitySet(current)
             s = util.OrderedIdentitySet(original)
             return (list(collection.difference(s)), list(collection.intersection(s)), list(s.difference(collection)))
     else:
index 74d94a4b4d98c1c91e0460957ed04176d1481276..96940ec205410c732f165f89ca77cac617dbf157 100644 (file)
@@ -696,6 +696,36 @@ class HistoryTest(PersistTest):
         f._state.commit(['someattr'])
         self.assertEquals(attributes.get_history(f._state, 'someattr'), ([], [old], []))
 
+    def test_dict_collections(self):
+        class Foo(fixtures.Base):
+            pass
+        class Bar(fixtures.Base):
+            pass
+
+        from sqlalchemy.orm.collections import attribute_mapped_collection
+            
+        attributes.register_class(Foo)
+        attributes.register_attribute(Foo, 'someattr', uselist=True, useobject=True, typecallable=attribute_mapped_collection('name'))
+
+        hi = Bar(name='hi')
+        there = Bar(name='there')
+        old = Bar(name='old')
+        new = Bar(name='new')
+
+        f = Foo()
+        self.assertEquals(attributes.get_history(f._state, 'someattr'), ([], [], []))
+
+        f.someattr['hi'] = hi
+        self.assertEquals(attributes.get_history(f._state, 'someattr'), ([hi], [], []))
+
+        f.someattr['there'] = there
+        self.assertEquals(tuple([set(x) for x in attributes.get_history(f._state, 'someattr')]), (set([hi, there]), set([]), set([])))
+        
+        f._state.commit(['someattr'])
+        self.assertEquals(tuple([set(x) for x in attributes.get_history(f._state, 'someattr')]), (set([]), set([hi, there]), set([])))
+
+
+
     def test_object_collections_mutate(self):
         class Foo(fixtures.Base):
             pass