Skip to content

Commit 13a87bd

Browse files
author
Cipher
committed
test: strengthen test_unspecified_reader_falls_back_silently to exercise fallback path
The previous test only verified LoadImage() succeeds in the happy path where optional dependencies exist. This does not validate the critical warn-and-skip fallback behavior when packages are missing. Update the test to: 1. Patch SUPPORTED_READERS to simulate missing optional package (ITKReader) 2. Trigger LoadImage() to exercise the exception-catching, warn, and skip path 3. Verify the debug log about the missing package was invoked 4. Restore original reader entries afterward This ensures the auto-select fallback path is actually tested, not just the happy path. Signed-off-by: Cipher <cipher@openclaw.ai>
1 parent 7712ff8 commit 13a87bd

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

tests/transforms/test_load_image.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,39 @@ def test_explicit_reader_not_installed_raises_runtime_error(self):
516516

517517
def test_unspecified_reader_falls_back_silently(self):
518518
"""When no reader is specified, missing optional readers should be silently skipped (no exception)."""
519-
# This should not raise even if some optional readers are unavailable.
520-
loader = LoadImage()
521-
self.assertIsInstance(loader, LoadImage)
519+
# Force the fallback path by simulating missing optional dependencies.
520+
# Patch the constructor to raise OptionalImportError for some readers,
521+
# then verify LoadImage still instantiates and logs warnings.
522+
import logging
523+
from monai.utils import OptionalImportError
524+
525+
# Patch SUPPORTED_READERS entries to raise OptionalImportError
526+
# This simulates optional packages not being installed
527+
original_readers = {}
528+
from monai.transforms.io.array import SUPPORTED_READERS
529+
530+
# Patch a few readers to fail (e.g., ITKReader)
531+
try:
532+
original_itk = SUPPORTED_READERS.get("itk")
533+
534+
def failing_reader(*args, **kwargs):
535+
raise OptionalImportError("itk not installed")
536+
537+
# Temporarily replace ITKReader with a failing version
538+
SUPPORTED_READERS["itk"] = failing_reader
539+
540+
# Capture log output to verify warn-and-skip was invoked
541+
with self.assertLogs("LoadImage", level="DEBUG") as cm:
542+
loader = LoadImage()
543+
self.assertIsInstance(loader, LoadImage)
544+
545+
# Verify we got the expected debug log about skipping the missing reader
546+
self.assertTrue(any("not installed" in msg for msg in cm.output),
547+
f"Expected 'not installed' in debug logs, got: {cm.output}")
548+
finally:
549+
# Restore original reader
550+
if original_itk is not None:
551+
SUPPORTED_READERS["itk"] = original_itk
522552

523553
def test_explicit_reader_available_succeeds(self):
524554
"""When the user explicitly names a reader whose package IS installed, no exception is raised."""

0 commit comments

Comments
 (0)