File size: 6,006 Bytes
60b6623
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Unit tests for image preprocessor service"""

import unittest
from unittest.mock import Mock, patch, MagicMock
import sys
import os
import io
from PIL import Image

# Add the app directory to the path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'app'))

from services.image_preprocessor import ImagePreprocessor

class TestImagePreprocessor(unittest.TestCase):
    """Test cases for image preprocessor service"""

    def setUp(self):
        """Set up test fixtures"""
        self.preprocessor = ImagePreprocessor()
        
        # Create a simple test image
        self.test_image = Image.new('RGB', (100, 100), color='red')
        self.test_image_bytes = io.BytesIO()
        self.test_image.save(self.test_image_bytes, format='JPEG')
        self.test_image_bytes.seek(0)

    def test_detect_mime_type_jpeg(self):
        """Test MIME type detection for JPEG"""
        # Arrange
        image_data = self.test_image_bytes.getvalue()
        filename = "test.jpg"
        
        # Act
        mime_type = self.preprocessor.detect_mime_type(image_data, filename)
        
        # Assert
        self.assertEqual(mime_type, "image/jpeg")

    def test_detect_mime_type_png(self):
        """Test MIME type detection for PNG"""
        # Arrange
        png_image = Image.new('RGB', (50, 50), color='blue')
        png_bytes = io.BytesIO()
        png_image.save(png_bytes, format='PNG')
        png_bytes.seek(0)
        image_data = png_bytes.getvalue()
        filename = "test.png"
        
        # Act
        mime_type = self.preprocessor.detect_mime_type(image_data, filename)
        
        # Assert
        self.assertEqual(mime_type, "image/png")

    def test_detect_mime_type_unknown(self):
        """Test MIME type detection for unknown file"""
        # Arrange
        unknown_data = b"not an image"
        filename = "test.unknown"
        
        # Act
        mime_type = self.preprocessor.detect_mime_type(unknown_data, filename)
        
        # Assert
        self.assertEqual(mime_type, "application/octet-stream")

    def test_needs_preprocessing_jpeg(self):
        """Test preprocessing check for JPEG (should not need preprocessing)"""
        # Act
        needs_preprocessing = self.preprocessor.needs_preprocessing("image/jpeg")
        
        # Assert
        self.assertFalse(needs_preprocessing)

    def test_needs_preprocessing_png(self):
        """Test preprocessing check for PNG (should not need preprocessing)"""
        # Act
        needs_preprocessing = self.preprocessor.needs_preprocessing("image/png")
        
        # Assert
        self.assertFalse(needs_preprocessing)

    def test_needs_preprocessing_pdf(self):
        """Test preprocessing check for PDF (should need preprocessing)"""
        # Act
        needs_preprocessing = self.preprocessor.needs_preprocessing("application/pdf")
        
        # Assert
        self.assertTrue(needs_preprocessing)

    def test_needs_preprocessing_heic(self):
        """Test preprocessing check for HEIC (should need preprocessing)"""
        # Act
        needs_preprocessing = self.preprocessor.needs_preprocessing("image/heic")
        
        # Assert
        self.assertTrue(needs_preprocessing)

    def test_preprocess_image_success(self):
        """Test successful image preprocessing"""
        # Arrange
        image_data = self.test_image_bytes.getvalue()
        filename = "test.jpg"
        
        # Act
        result = self.preprocessor.preprocess_image(image_data, filename)
        
        # Assert
        self.assertIsInstance(result, tuple)
        self.assertEqual(len(result), 3)
        processed_content, new_filename, mime_type = result
        self.assertIsInstance(processed_content, bytes)
        self.assertIsInstance(new_filename, str)
        self.assertIsInstance(mime_type, str)

    def test_preprocess_image_png_format(self):
        """Test image preprocessing with PNG format"""
        # Arrange
        image_data = self.test_image_bytes.getvalue()
        filename = "test.jpg"
        
        # Act
        result = self.preprocessor.preprocess_image(image_data, filename, target_format='PNG')
        
        # Assert
        processed_content, new_filename, mime_type = result
        self.assertIsInstance(processed_content, bytes)
        self.assertIsInstance(new_filename, str)
        self.assertGreater(len(new_filename), 0)

    def test_preprocess_image_jpeg_quality(self):
        """Test image preprocessing with JPEG quality setting"""
        # Arrange
        image_data = self.test_image_bytes.getvalue()
        filename = "test.jpg"
        
        # Act
        result = self.preprocessor.preprocess_image(image_data, filename, target_format='JPEG', quality=80)
        
        # Assert
        processed_content, new_filename, mime_type = result
        self.assertIsInstance(processed_content, bytes)
        self.assertIn('.jpg', new_filename.lower())

    def test_supported_mime_types(self):
        """Test that supported MIME types are defined"""
        # Assert
        self.assertIsInstance(self.preprocessor.SUPPORTED_IMAGE_MIME_TYPES, set)
        self.assertIn('image/jpeg', self.preprocessor.SUPPORTED_IMAGE_MIME_TYPES)
        self.assertIn('image/png', self.preprocessor.SUPPORTED_IMAGE_MIME_TYPES)
        self.assertIn('application/pdf', self.preprocessor.SUPPORTED_IMAGE_MIME_TYPES)

    def test_fitz_availability(self):
        """Test PyMuPDF availability flag"""
        # Assert
        self.assertIsInstance(self.preprocessor.FITZ_AVAILABLE, bool)

    def test_configuration_constants(self):
        """Test that configuration constants are defined"""
        # Assert
        self.assertIsInstance(self.preprocessor.PDF_ZOOM_FACTOR, float)
        self.assertIsInstance(self.preprocessor.PDF_COMPRESS_LEVEL, int)
        self.assertIsInstance(self.preprocessor.PDF_QUALITY_MODE, str)

if __name__ == '__main__':
    unittest.main()