Skip to content

Commit 726b3d6

Browse files
committed
fixed test
1 parent 00edca6 commit 726b3d6

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

services/efs-guardian/tests/unit/conftest.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ async def client(app: FastAPI) -> AsyncIterator[httpx.AsyncClient]:
125125
yield client
126126

127127

128+
@pytest.fixture
129+
def with_disabled_background_tasks(mocker: MockerFixture) -> None:
130+
mocker.patch("simcore_service_efs_guardian.core.application.setup_background_tasks")
131+
132+
128133
@pytest.fixture
129134
def with_disabled_redis_and_background_tasks(mocker: MockerFixture) -> None:
130135
mocker.patch("simcore_service_efs_guardian.core.application.setup_redis")
@@ -137,7 +142,7 @@ def with_disabled_redis_and_background_tasks(mocker: MockerFixture) -> None:
137142

138143

139144
@pytest.fixture
140-
async def efs_cleanup(app: FastAPI):
145+
async def efs_cleanup(app: FastAPI) -> AsyncIterator[None]:
141146
yield
142147

143148
aws_efs_settings: AwsEfsSettings = app.state.settings.EFS_GUARDIAN_AWS_EFS_SETTINGS

services/efs-guardian/tests/unit/test_efs_removal_policy_task.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,28 @@ async def project_in_db(
102102
yield row
103103

104104

105+
# Create a mock object manually
106+
mock_with_locked_project = MagicMock()
107+
108+
109+
# The stand-in decorator to replace the original one and record the function call
110+
def mock_decorator(*args, **kwargs):
111+
def _decorator(func):
112+
def wrapper(*args, **kwargs):
113+
mock_with_locked_project(*args, **kwargs) # Log the call
114+
return func(*args, **kwargs)
115+
116+
return wrapper
117+
118+
return _decorator
119+
120+
105121
@patch("simcore_service_efs_guardian.services.background_tasks.get_redis_lock_client")
106-
@patch("simcore_service_efs_guardian.services.background_tasks.lock_project")
122+
@patch(
123+
"simcore_service_efs_guardian.services.background_tasks.with_locked_project",
124+
new=mock_decorator,
125+
)
107126
async def test_efs_removal_policy_task(
108-
mock_lock_project: MagicMock,
109127
mock_get_redis_lock_client: MagicMock,
110128
faker: Faker,
111129
app: FastAPI,
@@ -116,7 +134,7 @@ async def test_efs_removal_policy_task(
116134
):
117135
# 1. Nothing should happen
118136
await removal_policy_task(app)
119-
assert not mock_lock_project.called
137+
assert not mock_with_locked_project.called
120138

121139
# 2. Lets create some project with data
122140
aws_efs_settings: AwsEfsSettings = app.state.settings.EFS_GUARDIAN_AWS_EFS_SETTINGS
@@ -148,7 +166,7 @@ async def test_efs_removal_policy_task(
148166

149167
# 3. Nothing should happen
150168
await removal_policy_task(app)
151-
assert not mock_lock_project.called
169+
assert not mock_with_locked_project.called
152170

153171
# 4. We will artifically change the project last change date
154172
app_settings: ApplicationSettings = app.state.settings
@@ -169,7 +187,7 @@ async def test_efs_removal_policy_task(
169187

170188
# 5. Now removal policy should remove those data
171189
await removal_policy_task(app)
172-
assert mock_lock_project.assert_called_once
190+
assert mock_with_locked_project.assert_called_once
173191
assert mock_get_redis_lock_client.assert_called_once
174192
projects_list = await efs_manager.list_projects_across_whole_efs()
175193
assert projects_list == []

0 commit comments

Comments
 (0)