Skip to content

Commit

Permalink
pytest fixture and readme fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
michelp committed Aug 29, 2021
1 parent 003ed11 commit 6b53982
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
1 change: 0 additions & 1 deletion .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:
- name: Build wheels
run: python -m cibuildwheel --output-dir wheelhouse
env:
CIBW_ENVIRONMENT:
CIBW_BEFORE_ALL_LINUX: ${{ format('sh tools/install_pg.sh {0}', github.ref) }}
- uses: actions/upload-artifact@v2
with:
Expand Down
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ $ pip install postgresql-wheel
```

Postgres binaries in the package can be found in the directory pointed
to by the `postgresql.pg_bin` global variable. `initdb` and `pg_ctl`
to by the `postgresql.pg_bin` global variable. Function wrappers
around all of the postgres binary programs, like `initdb` and `pg_ctl`
functions are provided for convenience:

```py3
Expand All @@ -40,6 +41,26 @@ functions are provided for convenience:
>>> print(q.fetchall())
...
[('PostgreSQL 13.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3), 64-bit',)]
>>> pg_ctl('-D testdatabase stop)

```

For the purposes of testing, convenience functions are provided for
setting up and tearing down databases:

```py3
>>> pgdata, con_str = postgresql.setup()
>>> postgresql.psql(f'-h {con_str} -c "select version()"')
>>> postgresql.teardown(pgdata)
```

There is also a pytest fixture called `tmp_postgres` that returns a
new connection string to a temporary databse local domain socket, and
can be used in a pytest:

```py3
>>> from postgresql import tmp_postgres
>>> def test_foo(tmp_postgres):
... postgresql.psql(f'-h {tmp_postgres} -c "select version()")
```
35 changes: 35 additions & 0 deletions postgresql/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import sys
import os
import shutil
from subprocess import check_output
from pathlib import Path
from tempfile import TemporaryDirectory
from time import sleep
import pytest

pg_bin = Path(__file__).parent / "bin"

Expand All @@ -27,6 +32,36 @@ def f(cmdline):
setattr(this, p, prog(p))


def setup(pgdata=None, log="db_test.log", user="postgres"):
if pgdata is None:
pgdata = TemporaryDirectory().name

log = Path(log)
try:
log.unlink()
except FileNotFoundError:
pass

initdb(f"-D {pgdata} --auth-local=trust --no-sync -U postgres")
pg_ctl(f'-D {pgdata} -o "-k {pgdata} -h \\"\\"" -l {log} start')
sleep(3)
con_str = f"host={pgdata} user={user}"
return pgdata, con_str


def teardown(pgdata):
msg = pg_ctl(f"-D {pgdata} stop")
shutil.rmtree(pgdata)
return msg


@pytest.fixture
def tmp_postgres():
pgdata, con_str = setup()
yield con_str
teardown(pgdata)


from . import _version

__version__ = _version.get_versions()["version"]
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def package_files(directory):
packages=["postgresql"],
package_data={"postgresql": package_files("postgresql")},
setup_requires=["cffi"],
install_requires=["pytest"],
cffi_modules=["postgresql/build.py:ffibuilder"],
python_requires=">=3.7,<3.10",
)
Empty file added tests/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions tests/test_postgresql.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
import postgresql


def test_setup_teardown():
pgdata, conn = postgresql.setup()
postgresql.teardown(pgdata)

0 comments on commit 6b53982

Please sign in to comment.