示例#1
0
def _db(app, request):
    """Session-wide test database."""
    if os.path.exists(TESTDB_PATH):
        os.unlink(TESTDB_PATH)

    if not request.config.getoption('--postgresql'):
        psef.models.db.create_all()
    else:
        db_name, generated = get_database_name(request)
        if generated:
            psef.models.db.create_all()

    manage.app = app
    manage.seed_force(psef.models.db)
    manage.test_data(psef.models.db)

    assert psef.models.Permission.query.all()
    psef.permissions.database_permissions_sanity_check(app)

    try:
        yield psef.models.db
    finally:
        if request.config.getoption('--postgresql'):
            db_name, generated = get_database_name(request)
            if generated:
                orm.session.close_all_sessions()
                psef.models.db.engine.dispose()
                drop_database(db_name)
        else:
            os.unlink(TESTDB_PATH)
示例#2
0
def db(app, request):
    """Session-wide test database."""
    if os.path.exists(TESTDB_PATH):
        os.unlink(TESTDB_PATH)

    if not request.config.getoption('--postgresql'):
        psef.models.db.create_all()
    else:
        db_name, generated = get_database_name(request)
        if generated:
            try:
                subprocess.check_output(
                    'psql -c "create database {}"'.format(
                        db_name.replace('postgresql:///', '')
                    ),
                    shell=True
                )
                subprocess.check_output(
                    'psql {} -c \'create extension "uuid-ossp"\''.format(
                        db_name.replace('postgresql:///', '')
                    ),
                    shell=True
                )
                subprocess.check_output(
                    'psql {} -c \'create extension "citext"\''.format(
                        db_name.replace('postgresql:///', '')
                    ),
                    shell=True
                )
            except subprocess.CalledProcessError as e:
                print(e.stdout, file=sys.stderr)
                print(e.stderr, file=sys.stderr)
            psef.models.db.create_all()

    manage.app = app
    manage.seed_force(psef.models.db)
    manage.test_data(psef.models.db)

    assert psef.models.Permission.query.all()
    psef.permissions.database_permissions_sanity_check(app)

    try:
        yield psef.models.db
    finally:
        if request.config.getoption('--postgresql'):
            db_name, generated = get_database_name(request)
            if generated:
                orm.session.close_all_sessions()
                psef.models.db.engine.dispose()
                subprocess.check_call(
                    'dropdb "{}"'.format(
                        db_name.replace('postgresql:///', '')
                    ),
                    shell=True,
                    stdout=sys.stdout,
                    stderr=sys.stderr
                )
        else:
            os.unlink(TESTDB_PATH)
示例#3
0
def db(app, request):
    """Session-wide test database."""
    if os.path.exists(TESTDB_PATH):
        os.unlink(TESTDB_PATH)

    if not request.config.getoption('--postgresql'):
        psef.models.db.create_all()

    manage.app = app
    manage.seed_force(psef.models.db)
    manage.test_data(psef.models.db)

    yield psef.models.db

    if request.config.getoption('--postgresql'):
        psef.models.db.drop_all()
        psef.models.db.create_all()
    else:
        os.unlink(TESTDB_PATH)
示例#4
0
def session(app, db, use_transaction):
    """Creates a new database session for a test."""
    connection = db.engine.connect()
    if use_transaction:
        transaction = connection.begin()

    options = dict(bind=connection, binds={}, autoflush=False)
    session = db.create_scoped_session(options=options)

    old_ses = psef.models.db.session
    psef.models.db.session = session

    try:
        yield session
    finally:
        psef.models.db.session = old_ses
        print(old_ses)

        if use_transaction:
            transaction.rollback()

        try:
            session.remove()
            connection.close()
        except:
            import traceback
            traceback.print_exc(file=sys.stderr)
            raise

        if not use_transaction:
            db.drop_all()
            sys.setrecursionlimit(5000)
            psef.models.db.create_all()
            manage.app = app
            manage.seed_force(psef.models.db)
            assert manage.test_data(psef.models.db) != 1
            psef.permissions.database_permissions_sanity_check(app)
示例#5
0
def session(app, db, fresh_db, monkeypatch):
    """Creates a new database session for a test."""
    connection = db.engine.connect()
    if not fresh_db:
        transaction = connection.begin()

    options = dict(bind=connection, binds={}, autoflush=False)
    session = db.create_scoped_session(options=options)

    old_uri = app.config['SQLALCHEMY_DATABASE_URI']

    if not fresh_db:
        session.begin_nested()

        # then each time that SAVEPOINT ends, reopen it
        @sqlalchemy.event.listens_for(session, 'after_transaction_end')
        def restart_savepoint(session, transaction):
            if transaction.nested and not transaction._parent.nested:
                # ensure that state is expired the way
                # session.commit() at the top level normally does
                # (optional step)
                session.expire_all()

                session.begin_nested()

    if fresh_db:
        with app.app_context():
            from flask_migrate import Migrate
            from flask_migrate import upgrade as db_upgrade
            logging.disable(logging.ERROR)
            Migrate(app, db)
            db_upgrade()
            logging.disable(logging.NOTSET)
        manage.seed_force(psef.models.db)
        manage.test_data(psef.models.db)

    try:
        psef.models.validator._update_session(session)
        with monkeypatch.context() as context:
            context.setattr(psef.models.db, 'session', session)
            if not fresh_db:
                context.setattr(session, 'remove', lambda: None)
            yield session
    finally:
        if not fresh_db:
            sqlalchemy.event.remove(
                session, 'after_transaction_end', restart_savepoint
            )
            transaction.rollback()

        try:
            session.remove()
            connection.close()
        except:
            import traceback
            traceback.print_exc(file=sys.stderr)
            raise

        if fresh_db:
            db.engine.dispose()
            db.drop_all()
            drop_database(old_uri)