Skip to content

Commit 5e4309e

Browse files
add basic warning/exception tests to bump cov
1 parent e47c371 commit 5e4309e

4 files changed

Lines changed: 70 additions & 13 deletions

File tree

mp_api/client/core/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def __init__(
198198
warnings.warn(
199199
"Ignoring `monty_decode`, as it is no longer a supported option in `mp_api`."
200200
"The client by default returns results consistent with `monty_decode=True`.",
201-
category=DeprecationWarning,
201+
category=MPRestWarning,
202202
stacklevel=2,
203203
)
204204

@@ -1360,7 +1360,7 @@ def new_str(self) -> str:
13601360

13611361
return (
13621362
f"\033[4m\033[1m{self.__class__.__name__}"
1363-
f"<{self.__class__.__base__.__name__}>\033[0;0m\033[0;0m"
1363+
f"<{orig_rester_name}>\033[0;0m\033[0;0m"
13641364
f"\n{extra}\n\n"
13651365
f"\033[1mFields not requested:\033[0;0m\n{fields_not_requested}"
13661366
)

tests/client/test_client.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,16 @@
4848
# "summary",
4949
] # temp
5050

51-
mpr = MPRester()
52-
5351
# Temporarily ignore molecules resters while molecules query operators are changed
54-
resters_to_test = [
55-
rester
56-
for rester in mpr._all_resters
57-
if (
58-
"molecule" not in rester._class_name.lower()
59-
and not (pmg_alloys is None and "alloys" in str(rester).lower())
60-
)
61-
]
52+
with MPRester() as mpr:
53+
resters_to_test = [
54+
rester
55+
for rester in mpr._all_resters
56+
if (
57+
"molecule" not in rester._class_name.lower()
58+
and not (pmg_alloys is None and "alloys" in str(rester).lower())
59+
)
60+
]
6261

6362

6463
@requires_api_key

tests/client/test_core_client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import pytest
22

3+
import json
4+
35
from mp_api.client import MPRester
46
from mp_api.client.core import BaseRester
7+
from mp_api.client.core.exceptions import MPRestError, MPRestWarning
8+
from mp_api.client.routes.materials.materials import MaterialsRester
59

610
from .conftest import requires_api_key
711

@@ -65,3 +69,28 @@ def test_fields_not_requested_excludes_requested_fields(mpr):
6569
AttributeError, match="data is available but has not been requested in"
6670
):
6771
deser_doc.tags
72+
73+
with pytest.raises(AttributeError, match="object has no attribute 'fake_field'"):
74+
deser_doc.fake_field
75+
76+
assert all(
77+
substr in str(deser_doc)
78+
for substr in ("MPDataDoc", "CoreTaskDoc", "task_id", "Fields not requested")
79+
)
80+
assert all(
81+
substr in deser_doc.__repr__()
82+
for substr in ("MPDataDoc", "CoreTaskDoc", "task_id", "fields_not_requested")
83+
)
84+
assert deser_doc.dict() == deser_doc.model_dump()
85+
assert isinstance(json.dumps(deser_doc.dict()), str)
86+
87+
88+
def test_warnings_exceptions():
89+
with pytest.warns(MPRestWarning, match="Ignoring `monty_decode`"):
90+
MaterialsRester(monty_decode=True)
91+
92+
with pytest.raises(MPRestError, match="Chunk size must be greater than zero"):
93+
MaterialsRester()._get_all_documents({}, chunk_size=-1)
94+
95+
with pytest.raises(MPRestError, match="Number of chunks must be greater than zero"):
96+
MaterialsRester()._get_all_documents({}, num_chunks=-1)

tests/client/test_mprester.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,35 @@ def test_oxygen_evolution_bad_input(self, mpr):
649649
with pytest.raises(ValueError, match="No available insertion electrode data"):
650650
_ = mpr.get_oxygen_evolution("mp-2207", "Al")
651651

652-
def test_monty_decode_warning(self):
652+
def test_warnings_exceptions(self, monkeypatch: pytest.MonkeyPatch):
653+
from mp_api.client.core.settings import MAPI_CLIENT_SETTINGS
654+
653655
with pytest.warns(MPRestWarning, match="Ignoring `monty_decode`"):
654656
MPRester(monty_decode=False)
657+
658+
with MPRester() as mpr:
659+
with pytest.raises(
660+
NotImplementedError,
661+
match="The MPRester\(\).query method has been replaced",
662+
):
663+
mpr.query(some_field=1.0)
664+
665+
with pytest.warns(
666+
MPRestWarning, match="No material found containing task mp-0"
667+
):
668+
assert mpr.get_material_id_from_task_id("mp-0") is None
669+
670+
for attr in mpr._deprecated_attributes:
671+
with pytest.warns(
672+
DeprecationWarning, match="Accessing.*data through MPRester\..*"
673+
):
674+
getattr(mpr, attr, None)
675+
676+
emmet_ver = mpr.get_emmet_version(mpr.endpoint)
677+
monkeypatch.setattr(
678+
MAPI_CLIENT_SETTINGS, "MIN_EMMET_VERSION", f"{emmet_ver.major + 1}.0.0"
679+
)
680+
with pytest.warns(
681+
MPRestWarning, match="The installed version of the mp-api"
682+
):
683+
MPRester()

0 commit comments

Comments
 (0)