Browse Source

Stop two tests leaving a database query running after they end

maziggy 20 hours ago
parent
commit
2022824b29

+ 5 - 0
backend/tests/unit/test_printer_kill_switch.py

@@ -240,6 +240,11 @@ async def test_unauthorized_print_state_is_cleared_when_print_ends(monkeypatch):
     monkeypatch.setattr(main_module.mqtt_relay, "on_printer_status", fake_status)
     monkeypatch.setattr(main_module.mqtt_relay, "on_printer_status", fake_status)
     monkeypatch.setattr(main_module.ws_manager, "send_printer_status", fake_status)
     monkeypatch.setattr(main_module.ws_manager, "send_printer_status", fake_status)
     monkeypatch.setattr(main_module, "_is_bambuddy_authorized_print", unauthorized)
     monkeypatch.setattr(main_module, "_is_bambuddy_authorized_print", unauthorized)
+    # The stop spawns this as a background task that reads the notification
+    # providers through a real session. Left real, the test ends with its
+    # query still running on aiosqlite's thread, which then posts the result
+    # to a closed event loop and warns under whichever test runs next.
+    monkeypatch.setattr(main_module, "_send_kill_switch_provider_notification", AsyncMock(return_value=True))
     monkeypatch.setattr("backend.app.services.finance_budget.is_printer_kill_switch_enabled", kill_switch_enabled)
     monkeypatch.setattr("backend.app.services.finance_budget.is_printer_kill_switch_enabled", kill_switch_enabled)
 
 
     active_state = SimpleNamespace(
     active_state = SimpleNamespace(

+ 5 - 0
backend/tests/unit/test_printer_manager_status_broadcast.py

@@ -295,6 +295,11 @@ class TestEndToEndUnderRunningLoop:
             # Persistence path opens a DB session; stub it out so this
             # Persistence path opens a DB session; stub it out so this
             # stays a pure unit test.
             # stays a pure unit test.
             patch.object(manager, "_persist_awaiting_plate_clear", new_callable=AsyncMock),
             patch.object(manager, "_persist_awaiting_plate_clear", new_callable=AsyncMock),
+            # So does the MQTT/notification emit, through its DB fallback for
+            # a printer with no client. Left real, its query is still running
+            # on aiosqlite's thread when the test's loop closes, and warns
+            # under whichever test runs next.
+            patch.object(manager, "_emit_plate_clear_change", new_callable=AsyncMock),
         ):
         ):
             manager.set_awaiting_plate_clear(7, False)
             manager.set_awaiting_plate_clear(7, False)
             # Yield repeatedly so run_coroutine_threadsafe has a chance
             # Yield repeatedly so run_coroutine_threadsafe has a chance