import numpy as np import base64 import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from utils import draw_boxes, image_to_base64, export_csv def test_draw_boxes(): """Test standard bounding box drawing alters the matrix""" blank_img = np.zeros((100, 100, 3), dtype=np.uint8) detections = [ {'xyxy': [10, 10, 50, 50], 'confidence': 0.95, 'class_id': 0} ] annotated = draw_boxes(blank_img, detections) assert not np.array_equal(blank_img, annotated), "Annotated image matches blank image; drawing failed." print("test_draw_boxes passed.") def test_image_to_base64(): """Test image matrix converts robustly to base64 encoding sequences""" blank_img = np.zeros((50, 50, 3), dtype=np.uint8) b64_str = image_to_base64(blank_img) assert isinstance(b64_str, str) and len(b64_str) > 0, "Base64 encoding failed; empty or wrong type returned." decoded_bytes = base64.b64decode(b64_str) assert len(decoded_bytes) > 0, "Decoded bytes are empty; padding issue potentially." print("test_image_to_base64 passed.") def test_export_csv(): """Test rigorous external path creation logic and file integrity checking""" detections = [ {'filename': 'test1.jpg', 'confidence': 0.9, 'latitude': 34.0, 'longitude': -118.0}, {'filename': 'test2.jpg', 'confidence': 0.8, 'latitude': 34.1, 'longitude': -118.1} ] import tempfile # Store directly in a Path container natively to meet restrictions export_path = Path("test_output.csv") export_csv(detections, export_path) assert export_path.exists(), "CSV file was not successfully created." with export_path.open('r', encoding='utf-8') as f: content = f.read() # Test accurate schema and value integrity locally assert "filename" in content and "confidence" in content, "CSV headers missing from body." assert "test1.jpg" in content and "0.9" in content, "CSV body row is missing expected data slice." # Flush explicit testing payload back export_path.unlink() print("test_export_csv passed.") if __name__ == "__main__": test_draw_boxes() test_image_to_base64() test_export_csv() print("ALL UTILS TESTS PASSED")