示例#1
0
def database(request):
    """Create a Postgres database for the tests, and drop it when the tests are done."""
    host = DB_OPTS.get("host")
    port = DB_OPTS.get("port")
    user = DB_OPTS.get("username")
    db_name = DB_OPTS["database"]

    with DatabaseJanitor(user, host, port, db_name, DB_VERSION):
        yield
示例#2
0
def database(request):
    '''
    Create a Postgres database for the tests, and drop it when the tests are done.
    '''
    pg_host = os.environ.get('TEST_DATABASE_HOST')
    pg_port = os.environ.get('TEST_DATABASE_PORT')
    pg_user = os.environ.get('TEST_DATABASE_USER') or 'postgres'
    pg_pass = os.environ.get('TEST_DATABASE_PASSWORD')
    pg_db = os.environ.get('TEST_DATABASE_NAME')

    janitor = DatabaseJanitor(user=pg_user,
                              port=pg_port,
                              host=pg_host,
                              db_name=pg_db,
                              version=11.7,
                              password=pg_pass)
    janitor.init()

    @request.addfinalizer
    def drop_database():
        janitor.drop()
示例#3
0
文件: conftest.py 项目: sdss/sdssdb
def database(dropdb, request):
    ''' Module fixture to initialize a real database or a test postgresql database '''
    if hasattr(request, 'param'):
        # yield a real database
        yield request.param
    else:
        # check if request is coming from a sqla db or peewee db
        issqla = 'sqladbs' in request.module.__name__ or 'sqlalchemy' in request.module.__name__
        # initialize the test database
        # uses https://github.com/ClearcodeHQ/pytest-postgresql
        janitor = DatabaseJanitor('postgres', 'localhost', 5432, 'test', '11.4')
        janitor.init()
        db = sqla_prepdb() if issqla else pw_prepdb()
        yield db
        db = None
        janitor.drop()
示例#4
0
def database():
    """
    Create a Postgres database for the tests, and drop it when the tests are done.
    """
    parsed_url = urlparse(TEST_DATABASE_URL)

    pg_host = parsed_url.hostname
    pg_port = parsed_url.port
    pg_user = parsed_url.username
    pg_db = parsed_url.path[1:]

    janitor = DatabaseJanitor(pg_user, pg_host, pg_port, pg_db, PG_VERSION)

    try:
        janitor.init()
    except psycopg2.errors.DuplicateDatabase:
        print("`database` fixture: Database already created")

    yield

    janitor.drop()
示例#5
0
def database(request):
    config = get_config(request)
    pg_host = config.get("host")
    pg_port = config.get("port") or os.environ.get("PGPORT", 5432)
    pg_user = config.get("user")
    pg_db = config.get("db", "tests")
    pg_version = config.get("version", 10.1)

    janitor = DatabaseJanitor(pg_user, pg_host, pg_port, pg_db, pg_version)

    # In case the database already exists, possibly due to an aborted test run,
    # attempt to drop it before creating
    janitor.drop()

    # Create our Database.
    janitor.init()

    # Ensure our database gets deleted.
    @request.addfinalizer
    def drop_database():
        janitor.drop()

    return "postgresql://{}@{}:{}/{}".format(pg_user, pg_host, pg_port, pg_db)
示例#6
0
文件: conftest.py 项目: sdss/sdssdb
def dropdb():
    janitor = DatabaseJanitor('postgres', 'localhost', 5432, 'test', '11.4')
    janitor.drop()
示例#7
0
    def de_server_factory(request):
        '''
        actually make the fixture
        '''
        if port:
            host_port = (host, port)
        else:
            host_port = (host, get_random_port())

        db_info = {}

        proc_fixture = request.getfixturevalue(pg_prog_name)
        db_info['host'] = proc_fixture.host
        db_info['port'] = proc_fixture.port
        db_info['user'] = proc_fixture.user
        db_info['password'] = proc_fixture.password

        # used to find the version of postgres
        conn_fixture = request.getfixturevalue(pg_db_conn_name)

        # pseudo random database name for testing
        db_info['database'] = DE_DB_NAME + '_test_' + ''.join(
            random.choices(string.ascii_uppercase + string.digits, k=5))

        # Due to the multi-threaded/connection pooled nature
        # of the DE Server, it is cleaner to build out an
        # unscoped database.  The one created by the `DE_DB`
        # fixture is private to a single socket/connection
        # and cannot be shared cleanly.
        #
        # And even if we could share it, then we wouldn't
        # be testing the production data path or pooling
        # with those tricks

        # DatabaseJanitor will create and drop the tablespace for us
        with DatabaseJanitor(user=db_info['user'],
                             password=db_info['password'],
                             host=db_info['host'],
                             port=db_info['port'],
                             db_name=db_info['database'],
                             version=conn_fixture.server_version):
            # if you swap this for the `DE_DB` fixture, it will
            # block and changes will not be visable to the connection
            # fired up within the DE Server thread.
            with psycopg2.connect(**db_info) as connection:
                for filename in DE_SCHEMA:  # noqa: F405
                    with open(filename, 'r') as _fd, \
                         connection.cursor() as cur:
                        cur.execute(_fd.read())

            server_proc = DETestWorker(conf_path, channel_conf_path, host_port,
                                       db_info, conf_override,
                                       channel_conf_override)
            server_proc.start()
            # The following block only works if there are
            # active workers; if it is called before any workers
            # exist, then it will return and not block as requested.
            # so long as your config contains at least one worker,
            # this will work as you'd expect.
            server_proc.de_server.block_while(State.BOOT)

            if not server_proc.is_alive():
                raise RuntimeError('Could not start PrivateDEServer fixture')

            yield server_proc

            if server_proc.is_alive():
                server_proc.de_server.rpc_stop()

            server_proc.join()