def test_nativeext_interfaceexact(self): class A(object): __sa_instrumentation_manager__ = ( instrumentation.InstrumentationManager) register_class(A) ne_(type(manager_of_class(A)), instrumentation.ClassManager)
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_standard(self): class A(object): pass register_class(A) eq_(type(manager_of_class(A)), instrumentation.ClassManager)
def test_nativeext_submanager(self): class Mine(instrumentation.ClassManager): pass class A(object): __sa_instrumentation_manager__ = Mine register_class(A) eq_(type(manager_of_class(A)), Mine)
def test_customfinder_pass(self): class A(object): pass def find(cls): return None instrumentation.instrumentation_finders.insert(0, find) register_class(A) eq_(type(manager_of_class(A)), instrumentation.ClassManager)
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_subclassed(self): class MyEvents(events.InstanceEvents): pass class MyClassManager(instrumentation.ClassManager): dispatch = event.dispatcher(MyEvents) instrumentation.instrumentation_finders.insert( 0, lambda cls: MyClassManager) class A(object): pass register_class(A) manager = instrumentation.manager_of_class(A) assert issubclass(manager.dispatch._events, MyEvents)
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_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_single_down(self): class A(object): pass register_class(A) def mgr_factory(cls): return instrumentation.ClassManager(cls) class B(A): __sa_instrumentation_manager__ = staticmethod(mgr_factory) assert_raises_message( TypeError, "multiple instrumentation implementations", register_class, B, )
def test_alternate_finders(self): """Ensure the generic finder front-end deals with edge cases.""" class Unknown(object): pass class Known(MyBaseClass): pass register_class(Known) k, u = Known(), Unknown() assert instrumentation.manager_of_class(Unknown) is None assert instrumentation.manager_of_class(Known) is not None assert instrumentation.manager_of_class(None) is None assert attributes.instance_state(k) is not None assert_raises((AttributeError, KeyError), attributes.instance_state, u) assert_raises((AttributeError, KeyError), attributes.instance_state, None)
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_diamond_b2(self): def mgr_factory(cls): return instrumentation.ClassManager(cls) class A(object): pass class B1(A): pass class B2(A): __sa_instrumentation_manager__ = staticmethod(mgr_factory) class C(object): pass register_class(B2) assert_raises_message( TypeError, "multiple instrumentation implementations", register_class, B1, )
def test_none(self): class A(object): pass register_class(A) def mgr_factory(cls): return instrumentation.ClassManager(cls) class B(object): __sa_instrumentation_manager__ = staticmethod(mgr_factory) register_class(B) class C(object): __sa_instrumentation_manager__ = instrumentation.ClassManager register_class(C)
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)