def _test_before_update_o2m(self, passive_updates):
        sess = self._mapper_setup(passive_updates=passive_updates)

        Address, User = self.classes.Address, self.classes.User

        class AvoidReferencialError(Exception):

            """the test here would require ON UPDATE CASCADE on FKs
            for the flush to fully succeed; this exception is used
            to cancel the flush before we get that far.

            """

        def before_update(mapper, connection, target):
            if passive_updates:
                # we shouldn't be using committed value.
                # so, having switched target's primary key,
                # we expect no related items in the collection
                # since we are using passive_updates
                # this is a behavior change since #2350
                assert 'addresses' not in target.__dict__
                eq_(target.addresses, [])
            else:
                # in contrast with passive_updates=True,
                # here we expect the orm to have looked up the addresses
                # with the committed value (it needs to in order to
                # update the foreign keys).  So we expect addresses
                # collection to move with the user,
                # (just like they will be after the update)

                # collection is already loaded
                assert 'addresses' in target.__dict__
                eq_([a.id for a in target.addresses],
                    [a.id for a in [a1, a2]])
            raise AvoidReferencialError()
        from sqlalchemy import event
        event.listen(User, 'before_update', before_update)

        a1 = Address(email_address='jack1')
        a2 = Address(email_address='jack2')
        u1 = User(id=1, name='jack', addresses=[a1, a2])
        sess.add(u1)
        sess.commit()

        sess.expunge_all()
        u1 = sess.query(User).get(u1.id)
        u1.id = 2
        try:
            sess.flush()
        except AvoidReferencialError:
            pass
dataset/ETHPy150Open zzzeek/sqlalchemy/test/orm/test_unitofworkv2.py/LoadersUsingCommittedTest._test_before_update_o2m/original