Hello,
I'm having trouble testing my code which uses SQLAlchemy with nose[1].
I'm using SA 0.3.11.
My DB is wrapped in a class called DB. I use this class as follows:
# prepare DB
mydb = DB('sqlite', db='/path/to/sqlite/db')
mydb.createTables()
# perform tests on DB
# tear down DB
mydb.dropTables()
os.remove('/path/to/sqlite/db')
The idea is to have a clean DB for each individual test, so the DB
preparation steps are in my setup() and teardown() methods. The problem
occurs when I have two classes in the same file which have this code;
the second class run by nose gives me a long traceback[2].
What happens in the DB class __init__(), createTables() and dropTables()
methods? (The complete source can be viewed online[3].)
The __init__() method creates a BoundMetaData object for sqlite with
SingletonThreadPool pool class, defines the tables and mappers and
creates a session context.
import sqlalchemy as sa
def __init__(self, db, entity_name=None):
engine_src = 'sqlite:///%s' % db
self.ctx = None
self.metadata = sa.BoundMetaData(engine_src, \
poolclass=sa.pool.SingletonThreadPool)
self.entity_name = entity_name
self._defineTables()
def _defineTables(self):
appglobals = sa.Table('appglobals', self.metadata,
sa.Column('agid', sa.Integer, primary_key=True,
autoincrement=True),
sa.Column('kname', sa.String(20)),
sa.Column('kvalue', sa.String(255)),
sa.Column('ngid', sa.Integer),
mysql_engine='InnoDB')
# etc ...
self._assignMappers()
def _assignMappers(self):
self.ctx = SessionContext(sa.create_session)
# mappers have been created, do nothing
from sqlalchemy.orm.mapper import ClassKey
if sa.orm.mapper_registry.has_key(ClassKey(ReposHaveKits, \
self.entity_name)):
return
repos_have_kits = sa.Table('repos_have_kits', self.metadata,
autoload=True)
assign_mapper(self.ctx, ReposHaveKits, repos_have_kits,
entity_name=self.entity_name)
# etc ...
The createTables() method calls metadata.create_all(). The dropTables()
method calls metadata.drop_all() and deletes the current session
context.
def createTables(self):
self.metadata.create_all()
def dropTables(self):
self.metadata.drop_all()
# need to destroy the current session which may still contain old data
self.ctx.del_current()
How can I realize this concept of a completely new and isolated DB
environment for each single test case that's being run?
The test source is also available[4][5].
Thanks in advance,
Mike
[1] http://www.somethingaboutorange.com/mrl/projects/nose/
[2] http://rafb.net/p/f1GuIK83.html
[3]
http://svn.osgdc.org/browse/kusu/kusu/trunk/src/modules/core/src/lib/database.py?r=3169
[4]
http://svn.osgdc.org/browse/kusu/sandbox/mike/kusu-mmm/src/modules/kits/src/test/kits_test.py?r=3213
[5]
http://svn.osgdc.org/browse/kusu/sandbox/mike/kusu-mmm/src/modules/kits/src/test/tool_test.py?r=3213
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---