Exemple #1
0
def test_proc_with_password(
    postgres_with_password: PostgreSQLExecutor, ) -> None:
    """Check that password option to postgresql_proc factory is honored."""
    assert postgres_with_password.running() is True

    # no assertion necessary here; we just want to make sure it connects with
    # the password
    retry(
        lambda: psycopg.connect(
            dbname=postgres_with_password.user,
            user=postgres_with_password.user,
            password=postgres_with_password.password,
            host=postgres_with_password.host,
            port=postgres_with_password.port,
        ),
        possible_exception=psycopg.OperationalError,
    )

    with pytest.raises(psycopg.OperationalError):
        psycopg.connect(
            dbname=postgres_with_password.user,
            user=postgres_with_password.user,
            password="******",
            host=postgres_with_password.host,
            port=postgres_with_password.port,
        )
Exemple #2
0
    def version(self) -> Any:
        """Get postgresql's version."""
        if not self._version:
            check_for_psycopg()
            # could be called before self.dbname will be created.
            # Use default postgres database
            with psycopg.connect(
                    dbname="postgres",
                    user=self.user,
                    host=self.host,
                    port=self.port,
                    password=self.password,
                    options=self.options,
            ) as connection:
                version = str(connection.info.server_version)
                # Pad the version for releases before 10
                # if not we get 90524 instead of 090524
                if len(version) < 6:
                    version = "0" + version
                version_parts = []

                # extract version parts to construct a two-part version
                for i in range(0, len(version), 2):
                    j = i + 2
                    part = version[i:j]
                    if not int(part):
                        continue
                    version_parts.append(part)
                self._version = parse_version(".".join(version_parts[:2]))
        return self._version
    def postgresql_factory(request: FixtureRequest) -> Iterator[connection]:
        """
        Fixture factory for PostgreSQL.

        :param request: fixture request object
        :returns: postgresql client
        """
        check_for_psycopg()
        proc_fixture: Union[PostgreSQLExecutor,
                            NoopExecutor] = request.getfixturevalue(
                                process_fixture_name)

        pg_host = proc_fixture.host
        pg_port = proc_fixture.port
        pg_user = proc_fixture.user
        pg_password = proc_fixture.password
        pg_options = proc_fixture.options
        pg_db = dbname or proc_fixture.dbname
        pg_load = load or []

        with DatabaseJanitor(pg_user, pg_host, pg_port, pg_db,
                             proc_fixture.version, pg_password,
                             isolation_level) as janitor:
            db_connection: connection = psycopg.connect(
                dbname=pg_db,
                user=pg_user,
                password=pg_password,
                host=pg_host,
                port=pg_port,
                options=pg_options,
            )
            for load_element in pg_load:
                janitor.load(load_element)
            yield db_connection
            db_connection.close()
Exemple #4
0
def loader(sql_filename: str, **kwargs: Any) -> None:
    """Database loader for sql files"""
    db_connection = psycopg.connect(**kwargs)
    with open(sql_filename, "r") as _fd:
        with db_connection.cursor() as cur:
            cur.execute(_fd.read())
    db_connection.commit()
 def connect() -> connection:
     return psycopg.connect(
         dbname="postgres",
         user=self.user,
         password=self.password,
         host=self.host,
         port=self.port,
     )
def load_database(**kwargs: Any) -> None:
    db_connection: connection = psycopg.connect(**kwargs)
    with db_connection.cursor() as cur:
        cur.execute("CREATE TABLE stories (id serial PRIMARY KEY, name varchar);")
        cur.execute(
            "INSERT INTO stories (name) VALUES"
            "('Silmarillion'), ('Star Wars'), ('The Expanse'), ('Battlestar Galactica')"
        )
        db_connection.commit()
Exemple #7
0
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()