From 4d9edd7fd4e1766e87de5ec5f4ed4009d974eb14 Mon Sep 17 00:00:00 2001 From: counterweight Date: Sun, 21 Dec 2025 23:29:48 +0100 Subject: [PATCH] refactor: make mock_enqueue_job fixture opt-in instead of autouse Tests that call POST /api/counter/increment now explicitly request the mock_enqueue_job fixture. This prevents the mock from masking issues in other tests that don't need it. --- backend/tests/conftest.py | 6 ++++-- backend/tests/test_counter.py | 8 ++++---- backend/tests/test_permissions.py | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 5e76365..71d8df9 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -241,13 +241,15 @@ async def user_no_roles(client_factory): } -@pytest.fixture(autouse=True) +@pytest.fixture def mock_enqueue_job(): - """Mock job enqueueing for all tests. + """Mock job enqueueing for tests that hit the counter increment endpoint. pgqueuer requires PostgreSQL-specific features that aren't available in the test database setup. We mock the enqueue function to avoid connection issues while still testing the counter logic. + + Tests that call POST /api/counter/increment must use this fixture. """ mock = AsyncMock(return_value=1) # Return a fake job ID with patch("routes.counter.enqueue_random_number_job", mock): diff --git a/backend/tests/test_counter.py b/backend/tests/test_counter.py index 998142f..5ff5efa 100644 --- a/backend/tests/test_counter.py +++ b/backend/tests/test_counter.py @@ -97,7 +97,7 @@ async def test_get_counter_authenticated(client_factory): @pytest.mark.asyncio -async def test_increment_counter(client_factory): +async def test_increment_counter(client_factory, mock_enqueue_job): async with client_factory.get_db_session() as db: godfather = await create_user_with_roles( db, unique_email("gf"), "pass123", [ROLE_REGULAR] @@ -126,7 +126,7 @@ async def test_increment_counter(client_factory): @pytest.mark.asyncio -async def test_increment_counter_multiple(client_factory): +async def test_increment_counter_multiple(client_factory, mock_enqueue_job): async with client_factory.get_db_session() as db: godfather = await create_user_with_roles( db, unique_email("gf"), "pass123", [ROLE_REGULAR] @@ -157,7 +157,7 @@ async def test_increment_counter_multiple(client_factory): @pytest.mark.asyncio -async def test_get_counter_after_increment(client_factory): +async def test_get_counter_after_increment(client_factory, mock_enqueue_job): async with client_factory.get_db_session() as db: godfather = await create_user_with_roles( db, unique_email("gf"), "pass123", [ROLE_REGULAR] @@ -187,7 +187,7 @@ async def test_get_counter_after_increment(client_factory): # Counter is shared between users @pytest.mark.asyncio -async def test_counter_shared_between_users(client_factory): +async def test_counter_shared_between_users(client_factory, mock_enqueue_job): # Create godfather and invites for two users async with client_factory.get_db_session() as db: godfather = await create_user_with_roles( diff --git a/backend/tests/test_permissions.py b/backend/tests/test_permissions.py index 0180792..2f514f8 100644 --- a/backend/tests/test_permissions.py +++ b/backend/tests/test_permissions.py @@ -104,7 +104,7 @@ class TestCounterAccess: @pytest.mark.asyncio async def test_regular_user_can_increment_counter( - self, client_factory, regular_user + self, client_factory, regular_user, mock_enqueue_job ): async with client_factory.create(cookies=regular_user["cookies"]) as client: response = await client.post("/api/counter/increment") @@ -484,7 +484,7 @@ class TestAuditRecords: @pytest.mark.asyncio async def test_counter_increment_creates_audit_record( - self, client_factory, regular_user, admin_user + self, client_factory, regular_user, admin_user, mock_enqueue_job ): """Verify that counter increments are recorded and visible in audit.""" # Regular user increments counter