Skip to content

Commit 96050ec

Browse files
add test skips on missing packages, restore pourbaix tests
1 parent d1f199d commit 96050ec

File tree

4 files changed

+70
-34
lines changed

4 files changed

+70
-34
lines changed

mp_api/client/mprester.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ def contribs(self):
370370
self._contribs = None
371371
warnings.warn(
372372
"mpcontribs-client not installed. "
373-
"Install the package to query MPContribs data, or construct pourbaix diagrams: "
374-
"'pip install mpcontribs-client'"
373+
"Install the package to query MPContribs data, construct pourbaix diagrams, "
374+
"or to compute cohesive energies: 'pip install mpcontribs-client'"
375375
)
376376
except Exception as error:
377377
self._contribs = None

tests/materials/test_alloys.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
import pytest
44

5+
try:
6+
import pymatgen.analysis.alloys
7+
except ImportError:
8+
pytest.skip(
9+
"Please `pip install pymatgen-analysis-alloys` to use the `materials.alloys` endpoint",
10+
allow_module_level=True,
11+
)
12+
513
from mp_api.client.routes.materials.alloys import AlloysRester
614

715
from ..conftest import client_search_testing, requires_api_key

tests/materials/test_similarity.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
from ..conftest import client_search_testing, requires_api_key
1313

14+
try:
15+
import matminer
16+
except ImportError:
17+
matminer = None
18+
1419

1520
@pytest.fixture(scope="module")
1621
def test_struct():
@@ -52,10 +57,22 @@ def test_similarity_search():
5257
@requires_api_key
5358
def test_similarity_vector_search(test_struct):
5459
rester = SimilarityRester()
55-
fv = rester.fingerprint_structure(test_struct)
56-
assert isinstance(fv, np.ndarray)
57-
assert len(fv) == 122
58-
assert isinstance(rester._fingerprinter, SimilarityScorer)
60+
61+
# skip these tests if `matminer` is not installed
62+
if matminer is not None:
63+
fv = rester.fingerprint_structure(test_struct)
64+
assert isinstance(fv, np.ndarray)
65+
assert len(fv) == 122
66+
assert isinstance(rester._fingerprinter, SimilarityScorer)
67+
68+
assert all(
69+
isinstance(entry, SimilarityEntry)
70+
and isinstance(entry.dissimilarity, float)
71+
for entry in rester.find_similar(
72+
test_struct,
73+
top=2,
74+
)
75+
)
5976

6077
get_top = 5
6178
sim_entries = rester.find_similar("mp-149", top=get_top)
@@ -74,15 +91,7 @@ def test_similarity_vector_search(test_struct):
7491
with pytest.raises(MPRestError, match="No similarity data available for"):
7592
_ = rester.find_similar("mp-0")
7693

77-
assert all(
78-
isinstance(entry, SimilarityEntry) and isinstance(entry.dissimilarity, float)
79-
for entry in rester.find_similar(
80-
test_struct,
81-
top=2,
82-
)
83-
)
84-
8594
with pytest.raises(
8695
MPRestError, match="Please submit a pymatgen Structure or MP ID"
8796
):
88-
_ = rester.find_similar(fv)
97+
_ = rester.find_similar(np.random.rand(122))

tests/test_mprester.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838

3939
from .conftest import requires_api_key
4040

41+
try:
42+
import mpcontribs.client as contribs_client
43+
except ImportError:
44+
contribs_client = None
45+
4146

4247
@pytest.fixture()
4348
def mpr():
@@ -210,22 +215,21 @@ def test_get_entries_in_chemsys(self, mpr):
210215
for e in gibbs_entries:
211216
assert isinstance(e, GibbsComputedStructureEntry)
212217

213-
@pytest.mark.skip(reason="SSL issues")
218+
@pytest.mark.skipif(
219+
contribs_client is None,
220+
reason="`pip install mpcontribs-client` to use pourbaix functionality.",
221+
)
214222
def test_get_pourbaix_entries(self, mpr):
215-
# test input chemsys as a list of elements
216-
pbx_entries = mpr.get_pourbaix_entries(["Fe", "Cr"])
217-
for pbx_entry in pbx_entries:
218-
assert isinstance(pbx_entry, PourbaixEntry)
219-
220-
# test input chemsys as a string
221-
pbx_entries = mpr.get_pourbaix_entries("Fe-Cr")
222-
for pbx_entry in pbx_entries:
223-
assert isinstance(pbx_entry, PourbaixEntry)
224-
225-
# test use_gibbs kwarg
226-
pbx_entries = mpr.get_pourbaix_entries("Li-O", use_gibbs=300)
227-
for pbx_entry in pbx_entries:
228-
assert isinstance(pbx_entry, PourbaixEntry)
223+
# test input chemsys as a list of elements, chemsys, wiith and without kwargs
224+
for chemsys, kwargs in [
225+
[["Fe", "Cr"], {}],
226+
["Fe-Cr", {}],
227+
["Li-O", {"use_gibbs": 300}],
228+
]:
229+
pbx_entries = mpr.get_pourbaix_entries(chemsys, **kwargs)
230+
assert all(
231+
isinstance(pbx_entry, PourbaixEntry) for pbx_entry in pbx_entries
232+
)
229233

230234
# test solid_compat kwarg
231235
with pytest.raises(ValueError, match="Solid compatibility can only be"):
@@ -237,7 +241,7 @@ def test_get_pourbaix_entries(self, mpr):
237241
assert not any(e for e in pbx_entries if "Na" in e.composition)
238242

239243
# Ensure entries are pourbaix compatible
240-
PourbaixDiagram(pbx_entries)
244+
_ = PourbaixDiagram(pbx_entries)
241245

242246
# TODO - old tests copied from pymatgen with specific energy values. Update or delete
243247
# fe_two_plus = [e for e in pbx_entries if e.entry_id == "ion-0"][0]
@@ -251,7 +255,10 @@ def test_get_pourbaix_entries(self, mpr):
251255
# so4_two_minus = pbx_entries[9]
252256
# self.assertAlmostEqual(so4_two_minus.energy, 0.301511, places=3)
253257

254-
@pytest.mark.skip(reason="SSL issues")
258+
@pytest.mark.skipif(
259+
contribs_client is None,
260+
reason="`pip install mpcontribs-client` to use pourbaix functionality.",
261+
)
255262
def test_get_ion_entries(self, mpr):
256263
entries = mpr.get_entries_in_chemsys("Ti-O-H")
257264
pd = PhaseDiagram(entries)
@@ -262,7 +269,7 @@ def test_get_ion_entries(self, mpr):
262269
bi_v_entry_data = mpr.get_ion_reference_data_for_chemsys("Bi-V")
263270
bi_data = mpr.get_ion_reference_data_for_chemsys("Bi")
264271
v_data = mpr.get_ion_reference_data_for_chemsys("V")
265-
assert len(bi_v_entry_data) == len(bi_data) + v_data
272+
assert len(bi_v_entry_data) == len(bi_data + v_data)
266273

267274
# test an incomplete phase diagram
268275
entries = mpr.get_entries_in_chemsys("Ti-O")
@@ -296,7 +303,9 @@ def test_get_ion_entries(self, mpr):
296303
# the ref solid is Na2SO4, ground state mp-4770
297304
# the rf factor correction is necessary to make sure the composition
298305
# of the reference solid is normalized to a single formula unit
299-
ref_solid_entry = [e for e in ion_ref_entries if e.entry_id == "mp-4770"][0]
306+
ref_solid_entry = next(
307+
e for e in ion_ref_entries if e.entry_id.startswith("mp-4770")
308+
)
300309
rf = ref_solid_entry.composition.get_reduced_composition_and_factor()[1]
301310
solid_energy = ion_ref_pd.get_form_energy(ref_solid_entry) / rf
302311

@@ -380,6 +389,8 @@ def test_get_default_api_key_endpoint(self, monkeypatch: pytest.MonkeyPatch):
380389
assert MPRester().endpoint == self.default_endpoint
381390

382391
monkeypatch.delenv("MP_API_KEY", raising=False)
392+
monkeypatch.delenv("PMG_MAPI_KEY", raising=False)
393+
monkeypatch.setitem(SETTINGS, "PMG_MAPI_KEY", None)
383394
with pytest.raises(MPRestError, match="No API key found in request"):
384395
MPRester().get_structure_by_material_id("mp-149")
385396

@@ -405,6 +416,10 @@ def test_get_cohesive_energy_per_atom_utility(self):
405416
composition, toten_per_atom, atomic_energies
406417
) == pytest.approx(by_hand_e_coh)
407418

419+
@pytest.mark.skipif(
420+
contribs_client is None,
421+
reason="`pip install mpcontribs-client` to use cohesive energy functionality.",
422+
)
408423
def test_get_atom_references(self, mpr):
409424
ae = mpr.get_atom_reference_data(funcs=("PBE",))
410425
assert list(ae) == ["PBE"]
@@ -418,6 +433,10 @@ def test_get_atom_references(self, mpr):
418433
isinstance(v, float) for entries in ae.values() for v in entries.values()
419434
)
420435

436+
@pytest.mark.skipif(
437+
contribs_client is None,
438+
reason="`pip install mpcontribs-client` to use cohesive energy functionality.",
439+
)
421440
def test_get_cohesive_energy(self):
422441
ref_e_coh = {
423442
"atom": {

0 commit comments

Comments
 (0)