diff --git a/CHANGELOG.md b/CHANGELOG.md index 2358c648..c9eddcfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + +- Add comprehensive test coverage for warning context managers (`ignore()` and `strict()`) ([#832](https://github.com/stac-utils/pystac-client/pull/832)) + ### Changed - Make `get_collection` raise if `collection_id` is empty ([#809](https://github.com/stac-utils/pystac-client/pull/809)) diff --git a/pyproject.toml b/pyproject.toml index a39c29c9..35166f7b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -110,6 +110,10 @@ select = ["E", "F", "W", "I"] [tool.pytest.ini_options] markers = "vcr: records network activity" addopts = "--benchmark-skip --block-network" +filterwarnings = [ + "ignore::ResourceWarning", + "ignore::pytest.PytestUnraisableExceptionWarning", +] [tool.mypy] show_error_codes = true diff --git a/pystac_client/collection_search.py b/pystac_client/collection_search.py index b800f3ac..c31cf597 100644 --- a/pystac_client/collection_search.py +++ b/pystac_client/collection_search.py @@ -304,17 +304,21 @@ def __init__( if any([bbox, datetime, q, query, filter, sortby, fields]): if not self._collection_search_extension_enabled: warnings.warn( - str(DoesNotConformTo("COLLECTION_SEARCH")) - + ". Filtering will be performed client-side where only bbox, " - "datetime, and q arguments are supported" + DoesNotConformTo( + "COLLECTION_SEARCH", + "Filtering will be performed client-side where only bbox, " + "datetime, and q arguments are supported", + ) ) self._validate_client_side_args() else: if not self._collection_search_free_text_enabled: warnings.warn( - str(DoesNotConformTo("COLLECTION_SEARCH#FREE_TEXT")) - + ". Free-text search is not enabled for collection search" - "Free-text filters will be applied client-side." + DoesNotConformTo( + "COLLECTION_SEARCH#FREE_TEXT", + "Free-text search is not enabled for collection search" + "Free-text filters will be applied client-side.", + ) ) else: diff --git a/pystac_client/warnings.py b/pystac_client/warnings.py index e744f159..4ad7b228 100644 --- a/pystac_client/warnings.py +++ b/pystac_client/warnings.py @@ -79,7 +79,7 @@ def ignore() -> Iterator[None]: >>> from pystac_client import Client >>> from pystac_client.warnings import ignore >>> with ignore(): - ... Client.open("https://perfect-api.test") + ... Client.open("https://imperfect-api.test") For finer-grained control: diff --git a/tests/test_warnings.py b/tests/test_warnings.py new file mode 100644 index 00000000..b878d628 --- /dev/null +++ b/tests/test_warnings.py @@ -0,0 +1,39 @@ +import pytest + +from pystac_client import Client +from pystac_client.warnings import DoesNotConformTo, ignore, strict + +from .helpers import TEST_DATA + + +class TestWarningContextManagers: + @pytest.mark.filterwarnings("error") + def test_ignore_context_manager(self) -> None: + """Test that ignore() context manager suppresses warnings.""" + api = Client.from_file(str(TEST_DATA / "planetary-computer-root.json")) + api.remove_conforms_to("COLLECTION_SEARCH") + + with ignore(): + api.collection_search(limit=10, max_collections=10, q="test") + + @pytest.mark.filterwarnings("error") + def test_strict_context_manager(self) -> None: + """Test that strict() context manager converts warnings to exceptions.""" + api = Client.from_file(str(TEST_DATA / "planetary-computer-root.json")) + api.remove_conforms_to("COLLECTION_SEARCH") + + with strict(): + with pytest.raises(DoesNotConformTo, match="COLLECTION_SEARCH"): + api.collection_search(limit=10, max_collections=10, q="test") + + def test_ignore_context_manager_cleanup(self) -> None: + """Test that ignore() properly restores warning filters after exit.""" + api = Client.from_file(str(TEST_DATA / "planetary-computer-root.json")) + api.remove_conforms_to("COLLECTION_SEARCH") + + with ignore(): + api.collection_search(limit=10, max_collections=10, q="test") + + with strict(): + with pytest.raises(DoesNotConformTo, match="COLLECTION_SEARCH"): + api.collection_search(limit=10, max_collections=10, q="test")