Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies = [
"typing-extensions>=3.7.4.1",
"requests>=2.23.0",
"monty>=2024.12.10",
"emmet-core>=0.87.1",
"emmet-core>=0.87.3rc2,<0.87.4",
"boto3",
"orjson >= 3.10,<4",
"pyarrow >= 20.0.0",
Expand All @@ -50,7 +50,7 @@ contribs = [
]
all = [
"custodian",
"emmet-core[all]>=0.87.1",
"emmet-core[all]>=0.87.3rc2,<0.87.4",
"fastmcp",
"flask",
]
Expand Down
24 changes: 13 additions & 11 deletions tests/client/core/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,32 @@
from mp_api.client.core.schemas import _DictLikeAccess, _convert_to_model


class TestClass(_DictLikeAccess):
class DummyClass(_DictLikeAccess):
a: int
b: float
c: list[str]


def test_dict_like_access():
instance = TestClass(a=1, b=2.0, c=["a", "b", "c"])
instance = DummyClass(a=1, b=2.0, c=["a", "b", "c"])
assert isinstance(instance, BaseModel)
assert all(
getattr(instance, field_name) == instance[field_name]
and instance[field_name] == instance.get(field_name)
for field_name in TestClass.model_fields
for field_name in DummyClass.model_fields
)

as_str = """TestClass(
as_str = """DummyClass(
a (int) : 1
b (float) : 2.0
c (list) : ['a', 'b', 'c']
)"""
assert str(instance) == as_str
assert repr(instance) == as_str

with pytest.raises(AttributeError, match="'TestClass' object has no attribute 'd'"):
with pytest.raises(
AttributeError, match="'DummyClass' object has no attribute 'd'"
):
instance.d
assert instance.get("d", None) == None

Expand All @@ -53,7 +55,7 @@ def test_model_generation():
{k: v for k, v in doc.items() if k != "b"} for doc in get_data()
),
}.items():
as_models = _convert_to_model(trial_data, TestClass, model_name=test_type)
as_models = _convert_to_model(trial_data, DummyClass, model_name=test_type)
assert all(isinstance(doc, BaseModel) for doc in as_models)
assert all(doc.__class__.__name__ == test_type for doc in as_models)

Expand All @@ -73,20 +75,20 @@ def test_model_generation():
else:
assert all(
getattr(doc, k) and doc.get(k)
for k in TestClass.model_fields
for k in DummyClass.model_fields
for doc in as_models
)

assert all(
substr in str(doc)
for substr in ("Fields not requested", "TestClass", test_type)
for substr in ("Fields not requested", "DummyClass", test_type)
for doc in as_models
)

# Test requesting unavailable fields
as_models = _convert_to_model(
[{k: v for k, v in doc.items() if k != "b"} for doc in get_data()],
TestClass,
DummyClass,
requested_fields=["b"],
)

Expand All @@ -96,7 +98,7 @@ def test_model_generation():
# Test accessing fields that weren't requested
as_models = _convert_to_model(
[{k: v for k, v in doc.items() if k == "b"} for doc in get_data()],
TestClass,
DummyClass,
requested_fields=["b"],
)
with pytest.raises(
Expand All @@ -105,4 +107,4 @@ def test_model_generation():
as_models[0].a

# Ensure graceful handling of empty iterator input (no docs returned)
assert _convert_to_model(iter([]), TestClass) == []
assert _convert_to_model(iter([]), DummyClass) == []
29 changes: 16 additions & 13 deletions tests/client/materials/test_electrodes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import warnings

import pytest
from pymatgen.core.periodic_table import Element
Expand Down Expand Up @@ -74,19 +75,21 @@ def test_insertion_client(insertion_rester):

@requires_api_key
def test_conversion_client(conversion_rester):
search_method = conversion_rester.search

excl = ConversionElectrodeRester._exclude_search_fields
client_search_testing(
search_method=search_method,
excluded_params=excluded_params + excl,
alt_name_dict=alt_name_dict,
custom_field_tests={
"battery_ids": ["mp-1067_Al"],
"working_ion": Element("Li"),
},
sub_doc_fields=sub_doc_fields,
)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
search_method = conversion_rester.search

excl = ConversionElectrodeRester._exclude_search_fields
client_search_testing(
search_method=search_method,
excluded_params=excluded_params + excl,
alt_name_dict=alt_name_dict,
custom_field_tests={
"battery_ids": ["mp-1067_Al"],
"working_ion": Element("Li"),
},
sub_doc_fields=sub_doc_fields,
)


@requires_api_key
Expand Down
21 changes: 14 additions & 7 deletions tests/client/molecules/test_jcesr.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import warnings

import pytest
from pymatgen.core.periodic_table import Element
Expand All @@ -15,9 +16,11 @@

@pytest.fixture
def rester():
rester = JcesrMoleculesRester()
yield rester
rester.session.close()
with warnings.catch_warnings():
warnings.simplefilter("ignore")
rester = JcesrMoleculesRester()
yield rester
rester.session.close()


excluded_params = [
Expand Down Expand Up @@ -64,8 +67,10 @@ def test_warning():

@requires_api_key
def test_pagination():
with JcesrMoleculesRester() as rester:
client_pagination(rester.search, "task_id")
with warnings.catch_warnings():
warnings.simplefilter("ignore")
with JcesrMoleculesRester() as rester:
client_pagination(rester.search, "task_id")


@requires_api_key
Expand All @@ -76,5 +81,7 @@ def test_pagination():
],
)
def test_sort(sort_field):
with JcesrMoleculesRester() as rester:
client_sort(rester.search, sort_field, default_fields=())
with warnings.catch_warnings():
warnings.simplefilter("ignore")
with JcesrMoleculesRester() as rester:
client_sort(rester.search, sort_field, default_fields=())
15 changes: 9 additions & 6 deletions tests/client/molecules/test_molecules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
point for other resters, therefore we include only basic tests here.
"""

import warnings
from mp_api.client.routes.molecules.molecules import MoleculeRester
from mp_api.client.routes.molecules import MOLECULES_RESTERS


def test_molecule_rester():
with MoleculeRester() as rester:
assert all(sub_rester in dir(rester) for sub_rester in MOLECULES_RESTERS)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
with MoleculeRester() as rester:
assert all(sub_rester in dir(rester) for sub_rester in MOLECULES_RESTERS)

assert all(
getattr(rester, k)._class_name == lazy_obj._class_name
for k, lazy_obj in MOLECULES_RESTERS.items()
)
assert all(
getattr(rester, k)._class_name == lazy_obj._class_name
for k, lazy_obj in MOLECULES_RESTERS.items()
)
Loading
Loading