From 123ad836af941da01802bb6ea3faa6a01995185f Mon Sep 17 00:00:00 2001 From: CleverThis Date: Tue, 5 May 2026 01:54:59 +0000 Subject: [PATCH] fix(test): fix ResourceRepository test mock exhaustion on retry The @database_retry decorator retries on DatabaseError up to 3 times. The ResourceRepository test used a side_effect list with only 2 items for session.query().filter_by().first(), which was exhausted on the second retry attempt, causing StopIteration to propagate unexpectedly. Fix: replace the side_effect list with a side_effect function that dispatches based on the model class being queried, ensuring the correct mock value is returned on every retry attempt. --- ...a_integrity_session_rollback_7489_steps.py | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/features/steps/tdd_data_integrity_session_rollback_7489_steps.py b/features/steps/tdd_data_integrity_session_rollback_7489_steps.py index af663a5b7..9c41547d7 100644 --- a/features/steps/tdd_data_integrity_session_rollback_7489_steps.py +++ b/features/steps/tdd_data_integrity_session_rollback_7489_steps.py @@ -268,11 +268,23 @@ def step_create_resource(context: Context) -> None: # Configure the session to return a resource type row (so type validation passes) type_row_mock = MagicMock() type_row_mock.name = "test/resource-type" - query_mock = MagicMock() - # First query (for resource type) returns the type row - # Second query (for existing resource) returns None - query_mock.filter_by.return_value.first.side_effect = [type_row_mock, None] - context.shared_session.query.return_value = query_mock + + # Use a side_effect function on session.query so that each call returns + # the correct mock regardless of how many times the @database_retry + # decorator retries the operation. + from cleveragents.infrastructure.database.models import ResourceTypeModel + + def query_side_effect(model_class: Any) -> MagicMock: + q = MagicMock() + if model_class is ResourceTypeModel: + # Type-lookup query: always return the type row + q.filter_by.return_value.first.return_value = type_row_mock + else: + # Duplicate-check query: always return None (no existing resource) + q.filter_by.return_value.first.return_value = None + return q + + context.shared_session.query.side_effect = query_side_effect try: context.repo_under_test.create(_make_fake_resource()) except Exception as exc: