From 9c51d164f61d6e114c5934f5755cb2ae6d3eb0b1 Mon Sep 17 00:00:00 2001 From: Pete Gadomski Date: Mon, 9 Oct 2023 13:57:14 -0600 Subject: [PATCH] feat: use pytest Instead of a custom validate script, just use pytest --- .github/workflows/ci.yaml | 6 +++--- requirements.in | 1 + requirements.txt | 35 ++++++++++++++++++++++-------- scripts/validate_collections.py | 38 --------------------------------- tests/test_collections.py | 13 +++++++++++ 5 files changed, 43 insertions(+), 50 deletions(-) delete mode 100755 scripts/validate_collections.py create mode 100644 tests/test_collections.py diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 5433604..3020953 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -7,7 +7,7 @@ on: pull_request: jobs: - validate: + test: runs-on: ubuntu-latest steps: - name: Checkout @@ -19,8 +19,8 @@ jobs: cache: "pip" - name: Install dependencies run: pip install -r requirements.txt - - name: Validate collections - run: python scripts/validate_collections.py + - name: Test collections + run: pytest lint: runs-on: ubuntu-latest steps: diff --git a/requirements.in b/requirements.in index 0dd68fa..8f9b637 100644 --- a/requirements.in +++ b/requirements.in @@ -2,4 +2,5 @@ black[jupyter] pip-tools pre-commit pystac[validation] +pytest ruff diff --git a/requirements.txt b/requirements.txt index e6a8b05..46cb9d7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,10 +9,12 @@ appnope==0.1.3 asttokens==2.4.0 # via stack-data attrs==23.1.0 - # via jsonschema + # via + # jsonschema + # referencing backcall==0.2.0 # via ipython -black[jupyter]==23.9.1 +black[jupyter]==23.10.0 # via -r requirements.in build==1.0.3 # via pip-tools @@ -32,22 +34,27 @@ filelock==3.12.4 # via virtualenv identify==2.5.30 # via pre-commit +iniconfig==2.0.0 + # via pytest ipython==8.16.1 # via black jedi==0.19.1 # via ipython -jsonschema==4.17.3 +jsonschema==4.19.1 # via pystac +jsonschema-specifications==2023.7.1 + # via jsonschema matplotlib-inline==0.1.6 # via ipython mypy-extensions==1.0.0 # via black nodeenv==1.8.0 # via pre-commit -packaging==23.1 +packaging==23.2 # via # black # build + # pytest parso==0.8.3 # via jedi pathspec==0.11.2 @@ -62,7 +69,9 @@ platformdirs==3.11.0 # via # black # virtualenv -pre-commit==3.4.0 +pluggy==1.3.0 + # via pytest +pre-commit==3.5.0 # via -r requirements.in prompt-toolkit==3.0.39 # via ipython @@ -74,15 +83,23 @@ pygments==2.16.1 # via ipython pyproject-hooks==1.0.0 # via build -pyrsistent==0.19.3 - # via jsonschema -pystac[validation]==1.8.3 +pystac[validation]==1.8.4 + # via -r requirements.in +pytest==7.4.2 # via -r requirements.in python-dateutil==2.8.2 # via pystac pyyaml==6.0.1 # via pre-commit -ruff==0.0.292 +referencing==0.30.2 + # via + # jsonschema + # jsonschema-specifications +rpds-py==0.10.6 + # via + # jsonschema + # referencing +ruff==0.1.0 # via -r requirements.in six==1.16.0 # via diff --git a/scripts/validate_collections.py b/scripts/validate_collections.py deleted file mode 100755 index 8a24726..0000000 --- a/scripts/validate_collections.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python3 - -"""Validates all collections in ingestion-data/collections""" - -import json -import sys -from pathlib import Path - -from pystac import Collection, STACValidationError - -root = Path(__file__).parents[1] -collections = root / "ingestion-data" / "collections" - -errors = dict() -for path in collections.rglob("*.json"): - try: - collection = Collection.from_file(str(path)) - except Exception as error: - errors[path.name] = { - "type": "error", - "message": f"cannot read collection, {type(error)}: {error}", - } - continue - try: - collection.validate() - except STACValidationError as error: - if isinstance(error.source, list): - message = [str(e) for e in error.source] - else: - message = str(error.source) - errors[path.name] = { - "type": "invalid", - "message": message, - } - -if errors: - json.dump(errors, sys.stdout, indent=2) - sys.exit(1) diff --git a/tests/test_collections.py b/tests/test_collections.py new file mode 100644 index 0000000..df03339 --- /dev/null +++ b/tests/test_collections.py @@ -0,0 +1,13 @@ +from pathlib import Path + +import pytest +from pystac import Collection + +ROOT = Path(__file__).parents[1] +COLLECTIONS_PATH = ROOT / "ingestion-data" / "collections" + + +@pytest.mark.parametrize("path", COLLECTIONS_PATH.rglob("*.json")) +def test_validate(path: Path) -> None: + collection = Collection.from_file(str(path)) + collection.validate()