|
1 | 1 | import asyncio |
2 | 2 | import importlib |
3 | 3 | import sys |
4 | | -import pytest |
5 | 4 | from unittest.mock import patch |
6 | 5 |
|
| 6 | +import pytest |
| 7 | + |
7 | 8 | from datafog.services.image_service import ImageService |
8 | 9 |
|
9 | 10 |
|
10 | 11 | def test_no_torch_import_when_donut_disabled(): |
11 | 12 | """Test that torch is not imported when use_donut is False""" |
12 | 13 | # Remove torch and transformers from sys.modules if they're already imported |
13 | | - if 'torch' in sys.modules: |
14 | | - del sys.modules['torch'] |
15 | | - if 'transformers' in sys.modules: |
16 | | - del sys.modules['transformers'] |
17 | | - |
| 14 | + if "torch" in sys.modules: |
| 15 | + del sys.modules["torch"] |
| 16 | + if "transformers" in sys.modules: |
| 17 | + del sys.modules["transformers"] |
| 18 | + |
18 | 19 | # Create ImageService with use_donut=False |
19 | | - image_service = ImageService(use_donut=False, use_tesseract=True) |
20 | | - |
| 20 | + # The variable is used indirectly by creating the service which affects sys.modules |
| 21 | + _ = ImageService(use_donut=False, use_tesseract=True) |
| 22 | + |
21 | 23 | # Verify that torch and transformers were not imported |
22 | | - assert 'torch' not in sys.modules |
23 | | - assert 'transformers' not in sys.modules |
| 24 | + assert "torch" not in sys.modules |
| 25 | + assert "transformers" not in sys.modules |
24 | 26 |
|
25 | 27 |
|
26 | 28 | def test_lazy_import_mechanism(): |
27 | 29 | """Test the lazy import mechanism for DonutProcessor""" |
28 | 30 | # This test verifies that the DonutProcessor class has been refactored |
29 | 31 | # to use lazy imports. We don't need to actually test the imports themselves, |
30 | 32 | # just that the structure is correct. |
31 | | - |
| 33 | + |
32 | 34 | # Create ImageService with use_donut=True |
33 | 35 | image_service = ImageService(use_donut=True, use_tesseract=False) |
34 | | - |
| 36 | + |
35 | 37 | # Check that the donut_processor was created |
36 | 38 | assert image_service.donut_processor is not None |
37 | | - |
| 39 | + |
38 | 40 | # Verify that the extract_text_from_image method exists |
39 | | - assert hasattr(image_service.donut_processor, 'extract_text_from_image') |
40 | | - |
| 41 | + assert hasattr(image_service.donut_processor, "extract_text_from_image") |
| 42 | + |
41 | 43 | # Mock the imports to verify they're only imported when needed |
42 | | - with patch('importlib.import_module') as mock_import: |
| 44 | + with patch("importlib.import_module") as mock_import_fn: |
43 | 45 | # Create a new processor to avoid side effects |
44 | 46 | from datafog.processing.image_processing.donut_processor import DonutProcessor |
| 47 | + |
45 | 48 | processor = DonutProcessor() |
46 | | - |
| 49 | + |
47 | 50 | # At this point, torch should not have been imported |
48 | | - assert 'torch' not in sys.modules |
49 | | - assert 'transformers' not in sys.modules |
50 | | - |
| 51 | + assert "torch" not in sys.modules |
| 52 | + assert "transformers" not in sys.modules |
| 53 | + |
51 | 54 | # Mock the ensure_installed method to avoid actual installation |
52 | | - with patch.object(processor, 'ensure_installed'): |
| 55 | + with patch.object(processor, "ensure_installed"): |
53 | 56 | # Call extract_text_from_image with None (it will fail but that's ok) |
54 | 57 | try: |
55 | 58 | # This will attempt to import torch and transformers |
56 | 59 | asyncio.run(processor.extract_text_from_image(None)) |
57 | | - except: |
| 60 | + except Exception: # Be explicit about what we're catching |
58 | 61 | pass |
59 | | - |
| 62 | + |
60 | 63 | # Verify that ensure_installed was called for torch and transformers |
61 | 64 | assert processor.ensure_installed.call_count >= 1 |
| 65 | + # Verify that the mock was used |
| 66 | + assert mock_import_fn.called |
0 commit comments