Hi!

I am trying to make a base class for our tests, that after each test case 
all the changes made by tests and the tested code are rolled back.
I saw the pattern here 
http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html#joining-a-session-into-an-external-transaction,
 
but i have some problems.

class BaseTest(unittest.TestCase):
    """
    
http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html#joining-a-session-into-an-external-transaction
    """
    def __call__(self, result=None):
        """
        Wrapper around default __call__ method to perform common test
        set up. This means that user-defined Test Cases aren't required to
        include a call to super().setUp().
        """
        testMethod = getattr(self, self._testMethodName)
        skipped = (getattr(self.__class__, "__unittest_skip__", False) or
            getattr(testMethod, "__unittest_skip__", False))

        if not skipped:
            try:
                self._pre_setup()
            except (KeyboardInterrupt, SystemExit):
                raise
            except Exception:
                result.addError(self, sys.exc_info())
                return
        super(BaseTest, self).__call__(result)
        if not skipped:
            try:
                self._post_teardown()
            except (KeyboardInterrupt, SystemExit):
                raise
            except Exception:
                result.addError(self, sys.exc_info())
                return

    def _pre_setup(self):
        # connect to the database
        self._connection = apilib.engine.connect()
        # begin a non-ORM transaction
        self._transaction = self._connection.begin()
        # make apilib.Session to be inside our transaction
        apilib.Session.configure(bind=self._connection)
        self.session = sessionmaker(bind=self._connection)()

    def _post_teardown(self):
        # roll back all changes made by the tests
        self._transaction.rollback()
        self.session.close()
        apilib.Session.configure(bind=apilib.engine)
        # return connection to the Engine
        self._connection.close()


1. The tests code uses apilib.Session, while the tests use self.session.
2. Looks like if in code some does an additional session.rollback(), all 
the enclosing transaction is rolled back, and all objects created in 
setUp() (which use self.session) are lost.
3. Did i do correctly that i made the tested code use the session with test 
connection in transaction? Otherwise i guess sessions in the tested code 
and test itself would be in different transactions, and the tested code 
would not see changes made in setUp()
4. Is this the best pattern for our needs?

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to