pytest-odc


Namepytest-odc JSON
Version 0.3.0 PyPI version JSON
download
home_page
SummaryA pytest plugin for simplifying ODC database tests
upload_time2023-08-04 02:15:29
maintainer
docs_urlNone
authorOpen Data Cube
requires_python>=3.8
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![PyPI version](https://img.shields.io/pypi/v/pytest-odc.svg)](https://pypi.org/project/pytest-odc)

[![Python versions](https://img.shields.io/pypi/pyversions/pytest-odc.svg)](https://pypi.org/project/pytest-odc)

# pytest-odc

A [pytest](https://docs.pytest.org/) [plugin](https://docs.pytest.org/en/7.1.x/how-to/plugins.html) for simplifying [Open Datacube](https://www.opendatacube.org/) 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](https://docs.pytest.org/) [fixtures](https://docs.pytest.org/en/stable/explanation/fixtures.html), 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](https://github.com/opendatacube/datacube-explorer/blob/develop/cubedash/testutils/database.py) into your Python environment.
   
   `pip install pytest-odc`

2. Create at least one each of an ODC [Metadata Type](https://datacube-core.readthedocs.io/en/latest/installation/metadata-types.html), [Product](https://datacube-core.readthedocs.io/en/latest/installation/product-definitions.html) and [Dataset](https://datacube-core.readthedocs.io/en/latest/installation/dataset-documents.html) YAML files. These will be loaded into an ODC Database before your test code runs.

3. Create a [pytest](https://docs.pytest.org/) file using this template:

    ```python
    """
    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](https://tox.readthedocs.io/en/latest/), 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](http://www.apache.org/licenses/LICENSE-2.0) license,
\"pytest-odc\" is free and open source software

Issues
------

If you encounter any problems, please [file an
issue](https://github.com/omad/pytest-odc/issues) 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/*
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "pytest-odc",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "",
    "author": "Open Data Cube",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/62/e6/c20c11073d9f64ec083a52f230529c9f0b9f35f84ff68bb41a6de55ad8dd/pytest-odc-0.3.0.tar.gz",
    "platform": null,
    "description": "[![PyPI version](https://img.shields.io/pypi/v/pytest-odc.svg)](https://pypi.org/project/pytest-odc)\n\n[![Python versions](https://img.shields.io/pypi/pyversions/pytest-odc.svg)](https://pypi.org/project/pytest-odc)\n\n# pytest-odc\n\nA [pytest](https://docs.pytest.org/) [plugin](https://docs.pytest.org/en/7.1.x/how-to/plugins.html) for simplifying [Open Datacube](https://www.opendatacube.org/) database tests.\n\n# Testing Open Datacube Applications\n\nAnyone 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.\n\nThere's two challenges here:\n\n- ODC requires a PostgreSQL server accessible anywhere test are run, which can be challenging to set up.\n- The database should be reset every time a test is run, to make it possible to repeatedly run tests.\n\n## The Solution\n\nThis package provides reusable [pytest](https://docs.pytest.org/) [fixtures](https://docs.pytest.org/en/stable/explanation/fixtures.html), 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.\n\n### Steps to Create New Tests\n\n1. Install the [ODC Test Plugin](https://github.com/opendatacube/datacube-explorer/blob/develop/cubedash/testutils/database.py) into your Python environment.\n   \n   `pip install pytest-odc`\n\n2. Create at least one each of an ODC [Metadata Type](https://datacube-core.readthedocs.io/en/latest/installation/metadata-types.html), [Product](https://datacube-core.readthedocs.io/en/latest/installation/product-definitions.html) and [Dataset](https://datacube-core.readthedocs.io/en/latest/installation/dataset-documents.html) YAML files. These will be loaded into an ODC Database before your test code runs.\n\n3. Create a [pytest](https://docs.pytest.org/) file using this template:\n\n    ```python\n    \"\"\"\n    Template pytest module for testing ODC Code\n    \"\"\"\n    import pytest\n\n    # UPDATE: these lists with the data you want loaded into your database\n    # Each string is for a file, relative to this python module.\n    METADATA_TYPES = [\"metadata/eo3_metadata.yaml\",]\n    PRODUCTS = [\"products/ga_ls8c_ard_3.odc-product.yaml\",]\n    DATASETS = [\"datasets/ga_ls8c_ard_3-sample.yaml\",]\n\n    # Use the 'auto_odc_db' fixture to populate the database with sample data.\n    pytestmark = pytest.mark.usefixtures(\"auto_odc_db\")\n\n    def test_my_odc_code(odc_db: \"datacube.Datacube\"):\n        ### REPLACE: with your own test code\n        my_datasets = odc_db.find_datasets(product='ga_ls8c_ard_3')\n        assert len(my_datasets) == 3\n    ```\n\n4. Run your new tests.\n\n       pytest tests/\n\n# More Details / How it Works\n\nThe four provided pytest fixtures compose together to ensure an ODC Database is available and preloaded with test data for each test module.\n\n\n## Fixtures\n\n### `postgresql_server`\n\nThis fixture either:\n\n- Runs a temporary PostgreSQL server using Docker\n- Or checks that a test PostgreSQL server is already available via the `ODC_TEST_DB_URL` environment variable.\n\n### `odc_db`\n\nThis fixture provides a `datacube.Datacube()` object configured to connect to the temporary test database. \n\nIt can be added to the parameters for a test for getting access to the temporary database.\n\n### `odc_test_db`\n\nThis fixture creates all the ODC Database tables.\n\n### `auto_odc_db`\n\nThis 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.\n\n\nContributing\n------------\n\nContributions are very welcome. Tests can be run with\n[tox](https://tox.readthedocs.io/en/latest/), please ensure the coverage\nat least stays the same before you submit a pull request.\n\nLicense\n-------\n\nDistributed under the terms of the [Apache Software License\n2.0](http://www.apache.org/licenses/LICENSE-2.0) license,\n\\\"pytest-odc\\\" is free and open source software\n\nIssues\n------\n\nIf you encounter any problems, please [file an\nissue](https://github.com/omad/pytest-odc/issues) along\nwith a detailed description.\n\nRelease Process\n---------------\n\n```\n# Update version number!\ngit tag {version_number}\nrm -rf dist/\npython -m build\ntwine upload --repository testpypi dist/*\ntwine upload dist/*\n```\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A pytest plugin for simplifying ODC database tests",
    "version": "0.3.0",
    "project_urls": {
        "Homepage": "https://www.opendatacube.org/",
        "Repository": "https://github.com/opendatacube/pytest-odc"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0554d41f8c95813891a7f6eb7e17ec2fd9c49dde88c8c96faf3282694a0cea95",
                "md5": "fc277652e9a882ba5e7e1a6b85baa19a",
                "sha256": "c32ec740c78c3d9772d336f7aaf18eec09db8dbd4d2d39a1cba70a125d121baf"
            },
            "downloads": -1,
            "filename": "pytest_odc-0.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fc277652e9a882ba5e7e1a6b85baa19a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 10989,
            "upload_time": "2023-08-04T02:15:27",
            "upload_time_iso_8601": "2023-08-04T02:15:27.174989Z",
            "url": "https://files.pythonhosted.org/packages/05/54/d41f8c95813891a7f6eb7e17ec2fd9c49dde88c8c96faf3282694a0cea95/pytest_odc-0.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "62e6c20c11073d9f64ec083a52f230529c9f0b9f35f84ff68bb41a6de55ad8dd",
                "md5": "c9928af55b5bd08dfb7e00f9d3aa6bb9",
                "sha256": "51664ff7fe6786029e8739d2a662cb91cc8a7a7d9e53b029c425067d54be753a"
            },
            "downloads": -1,
            "filename": "pytest-odc-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "c9928af55b5bd08dfb7e00f9d3aa6bb9",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 10918,
            "upload_time": "2023-08-04T02:15:29",
            "upload_time_iso_8601": "2023-08-04T02:15:29.050996Z",
            "url": "https://files.pythonhosted.org/packages/62/e6/c20c11073d9f64ec083a52f230529c9f0b9f35f84ff68bb41a6de55ad8dd/pytest-odc-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-04 02:15:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "opendatacube",
    "github_project": "pytest-odc",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "pytest-odc"
}
        
Elapsed time: 0.11063s