|
| 1 | +"""Unit tests for model loading robustness""" |
| 2 | +import os |
| 3 | +import tempfile |
| 4 | +import pytest |
| 5 | +import torch |
| 6 | +from PIL import Image |
| 7 | + |
| 8 | +from perceptionmetrics.models.torch_detection import TorchImageDetectionModel |
| 9 | +from perceptionmetrics.models.torch_segmentation import TorchImageSegmentationModel |
| 10 | + |
| 11 | + |
| 12 | +class TestModelLoadingExceptions: |
| 13 | + """Test that model loading raises appropriate exceptions""" |
| 14 | + |
| 15 | + @pytest.fixture |
| 16 | + def temp_files(self): |
| 17 | + """Create temporary test files""" |
| 18 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 19 | + # Create dummy model file (corrupted) |
| 20 | + bad_model = os.path.join(tmpdir, "bad_model.pt") |
| 21 | + with open(bad_model, "w") as f: |
| 22 | + f.write("this is not a pytorch model") |
| 23 | + |
| 24 | + # Create dummy ontology |
| 25 | + ontology = os.path.join(tmpdir, "ontology.json") |
| 26 | + import json |
| 27 | + with open(ontology, "w") as f: |
| 28 | + json.dump({ |
| 29 | + "car": {"idx": 0, "rgb": [0, 0, 0]}, |
| 30 | + "person": {"idx": 1, "rgb": [255, 0, 0]}, |
| 31 | + }, f) |
| 32 | + |
| 33 | + # Create dummy config |
| 34 | + config = os.path.join(tmpdir, "config.json") |
| 35 | + with open(config, "w") as f: |
| 36 | + json.dump({ |
| 37 | + "resize": {"width": 512, "height": 512}, |
| 38 | + "normalization": { |
| 39 | + "mean": [0.485, 0.456, 0.406], |
| 40 | + "std": [0.229, 0.224, 0.225] |
| 41 | + }, |
| 42 | + "batch_size": 1, |
| 43 | + "model_format": "torchvision" |
| 44 | + }, f) |
| 45 | + |
| 46 | + yield { |
| 47 | + "bad_model": bad_model, |
| 48 | + "ontology": ontology, |
| 49 | + "config": config, |
| 50 | + "tmpdir": tmpdir |
| 51 | + } |
| 52 | + |
| 53 | + def test_detection_model_bad_file_raises_specific_error(self, temp_files): |
| 54 | + """Test that loading corrupted model raises RuntimeError, not generic Exception""" |
| 55 | + with pytest.raises(RuntimeError) as exc_info: |
| 56 | + TorchImageDetectionModel( |
| 57 | + model=temp_files["bad_model"], |
| 58 | + model_cfg=temp_files["config"], |
| 59 | + ontology_fname=temp_files["ontology"] |
| 60 | + ) |
| 61 | + |
| 62 | + # Check error message is informative |
| 63 | + error_msg = str(exc_info.value) |
| 64 | + assert "Failed to load model" in error_msg |
| 65 | + assert "TorchScript error" in error_msg or "PyTorch error" in error_msg |
| 66 | + |
| 67 | + def test_segmentation_model_bad_file_raises_specific_error(self, temp_files): |
| 68 | + """Test that loading corrupted segmentation model raises RuntimeError""" |
| 69 | + with pytest.raises(RuntimeError) as exc_info: |
| 70 | + TorchImageSegmentationModel( |
| 71 | + model=temp_files["bad_model"], |
| 72 | + model_cfg=temp_files["config"], |
| 73 | + ontology_fname=temp_files["ontology"] |
| 74 | + ) |
| 75 | + |
| 76 | + error_msg = str(exc_info.value) |
| 77 | + assert "Failed to load model" in error_msg |
| 78 | + |
| 79 | + def test_detection_model_missing_file_raises_file_not_found(self, temp_files): |
| 80 | + """Test that missing model file raises FileNotFoundError""" |
| 81 | + with pytest.raises(FileNotFoundError) as exc_info: |
| 82 | + TorchImageDetectionModel( |
| 83 | + model="/nonexistent/path/model.pt", |
| 84 | + model_cfg=temp_files["config"], |
| 85 | + ontology_fname=temp_files["ontology"] |
| 86 | + ) |
| 87 | + |
| 88 | + assert "Model file not found" in str(exc_info.value) |
| 89 | + |
| 90 | + def test_segmentation_model_missing_file_raises_file_not_found(self, temp_files): |
| 91 | + """Test that missing segmentation model file raises FileNotFoundError""" |
| 92 | + with pytest.raises(FileNotFoundError) as exc_info: |
| 93 | + TorchImageSegmentationModel( |
| 94 | + model="/nonexistent/path/model.pt", |
| 95 | + model_cfg=temp_files["config"], |
| 96 | + ontology_fname=temp_files["ontology"] |
| 97 | + ) |
| 98 | + |
| 99 | + assert "Model file not found" in str(exc_info.value) |
0 commit comments