Skip to content

Commit 2f266b9

Browse files
add utils tests and remove legacy id validation
1 parent 438878e commit 2f266b9

2 files changed

Lines changed: 35 additions & 24 deletions

File tree

mp_api/client/core/utils.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,7 @@ def load_json(json_like: str | bytes, deser: bool = False, encoding: str = "utf-
4444
return MontyDecoder().process_decoded(data) if deser else data
4545

4646

47-
def _legacy_id_validation(id_list: list[str]) -> list[str]:
48-
"""Legacy utility to validate IDs, pre-AlphaID transition.
49-
50-
This function is temporarily maintained to allow for
51-
backwards compatibility with older versions of emmet, and will
52-
not be preserved.
53-
"""
54-
pattern = "(mp|mvc|mol|mpcule)-.*"
55-
if malformed_ids := {
56-
entry for entry in id_list if re.match(pattern, entry) is None
57-
}:
58-
raise ValueError(
59-
f"{'Entry' if len(malformed_ids) == 1 else 'Entries'}"
60-
f" {', '.join(malformed_ids)}"
61-
f"{'is' if len(malformed_ids) == 1 else 'are'} not formatted correctly!"
62-
)
63-
64-
return id_list
65-
66-
67-
def validate_ids(id_list: list[str]):
47+
def validate_ids(id_list: list[str]) -> list[str]:
6848
"""Function to validate material and task IDs.
6949
7050
Args:
@@ -85,6 +65,4 @@ def validate_ids(id_list: list[str]):
8565
# TODO: after the transition to AlphaID in the document models,
8666
# The following line should be changed to
8767
# return [validate_identifier(idx,serialize=True) for idx in id_list]
88-
if validate_identifier:
89-
return [str(validate_identifier(idx)) for idx in id_list]
90-
return _legacy_id_validation(id_list)
68+
return [validate_identifier(idx).string for idx in id_list]

tests/core/test_utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Test client core utilities."""
2+
3+
import pytest
4+
5+
from emmet.core.mpid import MPID, AlphaID
6+
7+
from mp_api.client.core.utils import validate_ids
8+
from mp_api.client.core.settings import MAPIClientSettings
9+
10+
def test_id_validation():
11+
max_num_idxs = MAPIClientSettings().MAX_LIST_LENGTH
12+
13+
14+
with pytest.raises(ValueError,match="too long"):
15+
_ = validate_ids(
16+
[f"mp-{x}" for x in range(max_num_idxs + 1)]
17+
)
18+
19+
# For all legacy MPIDs, ensure these validate correctly
20+
assert all(
21+
isinstance(x,str) and MPID(x).string == x
22+
for x in validate_ids(
23+
[f"mp-{y}" for y in range(max_num_idxs)]
24+
)
25+
)
26+
27+
# For all new AlphaIDs, ensure these validate correctly
28+
assert all(
29+
isinstance(x,str) and AlphaID(x).string == x
30+
for x in validate_ids(
31+
[y + AlphaID._cut_point for y in range(max_num_idxs)]
32+
)
33+
)

0 commit comments

Comments
 (0)