In the process of trying to find an efficient way to manage a test database for a large set of tests for a database library I'm writing that uses SQLAlchemy, I came across this page:
http://alextechrants.blogspot.fi/2013/08/unit-testing-sqlalchemy-apps.html This is definitely what I want to do, with one catch: I already have a session management system in place for the library that seems to conflict with the sample code given on the webpage, and I'm not having luck reconciling it. Basically I have an 'init_session' I use that looks as follows: def init_session(db_user, db_password, **kwargs): """Initialize database session""" if 'hostname' not in kwargs or 'db_name' not in kwargs: db_host, db_name = load_db_config() kwargs.setdefault('hostname', db_host) kwargs.setdefault('db_name', db_name) dbconn_string = create_dbconn_string(db_user, db_password, **kwargs) engine = create_engine(dbconn_string) # Ensure connection information is valid try: engine.execute('select 1').scalar() except sqlalchemy.exc.DBAPIError, e: raise PermissionsException(e) Session.configure(bind=engine) Session is defined as follows (in another file at module-level): Session = scoped_session(sessionmaker()) >From all other modules, I simply do a: from <db-meta-module> import Session and I am able to use Session to manage all my access to the ORM. My attempt to modify the code on the webpage currently looks like this: import unittest2 as unittest from tagopsdb.database import init_database, init_session from tagopsdb.database.meta import Session def setup_module(): global transaction, connection, engine # Connect to the database and create the schema within a transaction engine = init_session('dbtest', 'dbtestpasswd', hostname='localhost', db_name='TagOpsDB_Testing') init_database() # If you want to insert fixtures to the DB, do it here def teardown_module(): # Roll back the top level transaction and disconnect from the database Session.rollback() Session.close() engine.dispose() class DatabaseTest(unittest.TestCase): """Base unittest class to manage nested transactions for testing""" def setup(self): self.__transaction = Session.begin_nested() def teardown(self): self.__transaction.rollback() I modified my test classes to subclass DatabaseTest, but an attempt to run the tests results in: UnboundExecutionError: Could not locate a bind configured on mapper Mapper|Environments|environments or this Session This does not completely surprise me as my understanding about sessions in SQLAlchemy is still not solid enough to always understand why my current code works (and I have a full application using this without issue at the moment). So I guess the question is: am I missing something really obvious here, or will I need to rethink how I deal with sessions in my library code? Thanks in advance. -- - Ken Lareau -- 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. For more options, visit https://groups.google.com/groups/opt_out.