def test_one_to_many_delete_all(self): mapper(User, users, properties={ 'addresses': relationship(Address), }) mapper(Address, addresses) sess = create_session() a1, a2 = Address(email_address='a1'), Address(email_address='a2') u1 = User(name='u1', addresses=[a1, a2]) sess.add(u1) sess.flush() sess.delete(u1) sess.delete(a1) sess.delete(a2) self.assert_sql_execution( testing.db, sess.flush, CompiledSQL("DELETE FROM addresses WHERE addresses.id = :id", [{ 'id': a1.id }, { 'id': a2.id }]), CompiledSQL("DELETE FROM users WHERE users.id = :id", {'id': u1.id}), )
def test_one_to_many_delete_parent(self): mapper(User, users, properties={ 'addresses': relationship(Address), }) mapper(Address, addresses) sess = create_session() a1, a2 = Address(email_address='a1'), Address(email_address='a2') u1 = User(name='u1', addresses=[a1, a2]) sess.add(u1) sess.flush() sess.delete(u1) self.assert_sql_execution( testing.db, sess.flush, CompiledSQL( "UPDATE addresses SET user_id=:user_id WHERE " "addresses.id = :addresses_id", lambda ctx: [{ u'addresses_id': a1.id, 'user_id': None }]), CompiledSQL( "UPDATE addresses SET user_id=:user_id WHERE " "addresses.id = :addresses_id", lambda ctx: [{ u'addresses_id': a2.id, 'user_id': None }]), CompiledSQL("DELETE FROM users WHERE users.id = :id", {'id': u1.id}), )
def test_many_to_one_delete_target(self): mapper(User, users) mapper(Address, addresses, properties={'user': relationship(User)}) sess = create_session() u1 = User(name='u1') a1, a2 = Address(email_address='a1', user=u1), \ Address(email_address='a2', user=u1) sess.add_all([a1, a2]) sess.flush() sess.delete(u1) a1.user = a2.user = None self.assert_sql_execution( testing.db, sess.flush, CompiledSQL( "UPDATE addresses SET user_id=:user_id WHERE " "addresses.id = :addresses_id", lambda ctx: [{ u'addresses_id': a1.id, 'user_id': None }]), CompiledSQL( "UPDATE addresses SET user_id=:user_id WHERE " "addresses.id = :addresses_id", lambda ctx: [{ u'addresses_id': a2.id, 'user_id': None }]), CompiledSQL("DELETE FROM users WHERE users.id = :id", {'id': u1.id}), )
def test_one_to_many_save(self): mapper(User, users, properties={ 'addresses': relationship(Address), }) mapper(Address, addresses) sess = create_session() a1, a2 = Address(email_address='a1'), Address(email_address='a2') u1 = User(name='u1', addresses=[a1, a2]) sess.add(u1) self.assert_sql_execution( testing.db, sess.flush, CompiledSQL("INSERT INTO users (name) VALUES (:name)", {'name': 'u1'}), CompiledSQL( "INSERT INTO addresses (user_id, email_address) " "VALUES (:user_id, :email_address)", lambda ctx: { 'email_address': 'a1', 'user_id': u1.id }), CompiledSQL( "INSERT INTO addresses (user_id, email_address) " "VALUES (:user_id, :email_address)", lambda ctx: { 'email_address': 'a2', 'user_id': u1.id }), )
def test_many_to_one_delete_unloaded(self): mapper(User, users) mapper(Address, addresses, properties={'parent': relationship(User)}) parent = User(name='p1') c1, c2 = Address(email_address='c1', parent=parent), \ Address(email_address='c2', parent=parent) session = Session() session.add_all([c1, c2]) session.add(parent) session.flush() pid = parent.id c1id = c1.id c2id = c2.id session.expire(parent) session.expire(c1) session.expire(c2) session.delete(c1) session.delete(c2) session.delete(parent) # testing that relationships # are loaded even if all ids/references are # expired self.assert_sql_execution( testing.db, session.flush, AllOf( # ensure all three m2os are loaded. # the selects here are in fact unexpiring # each row - the m2o comes from the identity map. CompiledSQL( "SELECT addresses.id AS addresses_id, addresses.user_id AS " "addresses_user_id, addresses.email_address AS " "addresses_email_address FROM addresses WHERE addresses.id = " ":param_1", lambda ctx: {'param_1': c1id}), CompiledSQL( "SELECT addresses.id AS addresses_id, addresses.user_id AS " "addresses_user_id, addresses.email_address AS " "addresses_email_address FROM addresses WHERE addresses.id = " ":param_1", lambda ctx: {'param_1': c2id}), CompiledSQL( "SELECT users.id AS users_id, users.name AS users_name " "FROM users WHERE users.id = :param_1", lambda ctx: {'param_1': pid}), ), CompiledSQL("DELETE FROM addresses WHERE addresses.id = :id", lambda ctx: [{ 'id': c1id }, { 'id': c2id }]), CompiledSQL("DELETE FROM users WHERE users.id = :id", lambda ctx: {'id': pid}), )
def test_pk_violation(self): s = self.session() a1 = Address(email_address='foo') u1 = User(id=1, name='ed', addresses=[a1]) s.add(u1) s.commit() a2 = Address(email_address='bar') u2 = User(id=1, name='jack', addresses=[a2]) u1.name = 'edward' a1.email_address = 'foober' s.add(u2) assert_raises(sa_exc.FlushError, s.commit) assert_raises(sa_exc.InvalidRequestError, s.commit) s.rollback() assert u2 not in s assert a2 not in s assert u1 in s assert a1 in s assert u1.name == 'ed' assert a1.email_address == 'foo' u1.name = 'edward' a1.email_address = 'foober' s.commit() eq_( s.query(User).all(), [User(id=1, name='edward', addresses=[Address(email_address='foober')])] )
def test_pk_violation_with_savepoint(self): s = self.session() a1 = Address(email_address='foo') u1 = User(id=1, name='ed', addresses=[a1]) s.add(u1) s.commit() a2 = Address(email_address='bar') u2 = User(id=1, name='jack', addresses=[a2]) u1.name = 'edward' a1.email_address = 'foober' s.begin_nested() s.add(u2) assert_raises(sa_exc.FlushError, s.commit) assert_raises(sa_exc.InvalidRequestError, s.commit) s.rollback() assert u2 not in s assert a2 not in s assert u1 in s assert a1 in s s.commit() assert s.query(User).all() == [ User(id=1, name='edward', addresses=[Address(email_address='foober')]) ]
def test_savepoint_rollback_collections(self): s = self.session() u1 = User(name='ed', addresses=[Address(email_address='foo')]) s.add(u1) s.commit() u1.name='edward' u1.addresses.append(Address(email_address='bar')) s.begin_nested() u2 = User(name='jack', addresses=[Address(email_address='bat')]) s.add(u2) eq_(s.query(User).order_by(User.id).all(), [ User(name='edward', addresses=[Address(email_address='foo'), Address(email_address='bar')]), User(name='jack', addresses=[Address(email_address='bat')]) ] ) s.rollback() eq_(s.query(User).order_by(User.id).all(), [ User(name='edward', addresses=[Address(email_address='foo'), Address(email_address='bar')]), ] ) s.commit() eq_(s.query(User).order_by(User.id).all(), [ User(name='edward', addresses=[Address(email_address='foo'), Address(email_address='bar')]), ] )
def test_o2m_flush_size(self): mapper(User, users, properties={ 'addresses': relationship(Address), }) mapper(Address, addresses) sess = create_session() u1 = User(name='ed') sess.add(u1) self._assert_uow_size(sess, 2) sess.flush() u1.name = 'jack' self._assert_uow_size(sess, 2) sess.flush() a1 = Address(email_address='foo') sess.add(a1) sess.flush() u1.addresses.append(a1) self._assert_uow_size(sess, 6) sess.flush() sess = create_session() u1 = sess.query(User).first() u1.name = 'ed' self._assert_uow_size(sess, 2) u1.addresses self._assert_uow_size(sess, 6)
def test_update_deleted_on_rollback_cascade(self): s = self.session() u1 = User(name='ed', addresses=[Address(email_address='foo')]) s.add(u1) s.commit() s.delete(u1) assert u1 in s.deleted assert u1.addresses[0] in s.deleted s.rollback() assert u1 in s assert u1 not in s.deleted assert u1.addresses[0] not in s.deleted
def test_update_deleted_on_rollback_orphan(self): s = self.session() u1 = User(name='ed', addresses=[Address(email_address='foo')]) s.add(u1) s.commit() a1 = u1.addresses[0] u1.addresses.remove(a1) s.flush() eq_(s.query(Address).filter(Address.email_address == 'foo').all(), []) s.rollback() assert a1 not in s.deleted assert u1.addresses == [a1]