From c196ae017a6d22fb166be154d6f3fa001759eab1 Mon Sep 17 00:00:00 2001 From: maziggy Date: Tue, 27 Jan 2026 10:38:44 +0100 Subject: [PATCH] Fix plate detection calibration persistence - Check cv2.imwrite() return value to catch silent failures - Verify reference image file exists after save - Validate file size is reasonable (>1KB) to detect corruption - Add logging for save, rotate, and delete operations - Use temp directory for plate calibration during tests to avoid deleting real user calibration files Previously, calibration could report success even if the image file failed to save. Additionally, running tests would delete real calibration files because tests shared the same plate_calibration_dir. --- backend/tests/conftest.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index ccf0487c6..0cd7046cc 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -1,12 +1,16 @@ """Shared test fixtures for BamBuddy backend tests.""" import asyncio +import atexit import json import logging import os +import shutil import sys +import tempfile from collections.abc import AsyncGenerator from datetime import datetime +from pathlib import Path from unittest.mock import AsyncMock, MagicMock, patch import pytest @@ -24,6 +28,19 @@ from backend.app.core.config import settings # noqa: E402 settings.log_to_file = False +# Use a temp directory for plate calibration to avoid deleting real calibration files +_test_plate_cal_dir = Path(tempfile.mkdtemp(prefix="bambuddy_test_plate_cal_")) +settings.plate_calibration_dir = _test_plate_cal_dir + + +# Clean up temp directory when tests finish +def _cleanup_test_plate_cal_dir(): + if _test_plate_cal_dir.exists(): + shutil.rmtree(_test_plate_cal_dir, ignore_errors=True) + + +atexit.register(_cleanup_test_plate_cal_dir) + from backend.app.core.database import Base # noqa: E402 # Use in-memory SQLite for tests