Skip to main content

Pytest fixtures for tests involving databases managed by alembic migrations

Project description

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.

Project details


Download files

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

Source Distribution

alembic_migration_fixtures-0.1.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

alembic_migration_fixtures-0.1.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file alembic_migration_fixtures-0.1.0.tar.gz.

File metadata

  • Download URL: alembic_migration_fixtures-0.1.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.9.6 Linux/5.8.0-1036-azure

File hashes

Hashes for alembic_migration_fixtures-0.1.0.tar.gz
Algorithm Hash digest
SHA256 02a21efb54fc7052d6165a2715f91e5549aff3cd762976a6b8aeeda263c97741
MD5 e7b8868c245fe8adbb69d356be94483c
BLAKE2b-256 47627311624ab66e199c0906282f5f0e73e2f81adc8fa1d25ed0f2df828f458b

See more details on using hashes here.

File details

Details for the file alembic_migration_fixtures-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for alembic_migration_fixtures-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e5899055eb95c3bfb408d39bb5702b91483b28cf686a20500a46d8f266b6f14d
MD5 239a2bd0f5fd3e069fb185b8428157ae
BLAKE2b-256 4fcaca28b68e5cd1e61ab45ecf4eaba521c2b927f50d3948f28b134d78b36802

See more details on using hashes here.

Supported by

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