Skip to content

Fix #481: make tests of Pubchem more robust#1219

Open
mhucka wants to merge 8 commits intomainfrom
mh-revised-fix-481
Open

Fix #481: make tests of Pubchem more robust#1219
mhucka wants to merge 8 commits intomainfrom
mh-revised-fix-481

Conversation

@mhucka
Copy link
Contributor

@mhucka mhucka commented Mar 12, 2026

This implements a recommendation from Pavol Juhas on PR #1113 about using mocking to avoid flaky test behavior in pubchem_test.py.

In order to avoid failures in CI, this follows a [recommendation from
Pavol Juhas on PR #1113](
#1113 (review))
about using mocking to avoid flaky test behavior in `pubchem_test.py`.
@mhucka mhucka force-pushed the mh-revised-fix-481 branch from 1b9ad4c to 70ddda8 Compare March 12, 2026 22:25
@mhucka mhucka added the area/dependencies Involves packages or other software that qsim depends on label Mar 13, 2026
@mhucka mhucka self-assigned this Mar 13, 2026
@mhucka mhucka linked an issue Mar 13, 2026 that may be closed by this pull request
@mhucka mhucka added the area/health Involves code and/or project health label Mar 13, 2026
@mhucka mhucka requested a review from pavoljuhas March 13, 2026 21:21
@mhucka mhucka changed the title Fix #481: add mocking of Pubchem network calls Update pubchem interface and fix #481 Mar 13, 2026
@mhucka
Copy link
Contributor Author

mhucka commented Mar 13, 2026

/gemini review

@mhucka mhucka enabled auto-merge March 13, 2026 22:15
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the PubChem interface to be compatible with a new version of the pubchempy library and improves the test suite by using mocking to avoid flaky tests. The changes are well-implemented. I've added a few suggestions to further improve the robustness of the API call retry mechanism and the structure of the test suite.

@mhucka mhucka marked this pull request as draft March 14, 2026 02:13
auto-merge was automatically disabled March 14, 2026 02:13

Pull request was converted to draft

@mhucka mhucka force-pushed the mh-revised-fix-481 branch from dda1837 to 1b8114b Compare March 14, 2026 04:02
@mhucka mhucka marked this pull request as ready for review March 14, 2026 04:42
@mhucka mhucka enabled auto-merge March 14, 2026 04:42
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the tests for the PubChem interface to use mocking, which resolves flakiness by avoiding calls to the live API in unit tests. It also introduces a new integration test for the live API, complete with a retry mechanism to improve robustness. The changes are well-structured and the new tests are thorough. I've identified some redundant try-except ImportError blocks in the new test methods. Since the test class is already skipped if pubchempy is not available, these blocks are unnecessary and can be removed or simplified for better code clarity.

mhucka added 4 commits March 14, 2026 05:26
Gemini Code Assist is right; they're not needed.
This is test code. This just doesn't seem worth doing more.
@mhucka mhucka changed the title Update pubchem interface and fix #481 Update Pubchem interface and fix #481 Mar 16, 2026
@mhucka mhucka changed the title Update Pubchem interface and fix #481 Fix #481: make tests of Pubchem more robust Mar 16, 2026


def mock_get_compounds(name, searchtype, record_type='2d'):
if name == 'water' and record_type == '3d':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider rewriting with match statement.


@using_pubchempy
class OpenFermionPubChemTest(unittest.TestCase):
def _get_geometry_with_retries(self, name):
Copy link
Contributor

@pavoljuhas pavoljuhas Mar 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only used in test_geometry_from_pubchem_live_api which is deselected by default. I suggest to delete it and call geometry_from_pubchem directly.

If the live service proves to be a problem for the CI test, I suggest to add the pytest-retry plugin and decorate test_geometry_from_pubchem_live_api with its flaky mark.


@patch('time.sleep', return_value=None)
@patch('pubchempy.get_compounds')
def test_geometry_from_pubchem_retry_success(self, mock_get_compounds, mock_sleep):
Copy link
Contributor

@pavoljuhas pavoljuhas Mar 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is testing a test helper _get_geometry_with_retries, which is itself unnecessary per comment above. Tests should be only covering the public API (possibly protected APIs in exceptional cases), but testing a test code should be avoided. Please delete.


@patch('time.sleep', return_value=None)
@patch('pubchempy.get_compounds')
def test_geometry_from_pubchem_retry_failure(self, mock_get_compounds, mock_sleep):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, please delete.

Copy link
Contributor

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now let us avoid implementing retries for a test that is not selected.

If it is a problem later we can use the pytest-retry plugin for that one test instead.

Otherwise LGTM.

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

Labels

area/dependencies Involves packages or other software that qsim depends on area/health Involves code and/or project health

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tests flaky due to dependency on network

2 participants