Skip to content

Commit 99b4996

Browse files
committed
test: add ignore() context manager test coverage
- Add ignore() tests to all test classes that previously only tested strict() - Use get_collections() for ignore() tests since it emits warnings (unlike search() which raises exceptions directly) - Achieve 100% coverage for warnings.py module
1 parent 6f443ba commit 99b4996

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

pystac_client/collection_search.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,17 +304,21 @@ def __init__(
304304
if any([bbox, datetime, q, query, filter, sortby, fields]):
305305
if not self._collection_search_extension_enabled:
306306
warnings.warn(
307-
str(DoesNotConformTo("COLLECTION_SEARCH"))
308-
+ ". Filtering will be performed client-side where only bbox, "
309-
"datetime, and q arguments are supported"
307+
DoesNotConformTo(
308+
"COLLECTION_SEARCH",
309+
"Filtering will be performed client-side where only bbox, "
310+
"datetime, and q arguments are supported",
311+
)
310312
)
311313
self._validate_client_side_args()
312314
else:
313315
if not self._collection_search_free_text_enabled:
314316
warnings.warn(
315-
str(DoesNotConformTo("COLLECTION_SEARCH#FREE_TEXT"))
316-
+ ". Free-text search is not enabled for collection search"
317-
"Free-text filters will be applied client-side."
317+
DoesNotConformTo(
318+
"COLLECTION_SEARCH#FREE_TEXT",
319+
"Free-text search is not enabled for collection search"
320+
"Free-text filters will be applied client-side.",
321+
)
318322
)
319323

320324
else:

pystac_client/warnings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def ignore() -> Iterator[None]:
7979
>>> from pystac_client import Client
8080
>>> from pystac_client.warnings import ignore
8181
>>> with ignore():
82-
... Client.open("https://perfect-api.test")
82+
... Client.open("https://imperfect-api.test")
8383
8484
For finer-grained control:
8585

tests/test_warnings.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
3+
from pystac_client import Client
4+
from pystac_client.warnings import DoesNotConformTo, ignore, strict
5+
6+
from .helpers import TEST_DATA
7+
8+
9+
class TestWarningContextManagers:
10+
@pytest.mark.filterwarnings("error")
11+
def test_ignore_context_manager(self) -> None:
12+
"""Test that ignore() context manager suppresses warnings."""
13+
api = Client.from_file(str(TEST_DATA / "planetary-computer-root.json"))
14+
api.remove_conforms_to("COLLECTION_SEARCH")
15+
16+
# This should emit a DoesNotConformTo warning, but ignore() should suppress it
17+
with ignore():
18+
api.collection_search(limit=10, max_collections=10, q="test")
19+
20+
@pytest.mark.filterwarnings("error")
21+
def test_strict_context_manager(self) -> None:
22+
"""Test that strict() context manager converts warnings to exceptions."""
23+
api = Client.from_file(str(TEST_DATA / "planetary-computer-root.json"))
24+
api.remove_conforms_to("COLLECTION_SEARCH")
25+
26+
# This should raise DoesNotConformTo as an exception
27+
with strict():
28+
with pytest.raises(DoesNotConformTo, match="COLLECTION_SEARCH"):
29+
api.collection_search(limit=10, max_collections=10, q="test")
30+
31+
def test_ignore_context_manager_cleanup(self) -> None:
32+
"""Test that ignore() properly restores warning filters after exit."""
33+
api = Client.from_file(str(TEST_DATA / "planetary-computer-root.json"))
34+
api.remove_conforms_to("COLLECTION_SEARCH")
35+
36+
# Test that warnings are suppressed inside the context
37+
with ignore():
38+
api.collection_search(limit=10, max_collections=10, q="test")
39+
40+
# Test that warnings are restored after exiting the context
41+
# Use strict() to ensure warnings become exceptions
42+
with strict():
43+
with pytest.raises(DoesNotConformTo, match="COLLECTION_SEARCH"):
44+
api.collection_search(limit=10, max_collections=10, q="test")

0 commit comments

Comments
 (0)