-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_project.py
More file actions
52 lines (43 loc) · 1.59 KB
/
test_project.py
File metadata and controls
52 lines (43 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import pytest
import numpy as np
from unittest.mock import patch, MagicMock
import project
import base64
import cv2
# Sample test image data
TEST_IMAGE = np.zeros((28, 28), dtype=np.uint8)
@pytest.fixture(autouse=True)
def reset_model():
"""Reset model state before each test"""
project._model = None
def test_load_model():
"""Test model loading functionality"""
with patch('project.models.load_model') as mock_load:
project.load_model("test_model.keras")
mock_load.assert_called_once_with("test_model.keras")
assert project._model is not None
def test_preprocess_image():
"""Test image preprocessing pipeline"""
# Create test base64 image (black square)
_, img_encoded = cv2.imencode('.png', TEST_IMAGE)
img_base64 = base64.b64encode(img_encoded).decode('utf-8')
# Test with data URL
data_url = f"data:image/png;base64,{img_base64}"
processed = project.preprocess_image(data_url)
# Verify output shape and properties
assert processed.shape == (1, 28, 28, 1)
assert processed.dtype == np.float32
assert np.max(processed) <= 1.0
assert np.min(processed) >= 0.0
def test_predict_digit():
"""Test digit prediction functionality"""
# Setup mock model
mock_model = MagicMock()
mock_model.predict.return_value = np.array([[0.1]*9 + [0.9]]) # Predict digit 9
project._model = mock_model
# Create test input
test_input = np.zeros((1, 28, 28, 1))
digit = project.predict_digit(test_input)
# Verify predictions
assert digit == 9
mock_model.predict.assert_called_once_with(test_input)