Exemplo n.º 1
0
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
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
Exemplo n.º 3
0
    def setUp(self):
        self.session = DBSession()

        if not self.session.bind:
            self.session.bind = self.engine

        transaction.begin()
Exemplo n.º 4
0
def _get_db_session():
    'we can call this from scripts to access valid DBSession'
    # not sure we want to do it this way - but let's use for now
    session = DBSession()

    try:
        session.get_bind()
    except UnboundExecutionError:
        session.bind = create_engine('sqlite:///' + _db_file)

    return session
Exemplo n.º 5
0
def _get_db_session():
    'we can call this from scripts to access valid DBSession'
    # not sure we want to do it this way - but let's use for now
    session = DBSession()

    try:
        session.get_bind()
    except UnboundExecutionError:
        session.bind = create_engine('sqlite:///' + _db_file)

    return session
    def setUp(self):
        self.session = DBSession()

        if not self.session.bind:
            self.session.bind = self.engine

        transaction.begin()
Exemplo n.º 7
0
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)