pytest_pg 0.0.28a1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytest_pg-0.0.28a1/.github/CODEOWNERS +1 -0
- pytest_pg-0.0.28a1/.github/dependabot.yml +8 -0
- pytest_pg-0.0.28a1/.github/workflows/ci.yml +51 -0
- pytest_pg-0.0.28a1/.gitignore +128 -0
- pytest_pg-0.0.28a1/.python-version +1 -0
- pytest_pg-0.0.28a1/CHANGELOG.md +103 -0
- pytest_pg-0.0.28a1/LICENSE +21 -0
- pytest_pg-0.0.28a1/Makefile +28 -0
- pytest_pg-0.0.28a1/PKG-INFO +116 -0
- pytest_pg-0.0.28a1/README.md +87 -0
- pytest_pg-0.0.28a1/pyproject.toml +80 -0
- pytest_pg-0.0.28a1/pytest_pg/__init__.py +19 -0
- pytest_pg-0.0.28a1/pytest_pg/fixtures.py +139 -0
- pytest_pg-0.0.28a1/pytest_pg/py.typed +1 -0
- pytest_pg-0.0.28a1/pytest_pg/utils.py +118 -0
- pytest_pg-0.0.28a1/tests/__init__.py +0 -0
- pytest_pg-0.0.28a1/tests/conftest.py +1 -0
- pytest_pg-0.0.28a1/tests/test_pg.py +37 -0
- pytest_pg-0.0.28a1/tests/test_utils.py +60 -0
- pytest_pg-0.0.28a1/uv.lock +397 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* @anna-money/backend
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- master
|
|
7
|
+
tags: ['v*']
|
|
8
|
+
pull_request:
|
|
9
|
+
branches:
|
|
10
|
+
- master
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
jobs:
|
|
14
|
+
test:
|
|
15
|
+
name: Test
|
|
16
|
+
runs-on: ubuntu-latest
|
|
17
|
+
timeout-minutes: 15
|
|
18
|
+
|
|
19
|
+
strategy:
|
|
20
|
+
fail-fast: false
|
|
21
|
+
matrix:
|
|
22
|
+
python: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
|
23
|
+
pytest: ["pytest>=8.0,<8.1", "pytest>=8.1,<8.2", "pytest>=8.2,<8.3", "pytest>=8.3,<8.4", "pytest>=9.0,<9.1"]
|
|
24
|
+
|
|
25
|
+
env:
|
|
26
|
+
UV_EXTRA_ARGS: --with "${{ matrix.pytest }}"
|
|
27
|
+
|
|
28
|
+
steps:
|
|
29
|
+
- uses: actions/checkout@v6
|
|
30
|
+
with:
|
|
31
|
+
fetch-depth: 0
|
|
32
|
+
- uses: astral-sh/setup-uv@v7
|
|
33
|
+
- run: echo ${{ matrix.python }} > .python-version
|
|
34
|
+
- run: make deps
|
|
35
|
+
- run: make lint MODE=ci
|
|
36
|
+
- run: make test
|
|
37
|
+
|
|
38
|
+
deploy:
|
|
39
|
+
name: Deploy
|
|
40
|
+
runs-on: ubuntu-latest
|
|
41
|
+
needs: test
|
|
42
|
+
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
|
|
43
|
+
steps:
|
|
44
|
+
- uses: actions/checkout@v6
|
|
45
|
+
with:
|
|
46
|
+
fetch-depth: 0
|
|
47
|
+
- uses: astral-sh/setup-uv@v7
|
|
48
|
+
- run: uv build
|
|
49
|
+
- env:
|
|
50
|
+
UV_PUBLISH_TOKEN: ${{ secrets.PYPI_TOKEN }}
|
|
51
|
+
run: uv publish
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
pip-wheel-metadata/
|
|
24
|
+
share/python-wheels/
|
|
25
|
+
*.egg-info/
|
|
26
|
+
.installed.cfg
|
|
27
|
+
*.egg
|
|
28
|
+
MANIFEST
|
|
29
|
+
|
|
30
|
+
# PyInstaller
|
|
31
|
+
# Usually these files are written by a python script from a template
|
|
32
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
33
|
+
*.manifest
|
|
34
|
+
*.spec
|
|
35
|
+
|
|
36
|
+
# Installer logs
|
|
37
|
+
pip-log.txt
|
|
38
|
+
pip-delete-this-directory.txt
|
|
39
|
+
|
|
40
|
+
# Unit test / coverage reports
|
|
41
|
+
htmlcov/
|
|
42
|
+
.tox/
|
|
43
|
+
.nox/
|
|
44
|
+
.coverage
|
|
45
|
+
.coverage.*
|
|
46
|
+
.cache
|
|
47
|
+
nosetests.xml
|
|
48
|
+
coverage.xml
|
|
49
|
+
*.cover
|
|
50
|
+
*.py,cover
|
|
51
|
+
.hypothesis/
|
|
52
|
+
.pytest_cache/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
target/
|
|
76
|
+
|
|
77
|
+
# Jupyter Notebook
|
|
78
|
+
.ipynb_checkpoints
|
|
79
|
+
|
|
80
|
+
# IPython
|
|
81
|
+
profile_default/
|
|
82
|
+
ipython_config.py
|
|
83
|
+
|
|
84
|
+
# pipenv
|
|
85
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
86
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
87
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
88
|
+
# install all needed dependencies.
|
|
89
|
+
#Pipfile.lock
|
|
90
|
+
|
|
91
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
|
92
|
+
__pypackages__/
|
|
93
|
+
|
|
94
|
+
# Celery stuff
|
|
95
|
+
celerybeat-schedule
|
|
96
|
+
celerybeat.pid
|
|
97
|
+
|
|
98
|
+
# SageMath parsed files
|
|
99
|
+
*.sage.py
|
|
100
|
+
|
|
101
|
+
# Environments
|
|
102
|
+
.env
|
|
103
|
+
.venv
|
|
104
|
+
env/
|
|
105
|
+
venv/
|
|
106
|
+
ENV/
|
|
107
|
+
env.bak/
|
|
108
|
+
venv.bak/
|
|
109
|
+
|
|
110
|
+
# Spyder project settings
|
|
111
|
+
.spyderproject
|
|
112
|
+
.spyproject
|
|
113
|
+
|
|
114
|
+
# Rope project settings
|
|
115
|
+
.ropeproject
|
|
116
|
+
|
|
117
|
+
# mkdocs documentation
|
|
118
|
+
/site
|
|
119
|
+
|
|
120
|
+
# mypy
|
|
121
|
+
.mypy_cache/
|
|
122
|
+
.dmypy.json
|
|
123
|
+
dmypy.json
|
|
124
|
+
|
|
125
|
+
# Pyre type checker
|
|
126
|
+
.pyre/
|
|
127
|
+
|
|
128
|
+
.idea/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.10
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
## v0.0.28 (2026-05-06)
|
|
2
|
+
|
|
3
|
+
* [Reduce PostgreSQL container startup time](https://github.com/anna-money/pytest-pg/pull/230)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## v0.0.27 (2026-05-01)
|
|
7
|
+
|
|
8
|
+
* [Resolve DOCKER_HOST from docker context](https://github.com/anna-money/pytest-pg/pull/228)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
## v0.0.26 (2026-02-23)
|
|
12
|
+
|
|
13
|
+
* [Time calculation fix](https://github.com/anna-money/pytest-pg/pull/225)
|
|
14
|
+
* [Add `pg_18` fixture](https://github.com/anna-money/pytest-pg/pull/226)
|
|
15
|
+
* [Updated README, dropped python 3.9 support, added python 3.14 support](https://github.com/anna-money/pytest-pg/pull/227)
|
|
16
|
+
|
|
17
|
+
## v0.0.25 (2025-05-18)
|
|
18
|
+
|
|
19
|
+
* Add `pg_17` fixture
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## v0.0.24 (2025-05-11)
|
|
23
|
+
|
|
24
|
+
* [Downgrade pytest](https://github.com/anna-money/pytest-pg/pull/224)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
## v0.0.23 (2025-04-11)
|
|
28
|
+
|
|
29
|
+
* [Do not pull if we have an image locally](https://github.com/anna-money/pytest-pg/pull/223)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
## v0.0.22 (2025-04-03)
|
|
33
|
+
|
|
34
|
+
* Override docker host with env variable
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
## v0.0.17 (2024-04-03)
|
|
38
|
+
|
|
39
|
+
* Override docker host with env variable
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
## v0.0.15 (2023-05-04)
|
|
43
|
+
|
|
44
|
+
* Pin version of `urllib3` ([see for more details](https://github.com/docker/docker-py/issues/3113))
|
|
45
|
+
* Add `pg_15` fixture
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
## v0.0.14 (2022-09-19)
|
|
49
|
+
|
|
50
|
+
* Disable jit by default
|
|
51
|
+
* Set bgwriter_lru_maxpages
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
## v0.0.12 (2022-01-18)
|
|
55
|
+
|
|
56
|
+
* Ditch logging because it seems to be useless
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
## v0.0.11 (2022-01-09)
|
|
60
|
+
|
|
61
|
+
* Move volume to tmpfs
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
## v0.0.10 (2022-01-08)
|
|
65
|
+
|
|
66
|
+
* Volumes used to remain after running test consuming disk space
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
## v0.0.9 (2021-12-28)
|
|
70
|
+
|
|
71
|
+
* Disable fsync, full_page_writes, synchronous_commit to speedup the tests
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
## v0.0.8 (2021-12-26)
|
|
75
|
+
|
|
76
|
+
* Add typing marker, mypy. Run tests in CI
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
## v0.0.7 (2021-12-25)
|
|
80
|
+
|
|
81
|
+
* Fix is_ready based on psycopg2
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
## v0.0.6 (2021-12-25)
|
|
85
|
+
|
|
86
|
+
* Add all recent major versions of PG
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
## v0.0.4 (2021-12-10)
|
|
90
|
+
|
|
91
|
+
* Fix is_postgres_ready usage
|
|
92
|
+
|
|
93
|
+
## v0.0.3 (2021-12-10)
|
|
94
|
+
|
|
95
|
+
* Fix uuid generation
|
|
96
|
+
|
|
97
|
+
## v0.0.2 (2021-12-10)
|
|
98
|
+
|
|
99
|
+
* Follow pytest guide for plugins
|
|
100
|
+
|
|
101
|
+
## v0.0.1 (2021-12-10)
|
|
102
|
+
|
|
103
|
+
* A first version
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Yury Pliner
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
.PHONY: all uv deps lint test
|
|
2
|
+
|
|
3
|
+
all: deps lint test
|
|
4
|
+
|
|
5
|
+
UV_EXTRA_ARGS ?=
|
|
6
|
+
|
|
7
|
+
uv:
|
|
8
|
+
@which uv >/dev/null 2>&1 || { \
|
|
9
|
+
echo "uv is not installed"; \
|
|
10
|
+
exit 1;\
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
deps: uv
|
|
14
|
+
@uv sync --all-extras
|
|
15
|
+
|
|
16
|
+
lint: deps
|
|
17
|
+
ifeq ($(MODE), ci)
|
|
18
|
+
@uv run $(UV_EXTRA_ARGS) ruff format pytest_pg tests --check
|
|
19
|
+
@uv run $(UV_EXTRA_ARGS) ruff check pytest_pg tests
|
|
20
|
+
@uv run $(UV_EXTRA_ARGS) pyright
|
|
21
|
+
else
|
|
22
|
+
@uv run $(UV_EXTRA_ARGS) ruff format pytest_pg tests
|
|
23
|
+
@uv run $(UV_EXTRA_ARGS) ruff check pytest_pg tests --fix
|
|
24
|
+
@uv run $(UV_EXTRA_ARGS) pyright
|
|
25
|
+
endif
|
|
26
|
+
|
|
27
|
+
test: deps
|
|
28
|
+
@uv run $(UV_EXTRA_ARGS) pytest -vv --rootdir tests .
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pytest_pg
|
|
3
|
+
Version: 0.0.28a1
|
|
4
|
+
Summary: A tiny plugin for pytest which runs PostgreSQL in Docker
|
|
5
|
+
Project-URL: Homepage, https://github.com/anna-money/pytest-pg
|
|
6
|
+
Author-email: Yury Pliner <yury.pliner@gmail.com>
|
|
7
|
+
License: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
10
|
+
Classifier: Framework :: Pytest
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Requires-Dist: docker>=7.0.0
|
|
23
|
+
Requires-Dist: pytest>=8.0
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: pyright==1.1.407; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest==9.0.2; extra == 'dev'
|
|
27
|
+
Requires-Dist: ruff==0.14.10; extra == 'dev'
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# pytest-pg
|
|
31
|
+
|
|
32
|
+
A pytest plugin that provides session-scoped fixtures for running PostgreSQL inside Docker containers.
|
|
33
|
+
It automatically spins up a container, waits for PostgreSQL to become ready, exposes connection details
|
|
34
|
+
(`host`, `port`, `user`, `password`, `database`) via a `PG` dataclass, and tears the container down after
|
|
35
|
+
the test session. Pre-built fixtures are available for PostgreSQL versions 11 through 18 as well as `latest`.
|
|
36
|
+
|
|
37
|
+
Readiness checks work with any of the common drivers — asyncpg, psycopg2, or psycopg3.
|
|
38
|
+
|
|
39
|
+
To speed up tests, pytest-pg does the following tweaks:
|
|
40
|
+
|
|
41
|
+
1. fsync=off
|
|
42
|
+
2. full_page_writes=off
|
|
43
|
+
3. synchronous_commit=off
|
|
44
|
+
4. jit=off
|
|
45
|
+
5. bgwriter_lru_maxpages=0
|
|
46
|
+
6. data directory is mounted to a tmpfs
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# How to use?
|
|
50
|
+
|
|
51
|
+
You can use the following fixtures:
|
|
52
|
+
|
|
53
|
+
* `pg` – the latest PostgreSQL image available
|
|
54
|
+
* `pg_11` – PostgreSQL 11
|
|
55
|
+
* `pg_12` – PostgreSQL 12
|
|
56
|
+
* `pg_13` – PostgreSQL 13
|
|
57
|
+
* `pg_14` – PostgreSQL 14
|
|
58
|
+
* `pg_15` – PostgreSQL 15
|
|
59
|
+
* `pg_16` – PostgreSQL 16
|
|
60
|
+
* `pg_17` – PostgreSQL 17
|
|
61
|
+
* `pg_18` – PostgreSQL 18
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
import asyncpg
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
async def test_asyncpg_query(pg):
|
|
68
|
+
conn = await asyncpg.connect(
|
|
69
|
+
user=pg.user,
|
|
70
|
+
password=pg.password,
|
|
71
|
+
database=pg.database,
|
|
72
|
+
host=pg.host,
|
|
73
|
+
port=pg.port,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
await conn.execute("CREATE TABLE test_table (id serial PRIMARY KEY, value text);")
|
|
77
|
+
await conn.execute("INSERT INTO test_table (value) VALUES ($1)", "hello")
|
|
78
|
+
row = await conn.fetchrow("SELECT value FROM test_table WHERE id = $1", 1)
|
|
79
|
+
|
|
80
|
+
assert row["value"] == "hello"
|
|
81
|
+
|
|
82
|
+
await conn.close()
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
Also `run_pg` context manager is available, you can use it to create your own fixture, using docker image you need:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
import os
|
|
90
|
+
|
|
91
|
+
import pytest
|
|
92
|
+
import pytest_pg
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@pytest.fixture(scope='session', autouse=True)
|
|
96
|
+
def postgres_env_vars() -> Generator[None]:
|
|
97
|
+
docker_image = 'postgres:18'
|
|
98
|
+
with pytest_pg.run_pg(docker_image) as pg:
|
|
99
|
+
os.environ['POSTGRES_USER'] = pg.user
|
|
100
|
+
os.environ['POSTGRES_PASSWORD'] = pg.password
|
|
101
|
+
os.environ['POSTGRES_HOST'] = pg.host
|
|
102
|
+
os.environ['POSTGRES_PORT'] = str(pg.port)
|
|
103
|
+
os.environ['POSTGRES_DBNAME'] = pg.database
|
|
104
|
+
yield
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
# or like so:
|
|
108
|
+
@pytest.fixture(scope='session', autouse=True)
|
|
109
|
+
def postgres_env_vars(pg_18: pytest_pg.PG) -> Generator[None]:
|
|
110
|
+
os.environ['POSTGRES_USER'] = pg_18.user
|
|
111
|
+
os.environ['POSTGRES_PASSWORD'] = pg_18.password
|
|
112
|
+
os.environ['POSTGRES_HOST'] = pg_18.host
|
|
113
|
+
os.environ['POSTGRES_PORT'] = str(pg_18.port)
|
|
114
|
+
os.environ['POSTGRES_DBNAME'] = pg_18.database
|
|
115
|
+
yield
|
|
116
|
+
```
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# pytest-pg
|
|
2
|
+
|
|
3
|
+
A pytest plugin that provides session-scoped fixtures for running PostgreSQL inside Docker containers.
|
|
4
|
+
It automatically spins up a container, waits for PostgreSQL to become ready, exposes connection details
|
|
5
|
+
(`host`, `port`, `user`, `password`, `database`) via a `PG` dataclass, and tears the container down after
|
|
6
|
+
the test session. Pre-built fixtures are available for PostgreSQL versions 11 through 18 as well as `latest`.
|
|
7
|
+
|
|
8
|
+
Readiness checks work with any of the common drivers — asyncpg, psycopg2, or psycopg3.
|
|
9
|
+
|
|
10
|
+
To speed up tests, pytest-pg does the following tweaks:
|
|
11
|
+
|
|
12
|
+
1. fsync=off
|
|
13
|
+
2. full_page_writes=off
|
|
14
|
+
3. synchronous_commit=off
|
|
15
|
+
4. jit=off
|
|
16
|
+
5. bgwriter_lru_maxpages=0
|
|
17
|
+
6. data directory is mounted to a tmpfs
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# How to use?
|
|
21
|
+
|
|
22
|
+
You can use the following fixtures:
|
|
23
|
+
|
|
24
|
+
* `pg` – the latest PostgreSQL image available
|
|
25
|
+
* `pg_11` – PostgreSQL 11
|
|
26
|
+
* `pg_12` – PostgreSQL 12
|
|
27
|
+
* `pg_13` – PostgreSQL 13
|
|
28
|
+
* `pg_14` – PostgreSQL 14
|
|
29
|
+
* `pg_15` – PostgreSQL 15
|
|
30
|
+
* `pg_16` – PostgreSQL 16
|
|
31
|
+
* `pg_17` – PostgreSQL 17
|
|
32
|
+
* `pg_18` – PostgreSQL 18
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import asyncpg
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
async def test_asyncpg_query(pg):
|
|
39
|
+
conn = await asyncpg.connect(
|
|
40
|
+
user=pg.user,
|
|
41
|
+
password=pg.password,
|
|
42
|
+
database=pg.database,
|
|
43
|
+
host=pg.host,
|
|
44
|
+
port=pg.port,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
await conn.execute("CREATE TABLE test_table (id serial PRIMARY KEY, value text);")
|
|
48
|
+
await conn.execute("INSERT INTO test_table (value) VALUES ($1)", "hello")
|
|
49
|
+
row = await conn.fetchrow("SELECT value FROM test_table WHERE id = $1", 1)
|
|
50
|
+
|
|
51
|
+
assert row["value"] == "hello"
|
|
52
|
+
|
|
53
|
+
await conn.close()
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
Also `run_pg` context manager is available, you can use it to create your own fixture, using docker image you need:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
import os
|
|
61
|
+
|
|
62
|
+
import pytest
|
|
63
|
+
import pytest_pg
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@pytest.fixture(scope='session', autouse=True)
|
|
67
|
+
def postgres_env_vars() -> Generator[None]:
|
|
68
|
+
docker_image = 'postgres:18'
|
|
69
|
+
with pytest_pg.run_pg(docker_image) as pg:
|
|
70
|
+
os.environ['POSTGRES_USER'] = pg.user
|
|
71
|
+
os.environ['POSTGRES_PASSWORD'] = pg.password
|
|
72
|
+
os.environ['POSTGRES_HOST'] = pg.host
|
|
73
|
+
os.environ['POSTGRES_PORT'] = str(pg.port)
|
|
74
|
+
os.environ['POSTGRES_DBNAME'] = pg.database
|
|
75
|
+
yield
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# or like so:
|
|
79
|
+
@pytest.fixture(scope='session', autouse=True)
|
|
80
|
+
def postgres_env_vars(pg_18: pytest_pg.PG) -> Generator[None]:
|
|
81
|
+
os.environ['POSTGRES_USER'] = pg_18.user
|
|
82
|
+
os.environ['POSTGRES_PASSWORD'] = pg_18.password
|
|
83
|
+
os.environ['POSTGRES_HOST'] = pg_18.host
|
|
84
|
+
os.environ['POSTGRES_PORT'] = str(pg_18.port)
|
|
85
|
+
os.environ['POSTGRES_DBNAME'] = pg_18.database
|
|
86
|
+
yield
|
|
87
|
+
```
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pytest_pg"
|
|
3
|
+
dynamic = ["version"]
|
|
4
|
+
description = "A tiny plugin for pytest which runs PostgreSQL in Docker"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = {text = "MIT"}
|
|
7
|
+
authors = [{name = "Yury Pliner", email = "yury.pliner@gmail.com"}]
|
|
8
|
+
requires-python = ">=3.10"
|
|
9
|
+
dependencies = [
|
|
10
|
+
"docker>=7.0.0",
|
|
11
|
+
"pytest>=8.0",
|
|
12
|
+
]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"License :: OSI Approved :: MIT License",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
18
|
+
"Programming Language :: Python :: 3.10",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Programming Language :: Python :: 3.13",
|
|
22
|
+
"Programming Language :: Python :: 3.14",
|
|
23
|
+
"Operating System :: OS Independent",
|
|
24
|
+
"Development Status :: 5 - Production/Stable",
|
|
25
|
+
"Framework :: Pytest",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/anna-money/pytest-pg"
|
|
30
|
+
|
|
31
|
+
[project.entry-points.pytest11]
|
|
32
|
+
pytest_pg = "pytest_pg"
|
|
33
|
+
|
|
34
|
+
[project.optional-dependencies]
|
|
35
|
+
dev = [
|
|
36
|
+
"pytest==9.0.2",
|
|
37
|
+
"ruff==0.14.10",
|
|
38
|
+
"pyright==1.1.407",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[build-system]
|
|
42
|
+
requires = ["hatchling", "hatch-vcs"]
|
|
43
|
+
build-backend = "hatchling.build"
|
|
44
|
+
|
|
45
|
+
[tool.hatch.version]
|
|
46
|
+
source = "vcs"
|
|
47
|
+
|
|
48
|
+
[tool.hatch.build.targets.wheel]
|
|
49
|
+
packages = ["pytest_pg"]
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
norecursedirs = [".git", ".venv"]
|
|
53
|
+
|
|
54
|
+
[tool.ruff]
|
|
55
|
+
line-length = 120
|
|
56
|
+
target-version = "py310"
|
|
57
|
+
|
|
58
|
+
[tool.ruff.lint]
|
|
59
|
+
select = ["SIM", "C4", "ANN", "N", "LOG", "G", "F", "E", "W", "Q", "I", "UP", "T20", "COM", "RSE", "RET", "ISC", "ASYNC", "SLOT", "RUF", "DTZ"]
|
|
60
|
+
ignore = ["COM812", "ISC001", "ASYNC109", "RUF018", "RUF022", "ANN401", "ANN204", "E501"]
|
|
61
|
+
|
|
62
|
+
[tool.pyright]
|
|
63
|
+
include = ["pytest_pg", "tests"]
|
|
64
|
+
pythonVersion = "3.10"
|
|
65
|
+
pythonPlatform = "All"
|
|
66
|
+
stubPath = ""
|
|
67
|
+
typeCheckingMode = "strict"
|
|
68
|
+
useLibraryCodeForTypes = true
|
|
69
|
+
reportPrivateImportUsage = false
|
|
70
|
+
reportMissingTypeStubs = false
|
|
71
|
+
reportUnknownVariableType = false
|
|
72
|
+
reportUnknownMemberType = false
|
|
73
|
+
reportUnknownLambdaType = false
|
|
74
|
+
reportUnknownArgumentType = false
|
|
75
|
+
reportUnknownParameterType = false
|
|
76
|
+
reportUntypedFunctionDecorator = false
|
|
77
|
+
reportUnusedImport = false
|
|
78
|
+
reportMissingTypeArgument = false
|
|
79
|
+
reportImportCycles = false
|
|
80
|
+
reportDeprecated = false
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from importlib.metadata import version as _get_version
|
|
2
|
+
|
|
3
|
+
from .fixtures import PG, pg, pg_11, pg_12, pg_13, pg_14, pg_15, pg_16, pg_17, pg_18, run_pg
|
|
4
|
+
|
|
5
|
+
__all__: tuple[str, ...] = (
|
|
6
|
+
"PG",
|
|
7
|
+
"run_pg",
|
|
8
|
+
"pg",
|
|
9
|
+
"pg_11",
|
|
10
|
+
"pg_12",
|
|
11
|
+
"pg_13",
|
|
12
|
+
"pg_14",
|
|
13
|
+
"pg_15",
|
|
14
|
+
"pg_16",
|
|
15
|
+
"pg_17",
|
|
16
|
+
"pg_18",
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
__version__ = _get_version("pytest_pg")
|