Skip to main content

PyPI version

Python versions

pytest-odc

A pytest plugin for simplifying Open Datacube database tests.

Testing Open Datacube Applications

Anyone writing applications using an ODC Database needs to be able to test their code. This requires an ODC Database containing a known set of data.

There's two challenges here:

  • ODC requires a PostgreSQL server accessible anywhere test are run, which can be challenging to set up.
  • The database should be reset every time a test is run, to make it possible to repeatedly run tests.

The Solution

This package provides reusable pytest fixtures, to make it easy to write test code against an ODC Database. It handles starting and stopping a temporary PostgreSQL server using Docker, initialising it for ODC use, and loading/wiping sets of test data on a per test module basis.

Steps to Create New Tests

  1. Install the ODC Test Plugin into your Python environment.

    pip install pytest-odc

  2. Create at least one each of an ODC Metadata Type, Product and Dataset YAML files. These will be loaded into an ODC Database before your test code runs.

  3. Create a pytest file using this template:

    """
    Template pytest module for testing ODC Code
    """
    import pytest
    
    # UPDATE: these lists with the data you want loaded into your database
    # Each string is for a file, relative to this python module.
    METADATA_TYPES = ["metadata/eo3_metadata.yaml",]
    PRODUCTS = ["products/ga_ls8c_ard_3.odc-product.yaml",]
    DATASETS = ["datasets/ga_ls8c_ard_3-sample.yaml",]
    
    # Use the 'auto_odc_db' fixture to populate the database with sample data.
    pytestmark = pytest.mark.usefixtures("auto_odc_db")
    
    def test_my_odc_code(odc_db: "datacube.Datacube"):
        ### REPLACE: with your own test code
        my_datasets = odc_db.find_datasets(product='ga_ls8c_ard_3')
        assert len(my_datasets) == 3
    
  4. Run your new tests.

    pytest tests/
    

More Details / How it Works

The four provided pytest fixtures compose together to ensure an ODC Database is available and preloaded with test data for each test module.

Fixtures

postgresql_server

This fixture either:

  • Runs a temporary PostgreSQL server using Docker
  • Or checks that a test PostgreSQL server is already available via the ODC_TEST_DB_URL environment variable.

odc_db

This fixture provides a datacube.Datacube() object configured to connect to the temporary test database.

It can be added to the parameters for a test for getting access to the temporary database.

odc_test_db

This fixture creates all the ODC Database tables.

auto_odc_db

This fixture should be used as a pytestmark autoload, marking the test module as having variables named METADATA_TYPES, PRODUCTS and DATASETS being lists of files to load into the test database.

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the Apache Software License 2.0 license, "pytest-odc" is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

Release Process

# Update version number!
git tag {version_number}
rm -rf dist/
python -m build
twine upload --repository testpypi dist/*
twine upload dist/*

Release files for pytest-odc 0.3.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 pytest-odc 0.3.0
File Size Uploaded
pytest-odc-0.3.0.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-odc 0.3.0
File Interpreter ABI Platform
pytest_odc-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.9 kB

Release files / pytest-odc-0.3.0.tar.gz

Download URL pytest-odc-0.3.0.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
51664ff7fe6786029e8739d2a662cb91cc8a7a7d9e53b029c425067d54be753a
BLAKE2b-256 checksum
How to use checksums
62e6c20c11073d9f64ec083a52f230529c9f0b9f35f84ff68bb41a6de55ad8dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / pytest_odc-0.3.0-py3-none-any.whl

Download URL pytest_odc-0.3.0-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c32ec740c78c3d9772d336f7aaf18eec09db8dbd4d2d39a1cba70a125d121baf
BLAKE2b-256 checksum
How to use checksums
0554d41f8c95813891a7f6eb7e17ec2fd9c49dde88c8c96faf3282694a0cea95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

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