Skip to main content

Reproducible SQL database state for testing routines

Project description

fastapi-testdb

FastAPI is a cool framework for building fast and reliable API. It provides various tools to create an app you want. Also it has convenient way to cover the code with tests. Read the articles for more details Testing and Testing a Database. It covers the most aspects of testing, but sadly one important thing was left behind scenes. You have to recreate the database every time before run each test to make tests reproducible, otherwise be ready to get unpredictible results after every test run.

This package helps you to make database tests reproducible.

Installation

pip install fastapi-testdb

Usage

Let's say you have a FastAPI instance app with dependency connect_db to get a connection to your database. Your SQLAlchemy ORM base class is BaseOrm. Common usage of TestDB for having repeatable tests:

  1. First create a class by call create_testdb:

    from fastapi_testdb import create_testdb
    
    testdb = create_testdb(app, connect_db, BaseOrm, dsn='sqlite:////tmp/your-testdb.sqlite')
    

    testdb is your new class type targeted to connect to the test database. In this case it is an SQLite DB, but you can use other databases - PostgreSQL, MySQL and everything that supported by SQLAlchemy.

    You have to pass connect_db because this dependecy must be replaced on a test run for not to harm you real database.

    BaseOrm will be used to get metadata when the SQL tables create.

  2. Now you can use the new class for creating reproducible DB state:

    def test_yourtestfunc():
        with testdb() as tdb:
            # In this `with` statement a new DB was created and all the tables from
            # `BaseOrm.metadata` were initiated. Now you have a completely empty tables in the DB.
            # Do your test here:
            ...
    

Usage as a decorator

It is also possible to use this as a decorator:

@testdb.initdb
def test_yourtestfunc(tdb):
    # A new DB is ready for testing. Do your test below
    ...

Insert test data

A new DB has created empty. If you have to fill tables with some data, you can use prefill_orm method. For example, let you have an ORM class for users:

class UserDB(BaseOrm):
    id: Mapped[int] = mapped_column(primary_key=True, index=True)
    name: Mapped[str] = mapped_column(index=True, unique=True)

To have some content on testing, call the method prefill_orm:

@testdb.initdb
def test_yourtestfunc(tdb):
    users_fixture = [
        {"id": 1, "name": "Bob"},
        {"id": 2, "name": "Henry"},
    ]
    tdb.prefill_orm(UserDB, users_fixture)

    # Now you have your users in the table:
    with TestClient(app) as client:
        client = app.get('/user/1')
        assert client.status_code == 200

Contribution

You are welcome to make contribution and proposals for improvement!

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

fastapi_testdb-0.1.2.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

fastapi_testdb-0.1.2-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_testdb-0.1.2.tar.gz.

File metadata

  • Download URL: fastapi_testdb-0.1.2.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.2

File hashes

Hashes for fastapi_testdb-0.1.2.tar.gz
Algorithm Hash digest
SHA256 73edaa0abe44c95d9b5bc2a472f099157f7362caeae009d5b315067bbef6cefc
MD5 00860ef0118b916cb9e477053958922e
BLAKE2b-256 c71ab95b45827fcf99e17023114f925432a99aec1d4d3682a4c9024811cc8e06

See more details on using hashes here.

File details

Details for the file fastapi_testdb-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: fastapi_testdb-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.2

File hashes

Hashes for fastapi_testdb-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d0f99d43f0c9ffb464702626c1db7b47df57517b21e6f5b7b41cdae54f022399
MD5 6fbe5bc27a285f9222e8dd3c8d3fb048
BLAKE2b-256 28202bc4a2bb6f88ed83b825696d637ffcf3daf8836dc98adab27fb8ca8c7b27

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