Skip to content

Conversation

vicaya
Copy link

@vicaya vicaya commented Aug 20, 2025

Fix issue #1780: the 2 test_init_successful_connection tests were basically noops due to misuse of MagicMock.

The errors went undetected until python 3.12+ tightened up the MagicMock implementation.

@YuriiMotov YuriiMotov changed the title Fix broken unit tests #1780 ✅ Fix broken unit tests (issue #1780) Sep 3, 2025
Copy link

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

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

LGTM

I suggest removing several lines to simplify tests (I tested it locally - it doesn't break tests).

Currently if you replace session.exec(select(1)) in this line with pass, this assertion will not fail as it's expected.
This PR fixes mocking the Session and after applying it, that test will not only ensure that there was no exception in init, but also that select(1) was actually called.

The same for https://github.com/fastapi/full-stack-fastapi-template/blob/master/backend/app/tests/scripts/test_test_pre_start.py

@vicaya, thank you!
Could you please take a look at my suggested changes?


from sqlmodel import select

import app.tests_pre_start # noqa: F401

Choose a reason for hiding this comment

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

Suggested change
import app.tests_pre_start # noqa: F401

I think this line is not needed


from sqlmodel import select

import app.backend_pre_start # noqa: F401

Choose a reason for hiding this comment

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

Suggested change
import app.backend_pre_start # noqa: F401

I think this line is not needed

exec_mock = MagicMock(return_value=True)
session_mock.configure_mock(**{"exec.return_value": exec_mock})
session_mock.__enter__.return_value = session_mock
session_mock.exec = Mock()

Choose a reason for hiding this comment

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

Suggested change
session_mock.exec = Mock()

I think this line is not needed

exec_mock = MagicMock(return_value=True)
session_mock.configure_mock(**{"exec.return_value": exec_mock})
session_mock.__enter__.return_value = session_mock
session_mock.exec = Mock()

Choose a reason for hiding this comment

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

Suggested change
session_mock.exec = Mock()

I think this line is not needed

@YuriiMotov YuriiMotov changed the title ✅ Fix broken unit tests (issue #1780) ✅ Fix incorrect mocking in unit tests (issue #1780) Sep 3, 2025
@YuriiMotov YuriiMotov linked an issue Sep 3, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Broken unit tests exposed by Python 3.12+
2 participants