|
1 | 1 | import json |
2 | 2 | import os |
| 3 | +import tempfile |
3 | 4 | import time |
4 | 5 | from datetime import datetime, timezone |
5 | 6 | from pathlib import Path |
6 | 7 | from unittest.mock import patch |
7 | 8 |
|
| 9 | +import onnx |
8 | 10 | import pytest |
9 | 11 | from dotenv import load_dotenv |
10 | 12 | from PIL import Image |
@@ -80,15 +82,33 @@ def test_engine_offline(tmpdir_factory, mock_wildfire_image, mock_forest_image): |
80 | 82 | assert engine._states["-1"]["last_predictions"][2][4] is False |
81 | 83 |
|
82 | 84 |
|
83 | | -# mock_isfile is a mock of the os.path.isfile() function which allows to simulate file existence |
84 | | -@patch("os.path.isfile") |
85 | | -def test_valid_model_path(mock_isfile): |
| 85 | +def create_dummy_onnx_model(model_path): |
| 86 | + """Creates a small dummy ONNX model.""" |
| 87 | + x = onnx.helper.make_tensor_value_info("input", onnx.TensorProto.FLOAT, [1, 2]) |
| 88 | + y = onnx.helper.make_tensor_value_info("output", onnx.TensorProto.FLOAT, [1, 2]) |
| 89 | + |
| 90 | + node = onnx.helper.make_node("Identity", inputs=["input"], outputs=["output"]) |
| 91 | + graph = onnx.helper.make_graph([node], "dummy_model", [x], [y]) |
| 92 | + model = onnx.helper.make_model(graph) |
| 93 | + |
| 94 | + onnx.save(model, model_path) |
| 95 | + |
| 96 | + |
| 97 | +@pytest.fixture |
| 98 | +def dummy_onnx_file(): |
| 99 | + """Fixture to create a temporary ONNX file.""" |
| 100 | + with tempfile.NamedTemporaryFile(suffix=".onnx", delete=False) as tmpfile: |
| 101 | + create_dummy_onnx_model(tmpfile.name) |
| 102 | + yield tmpfile.name # returns file path |
| 103 | + |
| 104 | + |
| 105 | +def test_valid_model_path(dummy_onnx_file): |
86 | 106 | """Tests Engine instanciation with a valid input model_path""" |
87 | | - mock_isfile.return_value = True # Simulates file existence |
88 | | - instance = Engine(model_path="model.onnx") |
| 107 | + instance = Engine(model_path=dummy_onnx_file) |
89 | 108 | assert instance.format == "onnx" |
90 | 109 |
|
91 | 110 |
|
| 111 | +# mock_isfile is a mock of the os.path.isfile() function which allows to simulate file existence |
92 | 112 | @patch("os.path.isfile") |
93 | 113 | def test_nonexistent_model(mock_isfile): |
94 | 114 | """Tests Engine instanciation with a non-existent input model_path""" |
|
0 commit comments