File size: 7,359 Bytes
e73ce34 | 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | from __future__ import annotations
import os
import tempfile
from unittest.mock import patch
import pytest
from PIL import Image
from app.modules.decimer import convert_image
from app.modules.decimer import get_predicted_segments
from app.modules.decimer import get_predicted_segments_from_file
from app.modules.decimer import get_segments
# Define a directory for temporary test files
TEST_FILES_DIR = "tests"
@pytest.fixture(scope="module")
def sample_gif_path():
return os.path.join(TEST_FILES_DIR, "segment_sample.gif")
@pytest.fixture(scope="module")
def sample_png_path():
return os.path.join(TEST_FILES_DIR, "segment_sample.png")
@pytest.fixture(scope="module")
def sample_image_path():
return os.path.join(TEST_FILES_DIR, "segment_sample.png")
@pytest.fixture(scope="module")
def small_image_path():
"""Small image (400x300) - should trigger direct prediction"""
return os.path.join(TEST_FILES_DIR, "small_molecule.png")
@pytest.fixture(scope="module")
def tiny_image_path():
"""Tiny image (200x150) - should trigger direct prediction"""
return os.path.join(TEST_FILES_DIR, "tiny_molecule.png")
@pytest.fixture(scope="module")
def caffeine_image_path():
"""Caffeine image for testing"""
return os.path.join(TEST_FILES_DIR, "caffeine.png")
# Test the convert_image function
def test_convert_image(sample_gif_path, sample_png_path):
converted_path = convert_image(sample_gif_path)
assert os.path.isfile(converted_path)
assert converted_path == sample_png_path
# Clean up the converted file
if os.path.exists(converted_path):
os.remove(converted_path)
# Test the get_segments function with GIF
def test_get_segments_gif(sample_gif_path):
image_name, segments = get_segments(sample_gif_path)
assert image_name == "segment_sample.gif"
assert isinstance(segments, list)
# Test the get_segments function with PNG
def test_get_segments_png(sample_png_path):
image_name, segments = get_segments(sample_png_path)
assert image_name == "segment_sample.png"
assert isinstance(segments, list)
# Test the get_predicted_segments function
@patch("app.modules.decimer.predict_SMILES")
def test_get_predicted_segments(mock_predict_smiles, sample_png_path):
mock_predict_smiles.return_value = "CCO"
predicted_smiles = get_predicted_segments(sample_png_path)
assert isinstance(predicted_smiles, str)
assert len(predicted_smiles) > 0
# Test get_predicted_segments_from_file with large image (should use segmentation)
@patch("app.modules.decimer.get_predicted_segments")
def test_get_predicted_segments_from_file_large_image(
mock_get_predicted_segments, caffeine_image_path
):
"""Test that large images (>=500 pixels) use segmentation approach"""
mock_get_predicted_segments.return_value = "CN1C=NC2=C1C(=O)N(C(=O)N2C)C"
with open(caffeine_image_path, "rb") as f:
content = f.read()
predicted_smiles = get_predicted_segments_from_file(content, "test_large.png")
assert isinstance(predicted_smiles, str)
assert len(predicted_smiles) > 0
mock_get_predicted_segments.assert_called_once()
# Test get_predicted_segments_from_file with small image (should use direct prediction)
@patch("app.modules.decimer.predict_SMILES")
def test_get_predicted_segments_from_file_small_image(
mock_predict_smiles, small_image_path
):
"""Test that small images (<500 pixels) use direct prediction"""
mock_predict_smiles.return_value = "CN1C=NC2=C1C(=O)N(C(=O)N2C)C"
with open(small_image_path, "rb") as f:
content = f.read()
predicted_smiles = get_predicted_segments_from_file(content, "test_small.png")
assert isinstance(predicted_smiles, str)
assert len(predicted_smiles) > 0
mock_predict_smiles.assert_called_once()
# Test get_predicted_segments_from_file with tiny image (should use direct prediction)
@patch("app.modules.decimer.predict_SMILES")
def test_get_predicted_segments_from_file_tiny_image(
mock_predict_smiles, tiny_image_path
):
"""Test that tiny images (<500 pixels) use direct prediction"""
mock_predict_smiles.return_value = "C1CCC1"
with open(tiny_image_path, "rb") as f:
content = f.read()
predicted_smiles = get_predicted_segments_from_file(content, "test_tiny.png")
assert isinstance(predicted_smiles, str)
assert len(predicted_smiles) > 0
mock_predict_smiles.assert_called_once()
# Test error handling in get_predicted_segments_from_file
def test_get_predicted_segments_from_file_cleanup():
"""Test that temporary files are always cleaned up, even on errors"""
test_content = b"invalid image content"
test_filename = "test_cleanup.png"
# This should fail but still clean up the file
try:
get_predicted_segments_from_file(test_content, test_filename)
except Exception:
pass # Expected to fail with invalid image content
# File should not exist after function completes
assert not os.path.exists(test_filename)
# Test image size detection logic
def test_image_size_detection():
"""Test that the image size detection works correctly"""
# Create temporary images with known sizes
with tempfile.NamedTemporaryFile(
suffix=".png", delete=False
) as tmp_large, tempfile.NamedTemporaryFile(
suffix=".png", delete=False
) as tmp_small:
try:
# Create large image (600x600)
large_img = Image.new("RGB", (600, 600), "white")
large_img.save(tmp_large.name)
# Create small image (300x300)
small_img = Image.new("RGB", (300, 300), "white")
small_img.save(tmp_small.name)
# Test with large image content
with open(tmp_large.name, "rb") as f:
large_content = f.read()
# Test with small image content
with open(tmp_small.name, "rb") as f:
small_content = f.read()
# Mock the prediction functions to verify which path is taken
with patch("app.modules.decimer.predict_SMILES") as mock_direct, patch(
"app.modules.decimer.get_predicted_segments"
) as mock_segment:
mock_direct.return_value = "direct_prediction"
mock_segment.return_value = "segmented_prediction"
# Test large image uses segmentation
result_large = get_predicted_segments_from_file(
large_content, "test_large_600x600.png"
)
assert result_large == "segmented_prediction"
mock_segment.assert_called()
mock_direct.assert_not_called()
# Reset mocks
mock_direct.reset_mock()
mock_segment.reset_mock()
# Test small image uses direct prediction
result_small = get_predicted_segments_from_file(
small_content, "test_small_300x300.png"
)
assert result_small == "direct_prediction"
mock_direct.assert_called()
mock_segment.assert_not_called()
finally:
# Clean up temporary files
for tmp_file in [tmp_large.name, tmp_small.name]:
if os.path.exists(tmp_file):
os.remove(tmp_file)
|