('remove', f, [4,5,6])
])
-
def test_lazytrackparent(self):
"""test that the "hasparent" flag works properly
when lazy loaders and backrefs are used
except sa_exc.ArgumentError, e:
assert False
+class GetNoValueTest(fixtures.ORMTest):
+ def _fixture(self, expected):
+ class Foo(object):
+ pass
+
+ class Bar(object):
+ pass
+
+ def lazy_callable(state, passive):
+ return expected
+
+ instrumentation.register_class(Foo)
+ instrumentation.register_class(Bar)
+ if expected is not None:
+ attributes.register_attribute(Foo,
+ "attr", useobject=True,
+ uselist=False, callable_=lazy_callable)
+ else:
+ attributes.register_attribute(Foo,
+ "attr", useobject=True,
+ uselist=False)
+
+ f1 = Foo()
+ return Foo.attr.impl,\
+ attributes.instance_state(f1), \
+ attributes.instance_dict(f1)
+
+
+ def test_passive_no_result(self):
+ attr, state, dict_ = self._fixture(attributes.PASSIVE_NO_RESULT)
+ eq_(
+ attr.get(state, dict_, passive=attributes.PASSIVE_NO_INITIALIZE),
+ attributes.PASSIVE_NO_RESULT
+ )
+
+ def test_passive_no_result_never_set(self):
+ attr, state, dict_ = self._fixture(attributes.NEVER_SET)
+ eq_(
+ attr.get(state, dict_, passive=attributes.PASSIVE_NO_INITIALIZE),
+ attributes.PASSIVE_NO_RESULT
+ )
+ assert 'attr' not in dict_
+
+ def test_passive_ret_never_set_never_set(self):
+ attr, state, dict_ = self._fixture(attributes.NEVER_SET)
+ eq_(
+ attr.get(state, dict_, passive=attributes.PASSIVE_RETURN_NEVER_SET),
+ attributes.NEVER_SET
+ )
+ assert 'attr' not in dict_
+
+ def test_passive_ret_never_set_empty(self):
+ attr, state, dict_ = self._fixture(None)
+ eq_(
+ attr.get(state, dict_, passive=attributes.PASSIVE_RETURN_NEVER_SET),
+ attributes.NEVER_SET
+ )
+ assert 'attr' not in dict_
+
+ def test_passive_no_result_empty(self):
+ attr, state, dict_ = self._fixture(None)
+ eq_(
+ attr.get(state, dict_, passive=attributes.PASSIVE_NO_RESULT),
+ None
+ )
+ assert 'attr' in dict_
+
+ def test_off_empty(self):
+ attr, state, dict_ = self._fixture(None)
+ eq_(
+ attr.get(state, dict_, passive=attributes.PASSIVE_OFF),
+ None
+ )
+ assert 'attr' in dict_
+
class UtilTest(fixtures.ORMTest):
def test_helpers(self):
class Foo(object):
assert a.user is u1
+
+
def test_backrefs_dont_lazyload(self):
users, Address, addresses, User = (self.tables.users,
self.classes.Address,
assert ad2 in u1.addresses
self.assert_sql_count(testing.db, go, 1)
+class GetterStateTest(_fixtures.FixtureTest):
+ """test lazyloader on non-existent attribute returns
+ expected attribute symbols, maintain expected state"""
+
+ run_inserts = None
+
+ def _u_ad_fixture(self, populate_user):
+ users, Address, addresses, User = (self.tables.users,
+ self.classes.Address,
+ self.tables.addresses,
+ self.classes.User)
+
+ mapper(User, users, properties={
+ 'addresses':relationship(Address, backref='user')
+ })
+ mapper(Address, addresses)
+
+ sess = create_session()
+ a1 = Address(email_address='a1')
+ sess.add(a1)
+ if populate_user:
+ a1.user = User(name='ed')
+ sess.flush()
+ if populate_user:
+ sess.expire_all()
+ return User, Address, sess, a1
+
+ def test_get_empty_passive_return_never_set(self):
+ User, Address, sess, a1 = self._u_ad_fixture(False)
+ eq_(
+ Address.user.impl.get(
+ attributes.instance_state(a1),
+ attributes.instance_dict(a1),
+ passive=attributes.PASSIVE_RETURN_NEVER_SET),
+ attributes.NEVER_SET
+ )
+ assert 'user_id' not in a1.__dict__
+ assert 'user' not in a1.__dict__
+
+ def test_history_empty_passive_return_never_set(self):
+ User, Address, sess, a1 = self._u_ad_fixture(False)
+ eq_(
+ Address.user.impl.get_history(
+ attributes.instance_state(a1),
+ attributes.instance_dict(a1),
+ passive=attributes.PASSIVE_RETURN_NEVER_SET),
+ ((), (), ())
+ )
+ assert 'user_id' not in a1.__dict__
+ assert 'user' not in a1.__dict__
+
+ def test_get_empty_passive_no_initialize(self):
+ User, Address, sess, a1 = self._u_ad_fixture(False)
+ eq_(
+ Address.user.impl.get(
+ attributes.instance_state(a1),
+ attributes.instance_dict(a1),
+ passive=attributes.PASSIVE_NO_INITIALIZE),
+ attributes.PASSIVE_NO_RESULT
+ )
+ assert 'user_id' not in a1.__dict__
+ assert 'user' not in a1.__dict__
+
+ def test_history_empty_passive_no_initialize(self):
+ User, Address, sess, a1 = self._u_ad_fixture(False)
+ eq_(
+ Address.user.impl.get_history(
+ attributes.instance_state(a1),
+ attributes.instance_dict(a1),
+ passive=attributes.PASSIVE_NO_INITIALIZE),
+ attributes.HISTORY_BLANK
+ )
+ assert 'user_id' not in a1.__dict__
+ assert 'user' not in a1.__dict__
+
+ def test_get_populated_passive_no_initialize(self):
+ User, Address, sess, a1 = self._u_ad_fixture(True)
+ eq_(
+ Address.user.impl.get(
+ attributes.instance_state(a1),
+ attributes.instance_dict(a1),
+ passive=attributes.PASSIVE_NO_INITIALIZE),
+ attributes.PASSIVE_NO_RESULT
+ )
+ assert 'user_id' not in a1.__dict__
+ assert 'user' not in a1.__dict__
+
+ def test_history_populated_passive_no_initialize(self):
+ User, Address, sess, a1 = self._u_ad_fixture(True)
+ eq_(
+ Address.user.impl.get_history(
+ attributes.instance_state(a1),
+ attributes.instance_dict(a1),
+ passive=attributes.PASSIVE_NO_INITIALIZE),
+ attributes.HISTORY_BLANK
+ )
+ assert 'user_id' not in a1.__dict__
+ assert 'user' not in a1.__dict__
+
+ def test_get_populated_passive_return_never_set(self):
+ User, Address, sess, a1 = self._u_ad_fixture(True)
+ eq_(
+ Address.user.impl.get(
+ attributes.instance_state(a1),
+ attributes.instance_dict(a1),
+ passive=attributes.PASSIVE_RETURN_NEVER_SET),
+ User(name='ed')
+ )
+
+ def test_history_populated_passive_return_never_set(self):
+ User, Address, sess, a1 = self._u_ad_fixture(True)
+ eq_(
+ Address.user.impl.get_history(
+ attributes.instance_state(a1),
+ attributes.instance_dict(a1),
+ passive=attributes.PASSIVE_RETURN_NEVER_SET),
+ ((), [User(name='ed'), ], ())
+ )
class M2OGetTest(_fixtures.FixtureTest):
run_inserts = 'once'