Skip to main content

Intelligent database cleaner for integration tests ported from Jbogard/Respawn.

Project description

Pyspawn

An intelligent database cleaner for integration tests ported from Jbogard/Respawn

To use, create a Checkpoint and initialize with tables you want to skip, or schemas you want to keep/ignore:

from pyspawn import Checkpoint
from pyspawn.adapters import SqlServerAdapter, PgAdapter

checkpoint = Checkpoint(
    tables_to_ignore=["A"],
    tables_to_include=["B", "C"],
    schemas_to_ignore=["schema1"],
    schemas_to_include=["schema2", "schema3"],
    check_temporal_table=False,
    reseed_identity=True,
    db_adapter=SqlServerAdapter()
)

In your tests, in the fixture setup, reset your checkpoint:

import pyodbc
from pyspawn import Checkpoint
from pyspawn.adapters import SqlServerAdapter, PgAdapter

conn_str:str = ""
with pyodbc.connect(conn_str) as conn:
    checkpoint = Checkpoint(
        tables_to_include=["A", "B", "C"],
        schemas_to_include=["dbo"],
        db_adapter = SqlServerAdapter()
    )
    checkpoint.reset(conn)

How does it work?

Pyspawn examines the SQL metadata intelligently to build a deterministic order of tables to delete based on foreign key relationships between tables. It navigates these relationships to build a DELETE script starting with the tables with no relationships and moving inwards until all tables are accounted for.

Once this in-order list of tables is created, the Checkpoint object keeps this list of tables privately so that the list of tables and the order is only calculated once.

In your tests, you Reset your checkpoint before each test run. If there are any tables/schemas that you don't want to be cleared out, include these in the configuration of your Checkpoint.

In benchmarks, a deterministic deletion of tables is faster than truncation, since truncation requires disabling or deleting foreign key constraints. Deletion results in easier test debugging/maintenance, as transaction rollbacks/post-test deletion still rely on that mechanism at the beginning of each test. If data comes in from another source, your test might fail. Respawning to your checkpoint assures you have a known starting point before each test.

Installing Pyspawn

You should install Pyspawn with pip:

pip install pyspawn 

Want to contribute?

Testing

To run tests docker-compose is dependent on a few environment variables from a .env-file located in the root directory.
You can supply your own variables according to the specifications of the "environment" lists spesificed in the compose file, or you can copy the demo values from the composeArgs in the testing pipeline

When the .env-file is in place all you have to run is:

docker-compose -f docker-compose.yaml -f docker-compose.testing.yaml build
docker-compose -f docker-compose.yaml -f docker-compose.testing.yaml run --rm pyspawn

Honors

Lastly I'd like to send my thanks to Jbogard for creating Respawn in the first place! I've found it to be a fantastic tool to greatly improve the testing experience and general code quality.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

pyspawn-1.2.3-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file pyspawn-1.2.3-py3-none-any.whl.

File metadata

  • Download URL: pyspawn-1.2.3-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for pyspawn-1.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 732b3e472ae9a745ed86b9352a38a329bd805e7c955de70d0668de2707500fae
MD5 82a0a6f9b473e4e6fbad2fa9cd76bc81
BLAKE2b-256 05ad7eb8459d45c96e1a947dcdd4380684fae45bf162b98b50d9ae6e4e351519

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page