Skip to content

Commit 244a82f

Browse files
authored
Emmet v0.87.0 compatibility (#1087)
2 parents 64c0ae9 + d59f060 commit 244a82f

47 files changed

Lines changed: 2104 additions & 992 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dev/generate_mcp_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ def regenerate_tools(
5353
from datetime import datetime
5454
from typing import Literal
5555
56+
from emmet.core.band_theory import BSPathType
5657
from emmet.core.chemenv import (
5758
COORDINATION_GEOMETRIES,
5859
COORDINATION_GEOMETRIES_IUCR,
5960
COORDINATION_GEOMETRIES_IUPAC,
6061
COORDINATION_GEOMETRIES_NAMES,
6162
)
62-
from emmet.core.band_theory import BSPathType
6363
from emmet.core.electronic_structure import DOSProjectionType
6464
from emmet.core.grain_boundary import GBTypeEnum
6565
from emmet.core.mpid import MPID

mp_api/_test_utils.py

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from __future__ import annotations
66

7+
from enum import Enum
8+
79
try:
810
import pytest
911
except ImportError as exc:
@@ -86,19 +88,64 @@ def client_search_testing(
8688
assert doc[alt_name_dict.get(param, param)] is not None
8789

8890

89-
def client_pagination(search_method: Callable, id_name: str):
90-
page_1 = search_method(_page=1, chunk_size=NUM_DOCS, fields=[id_name])
91-
page_2 = search_method(_page=2, chunk_size=NUM_DOCS, fields=[id_name])
91+
def client_pagination(
92+
search_method: Callable, id_name: str, additional_fields: list[str] | None = None
93+
) -> None:
94+
"""Test pagination on an endpoint.
95+
96+
Args:
97+
search_method (Callable) : Client search method to use
98+
id_name (str) : the name of a field which uniquely indexes a series of documents
99+
additional_fields (list of str) : Optional other fields to retrieve.
100+
101+
Raises:
102+
AssertionError if pagination does not result in unique sets of documents
103+
"""
104+
fields = [id_name, *(additional_fields or [])]
105+
page_1 = search_method(_page=1, chunk_size=NUM_DOCS, fields=fields)
106+
page_2 = search_method(_page=2, chunk_size=NUM_DOCS, fields=fields)
92107
assert all(len(results) == NUM_DOCS for results in (page_1, page_2))
93108
assert {str(getattr(doc, id_name)) for doc in page_1}.intersection(
94109
{str(getattr(doc, id_name)) for doc in page_2}
95110
) == set()
96111

97112

98-
def client_sort(search_method: Callable, sort_fields: str | Sequence[str]):
113+
def client_sort(
114+
search_method: Callable,
115+
sort_fields: str | Sequence[str],
116+
aux_query: dict[str, Any] | None = None,
117+
default_fields: tuple[str, ...] = ("deprecated", "material_id"),
118+
):
119+
"""Test sorting on an endpoint.
120+
121+
Args:
122+
search_method (Callable) : Client search method to use
123+
sort_fields (str or Sequence of str) : fields to sort on
124+
aux_query (dict) : auxiliary query needed to filter documents
125+
default_fields (list): default fields to return
126+
127+
Raises:
128+
AssertionError if sorting in ascending or descending order does not work.
129+
"""
130+
131+
def _normalize(doc, field: str):
132+
v = getattr(doc, field)
133+
# serialize enums
134+
return v.value if isinstance(v, Enum) else v
135+
136+
user_query = {
137+
k: v
138+
for k, v in (aux_query or {}).items()
139+
if k not in ("_page", "_sort_fields", "chunk_size", "fields")
140+
}
99141
for sort_field in [sort_fields] if isinstance(sort_fields, str) else sort_fields:
142+
100143
asc = search_method(
101-
_page=1, _sort_fields=sort_field, chunk_size=NUM_DOCS, fields=[sort_field]
144+
_page=1,
145+
_sort_fields=sort_field,
146+
chunk_size=NUM_DOCS,
147+
fields=[sort_field, *default_fields],
148+
**user_query,
102149
)
103150
desc = search_method(
104151
_page=1,
@@ -108,12 +155,12 @@ def client_sort(search_method: Callable, sort_fields: str | Sequence[str]):
108155
)
109156

110157
idxs = list(range(NUM_DOCS))
111-
assert sorted(idxs, key=lambda idx: getattr(asc[idx], sort_field)) == idxs
158+
assert sorted(idxs, key=lambda idx: _normalize(asc[idx], sort_field)) == idxs
112159

113160
assert (
114161
sorted(
115162
idxs,
116-
key=lambda idx: getattr(desc[idx], sort_field),
163+
key=lambda idx: _normalize(desc[idx], sort_field),
117164
reverse=True,
118165
)
119166
== idxs

0 commit comments

Comments
 (0)