Skip to content

Commit 79c20ee

Browse files
committed
fixed rulebreakers
1 parent d38d2af commit 79c20ee

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

notes/story-1.6-tkt.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Runtime Breakers
2-
- [x] SparkService.__init__ — move field assignments above create_spark_session().
2+
3+
- [x] SparkService.**init** — move field assignments above create_spark_session().
34
- [x] pyspark_udfs.ensure_installed — drop the stray self.
45
- [x] CLI enum mismatch — convert "scan" → [OperationType.SCAN].
5-
- [x] Guard Donut: import torch/transformers only if use_donut is true.
6+
- [x] Guard Donut: import torch/transformers only if use_donut is true.

tests/test_donut_lazy_import.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,66 @@
11
import asyncio
22
import importlib
33
import sys
4-
import pytest
54
from unittest.mock import patch
65

6+
import pytest
7+
78
from datafog.services.image_service import ImageService
89

910

1011
def test_no_torch_import_when_donut_disabled():
1112
"""Test that torch is not imported when use_donut is False"""
1213
# 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+
1819
# 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+
2123
# 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
2426

2527

2628
def test_lazy_import_mechanism():
2729
"""Test the lazy import mechanism for DonutProcessor"""
2830
# This test verifies that the DonutProcessor class has been refactored
2931
# to use lazy imports. We don't need to actually test the imports themselves,
3032
# just that the structure is correct.
31-
33+
3234
# Create ImageService with use_donut=True
3335
image_service = ImageService(use_donut=True, use_tesseract=False)
34-
36+
3537
# Check that the donut_processor was created
3638
assert image_service.donut_processor is not None
37-
39+
3840
# 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+
4143
# 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:
4345
# Create a new processor to avoid side effects
4446
from datafog.processing.image_processing.donut_processor import DonutProcessor
47+
4548
processor = DonutProcessor()
46-
49+
4750
# 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+
5154
# Mock the ensure_installed method to avoid actual installation
52-
with patch.object(processor, 'ensure_installed'):
55+
with patch.object(processor, "ensure_installed"):
5356
# Call extract_text_from_image with None (it will fail but that's ok)
5457
try:
5558
# This will attempt to import torch and transformers
5659
asyncio.run(processor.extract_text_from_image(None))
57-
except:
60+
except Exception: # Be explicit about what we're catching
5861
pass
59-
62+
6063
# Verify that ensure_installed was called for torch and transformers
6164
assert processor.ensure_installed.call_count >= 1
65+
# Verify that the mock was used
66+
assert mock_import_fn.called

0 commit comments

Comments
 (0)