Skip to main content

pytest fixtures for PostgreSQL

Project description

Lathorp - PostgreSQL Testing Fixtures

Lathorp provides handy pytest fixtures for test code that involves access to a PostgreSQL database.

For example, if the code under test writes data to a database, or queries it for data, you want to write tests to see that it writes and reads the data as you expect. You need an isolated and controlled environment for testing, so you need to set up a temporary database just for testing and fill it up with known data. Setting up a new database is a relatively time consuming operation that slows your tests down. You want to do this just once per testing session and let multiple tests share the same temporary database instance. However, many tests should be isolated from each other - you don't want side effects from a test (say, data written to the database) leaking to another test. The solution is to write known data before each test and delete it all after the test has finished.

The Fixtures

The pg fixture is a session-scoped fixture that creates a new temporary database once per test session and deletes it after the session is done.

The pg_connect fixture is a function-scoped fixture that you use to establish a connection to the temporary database. You can optionally load data into the database before the test begins, by pointing to data files that PostgreSQL can read. The data is automatically deleted after the test function returns, so that it doesn't leak to other tests that may expect different data.

Utility Functions

A utility function called load_schema_definitions can be used to create the database structure by reading SQL DDL files that you provide. Embed it in your own session-scoped fixture and call that together with pg_connect.

Another utility, copy_data, copies data from files in CSV or text format (as supported by PostgreSQL). It can be used directly, or automatically by passing a path to the files to the pg_connect fixture.

A third utility, delete_data, deletes all data from a set of tables. It is used automatically by pg_connect to remove data from tables that it loaded with copy_data. You can also use it directly to clean up any changes made by your test.

Using Lathorp in Your Project

  1. Include the lathorp package with the development packages of your projects. With pipenv:
pipenv install --dev lathorp
  1. Import fixtures from lathorp in your tests. You can also import them in conftest.py to make them available to all your tests. See tests/conftest.py in this project for an example.

  2. Add either pg or pg_connect as arguments to your test functions, as shown below. There's no point in adding both, since pg_connect itself uses pg.

def my_test(pg_connect):
    """A test that connects to the temporary test database."""
    with pg_connect() as conn:
        with conn.cursor() as cursor:
            cursor.execute("SELECT 'Hello, world!' AS hello;")

For more examples, see tests/test_fixtures.py in this project.

  1. Lathorp truly shines when you use it together with schema definitions (SQL DDL) files and test data files (CSV or PostgreSQL-readable text). Create a session-scoped fixture that calls load_schema_definitions and give it the path to your schema definitions. Then use this fixture along with pg_connect and give it the path to your data files.

    # In conftest.py
    import pathlib
    from lathorp import load_schema_definitions
    from lathorp.fixtures import pg
    from lathorp.fixtures import pg_connect
    
    def init_schema(pg):
        load_schema_definitions(pathlib.Path('path/to/my/ddl/file_or_directory'))
    
    # In your test module
    def test_my_fun(init_schema, pg_connect):
        conn = pg_connect(pathlib.Path('path/to/my_table.csv'))  # loads data into my_table
        with conn.cursor() as cursor:
            cursor.execute('SELECT * FROM my_table;')
    

    Now every test can have access to an initialized database with test-specific data.

Why "Lathorp?

The name is a reference to Dr. Emmett Lathorp "Doc" Brown, the crazy scientist and inventor of the DeLorean time machine from Back to the Future trilogy.

The Lathorp library too lets you go back in time to a fresh database after every test.

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

lathorp-0.2.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

lathorp-0.2.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file lathorp-0.2.0.tar.gz.

File metadata

  • Download URL: lathorp-0.2.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for lathorp-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a08db71302b81b112b9e0b8386b908b7e2b41e0e7158a4230ebb04cb337ab5bf
MD5 646f3279f3d7f4f1e885b4adeb516fe8
BLAKE2b-256 edca1d5decdea0ea179e45d3a5b9e23c5e3dce28260a3301b96e7fdfe221c9f1

See more details on using hashes here.

File details

Details for the file lathorp-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: lathorp-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for lathorp-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0acf7bc3268191974573f16f0e27d74858a257bb80174db9b02f9668d63e2fe4
MD5 b46708d66fee20ea2d992298a4e7f3f0
BLAKE2b-256 920f766e33131638f81b5b93ce83db4b7d248158284359633fe518d3b7f49a69

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