Skip to content

Commit

Permalink
also test default and invalid api key
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Sep 28, 2024
1 parent b3ff75a commit 434ed19
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions tests/test_mprester.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from pymatgen.phonon.dos import PhononDos

from mp_api.client import MPRester
from mp_api.client.core.client import MPRestError
from mp_api.client.core.settings import MAPIClientSettings


Expand Down Expand Up @@ -352,14 +353,21 @@ def test_get_api_key_from_settings(self, monkeypatch: pytest.MonkeyPatch):
get "PMG_MAPI_KEY" from "SETTINGS".
"""
monkeypatch.delenv("MP_API_KEY", raising=False)
assert os.environ.get("MP_API_KEY") is None

# patch pymatgen.core.SETTINGS to contain PMG_MAPI_KEY
monkeypatch.setitem(SETTINGS, "PMG_MAPI_KEY", self.fake_mp_api_key)

assert MPRester().api_key == self.fake_mp_api_key

def test_get_default_endpoint(self, monkeypatch: pytest.MonkeyPatch):
def test_get_default_endpoint_api_key(self, monkeypatch: pytest.MonkeyPatch):
monkeypatch.delenv("MP_API_ENDPOINT", raising=False)
assert os.environ.get("MP_API_ENDPOINT") is None
assert MPRester().endpoint == self.default_endpoint

monkeypatch.delenv("MP_API_KEY", raising=False)
with pytest.raises(MPRestError, match="No API key found in request"):
MPRester().get_structure_by_material_id("mp-149")

def test_invalid_api_key(self, monkeypatch):
monkeypatch.setenv("MP_API_KEY", "INVALID")
with pytest.raises(ValueError, match="Keys for the new API are 32 characters"):
MPRester().get_structure_by_material_id("mp-149")

0 comments on commit 434ed19

Please sign in to comment.