def test_collection_with_backref(self): for base in (object, MyBaseClass, MyClass): class Post(base): pass class Blog(base): pass register_class(Post) register_class(Blog) attributes.register_attribute( Post, "blog", uselist=False, backref="posts", trackparent=True, useobject=True, ) attributes.register_attribute( Blog, "posts", uselist=True, backref="blog", trackparent=True, useobject=True, ) b = Blog() (p1, p2, p3) = (Post(), Post(), Post()) b.posts.append(p1) b.posts.append(p2) b.posts.append(p3) self.assert_(b.posts == [p1, p2, p3]) self.assert_(p2.blog is b) p3.blog = None self.assert_(b.posts == [p1, p2]) p4 = Post() p4.blog = b self.assert_(b.posts == [p1, p2, p4]) p4.blog = b p4.blog = b self.assert_(b.posts == [p1, p2, p4]) # assert no failure removing None p5 = Post() p5.blog = None del p5.blog
def test_null_instrumentation(self): class Foo(MyBaseClass): pass register_class(Foo) attributes.register_attribute(Foo, "name", uselist=False, useobject=False) attributes.register_attribute(Foo, "bars", uselist=True, trackparent=True, useobject=True) assert Foo.name == attributes.manager_of_class(Foo)["name"] assert Foo.bars == attributes.manager_of_class(Foo)["bars"]
def test_instance_dict(self): class User(MyClass): pass register_class(User) attributes.register_attribute(User, "user_id", uselist=False, useobject=False) attributes.register_attribute(User, "user_name", uselist=False, useobject=False) attributes.register_attribute(User, "email_address", uselist=False, useobject=False) u = User() u.user_id = 7 u.user_name = "john" u.email_address = "*****@*****.**" eq_( u.__dict__, { "_my_state": u._my_state, "_goofy_dict": { "user_id": 7, "user_name": "john", "email_address": "*****@*****.**", }, }, )
def test_inheritance(self): """tests that attributes are polymorphic""" for base in (object, MyBaseClass, MyClass): class Foo(base): pass class Bar(Foo): pass register_class(Foo) register_class(Bar) def func1(state, passive): return "this is the foo attr" def func2(state, passive): return "this is the bar attr" def func3(state, passive): return "this is the shared attr" attributes.register_attribute(Foo, "element", uselist=False, callable_=func1, useobject=True) attributes.register_attribute(Foo, "element2", uselist=False, callable_=func3, useobject=True) attributes.register_attribute(Bar, "element", uselist=False, callable_=func2, useobject=True) x = Foo() y = Bar() assert x.element == "this is the foo attr" assert y.element == "this is the bar attr", y.element assert x.element2 == "this is the shared attr" assert y.element2 == "this is the shared attr"
def test_basic(self): for base in (object, MyBaseClass, MyClass): class User(base): pass register_class(User) attributes.register_attribute(User, "user_id", uselist=False, useobject=False) attributes.register_attribute(User, "user_name", uselist=False, useobject=False) attributes.register_attribute(User, "email_address", uselist=False, useobject=False) u = User() u.user_id = 7 u.user_name = "john" u.email_address = "*****@*****.**" eq_(u.user_id, 7) eq_(u.user_name, "john") eq_(u.email_address, "*****@*****.**") attributes.instance_state(u)._commit_all( attributes.instance_dict(u)) eq_(u.user_id, 7) eq_(u.user_name, "john") eq_(u.email_address, "*****@*****.**") u.user_name = "heythere" u.email_address = "*****@*****.**" eq_(u.user_id, 7) eq_(u.user_name, "heythere") eq_(u.email_address, "*****@*****.**")
def test_history(self): for base in (object, MyBaseClass, MyClass): class Foo(base): pass class Bar(base): pass register_class(Foo) register_class(Bar) attributes.register_attribute(Foo, "name", uselist=False, useobject=False) attributes.register_attribute(Foo, "bars", uselist=True, trackparent=True, useobject=True) attributes.register_attribute(Bar, "name", uselist=False, useobject=False) f1 = Foo() f1.name = "f1" eq_( attributes.get_state_history(attributes.instance_state(f1), "name"), (["f1"], (), ()), ) b1 = Bar() b1.name = "b1" f1.bars.append(b1) eq_( attributes.get_state_history(attributes.instance_state(f1), "bars"), ([b1], [], []), ) attributes.instance_state(f1)._commit_all( attributes.instance_dict(f1)) attributes.instance_state(b1)._commit_all( attributes.instance_dict(b1)) eq_( attributes.get_state_history(attributes.instance_state(f1), "name"), ((), ["f1"], ()), ) eq_( attributes.get_state_history(attributes.instance_state(f1), "bars"), ((), [b1], ()), ) f1.name = "f1mod" b2 = Bar() b2.name = "b2" f1.bars.append(b2) eq_( attributes.get_state_history(attributes.instance_state(f1), "name"), (["f1mod"], (), ["f1"]), ) eq_( attributes.get_state_history(attributes.instance_state(f1), "bars"), ([b2], [b1], []), ) f1.bars.remove(b1) eq_( attributes.get_state_history(attributes.instance_state(f1), "bars"), ([b2], [], [b1]), )
def test_deferred(self): for base in (object, MyBaseClass, MyClass): class Foo(base): pass data = {"a": "this is a", "b": 12} def loader(state, keys): for k in keys: state.dict[k] = data[k] return attributes.ATTR_WAS_SET manager = register_class(Foo) manager.deferred_scalar_loader = loader attributes.register_attribute(Foo, "a", uselist=False, useobject=False) attributes.register_attribute(Foo, "b", uselist=False, useobject=False) if base is object: assert Foo not in ( instrumentation._instrumentation_factory._state_finders) else: assert Foo in ( instrumentation._instrumentation_factory._state_finders) f = Foo() attributes.instance_state(f)._expire(attributes.instance_dict(f), set()) eq_(f.a, "this is a") eq_(f.b, 12) f.a = "this is some new a" attributes.instance_state(f)._expire(attributes.instance_dict(f), set()) eq_(f.a, "this is a") eq_(f.b, 12) attributes.instance_state(f)._expire(attributes.instance_dict(f), set()) f.a = "this is another new a" eq_(f.a, "this is another new a") eq_(f.b, 12) attributes.instance_state(f)._expire(attributes.instance_dict(f), set()) eq_(f.a, "this is a") eq_(f.b, 12) del f.a eq_(f.a, None) eq_(f.b, 12) attributes.instance_state(f)._commit_all( attributes.instance_dict(f)) eq_(f.a, None) eq_(f.b, 12)