Skip to content

Commit a42f8d9

Browse files
committed
Import module for test only after mocking out the functions used to generate the module-level variables
1 parent 83dfb75 commit a42f8d9

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

tests/server/test_feedback.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
from unittest import mock
21
from unittest.mock import MagicMock
32

43
import pytest
54
from backports.entry_points_selectable import entry_points
65
from pytest_mock import MockerFixture
76

8-
from murfey.server.feedback import feedback_callback
9-
107
feedback_callback_params_matrix = (
118
# Murfey workflows currently present in pyproject.toml
129
("atlas_update",),
@@ -37,27 +34,27 @@ def test_feedback_callback(
3734
# Unpack test params
3835
(entry_point_name,) = test_params
3936

40-
# Mock the Murfey database creation in the main body of the module
41-
mock_get_security_config = mocker.patch(
42-
"murfey.server.feedback.get_security_config"
43-
)
37+
# Patch the functions used to generate the module-level variables
38+
mock_get_security_config = mocker.patch("murfey.util.config.get_security_config")
4439
mock_get_security_config.return_value = MagicMock()
45-
mock_url = mocker.patch("murfey.server.feedback.url")
46-
mock_url.return_value = mock.sentinel
47-
mock_create_engine = mocker.patch("murfey.server.feedback.create_engine")
40+
mock_url = mocker.patch("murfey.server.murfey_db.url")
41+
mock_url.return_value = MagicMock()
42+
mock_create_engine = mocker.patch("sqlmodel.create_engine")
4843
mock_create_engine.return_value = MagicMock()
4944
mock_murfey_db = MagicMock()
50-
mock_sql_session = mocker.patch("murfey.server.feedback.Session")
45+
mock_sql_session = mocker.patch("sqlmodel.Session")
5146
mock_sql_session.return_value = mock_murfey_db
5247

5348
# Load the entry point and patch the executable it calls
5449
eps = list(entry_points().select(group="murfey.workflows", name=entry_point_name))
5550
assert len(eps) == 1 # Entry point should be present and unique
5651
mock_function = mocker.patch(eps[0].value.replace(":", "."))
5752

58-
header = {"dummy": "dummy"}
59-
message = {"register": entry_point_name}
53+
# Initialise after mocking
54+
from murfey.server.feedback import feedback_callback
6055

6156
# Run the function and check that it calls the entry point correctly
57+
header = {"dummy": "dummy"}
58+
message = {"register": entry_point_name}
6259
feedback_callback(header, message, mock_murfey_db)
6360
mock_function.assert_called_once_with(message=message, murfey_db=mock_murfey_db)

0 commit comments

Comments
 (0)