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)
class RdbContextManager(object): """ Context manager for RDB tests. Configures the entity repository to use the RDB implementation as a default, sets up an outer transaction before the test is run and rolls this transaction back after the test has finished. """ def __init__(self, autoflush=True, engine_name=None): self.__autoflush = autoflush if engine_name is None: # Use the name of the default RDB repository for engine lookup. engine_name = REPOSITORY_TYPES.RDB self.__engine_name = engine_name self.__connection = None self.__transaction = None self.__session = None self.__old_autoflush_flag = None 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 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
def test_order_clause_list(self): # This emulates customized comparators which return clause lists # for .asc and .desc operations. old_asc = Person.name.asc expected_expr = OrderClauseList(Person.id.asc(), Person.name.asc(), Person.age.asc()) self.assert_equal(str(expected_expr), 'person.id ASC, person.name ASC, person.age ASC') try: Person.name.asc = lambda: OrderClauseList(Person.id.asc(), old_asc( )) expr = self._run_visitor('two-asc-asc') self.assert_equal(str(expr), str(expected_expr)) finally: Person.name.asc = old_asc # Make sure the correct ORDER BY clause is generated. q = Session.query(Person).order_by(expr) # pylint: disable=E1101 q_str = str(q.statement) self.assert_not_equal(q_str.find("ORDER BY %s" % expr), -1)
def test_order_clause_list(self): # This emulates customized comparators which return clause lists # for .asc and .desc operations. old_asc = Person.name.asc expected_expr = OrderClauseList(Person.id.asc(), Person.name.asc(), Person.age.asc()) self.assert_equal(str(expected_expr), 'person.id ASC, person.name ASC, person.age ASC') try: Person.name.asc = lambda : OrderClauseList(Person.id.asc(), old_asc()) expr = self._run_visitor('two-asc-asc') self.assert_equal(str(expr), str(expected_expr)) finally: Person.name.asc = old_asc # Make sure the correct ORDER BY clause is generated. q = Session.query(Person).order_by(expr) # pylint: disable=E1101 q_str = str(q.statement) self.assert_not_equal(q_str.find("ORDER BY %s" % expr), -1)