Skip to main content

Formatter PyPI version PyPI - Python Version

Description

Pytest fixture to simplify writing tests against databases managed with alembic. Before each test run, alembic migrations apply schema changes which then allows tests to only care about data. This way your application code, and the database migrations get executed by the test.

Only tested with PosgreSQL. However, code may work with other databases as well.

Installation

Install with pip install alembic-migration-fixtures or any other dependency manager. Afterwards, create a pytest fixture called database_engine returning an SQLAlchemy Engine instance.

WARNING

Do not specify the production / development / any other database where data is important in the engine fixture. If you do so, the tests WILL truncate all tables and data loss WILL occur.

Usage

This library provides a pytest fixture called test_db_session. Use this to replace the normal SQLAlchemy session used within the application, or else tests may not be independent of one another.

How the fixture works with your tests:

  1. Fixture recreates (wipes) the database schema based on the engine provided for the test session
  2. Fixture runs alembic migrations (equivalent to alembic upgrade heads)
  3. Fixture creates a test database session within a transaction for the test
  4. Your test sets up data and runs the test using the session (including COMMITing transactions)
  5. Your test verifies data is in the database
  6. Fixture rolls back the transaction (and any inner COMMITed transactions in the test)

This two-level transaction strategy makes it so any test is independent of one another, since the database is empty after each test. Since the database schema only gets re-created once per session, the test speed is only linearly dependent on the number of migrations.

Development

This library uses the poetry package manager, which has to be installed before installing other dependencies. Afterwards, run poetry install to create a virtualenv and install all dependencies. To then activate that environment, use poetry shell. To run a command in the environment without activating it, use poetry run <command>.

Black is used (and enforced via workflows) to format all code. Poetry will install it automatically, but running it is up to the user. To format the entire project, run black . inside the virtualenv.

Contributing

This project uses the Apache 2.0 license and is maintained by the data science team @ Barbora. All contribution are welcome in the form of PRs or raised issues.

Metadata

Release files for alembic-migration-fixtures 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for alembic-migration-fixtures 0.2.0
File Size Uploaded Provenance
alembic_migration_fixtures-0.2.0.tar.gz 7.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for alembic-migration-fixtures 0.2.0
File Interpreter ABI Platform
alembic_migration_fixtures-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.1 kB

Release files / alembic_migration_fixtures-0.2.0.tar.gz

Download URL alembic_migration_fixtures-0.2.0.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
24cdf4bf9fc1aa6532ca98ce466240f89351015002b058c0487c63e94cb1a521
BLAKE2b-256 checksum
How to use checksums
d3b7ead52e216f43bd01262a4e9368eb3d709e471db1a3b72e737c78437d830d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.11.3 Linux/5.15.0-1037-azure

Release files / alembic_migration_fixtures-0.2.0-py3-none-any.whl

Download URL alembic_migration_fixtures-0.2.0-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a93faf52b986613239e4e3f9cea0455cac4bf6e42a0822581395a8a866efd5c
BLAKE2b-256 checksum
How to use checksums
b4e589b326a871ee68d40720b5b3bf27b2f22a2a6e677bac25acecdbcadec414
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.11.3 Linux/5.15.0-1037-azure

Security for alembic-migration-fixtures 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page