Example #1
0
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()
Example #2
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()
Example #3
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)
Example #4
0
def dropdb():
    janitor = DatabaseJanitor('postgres', 'localhost', 5432, 'test', '11.4')
    janitor.drop()