コード例 #1
0
ファイル: hydra_base_fixtures.py プロジェクト: UMWRG/PywrApp
def db_with_users(db_empty, engine, request):
    db_empty.metadata.bind = engine

    DBSession = sessionmaker(bind=engine, autoflush=False, autocommit=False)
    # A DBSession() instance establishes all conversations with the database
    # and represents a "staging zone" for all the objects loaded into the
    # database session object. Any change made against the objects in the
    # session won't be persisted into the database until you call
    # session.commit(). If you're not happy about the changes, you can
    # revert all of them back to the last commit by calling
    # session.rollback()
    session = DBSession()

    # Patch the global session in hydra_base
    hydra_base.db.DBSession = session
    # Now apply the default users and roles
    create_default_users_and_perms()
    make_root_user()

    # Add some users
    create_user("UserA")
    create_user("UserB")
    create_user("UserC")

    session.commit()
    session.close()
    hydra_base.db.DBSession = None
    return db_empty
コード例 #2
0
def db_with_users(db_empty, engine, request):
    db_empty.metadata.bind = engine

    DBSession = sessionmaker(bind=engine, autoflush=False, autocommit=False)
    # A DBSession() instance establishes all conversations with the database
    # and represents a "staging zone" for all the objects loaded into the
    # database session object. Any change made against the objects in the
    # session won't be persisted into the database until you call
    # session.commit(). If you're not happy about the changes, you can
    # revert all of them back to the last commit by calling
    # session.rollback()
    session = DBSession()

    # Patch the global session in hydra_base
    hydra_base.db.DBSession = session
    # Now apply the default users and roles
    create_default_users_and_perms()
    make_root_user()

    # Add some users
    create_user("UserA")
    create_user("UserB")
    create_user("UserC")

    session.commit()
    session.close()
    hydra_base.db.DBSession = None
    return db_empty
コード例 #3
0
def session(db, engine, request):
    """Creates a new database session for a test."""
    db.metadata.bind = engine

    DBSession = sessionmaker(bind=engine, autoflush=False, autocommit=False)
    # A DBSession() instance establishes all conversations with the database
    # and represents a "staging zone" for all the objects loaded into the
    # database session object. Any change made against the objects in the
    # session won't be persisted into the database until you call
    # session.commit(). If you're not happy about the changes, you can
    # revert all of them back to the last commit by calling
    # session.rollback()
    session = DBSession()

    # Patch the global session in hydra_base
    hydra_base.db.DBSession = session

    # Now apply the default users and roles
    create_default_users_and_perms()
    make_root_user()

    # Add some users
    create_user("UserA")
    create_user("UserB")
    create_user("UserC")

    yield session

    # Tear down the session

    # First make sure everything can be and is committed.
    session.commit()
    # Finally drop all the tables.
    hydra_base.db.DeclarativeBase.metadata.drop_all()
コード例 #4
0
    def setUp(self):
        util.connect()
        create_default_users_and_perms()
        self.create_user("UserA")
        self.create_user("UserB")
        self.create_user("UserC")
        self.project_id = self.create_project().id

        self.fmt = config.get('DEFAULT', 'datetime_format',
                              "%Y-%m-%dT%H:%M:%S.%f000Z")

        yield  # perform the test
        # now tear down
        self.tearDown()
コード例 #5
0
def session(client, db, engine, request):
    """Creates a new database session for a test."""

    db.metadata.bind = engine

    DBSession = sessionmaker(bind=engine, autoflush=False, autocommit=False)
    # A DBSession() instance establishes all conversations with the database
    # and represents a "staging zone" for all the objects loaded into the
    # database session object. Any change made against the objects in the
    # session won't be persisted into the database until you call
    # session.commit(). If you're not happy about the changes, you can
    # revert all of them back to the last commit by calling
    session = DBSession()

    # Patch the global session in hydra_base
    hydra_base.db.DBSession = session

    if six.PY2 and isinstance(session.connection().connection.connection,
                              sqlite3.Connection):
        session.connection(
        ).connection.connection.text_factory = lambda x: unicode(
            x, 'utf-8', 'ignore')

    # Now apply the default users and roles
    #hydra_base.db.DBSession.begin_nested()
    create_default_users_and_perms()

    root_user_id = make_root_user()

    create_default_units_and_dimensions()

    pytest.root_user_id = root_user_id
    client.testutils = testing.TestUtil(client)
    pytest.user_a = client.testutils.create_user("UserA")
    pytest.user_b = client.testutils.create_user("UserB")
    pytest.user_c = client.testutils.create_user("UserC", role='developer')

    # Tear down the session
    #???
    hydra_base.db.close_session()
    # First make sure everything can be and is committed.
    try:
        session.commit()
        # Finally drop all the tables.
        hydra_base.db.DeclarativeBase.metadata.drop_all()
    except:
        session.rollback()