-
Notifications
You must be signed in to change notification settings - Fork 1
Add mock unit tests for Materials Project integration #477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: mp-api
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import unittest | ||
| from unittest.mock import MagicMock, patch | ||
| import sys | ||
|
|
||
| # Mock mp_api before importing structuretoolkit | ||
| mock_mp_api = MagicMock() | ||
| sys.modules["mp_api"] = mock_mp_api | ||
| sys.modules["mp_api.client"] = mock_mp_api.client | ||
|
|
||
| import structuretoolkit as stk | ||
|
|
||
| class TestMaterialsProject(unittest.TestCase): | ||
| @patch("mp_api.client.MPRester") | ||
| @patch("structuretoolkit.build.materialsproject.pymatgen_to_ase") | ||
| def test_search(self, mock_pymatgen_to_ase, mock_mp_rester): | ||
| # Setup mock for MPRester as a context manager | ||
| mock_mpr = MagicMock() | ||
| mock_mp_rester.return_value.__enter__.return_value = mock_mpr | ||
|
|
||
| # Setup mock for summary.search | ||
| mock_mpr.summary.search.return_value = [ | ||
| {"material_id": "mp-1", "structure": "mock_pmg_struct"} | ||
| ] | ||
|
|
||
| # Setup mock for pymatgen_to_ase | ||
| mock_pymatgen_to_ase.return_value = "mock_ase_struct" | ||
|
|
||
| # Call search | ||
| results = list(stk.build.materialsproject_search("Fe")) | ||
|
|
||
| # Assertions | ||
| self.assertEqual(len(results), 1) | ||
| self.assertEqual(results[0]["material_id"], "mp-1") | ||
| self.assertEqual(results[0]["structure"], "mock_ase_struct") | ||
| mock_mpr.summary.search.assert_called_once() | ||
| mock_pymatgen_to_ase.assert_called_once_with("mock_pmg_struct") | ||
|
Comment on lines
+13
to
+36
|
||
|
|
||
| @patch("mp_api.client.MPRester") | ||
| @patch("structuretoolkit.build.materialsproject.pymatgen_to_ase") | ||
| def test_by_id(self, mock_pymatgen_to_ase, mock_mp_rester): | ||
| # Setup mock for MPRester as a context manager | ||
| mock_mpr = MagicMock() | ||
| mock_mp_rester.return_value.__enter__.return_value = mock_mpr | ||
|
|
||
| # Setup mock for pymatgen_to_ase | ||
| mock_pymatgen_to_ase.side_effect = lambda x: f"ase_{x}" | ||
|
|
||
| # Test final=True | ||
| mock_mpr.get_structure_by_material_id.return_value = "pmg_struct" | ||
| res = stk.build.materialsproject_by_id("mp-1", final=True) | ||
| self.assertEqual(res, "ase_pmg_struct") | ||
| mock_mpr.get_structure_by_material_id.assert_called_with( | ||
| material_id="mp-1", final=True, conventional_unit_cell=False | ||
| ) | ||
|
|
||
| # Test final=False | ||
| mock_mpr.get_structure_by_material_id.return_value = ["pmg_1", "pmg_2"] | ||
| res = stk.build.materialsproject_by_id("mp-1", final=False) | ||
| self.assertEqual(res, ["ase_pmg_1", "ase_pmg_2"]) | ||
| mock_mpr.get_structure_by_material_id.assert_called_with( | ||
| material_id="mp-1", final=False, conventional_unit_cell=False | ||
| ) | ||
|
|
||
|
Comment on lines
+38
to
+63
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test module permanently overwrites
sys.modules["mp_api"]/sys.modules["mp_api.client"]at import time and never restores them. This can leak into other tests in the same process and also masks behavior when the realmp_apipackage is installed. Consider scoping this to the tests (e.g.,patch.dict(sys.modules, ...)insetUp/tearDownor a context manager) and restoring the originalsys.modulesentries afterward.