Skip to content

Commit d2d43f4

Browse files
less golden test data
1 parent 28da883 commit d2d43f4

2 files changed

Lines changed: 50 additions & 44 deletions

File tree

mp_api/client/mprester.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
)
1616
from emmet.core.vasp.calc_types import CalcType
1717
from packaging import version
18-
from pydantic import TypeAdapter
18+
from pydantic import BaseModel, TypeAdapter
1919
from pymatgen.analysis.phase_diagram import PhaseDiagram
2020
from pymatgen.analysis.pourbaix_diagram import IonEntry
2121
from pymatgen.core import Composition, Element, Structure
@@ -317,7 +317,7 @@ def __dir__(self):
317317
)
318318

319319
def __repr__(self) -> str:
320-
return f"MPRester({'v' + self.self.db_version if self.db_version else 'unknown version'})"
320+
return f"MPRester({'v' + self.db_version if self.db_version else 'unknown version'})"
321321

322322
def get_task_ids_associated_with_material_id(
323323
self, material_id: str, calc_types: list[CalcType] | None = None
@@ -645,7 +645,10 @@ def get_entries(
645645
)
646646

647647
for doc in docs:
648-
entry_list = doc.model_dump()["entries"].values()
648+
entry_list = (doc.model_dump() if isinstance(doc, BaseModel) else doc)[
649+
"entries"
650+
].values()
651+
649652
for entry_dict in entry_list:
650653
if not compatible_only:
651654
entry_dict["correction"] = 0.0

tests/client/test_mprester.py

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -191,74 +191,77 @@ def test_get_entry_by_material_id(self, mpr):
191191
assert e[0].composition.reduced_formula == "LiFePO4"
192192

193193
def test_get_entries(self, mpr):
194+
195+
# Avoiding "golden test data": freshly retrieve 5 thermo docs and
196+
# perform entry querying based off those entries
197+
thermo_docs = mpr.materials.thermo.search(
198+
num_chunks=1, chunk_size=5, num_elements=(2, 3)
199+
)
200+
194201
syms = ["Li", "Fe", "O"]
195202
chemsys = "Li-Fe-O"
196203
with pytest.warns(
197204
DeprecationWarning, match="The `inc_structure` argument is deprecated"
198205
):
199-
entries = mpr.get_entries(chemsys, inc_structure=False)
206+
entries = mpr.get_entries(thermo_docs[0].chemsys, inc_structure=False)
200207

201-
elements = {Element(sym) for sym in syms}
202-
for e in entries:
203-
assert isinstance(e, ComputedEntry)
204-
assert set(e.composition.elements).issubset(elements)
208+
assert all(isinstance(e, ComputedEntry) for e in entries)
209+
assert all(
210+
set(e.composition.elements).issubset(thermo_docs[0].elements)
211+
for e in entries
212+
)
205213

206214
# Formula
207-
formula = "SiO2"
208-
entries = mpr.get_entries(formula)
209-
210-
for e in entries:
211-
assert isinstance(e, ComputedEntry)
215+
entries = mpr.get_entries(thermo_docs[1].formula_pretty)
216+
assert all(isinstance(e, ComputedEntry) for e in entries)
212217

213218
# Property data
214-
formula = "BiFeO3"
215-
entries = mpr.get_entries(formula, property_data=["energy_above_hull"])
219+
entries = mpr.get_entries(
220+
thermo_docs[2].formula_pretty, property_data=["energy_above_hull"]
221+
)
216222

217-
for e in entries:
218-
assert e.data.get("energy_above_hull", None) is not None
223+
assert all(e.data.get("energy_above_hull", None) is not None for e in entries)
219224

220225
# Conventional structure
221-
entry = next(
222-
e
223-
for e in mpr.get_entry_by_material_id(
224-
"mp-22526", conventional_unit_cell=True
225-
)
226-
if e.entry_id == "mp-22526-r2SCAN"
226+
as_conv = mpr.get_entry_by_material_id(
227+
thermo_docs[3].material_id, conventional_unit_cell=True
227228
)
228-
229-
s = entry.structure
230-
assert pytest.approx(s.lattice.a) == s.lattice.b
231-
assert pytest.approx(s.lattice.a) != s.lattice.c
232-
assert pytest.approx(s.lattice.alpha) == 90
233-
assert pytest.approx(s.lattice.beta) == 90
234-
assert pytest.approx(s.lattice.gamma) == 120
229+
assert all(e.structure == e.structure.to_conventional() for e in as_conv)
235230

236231
# Ensure energy per atom is same
237-
entry = next(
238-
e
239-
for e in mpr.get_entry_by_material_id(
240-
"mp-22526", conventional_unit_cell=False
232+
non_standardized = mpr.get_entry_by_material_id(
233+
thermo_docs[3].material_id, conventional_unit_cell=False
234+
)
235+
assert all(
236+
e.uncorrected_energy_per_atom
237+
== pytest.approx(
238+
next(
239+
f for f in non_standardized if f.entry_id == e.entry_id
240+
).uncorrected_energy_per_atom
241241
)
242-
if e.entry_id == "mp-22526-r2SCAN"
242+
for e in as_conv
243243
)
244-
s = entry.structure
245-
assert pytest.approx(s.lattice.a) == s.lattice.b
246-
assert pytest.approx(s.lattice.a, abs=1e-3) == s.lattice.c
247-
assert pytest.approx(s.lattice.alpha, abs=1e-3) == s.lattice.beta
248-
assert pytest.approx(s.lattice.alpha, abs=1e-3) == s.lattice.gamma
249244

250245
# Additional criteria
251246
entry = mpr.get_entries(
252-
"mp-149",
253-
additional_criteria={"energy_above_hull": (0.0, 10)},
247+
thermo_docs[4].material_id,
248+
additional_criteria={
249+
"energy_above_hull": (0.0, 2 * thermo_docs[4].energy_above_hull)
250+
},
254251
property_data=["energy_above_hull"],
255252
)[0]
256253

257254
assert "energy_above_hull" in entry.data
258255

256+
# Test out of range
259257
entries = mpr.get_entries(
260-
"mp-149",
261-
additional_criteria={"energy_above_hull": (1, 10)},
258+
thermo_docs[4].material_id,
259+
additional_criteria={
260+
"energy_above_hull": (
261+
1.5 * thermo_docs[4].energy_above_hull,
262+
2 * thermo_docs[4].energy_above_hull,
263+
)
264+
},
262265
property_data=["energy_above_hull"],
263266
)
264267

0 commit comments

Comments
 (0)