Skip to content

Commit 0ac1f13

Browse files
committed
Add tests on model_path with dummy onnx models
1 parent bf77ee3 commit 0ac1f13

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

tests/test_engine.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import json
22
import os
3+
import tempfile
34
import time
45
from datetime import datetime, timezone
56
from pathlib import Path
67
from unittest.mock import patch
78

9+
import onnx
810
import pytest
911
from dotenv import load_dotenv
1012
from PIL import Image
@@ -80,15 +82,33 @@ def test_engine_offline(tmpdir_factory, mock_wildfire_image, mock_forest_image):
8082
assert engine._states["-1"]["last_predictions"][2][4] is False
8183

8284

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):
86106
"""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)
89108
assert instance.format == "onnx"
90109

91110

111+
# mock_isfile is a mock of the os.path.isfile() function which allows to simulate file existence
92112
@patch("os.path.isfile")
93113
def test_nonexistent_model(mock_isfile):
94114
"""Tests Engine instanciation with a non-existent input model_path"""

0 commit comments

Comments
 (0)