From 379d333c3139e82e7c61f525d0a9fd94e57bc155 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 14 Dec 2007 23:28:10 +0000 Subject: [PATCH] oof, history on collections were wrong. fixed byroot_tree test as well --- examples/adjacencytree/byroot_tree.py | 4 ++-- lib/sqlalchemy/orm/attributes.py | 5 +++-- test/orm/attributes.py | 30 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/examples/adjacencytree/byroot_tree.py b/examples/adjacencytree/byroot_tree.py index a67eeb7714..e57b11beec 100644 --- a/examples/adjacencytree/byroot_tree.py +++ b/examples/adjacencytree/byroot_tree.py @@ -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 diff --git a/lib/sqlalchemy/orm/attributes.py b/lib/sqlalchemy/orm/attributes.py index 01be8813f0..4f10ecc225 100644 --- a/lib/sqlalchemy/orm/attributes.py +++ b/lib/sqlalchemy/orm/attributes.py @@ -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: diff --git a/test/orm/attributes.py b/test/orm/attributes.py index 74d94a4b4d..96940ec205 100644 --- a/test/orm/attributes.py +++ b/test/orm/attributes.py @@ -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 -- 2.47.3