Metadata-Version: 2.3
Name: postgresql-testing
Version: 0.0.5
Summary: PostgreSQL testing helpers
Author: Oli Russell
Author-email: Oli Russell <mrxoliver@gmail.com>
License: MIT License
         
         Copyright (c) 2023 Oliver Russell
         
         Permission is hereby granted, free of charge, to any person obtaining a copy
         of this software and associated documentation files (the "Software"), to deal
         in the Software without restriction, including without limitation the rights
         to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
         copies of the Software, and to permit persons to whom the Software is
         furnished to do so, subject to the following conditions:
         
         The above copyright notice and this permission notice shall be included in all
         copies or substantial portions of the Software.
         
         THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
         IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
         FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
         AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
         LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
         OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
         SOFTWARE.
Requires-Dist: postgresql-binaries
Requires-Dist: psycopg[binary]==3.*
Requires-Python: >=3.12
Project-URL: repository, https://github.com/leontrolski/postgresql-testing
Description-Content-Type: text/markdown

# Postgresql testing

Simple Postgres helpers for testing with Python - no docker, brew, apt, etc - uses [postgresql-binaries](https://github.com/leontrolski/postgresql-binaries). The interface is simple, but close enough to the metal that you can use it to eg. have a new database per testing thread/use a fresh database from a `TEMPLATE` or archive.

```shell
pip install postgresql-testing 'postgresql-binaries==18.*'
```

Then to use, eg:

```python
import postgresql_testing

@pytest.fixture(scope="session")
def db() -> Iterator[str]:
    config = postgresql_testing.DatabaseConfig.default("testing-db")
    postgresql_testing.initdb(config.directory, on_existing="use")
    with postgresql_testing.serve(config):
        postgresql_testing.ensure_user(config)
        postgresql_testing.create_database(config, on_existing="replace")
        yield config.dsn
```

There are various useful flags and things - the source code is short enough to just dive in.

<hr>

There are a couple of helpers for creating/using template dbs and archives. I have some vague long term plan for some kind of "Docker layers for migrated databases" with clever caching (or not), but I'm not quite sure what it looks like yet.

Some benchmarking:

| number of tables | create from template | dump to archive | create from archive |
|---|---|---|---|
| 100 | 80ms | 80ms (0.2MB) | 150ms |
| 1000 | 500ms | 300ms (2MB) | 1100ms |

On macos using a ramdisk, it is slightly quicker:

| number of tables | create from template | dump to archive | create from archive |
|---|---|---|---|
| 100 | 70ms | 70ms | 120ms |
| 1000 | 350ms | 300ms | 1000ms |

See claims/advice from planet [Go](https://github.com/peterldowns/pgtestdb).
