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.0-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyspawn-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 17.7 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f050208cbdc695b40abbdc94b3d8fc3bfacce52525037870b268e3e19a23de6
MD5 8ae96371075251ef4c987da533430fa6
BLAKE2b-256 c69cb4979264cba6ff8b25594ed5c24061e2fd8c0d35cb975e6bd7c3bf473475

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