Skip to content

Commit 6b53982

Browse files
committed
pytest fixture and readme fixes.
1 parent 003ed11 commit 6b53982

File tree

6 files changed

+63
-2
lines changed

6 files changed

+63
-2
lines changed

.github/workflows/wheels.yml

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ jobs:
2424
- name: Build wheels
2525
run: python -m cibuildwheel --output-dir wheelhouse
2626
env:
27-
CIBW_ENVIRONMENT:
2827
CIBW_BEFORE_ALL_LINUX: ${{ format('sh tools/install_pg.sh {0}', github.ref) }}
2928
- uses: actions/upload-artifact@v2
3029
with:

README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ $ pip install postgresql-wheel
2525
```
2626

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

3132
```py3
@@ -40,6 +41,26 @@ functions are provided for convenience:
4041
>>> print(q.fetchall())
4142
...
4243
[('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',)]
44+
>>> pg_ctl('-D testdatabase stop)
4345

4446
```
4547

48+
For the purposes of testing, convenience functions are provided for
49+
setting up and tearing down databases:
50+
51+
```py3
52+
>>> pgdata, con_str = postgresql.setup()
53+
>>> postgresql.psql(f'-h {con_str} -c "select version()"')
54+
>>> postgresql.teardown(pgdata)
55+
```
56+
57+
There is also a pytest fixture called `tmp_postgres` that returns a
58+
new connection string to a temporary databse local domain socket, and
59+
can be used in a pytest:
60+
61+
```py3
62+
>>> from postgresql import tmp_postgres
63+
>>> def test_foo(tmp_postgres):
64+
... postgresql.psql(f'-h {tmp_postgres} -c "select version()")
65+
```
66+

postgresql/__init__.py

+35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import sys
2+
import os
3+
import shutil
24
from subprocess import check_output
35
from pathlib import Path
6+
from tempfile import TemporaryDirectory
7+
from time import sleep
8+
import pytest
49

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

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

2934

35+
def setup(pgdata=None, log="db_test.log", user="postgres"):
36+
if pgdata is None:
37+
pgdata = TemporaryDirectory().name
38+
39+
log = Path(log)
40+
try:
41+
log.unlink()
42+
except FileNotFoundError:
43+
pass
44+
45+
initdb(f"-D {pgdata} --auth-local=trust --no-sync -U postgres")
46+
pg_ctl(f'-D {pgdata} -o "-k {pgdata} -h \\"\\"" -l {log} start')
47+
sleep(3)
48+
con_str = f"host={pgdata} user={user}"
49+
return pgdata, con_str
50+
51+
52+
def teardown(pgdata):
53+
msg = pg_ctl(f"-D {pgdata} stop")
54+
shutil.rmtree(pgdata)
55+
return msg
56+
57+
58+
@pytest.fixture
59+
def tmp_postgres():
60+
pgdata, con_str = setup()
61+
yield con_str
62+
teardown(pgdata)
63+
64+
3065
from . import _version
3166

3267
__version__ = _version.get_versions()["version"]

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def package_files(directory):
2121
packages=["postgresql"],
2222
package_data={"postgresql": package_files("postgresql")},
2323
setup_requires=["cffi"],
24+
install_requires=["pytest"],
2425
cffi_modules=["postgresql/build.py:ffibuilder"],
2526
python_requires=">=3.7,<3.10",
2627
)

tests/__init__.py

Whitespace-only changes.

tests/test_postgresql.py

+5
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
import postgresql
2+
3+
4+
def test_setup_teardown():
5+
pgdata, conn = postgresql.setup()
6+
postgresql.teardown(pgdata)

0 commit comments

Comments
 (0)