pytest-postgresql 8.1.0__tar.gz → 9.0.0__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.
Files changed (53) hide show
  1. {pytest_postgresql-8.1.0 → pytest_postgresql-9.0.0}/CHANGES.rst +89 -0
  2. {pytest_postgresql-8.1.0 → pytest_postgresql-9.0.0}/PKG-INFO +257 -49
  3. pytest_postgresql-8.1.0/pytest_postgresql.egg-info/PKG-INFO → pytest_postgresql-9.0.0/README.rst +240 -71
  4. pytest_postgresql-9.0.0/pyproject.toml +215 -0
  5. pytest_postgresql-9.0.0/pyproject.toml.orig +196 -0
  6. {pytest_postgresql-8.1.0 → pytest_postgresql-9.0.0}/pytest_postgresql/__init__.py +1 -1
  7. pytest_postgresql-9.0.0/pytest_postgresql/_asyncio_compat.py +56 -0
  8. {pytest_postgresql-8.1.0 → pytest_postgresql-9.0.0}/pytest_postgresql/config.py +7 -3
  9. pytest_postgresql-9.0.0/pytest_postgresql/exceptions.py +55 -0
  10. pytest_postgresql-9.0.0/pytest_postgresql/executors/__init__.py +6 -0
  11. pytest_postgresql-8.1.0/pytest_postgresql/executor_noop.py → pytest_postgresql-9.0.0/pytest_postgresql/executors/noop.py +8 -3
  12. pytest_postgresql-8.1.0/pytest_postgresql/executor.py → pytest_postgresql-9.0.0/pytest_postgresql/executors/proc.py +92 -20
  13. {pytest_postgresql-8.1.0 → pytest_postgresql-9.0.0}/pytest_postgresql/factories/__init__.py +2 -2
  14. pytest_postgresql-9.0.0/pytest_postgresql/factories/_pg.py +75 -0
  15. pytest_postgresql-9.0.0/pytest_postgresql/factories/client.py +184 -0
  16. {pytest_postgresql-8.1.0 → pytest_postgresql-9.0.0}/pytest_postgresql/factories/noprocess.py +23 -5
  17. pytest_postgresql-9.0.0/pytest_postgresql/factories/process.py +231 -0
  18. pytest_postgresql-9.0.0/pytest_postgresql/janitor.py +337 -0
  19. pytest_postgresql-9.0.0/pytest_postgresql/loader.py +61 -0
  20. {pytest_postgresql-8.1.0 → pytest_postgresql-9.0.0}/pytest_postgresql/plugin.py +123 -1
  21. pytest_postgresql-9.0.0/pytest_postgresql/retry.py +71 -0
  22. pytest_postgresql-8.1.0/AUTHORS.rst +0 -20
  23. pytest_postgresql-8.1.0/CONTRIBUTING.rst +0 -62
  24. pytest_postgresql-8.1.0/COPYING +0 -674
  25. pytest_postgresql-8.1.0/MANIFEST.in +0 -2
  26. pytest_postgresql-8.1.0/README.rst +0 -405
  27. pytest_postgresql-8.1.0/pyproject.toml +0 -191
  28. pytest_postgresql-8.1.0/pytest_postgresql/exceptions.py +0 -9
  29. pytest_postgresql-8.1.0/pytest_postgresql/factories/client.py +0 -87
  30. pytest_postgresql-8.1.0/pytest_postgresql/factories/process.py +0 -185
  31. pytest_postgresql-8.1.0/pytest_postgresql/janitor.py +0 -166
  32. pytest_postgresql-8.1.0/pytest_postgresql/loader.py +0 -32
  33. pytest_postgresql-8.1.0/pytest_postgresql/retry.py +0 -48
  34. pytest_postgresql-8.1.0/pytest_postgresql.egg-info/SOURCES.txt +0 -40
  35. pytest_postgresql-8.1.0/pytest_postgresql.egg-info/dependency_links.txt +0 -1
  36. pytest_postgresql-8.1.0/pytest_postgresql.egg-info/entry_points.txt +0 -2
  37. pytest_postgresql-8.1.0/pytest_postgresql.egg-info/requires.txt +0 -5
  38. pytest_postgresql-8.1.0/pytest_postgresql.egg-info/top_level.txt +0 -1
  39. pytest_postgresql-8.1.0/pytest_postgresql.egg-info/zip-safe +0 -1
  40. pytest_postgresql-8.1.0/setup.cfg +0 -4
  41. pytest_postgresql-8.1.0/tests/test_chaining.py +0 -101
  42. pytest_postgresql-8.1.0/tests/test_config.py +0 -21
  43. pytest_postgresql-8.1.0/tests/test_executor.py +0 -409
  44. pytest_postgresql-8.1.0/tests/test_janitor.py +0 -65
  45. pytest_postgresql-8.1.0/tests/test_loader.py +0 -20
  46. pytest_postgresql-8.1.0/tests/test_noopexecutor.py +0 -43
  47. pytest_postgresql-8.1.0/tests/test_postgres_options_plugin.py +0 -146
  48. pytest_postgresql-8.1.0/tests/test_postgresql.py +0 -68
  49. pytest_postgresql-8.1.0/tests/test_template_database.py +0 -32
  50. pytest_postgresql-8.1.0/tests/test_version.py +0 -23
  51. pytest_postgresql-8.1.0/tests/test_windows_compatibility.py +0 -898
  52. {pytest_postgresql-8.1.0 → pytest_postgresql-9.0.0}/COPYING.lesser +0 -0
  53. {pytest_postgresql-8.1.0 → pytest_postgresql-9.0.0}/pytest_postgresql/py.typed +0 -0
@@ -3,6 +3,95 @@ CHANGELOG
3
3
 
4
4
  .. towncrier release notes start
5
5
 
6
+ pytest-postgresql 9.0.0 (2026-08-28)
7
+ ====================================
8
+
9
+ Breaking changes
10
+ ----------------
11
+
12
+ - Load functions passed to proc/noproc fixtures have to accept new `autocommit` argument. (`#902 <https://github.com/dbfixtures/pytest-postgresql/issues/902>`__)
13
+ - `postgresql_noproc` has an additional parameter `load_autocommit` which is grouped together with `load` making
14
+ positional arguments filled till `depends_on` incompatible. (`#902 <https://github.com/dbfixtures/pytest-postgresql/issues/902>`__)
15
+ - Arguments after `dbname` in `postgresql_noproc` are now keyword-only. Update callers that pass
16
+ `options`, `load`, `load_autocommit` or `depends_on` positionally. (`#1387 <https://github.com/dbfixtures/pytest-postgresql/issues/1387>`__)
17
+ - Arguments after `dbname` in `postgresql_proc` are now keyword-only. Update callers that pass
18
+ `options`, `startparams`, `unixsocketdir`, `postgres_options`, `load` or `load_autocommit` positionally. (`#1387 <https://github.com/dbfixtures/pytest-postgresql/issues/1387>`__)
19
+ - Arguments after `dbname` in `PostgreSQLExecutor`. Update callers that pass
20
+ +`shell`, `timeout`, `sleep`, `user`, `password`,
21
+ +`options`, or `postgres_options` positionally. (`#1387 <https://github.com/dbfixtures/pytest-postgresql/issues/1387>`__)
22
+
23
+
24
+ Deprecations
25
+ ------------
26
+
27
+ - Deprecated the unused ``version`` argument of ``DatabaseJanitor`` and ``AsyncDatabaseJanitor``. (`#1393 <https://github.com/dbfixtures/pytest-postgresql/issues/1393>`__)
28
+
29
+
30
+ Features
31
+ --------
32
+
33
+ - The database used to create/drop test databases and read the server version is no longer hardcoded to
34
+ ``postgres``. Set it with the ``postgresql_maintenance_dbname`` ini option, the
35
+ ``--postgresql-maintenance-dbname`` command line option, or the ``maintenance_dbname`` argument of
36
+ ``factories.postgresql_noproc`` and ``DatabaseJanitor``/``AsyncDatabaseJanitor``. Defaults to ``postgres``. (`#653 <https://github.com/dbfixtures/pytest-postgresql/issues/653>`__)
37
+ - Allow running the SQL loader connection with autocommit enabled, so ``.sql`` files containing statements that cannot run inside a transaction block (such as ``CREATE DATABASE``) can be loaded. This is configurable without a custom loader in three ways: the ``postgresql_load_autocommit`` ini option, the ``--postgresql-load-autocommit`` command line flag, or the ``load_autocommit`` argument of the ``postgresql_proc`` / ``postgresql_noproc`` factories (which also maps to an ``autocommit`` argument on ``DatabaseJanitor`` / ``AsyncDatabaseJanitor``). (`#902 <https://github.com/dbfixtures/pytest-postgresql/issues/902>`__)
38
+ - Added async PostgreSQL fixture support via ``postgresql_async`` factory and ``AsyncDatabaseJanitor``.
39
+ Added optional ``async`` extra (``pip install pytest-postgresql[async]``) providing ``pytest-asyncio`` (>= 1.4) and ``aiofiles`` dependencies. (`#1235 <https://github.com/dbfixtures/pytest-postgresql/issues/1235>`__)
40
+
41
+
42
+ Bugfixes
43
+ --------
44
+
45
+ - ``pg_ctl`` discovery no longer hands back a path that does not exist. A client-only PostgreSQL
46
+ install (Debian/Ubuntu's ``libpq-dev`` without the matching server package) makes
47
+ ``pg_config --bindir`` point at a directory holding no ``pg_ctl``; that path used to be returned
48
+ unchecked, and the failure only surfaced later as ``Could not found ...`` when the executor read the
49
+ server version. The path is now verified up front, and the ``ExecutableMissingException`` names the
50
+ locations that were checked along with the ways to fix it: install the server package, point at a
51
+ ``pg_ctl`` with ``--postgresql-exec``, or use ``postgresql_noproc`` against a server you run yourself.
52
+
53
+ A broken ``pg_config`` is reported the same way. Only ``FileNotFoundError`` used to be handled, so a
54
+ non-executable or failing ``pg_config`` escaped as a raw ``PermissionError``/``CalledProcessError``. (`#1031 <https://github.com/dbfixtures/pytest-postgresql/issues/1031>`__)
55
+ - Fixed ``DeprecationWarning`` on Python 3.14 from deprecated asyncio event-loop policy usage on Windows; use pytest-asyncio loop factories instead. (`#1295 <https://github.com/dbfixtures/pytest-postgresql/issues/1295>`__)
56
+
57
+
58
+ Documentation
59
+ -------------
60
+
61
+ - Corrected the README's description of how ``pg_ctl`` is located — it listed the discovery steps in
62
+ the wrong order — and spelled out that ``postgresql_proc`` needs a local PostgreSQL server, pointing
63
+ at ``postgresql_noproc`` for dockerised or otherwise externally managed servers. (`#1031 <https://github.com/dbfixtures/pytest-postgresql/issues/1031>`__)
64
+
65
+
66
+ Miscellaneous
67
+ -------------
68
+
69
+ - Pin GitHub Actions to immutable commit SHAs (`#1331 <https://github.com/dbfixtures/pytest-postgresql/issues/1331>`__)
70
+ - Enabled Ruff's `FBT <https://docs.astral.sh/ruff/rules/#flake8-boolean-trap-fbt>`_ rules to detect boolean positional arguments. (`#1387 <https://github.com/dbfixtures/pytest-postgresql/issues/1387>`__)
71
+ - ``postgresql_proc`` now tears down through a fixture ``yield`` instead of ``request.addfinalizer``.
72
+ Consuming it as a pytest fixture is unaffected. (`#1397 <https://github.com/dbfixtures/pytest-postgresql/issues/1397>`__)
73
+ - Enabled Ruff's `PT <https://docs.astral.sh/ruff/rules/#flake8-pytest-style-pt>`_ rules
74
+ to keep pytest usage consistent across the plugin and its test suite. (`#1397 <https://github.com/dbfixtures/pytest-postgresql/issues/1397>`__)
75
+ - Gathered all executors under common executors package. (`#1402 <https://github.com/dbfixtures/pytest-postgresql/issues/1402>`__)
76
+ - Add release-schedule workflow replacing manual release workflow. (`#1410 <https://github.com/dbfixtures/pytest-postgresql/issues/1410>`__)
77
+ - Migrated the Automerge workflow to `fizyk/actions-reuse` version 5.5.0. (`#1411 <https://github.com/dbfixtures/pytest-postgresql/issues/1411>`__)
78
+ - Add actionlint to pre-commit (`#1414 <https://github.com/dbfixtures/pytest-postgresql/issues/1414>`__)
79
+ - Configure Dependabot to update pre-commit dependencies. (`#1415 <https://github.com/dbfixtures/pytest-postgresql/issues/1415>`__)
80
+ - Add Python 3.15 to CI
81
+ - Add pyproject-fmt to pre-commit tools.
82
+ - Add zizmor to pre-commit and harden GitHub Actions workflow permissions.
83
+ - Deduplicate DatabaseJanitor codebase after async introduction.
84
+ - Extended coderabbit configuration
85
+ - Migrate developer environment from pipenv to uv
86
+ - Scope the ``test_postgres_terminate_connection`` (and async) connection check
87
+ to the test's own database via ``datname = current_database()``.
88
+
89
+ So a transient, already-closed janitor connection on the ``postgres`` maintenance
90
+ database no longer causes spurious timeouts on macOS CI.
91
+ - Set CodeRabbit review profile to assertive for stricter pull request reviews.
92
+ - Update automerge to new 5.x version and change app id to client id
93
+
94
+
6
95
  pytest-postgresql 8.1.0 (2026-05-15)
7
96
  ====================================
8
97
 
@@ -1,40 +1,41 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pytest-postgresql
3
- Version: 8.1.0
3
+ Version: 9.0.0
4
4
  Summary: Postgresql fixtures and fixture factories for Pytest.
5
+ Keywords: fixture,postgresql,pytest,tests
6
+ Author: Grzegorz Śliwiński
5
7
  Author-email: Grzegorz Śliwiński <fizyk+pypi@fizyk.dev>
6
- Project-URL: Source, https://github.com/dbfixtures/pytest-postgresql
7
- Project-URL: Bug Tracker, https://github.com/dbfixtures/pytest-postgresql/issues
8
- Project-URL: Changelog, https://github.com/dbfixtures/pytest-postgresql/blob/v8.1.0/CHANGES.rst
9
- Keywords: tests,pytest,fixture,postgresql
8
+ License-Expression: LGPL-3.0-or-later
9
+ License-File: COPYING.lesser
10
10
  Classifier: Development Status :: 5 - Production/Stable
11
11
  Classifier: Environment :: Web Environment
12
+ Classifier: Framework :: Pytest
12
13
  Classifier: Intended Audience :: Developers
13
- Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)
14
14
  Classifier: Natural Language :: English
15
15
  Classifier: Operating System :: OS Independent
16
16
  Classifier: Programming Language :: Python
17
17
  Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3 :: Only
18
19
  Classifier: Programming Language :: Python :: 3.10
19
20
  Classifier: Programming Language :: Python :: 3.11
20
21
  Classifier: Programming Language :: Python :: 3.12
21
22
  Classifier: Programming Language :: Python :: 3.13
22
23
  Classifier: Programming Language :: Python :: 3.14
23
- Classifier: Programming Language :: Python :: 3 :: Only
24
24
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
25
25
  Classifier: Topic :: Software Development :: Testing
26
- Classifier: Framework :: Pytest
27
- Requires-Python: >=3.10
28
- Description-Content-Type: text/x-rst
29
- License-File: COPYING
30
- License-File: COPYING.lesser
31
- License-File: AUTHORS.rst
32
- Requires-Dist: pytest>=8.2
33
- Requires-Dist: port-for>=0.7.3
34
26
  Requires-Dist: mirakuru>=2.6.0
35
27
  Requires-Dist: packaging
28
+ Requires-Dist: port-for>=0.7.3
36
29
  Requires-Dist: psycopg>=3.0.0
37
- Dynamic: license-file
30
+ Requires-Dist: pytest>=8.2
31
+ Requires-Dist: aiofiles>=23.0 ; extra == 'async'
32
+ Requires-Dist: pytest-asyncio>=1.4 ; extra == 'async'
33
+ Requires-Python: >=3.10
34
+ Project-URL: Source, https://github.com/dbfixtures/pytest-postgresql
35
+ Project-URL: Bug Tracker, https://github.com/dbfixtures/pytest-postgresql/issues
36
+ Project-URL: Changelog, https://github.com/dbfixtures/pytest-postgresql/blob/v9.0.0/CHANGES.rst
37
+ Provides-Extra: async
38
+ Description-Content-Type: text/x-rst
38
39
 
39
40
  .. image:: https://raw.githubusercontent.com/dbfixtures/pytest-postgresql/main/logo.png
40
41
  :width: 100px
@@ -76,6 +77,57 @@ Quick Start
76
77
 
77
78
  You will also need to install ``psycopg`` (version 3). See `its installation instructions <https://www.psycopg.org/psycopg3/docs/basic/install.html>`_.
78
79
 
80
+ For async tests with ``psycopg.AsyncConnection``, install the optional async extra:
81
+
82
+ .. code-block:: sh
83
+
84
+ pip install pytest-postgresql[async]
85
+
86
+ This installs:
87
+
88
+ * ``pytest-asyncio`` (>= 1.4) — required for ``@pytest.mark.asyncio`` and
89
+ ``postgresql_async`` fixtures.
90
+ * ``aiofiles`` (>= 23.0) — required only when loading SQL files via the
91
+ async loader (``sql_async``).
92
+
93
+ On Windows, the plugin configures a ``SelectorEventLoop`` automatically for
94
+ asyncio tests when no earlier pytest-asyncio loop factory is registered. This
95
+ is required because ``psycopg`` async is incompatible with the default
96
+ ``ProactorEventLoop`` on Windows (`documented by
97
+ psycopg <https://www.psycopg.org/psycopg3/docs/advanced/async.html>`_). Without
98
+ it, ``postgresql_async`` tests fail with ``Psycopg cannot use the
99
+ 'ProactorEventLoop' to run in async mode``. No extra configuration is needed
100
+ when you install ``pytest-postgresql[async]``.
101
+
102
+ With ``pytest-asyncio`` >= 1.4 on Windows, the plugin registers a selector loop
103
+ factory via pytest-asyncio's loop-factory hook for **all** asyncio tests when
104
+ no prior factory is provided. On Python 3.14+, the legacy ``asyncio`` policy
105
+ fallback is not used because that API is deprecated.
106
+
107
+ When an earlier hook implementation already supplies loop factories, those are
108
+ preserved unchanged. Tests that use a prior factory may show different loop
109
+ names in pytest IDs (for example ``test_example[custom]`` instead of
110
+ ``test_example[selector]``).
111
+
112
+ If you use an older ``pytest-asyncio`` (< 1.4) on Windows with Python < 3.14,
113
+ the plugin falls back to setting a global ``WindowsSelectorEventLoopPolicy`` for
114
+ the **entire test session** — not only for postgresql async tests. That can change
115
+ event-loop behaviour for unrelated asyncio tests in the same run. Install
116
+ ``pytest-postgresql[async]`` (which pulls ``pytest-asyncio`` >= 1.4) to avoid
117
+ that legacy path.
118
+
119
+ **pytest-asyncio configuration**
120
+
121
+ pytest-asyncio 1.x defaults to ``asyncio_mode = strict``, so each async test must
122
+ be marked with ``@pytest.mark.asyncio``. If you set ``asyncio_mode = auto`` in
123
+ ``pytest.ini`` or ``pyproject.toml``, unmarked async test functions are detected
124
+ automatically — ``postgresql_async`` still requires the ``[async]`` extra.
125
+
126
+ .. code-block:: ini
127
+
128
+ [pytest]
129
+ asyncio_mode = strict
130
+
79
131
  .. note::
80
132
 
81
133
  While this plugin requires ``psycopg`` 3 to manage the database, your application code can still use ``psycopg`` 2.
@@ -92,6 +144,21 @@ Quick Start
92
144
  cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
93
145
  postgresql.commit()
94
146
 
147
+ For async code, use ``postgresql_async`` with ``pytest.mark.asyncio``:
148
+
149
+ .. code-block:: python
150
+
151
+ import pytest
152
+
153
+ @pytest.mark.asyncio
154
+ async def test_example_async(postgresql_async):
155
+ """Check main async postgresql fixture."""
156
+ async with postgresql_async.cursor() as cur:
157
+ await cur.execute(
158
+ "CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);"
159
+ )
160
+ await postgresql_async.commit()
161
+
95
162
  How to use
96
163
  ==========
97
164
 
@@ -103,7 +170,11 @@ How does it work
103
170
  ----------------
104
171
 
105
172
  .. image:: https://raw.githubusercontent.com/dbfixtures/pytest-postgresql/main/docs/images/architecture.svg
106
- :alt: Project Architecture Diagram
173
+ :alt: Project Architecture Diagram (sync fixtures)
174
+ :align: center
175
+
176
+ .. image:: https://raw.githubusercontent.com/dbfixtures/pytest-postgresql/main/docs/images/architecture_async.svg
177
+ :alt: Project Architecture Diagram (async fixtures)
107
178
  :align: center
108
179
 
109
180
  The plugin provides two main types of fixtures:
@@ -113,6 +184,42 @@ The plugin provides two main types of fixtures:
113
184
 
114
185
  * **postgresql** - A function-scoped fixture. It returns a connected ``psycopg.Connection``.
115
186
  After each test, it terminates leftover connections and drops the test database to ensure isolation.
187
+ * **postgresql_async** - The async counterpart. It returns a connected ``psycopg.AsyncConnection``.
188
+ Requires ``pytest-postgresql[async]`` (``pytest-asyncio`` >= 1.4), and each test must be
189
+ marked with ``@pytest.mark.asyncio``.
190
+
191
+ **Async fixtures**
192
+ ``postgresql_async`` and custom factories created with ``factories.postgresql_async`` are
193
+ async generator fixtures using ``pytest_asyncio.fixture``.
194
+
195
+ Minimum versions when installing manually instead of via ``[async]``:
196
+
197
+ .. code-block:: text
198
+
199
+ pytest-asyncio >= 1.4
200
+ aiofiles >= 23.0 # only for async SQL file loading
201
+
202
+ If ``pytest-asyncio`` is missing, fixture setup raises ``ImportError``.
203
+
204
+ **Async SQL file loading**
205
+
206
+ Process and noproc fixtures always populate their template database
207
+ synchronously during session setup (via ``DatabaseJanitor.load()``), even when
208
+ you use ``postgresql_async`` as the client fixture. SQL ``Path`` entries in a
209
+ process fixture ``load`` list are executed with the sync ``sql()`` loader.
210
+
211
+ Use ``sql_async`` (requires ``aiofiles`` from the ``[async]`` extra) when you
212
+ call ``AsyncDatabaseJanitor.load()`` directly with a ``Path``. Callable loaders
213
+ passed to ``AsyncDatabaseJanitor.load()`` may be sync or async; return values
214
+ that are awaitable are awaited automatically.
215
+
216
+ .. code-block:: python
217
+
218
+ from pathlib import Path
219
+ from pytest_postgresql import factories
220
+
221
+ postgresql_my_proc = factories.postgresql_proc(load=[Path("schema.sql")])
222
+ postgresql_my_async = factories.postgresql_async("postgresql_my_proc")
116
223
 
117
224
  **2. Process Fixtures**
118
225
  These manage the PostgreSQL server lifecycle.
@@ -136,6 +243,9 @@ You can create additional fixtures using factories:
136
243
  # Create a client fixture that uses the custom process
137
244
  postgresql_my = factories.postgresql('postgresql_my_proc')
138
245
 
246
+ # Async client fixture (requires pytest-postgresql[async], pytest-asyncio >= 1.4)
247
+ postgresql_my_async = factories.postgresql_async('postgresql_my_proc')
248
+
139
249
  .. note::
140
250
 
141
251
  Each process fixture can be configured independently through factory arguments.
@@ -171,19 +281,57 @@ Defining pre-population on the command line:
171
281
 
172
282
  .. code-block:: sh
173
283
 
174
- pytest --postgresql-populate-template=path/to/file.sql --postgresql-populate-template=path.to.function
284
+ pytest --postgresql-load=path/to/file.sql --postgresql-load=path.to.function
285
+
286
+ If a loaded ``.sql`` file contains statements that cannot run inside a transaction
287
+ block (for example ``CREATE DATABASE``), enable autocommit on the loader connection.
288
+ This can be set without writing a custom loader, via the factory argument, the command
289
+ line, or ``pytest.ini``:
290
+
291
+ .. code-block:: python
292
+
293
+ postgresql_my_proc = factories.postgresql_proc(load=[Path("with_create_db.sql")], load_autocommit=True)
294
+
295
+ .. code-block:: sh
296
+
297
+ pytest --postgresql-load-autocommit
175
298
 
176
299
  Connecting to an existing PostgreSQL database
177
300
  ----------------------------------------------
178
301
 
179
302
  To connect to an external server (e.g., running in Docker), use the ``postgresql_noproc`` fixture.
180
303
 
304
+ For async tests against an external server, create a client fixture with
305
+ ``factories.postgresql_async("postgresql_noproc")`` (see
306
+ ``tests/examples/test_drop_test_database_async.py``).
307
+
181
308
  .. code-block:: python
182
309
 
183
310
  postgresql_external = factories.postgresql('postgresql_noproc')
184
311
 
185
312
  By default, it connects to ``127.0.0.1:5432``.
186
313
 
314
+ Using a maintenance database other than ``postgres``
315
+ ----------------------------------------------------
316
+
317
+ Creating and dropping the test databases requires a connection to a database that already
318
+ exists - by default ``postgres``. If the test role merely lacks the privilege,
319
+ ``GRANT CONNECT ON DATABASE postgres TO myuser`` is the simpler fix. When ``postgres`` is
320
+ not reachable at all - behind a connection pooler with a fixed database list, or on a
321
+ managed server that does not expose it - point the noproc fixture at another database:
322
+
323
+ .. code-block:: sh
324
+
325
+ pytest --postgresql-maintenance-dbname=my_existing_db
326
+
327
+ .. code-block:: python
328
+
329
+ postgresql_external = factories.postgresql_noproc(maintenance_dbname="my_existing_db")
330
+
331
+ The database is only connected to, never created, modified or dropped. Avoid ``template1``:
332
+ ``CREATE DATABASE`` clones it when no template is given, and PostgreSQL does not expect the
333
+ source database to be in use while it is being copied.
334
+
187
335
  Chaining fixtures
188
336
  -----------------
189
337
 
@@ -223,94 +371,130 @@ You can define settings via fixture factory arguments, command line options, or
223
371
  .. list-table:: Configuration options
224
372
  :header-rows: 1
225
373
 
226
- * - PostgreSQL option
227
- - Fixture factory argument
374
+ * - Setting
375
+ - ``postgresql_proc`` argument
376
+ - ``postgresql_noproc`` argument
228
377
  - Command line option
229
378
  - pytest.ini option
230
- - Noop process fixture
231
379
  - Default
232
380
  * - Path to executable
233
381
  - executable
382
+ - n/a
234
383
  - --postgresql-exec
235
384
  - postgresql_exec
236
- - -
237
385
  - ``pg_config --bindir`` + ``pg_ctl``
238
386
  * - host
387
+ - host
239
388
  - host
240
389
  - --postgresql-host
241
390
  - postgresql_host
242
- - yes
243
391
  - 127.0.0.1
244
392
  * - port
393
+ - port
245
394
  - port
246
395
  - --postgresql-port
247
396
  - postgresql_port
248
- - yes (5432)
249
- - random
397
+ - random (proc), 5432 (noproc)
250
398
  * - Port search count
251
- -
399
+ - —
400
+ - n/a
252
401
  - --postgresql-port-search-count
253
402
  - postgresql_port_search_count
254
- - -
255
403
  - 5
256
404
  * - postgresql user
405
+ - user
257
406
  - user
258
407
  - --postgresql-user
259
408
  - postgresql_user
260
- - yes
261
409
  - postgres
262
410
  * - password
411
+ - password
263
412
  - password
264
413
  - --postgresql-password
265
414
  - postgresql_password
266
- - yes
267
415
  -
268
416
  * - Starting parameters (extra pg_ctl arguments)
269
417
  - startparams
418
+ - n/a
270
419
  - --postgresql-startparams
271
420
  - postgresql_startparams
272
- - -
273
421
  - -w
274
422
  * - Postgres exe extra arguments (passed via pg_ctl's -o argument)
275
423
  - postgres_options
424
+ - n/a
276
425
  - --postgresql-postgres-options
277
426
  - postgresql_postgres_options
278
- - -
279
427
  -
280
428
  * - Location for unixsockets
281
429
  - unixsocket
430
+ - n/a
282
431
  - --postgresql-unixsocketdir
283
432
  - postgresql_unixsocketdir
284
- - -
285
433
  - $TMPDIR
286
434
  * - Database name
287
435
  - dbname
436
+ - dbname (handles xdist)
288
437
  - --postgresql-dbname
289
438
  - postgresql_dbname
290
- - yes (handles xdist)
291
- - test
439
+ - tests
440
+ * - Maintenance database name
441
+ - n/a
442
+ - maintenance_dbname
443
+ - --postgresql-maintenance-dbname
444
+ - postgresql_maintenance_dbname
445
+ - postgres
292
446
  * - Default Schema (load list)
447
+ - load
293
448
  - load
294
449
  - --postgresql-load
295
450
  - postgresql_load
296
- - yes
297
451
  -
452
+ * - Autocommit for the SQL loader connection
453
+ - load_autocommit
454
+ - load_autocommit
455
+ - --postgresql-load-autocommit
456
+ - postgresql_load_autocommit
457
+ - False
298
458
  * - PostgreSQL connection options
459
+ - options
299
460
  - options
300
461
  - --postgresql-options
301
462
  - postgresql_options
302
- - yes
303
463
  -
304
464
  * - Drop test database on start
305
- -
465
+ - —
466
+ - —
306
467
  - --postgresql-drop-test-database
307
468
  -
308
- - -
309
469
  - false
470
+ * - Fixture to layer the template database on
471
+ - n/a
472
+ - depends_on
473
+ -
474
+ -
475
+ -
476
+
477
+ ``—`` means the setting applies to that fixture but has no factory argument;
478
+ ``n/a`` means the setting does not apply to that fixture at all.
310
479
 
311
480
  .. note::
312
481
 
313
- If the ``executable`` is not provided, the plugin attempts to find it by calling ``pg_config``. If that fails, it fallbacks to a common path like ``/usr/lib/postgresql/13/bin/pg_ctl``.
482
+ If the ``executable`` factory argument is not provided, the plugin looks for ``pg_ctl`` first at
483
+ the configured path (``--postgresql-exec`` / the ``postgresql_exec`` ini option,
484
+ ``/usr/lib/postgresql/14/bin/pg_ctl`` by default), then in the directory reported by
485
+ ``pg_config --bindir``.
486
+
487
+ Only the ``executable`` factory argument is taken at face value; the other two are
488
+ verified to exist before being used. If neither is there, an ``ExecutableMissingException``
489
+ is raised listing the locations that were checked.
490
+
491
+ .. note::
492
+
493
+ ``postgresql_proc`` starts and manages a PostgreSQL server for you, so it needs the
494
+ PostgreSQL **server** installed locally. Installing only the client libraries
495
+ (``libpq-dev`` on Debian/Ubuntu) provides ``pg_config`` but no ``pg_ctl``, and the plugin
496
+ will refuse to start. To test against a server you run yourself — a dockerised one, for
497
+ instance — use the ``postgresql_noproc`` fixture instead.
314
498
 
315
499
  Examples
316
500
  ========
@@ -366,7 +550,6 @@ Advanced Usage: DatabaseJanitor
366
550
  host=postgresql_proc.host,
367
551
  port=postgresql_proc.port,
368
552
  dbname="my_custom_db",
369
- version=postgresql_proc.version,
370
553
  password="secret_password",
371
554
  ):
372
555
  with psycopg.connect(
@@ -379,6 +562,40 @@ Advanced Usage: DatabaseJanitor
379
562
  # use connection
380
563
  pass
381
564
 
565
+ Advanced Usage: AsyncDatabaseJanitor
566
+ ------------------------------------
567
+
568
+ ``AsyncDatabaseJanitor`` is the async counterpart to ``DatabaseJanitor``. Use it
569
+ when managing database state with ``psycopg.AsyncConnection`` outside of standard
570
+ fixtures. It requires ``psycopg`` (a core dependency). Install
571
+ ``pytest-postgresql[async]`` when you need ``aiofiles`` for SQL file loading via
572
+ ``sql_async``, or ``pytest-asyncio`` for pytest async tests.
573
+
574
+ .. code-block:: python
575
+
576
+ import pytest
577
+ import psycopg
578
+ from pytest_postgresql.janitor import AsyncDatabaseJanitor
579
+
580
+ @pytest.mark.asyncio
581
+ async def test_manual_async_janitor(postgresql_proc):
582
+ async with AsyncDatabaseJanitor(
583
+ user=postgresql_proc.user,
584
+ host=postgresql_proc.host,
585
+ port=postgresql_proc.port,
586
+ dbname="my_custom_db",
587
+ password="secret_password",
588
+ ):
589
+ async with await psycopg.AsyncConnection.connect(
590
+ dbname="my_custom_db",
591
+ user=postgresql_proc.user,
592
+ host=postgresql_proc.host,
593
+ port=postgresql_proc.port,
594
+ password="secret_password",
595
+ ) as conn:
596
+ # use async connection
597
+ pass
598
+
382
599
  Connecting to PostgreSQL in Docker
383
600
  ----------------------------------
384
601
 
@@ -432,12 +649,3 @@ You can define a ``load`` function and pass it to your process fixture factory:
432
649
  assert cur.fetchone()[0] == 2
433
650
 
434
651
  The process fixture populates the **template database** once, and the client fixture clones it for every test. This is fast, clean, and ensures no dangling transactions. This approach works with both ``postgresql_proc`` and ``postgresql_noproc``.
435
-
436
- Release
437
- =======
438
-
439
- Install ``pipenv`` and dev dependencies, then run:
440
-
441
- .. code-block:: sh
442
-
443
- pipenv run tbump [NEW_VERSION]