def test_nativeext_submanager(self): class Mine(attributes.ClassManager): pass class A(object): __sa_instrumentation_manager__ = Mine attributes.register_class(A) eq_(type(attributes.manager_of_class(A)), Mine)
def test_defaulted_init(self): class X(object): def __init__(self_, a, b=123, c='abc'): self_.a = a self_.b = b self_.c = c attributes.register_class(X) o = X('foo') eq_(o.a, 'foo') eq_(o.b, 123) eq_(o.c, 'abc') class Y(object): unique = object() class OutOfScopeForEval(object): def __repr__(self_): # misleading repr return '123' outofscope = OutOfScopeForEval() def __init__(self_, u=unique, o=outofscope): self_.u = u self_.o = o attributes.register_class(Y) o = Y() assert o.u is Y.unique assert o.o is Y.outofscope
def test_standard(self): class A(object): pass attributes.register_class(A) eq_(type(attributes.manager_of_class(A)), attributes.ClassManager)
def test_defaulted_init(self): class X(object): def __init__(self_, a, b=123, c="abc"): self_.a = a self_.b = b self_.c = c attributes.register_class(X) o = X("foo") eq_(o.a, "foo") eq_(o.b, 123) eq_(o.c, "abc") class Y(object): unique = object() class OutOfScopeForEval(object): def __repr__(self_): # misleading repr return "123" outofscope = OutOfScopeForEval() def __init__(self_, u=unique, o=outofscope): self_.u = u self_.o = o attributes.register_class(Y) o = Y() assert o.u is Y.unique assert o.o is Y.outofscope
def test_customfinder_pass(self): class A(object): pass def find(cls): return None attributes.instrumentation_finders.insert(0, find) attributes.register_class(A) eq_(type(attributes.manager_of_class(A)), attributes.ClassManager)
def register(self, cls, canary): original_init = cls.__init__ attributes.register_class(cls) ne_(cls.__init__, original_init) manager = attributes.manager_of_class(cls) def on_init(state, instance, args, kwargs): canary.append((cls, 'on_init', type(instance))) manager.events.add_listener('on_init', on_init)
def test_single_down(self): class A(object): pass attributes.register_class(A) mgr_factory = lambda cls: attributes.ClassManager(cls) class B(A): __sa_instrumentation_manager__ = staticmethod(mgr_factory) self.assertRaises(TypeError, attributes.register_class, B)
def test_diamond_c_b(self): mgr_factory = lambda cls: attributes.ClassManager(cls) class A(object): pass class B1(A): pass class B2(A): __sa_instrumentation_manager__ = mgr_factory class C(object): pass attributes.register_class(C) self.assertRaises(TypeError, attributes.register_class, B1)
def test_subclassed(self): class MyEvents(attributes.Events): pass class MyClassManager(attributes.ClassManager): event_registry_factory = MyEvents attributes.instrumentation_finders.insert(0, lambda cls: MyClassManager) class A(object): pass attributes.register_class(A) manager = attributes.manager_of_class(A) assert isinstance(manager.events, MyEvents)
def test_register_reserved_attribute(self): class T(object): pass attributes.register_class(T) manager = attributes.manager_of_class(T) sa = attributes.ClassManager.STATE_ATTR ma = attributes.ClassManager.MANAGER_ATTR fails = lambda method, attr: self.assertRaises(KeyError, getattr(manager, method), attr, property()) fails("install_member", sa) fails("install_member", ma) fails("install_descriptor", sa) fails("install_descriptor", ma)
def test_register_reserved_attribute(self): class T(object): pass attributes.register_class(T) manager = attributes.manager_of_class(T) sa = attributes.ClassManager.STATE_ATTR ma = attributes.ClassManager.MANAGER_ATTR fails = lambda method, attr: self.assertRaises( KeyError, getattr(manager, method), attr, property()) fails('install_member', sa) fails('install_member', ma) fails('install_descriptor', sa) fails('install_descriptor', ma)
def test_uninstrument(self): class A(object):pass manager = attributes.register_class(A) assert attributes.manager_of_class(A) is manager attributes.unregister_class(A) assert attributes.manager_of_class(A) is None
def test_basic(self): import pickle global A class A(object): pass def canary(instance): assert False try: attributes.register_class(A) manager = attributes.manager_of_class(A) manager.events.add_listener('on_load', canary) a = A() p_a = pickle.dumps(a) re_a = pickle.loads(p_a) finally: del A
def test_none(self): class A(object): pass attributes.register_class(A) mgr_factory = lambda cls: attributes.ClassManager(cls) class B(object): __sa_instrumentation_manager__ = staticmethod(mgr_factory) attributes.register_class(B) class C(object): __sa_instrumentation_manager__ = attributes.ClassManager attributes.register_class(C)
def test_nativeext_interfaceexact(self): class A(object): __sa_instrumentation_manager__ = sa.orm.interfaces.InstrumentationManager attributes.register_class(A) ne_(type(attributes.manager_of_class(A)), attributes.ClassManager)