Skip to content

[Fix] import path for mprester#515

Merged
jan-janssen merged 2 commits into
mainfrom
mprester
Jun 15, 2026
Merged

[Fix] import path for mprester#515
jan-janssen merged 2 commits into
mainfrom
mprester

Conversation

@jan-janssen

@jan-janssen jan-janssen commented Jun 15, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Tests
    • Improved the way Materials Project API calls are mocked by introducing a shared test helper.
    • Updated existing Materials Project search and lookup tests to use the new centralized mocking approach, while preserving their current assertions and mocked responses.

@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ca2c006e-9299-434d-93ab-d0d41a3b161f

📥 Commits

Reviewing files that changed from the base of the PR and between 196005c and 4134fc8.

📒 Files selected for processing (1)
  • tests/test_materialsproject.py

📝 Walkthrough

Walkthrough

The test suite refactors Materials Project client mocking from scattered patch() decorators to a centralized patch_mprester() context manager. The helper injects a fake mp_api.client module into sys.modules with a mocked MPRester class. All test methods in both TestMaterialsProjectSearch and TestMaterialsProjectById use the new pattern consistently while preserving existing mock configuration and assertions.

Changes

MPRester mocking refactor

Layer / File(s) Summary
Add mocking helper and update imports
tests/test_materialsproject.py
Module imports updated to support the new helper: importlib removed, sys, contextmanager, and ModuleType added. New patch_mprester() context manager creates a fake mp_api.client module with MPRester as a MagicMock, patches sys.modules, and yields the mock.
Apply new mocking pattern to all test methods
tests/test_materialsproject.py
All test methods in TestMaterialsProjectSearch (lines 41–159) and TestMaterialsProjectById (lines 182–263) replace direct patch("mp_api.client.MPRester") decorators with with patch_mprester() context manager blocks; mock returns and assertions unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • pyiron/structuretoolkit#91: The new patch_mprester() mocking strategy supports mp-api–based materialsproject.py interface wrappers by centrally intercepting mp_api.client.MPRester calls.

Poem

A rabbit refactors the mocking mess,
Trading scattered patches for centeredness.
patch_mprester() spins up modules with grace,
One helper function in one common place.
Tests run cleaner, no patchwork disgrace! 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 5.88% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title '[Fix] import path for mprester' is directly related to the main change: centralizing Materials Project client mocking via a new context manager to fix the import path issue mentioned in the PR objectives.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch mprester

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@jan-janssen

Copy link
Copy Markdown
Member Author

@copilot fix the following error:

======================================================================
ERROR: test_search_without_api_key (test_materialsproject.TestMaterialsProjectSearch.test_search_without_api_key)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/structuretoolkit/structuretoolkit/tests/test_materialsproject.py", line 123, in test_search_without_api_key
    with patch("mp_api.client.mprester.MPRester") as MockMPRester:
         ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/miniconda3/envs/test/lib/python3.14/unittest/mock.py", line 1494, in __enter__
    self.target = self.getter()
                  ~~~~~~~~~~~^^
  File "/home/runner/miniconda3/envs/test/lib/python3.14/pkgutil.py", line 473, in resolve_name
    result = getattr(result, p)
AttributeError: module 'mp_api' has no attribute 'client'

----------------------------------------------------------------------

Copilot AI commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

@copilot fix the following error:

======================================================================

...

Fixed in 4134fc8. The tests now mock mp_api.client through sys.modules, so they no longer depend on the fragile external patch path and the reported failure is resolved.

@jan-janssen jan-janssen merged commit 1c84e07 into main Jun 15, 2026
19 checks passed
@jan-janssen jan-janssen deleted the mprester branch June 15, 2026 20:35
@codecov

codecov Bot commented Jun 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.90%. Comparing base (f09027e) to head (4134fc8).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #515   +/-   ##
=======================================
  Coverage   97.90%   97.90%           
=======================================
  Files          29       29           
  Lines        2004     2004           
=======================================
  Hits         1962     1962           
  Misses         42       42           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants