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 try: janitor.drop() except InvalidCatalogName: # We can safely ignore this exception as that means there was # no leftover database pass # 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)
def test_unsupported_version(request: FixtureRequest) -> None: """Check that the error gets raised on unsupported postgres version.""" config = get_config(request) port = get_port(config["port"]) assert port is not None executor = PatchedPostgreSQLExecutor( executable=config["exec"], host=config["host"], port=port, datadir="/tmp/error", unixsocketdir=config["unixsocketdir"], logfile="/tmp/version.error.log", startparams=config["startparams"], dbname="random_name", ) with pytest.raises(PostgreSQLUnsupported): executor.start()
def test_executor_init_with_password( request: FixtureRequest, monkeypatch: pytest.MonkeyPatch, tmp_path_factory: pytest.TempPathFactory, locale: str, ) -> None: """Test whether the executor initializes properly.""" config = get_config(request) monkeypatch.setenv("LC_ALL", locale) port = get_port(config["port"]) assert port is not None tmpdir = tmp_path_factory.mktemp(f"pytest-postgresql-{request.node.name}") datadir = tmpdir / f"data-{port}" datadir.mkdir() logfile_path = tmpdir / f"postgresql.{port}.log" executor = PostgreSQLExecutor( executable=config["exec"], host=config["host"], port=port, datadir=str(datadir), unixsocketdir=config["unixsocketdir"], logfile=str(logfile_path), startparams=config["startparams"], password="******", dbname="somedatabase", ) with executor: assert executor.running() psycopg.connect( dbname=executor.user, user=executor.user, password=executor.password, host=executor.host, port=executor.port, ) with pytest.raises(psycopg.OperationalError): psycopg.connect( dbname=executor.user, user=executor.user, password="******", host=executor.host, port=executor.port, ) assert not executor.running()
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)
def postgresql_noproc_fixture( request: FixtureRequest) -> Iterator[NoopExecutor]: """ Noop Process fixture for PostgreSQL. :param request: fixture request object :returns: tcp executor-like object """ config = get_config(request) pg_host = host or config["host"] pg_port = port or config["port"] or 5432 pg_user = user or config["user"] pg_password = password or config["password"] pg_dbname = xdistify_dbname(dbname or config["dbname"]) pg_options = options or config["options"] pg_load = load or config["load"] noop_exec = NoopExecutor( host=pg_host, port=pg_port, user=pg_user, password=pg_password, dbname=pg_dbname, options=pg_options, ) template_dbname = f"{noop_exec.dbname}_tmpl" with DatabaseJanitor( user=noop_exec.user, host=noop_exec.host, port=noop_exec.port, dbname=template_dbname, version=noop_exec.version, password=noop_exec.password, ) as janitor: for load_element in pg_load: janitor.load(load_element) yield noop_exec
def postgresql_proc_fixture( request: FixtureRequest, tmp_path_factory: TempPathFactory) -> Iterator[PostgreSQLExecutor]: """ Process fixture for PostgreSQL. :param request: fixture request object :param tmp_path_factory: temporary path object (fixture) :returns: tcp executor """ config = get_config(request) postgresql_ctl = executable or config["exec"] logfile_prefix = logs_prefix or config["logsprefix"] pg_dbname = dbname or config["dbname"] pg_load = load or config["load"] # check if that executable exists, as it's no on system PATH # only replace if executable isn't passed manually if not os.path.exists(postgresql_ctl) and executable is None: pg_bindir = subprocess.check_output( ["pg_config", "--bindir"], universal_newlines=True).strip() postgresql_ctl = os.path.join(pg_bindir, "pg_ctl") tmpdir = tmp_path_factory.mktemp( f"pytest-postgresql-{request.fixturename}") if logfile_prefix: warn( f"logfile_prefix and logsprefix config option is deprecated, " f"and will be dropped in future releases. All fixture related " f"data resides within {tmpdir}", DeprecationWarning, ) pg_port = get_port(port) or get_port(config["port"]) assert pg_port is not None datadir = tmpdir / f"data-{pg_port}" datadir.mkdir() logfile_path = tmpdir / f"{logfile_prefix}postgresql.{pg_port}.log" if platform.system() == "FreeBSD": with (datadir / "pg_hba.conf").open(mode="a") as conf_file: conf_file.write("host all all 0.0.0.0/0 trust\n") postgresql_executor = PostgreSQLExecutor( executable=postgresql_ctl, host=host or config["host"], port=pg_port, user=user or config["user"], password=password or config["password"], dbname=pg_dbname, options=options or config["options"], datadir=str(datadir), unixsocketdir=unixsocketdir or config["unixsocketdir"], logfile=str(logfile_path), startparams=startparams or config["startparams"], postgres_options=postgres_options or config["postgres_options"], ) # start server with postgresql_executor: postgresql_executor.wait_for_postgres() template_dbname = f"{postgresql_executor.dbname}_tmpl" with DatabaseJanitor( user=postgresql_executor.user, host=postgresql_executor.host, port=postgresql_executor.port, dbname=template_dbname, version=postgresql_executor.version, password=postgresql_executor.password, ) as janitor: for load_element in pg_load: janitor.load(load_element) yield postgresql_executor