class BaseTestCase(unittest.TestCase): @classmethod def setUpClass(cls): cls.engine = create_engine(sqlalchemy_url) Base.metadata.create_all(cls.engine) def setUp(self): self.session = DBSession() if not self.session.bind: self.session.bind = self.engine transaction.begin() def tearDown(self): # for unit testing, we throw away any modifications we may have made. transaction.abort() self.session.close() def add_objs_and_assert_ids(self, objs): if type(objs) is list: for o in objs: self.session.add(o) self.session.flush() for o in objs: assert o.id is not None else: self.session.add(objs) self.session.flush() assert objs.id is not None
def session(): """ makes a copy of the oil database, and creates a session object to it When done, closes the session and deletes the DB file This is kind kludgy, but hopefully works """ orig = os.path.join(os.path.split(oil_library.__file__)[0], "OilLib.db") # NOTE: this should probably be in a temp dir... db_file = "./OilLibCopy.db" # make a copy: shutil.copy(orig, db_file) settings = {"sqlalchemy.url": 'sqlite:///{0}'.format(db_file)} engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) DBSession.configure() yield DBSession DBSession.close() os.remove(db_file)
def session(): """ makes a copy of the oil database, and creates a session object to it When done, closes the session and deletes the DB file This is kind kludgy, but hopefully works """ orig = os.path.join(os.path.split(oil_library.__file__)[0], "OilLib.db") # NOTE: this should probably be in a temp dir... db_file = "./OilLibCopy.db" # make a copy: shutil.copy(orig, db_file) settings = {"sqlalchemy.url": 'sqlite:///{0}'.format(db_file)} engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) DBSession.configure(extension=ZopeTransactionExtension()) yield DBSession DBSession.close() os.remove(db_file)