def __exit__(self, ext_type, value, tb): # Roll back the outer transaction and close the connection. self.__session.close() self.__transaction.rollback() self.__connection.close() # Remove the session we created. Session.remove() # Restore autoflush flag. Session.configure(autoflush=self.__old_autoflush_flag)
def __enter__(self): # We set up an outer transaction that allows us to roll back all # changes (including commits) the unittest may want to make. engine = get_engine(self.__engine_name) self.__connection = engine.connect() self.__transaction = self.__connection.begin() # Configure the autoflush behavior of the session. self.__old_autoflush_flag = Session.autoflush # pylint:disable=E1101 Session.remove() Session.configure(autoflush=self.__autoflush) # Throw out the Zope transaction manager for testing. Session.configure(extension=None) # Create a new session for the tests. self.__session = Session(bind=self.__connection) return self.__session