|
| 1 | +""" |
| 2 | +Pytest configuration for agentic_idp extraction tests. |
| 3 | +These tests require real strands/pyarrow packages and are automatically skipped when unavailable. |
| 4 | +
|
| 5 | +To run these tests locally: |
| 6 | + pytest -m agentic tests/unit/extraction/agentic_idp/ |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import sys |
| 11 | + |
| 12 | + |
| 13 | +def _should_skip_collection(config): |
| 14 | + """Determine if we should skip collection based on environment and pytest marks.""" |
| 15 | + # Check if user explicitly requested agentic tests via -m agentic |
| 16 | + if config.option.markexpr and "agentic" in config.option.markexpr: |
| 17 | + return False |
| 18 | + |
| 19 | + # Skip in CI |
| 20 | + if os.getenv("CI"): |
| 21 | + return True |
| 22 | + |
| 23 | + # Check if strands is available and not mocked |
| 24 | + try: |
| 25 | + from unittest.mock import MagicMock |
| 26 | + |
| 27 | + import strands |
| 28 | + |
| 29 | + if isinstance(strands, MagicMock): |
| 30 | + return True |
| 31 | + except ImportError: |
| 32 | + return True |
| 33 | + |
| 34 | + return False |
| 35 | + |
| 36 | + |
| 37 | +def pytest_ignore_collect(collection_path, config): |
| 38 | + """Skip collection of test files in this directory if strands unavailable.""" |
| 39 | + if _should_skip_collection(config) and "agentic_idp" in str(collection_path): |
| 40 | + return True |
| 41 | + return False |
| 42 | + |
| 43 | + |
| 44 | +def pytest_configure(config): |
| 45 | + """Remove mocked modules to allow real imports.""" |
| 46 | + config.addinivalue_line( |
| 47 | + "markers", |
| 48 | + "agentic: mark test as requiring real strands package (run with -m agentic)", |
| 49 | + ) |
| 50 | + |
| 51 | + if _should_skip_collection(config): |
| 52 | + return |
| 53 | + |
| 54 | + # Only remove modules that are actually MagicMock instances |
| 55 | + from unittest.mock import MagicMock |
| 56 | + |
| 57 | + modules_to_unmock = [ |
| 58 | + "strands", |
| 59 | + "strands.models", |
| 60 | + "strands.models.bedrock", |
| 61 | + "strands.types", |
| 62 | + "strands.types.content", |
| 63 | + "strands.types.media", |
| 64 | + "strands.hooks", |
| 65 | + "strands.hooks.events", |
| 66 | + "pyarrow", |
| 67 | + ] |
| 68 | + |
| 69 | + # Remove mocked modules |
| 70 | + for module_name in modules_to_unmock: |
| 71 | + if module_name in sys.modules and isinstance( |
| 72 | + sys.modules[module_name], MagicMock |
| 73 | + ): |
| 74 | + sys.modules.pop(module_name, None) |
| 75 | + |
| 76 | + # Remove any modules that imported the mocked modules so they get re-imported fresh |
| 77 | + modules_to_reload = [ |
| 78 | + "idp_common.extraction.agentic_idp", |
| 79 | + "idp_common.extraction.service", |
| 80 | + "PIL", |
| 81 | + "PIL.Image", |
| 82 | + "PIL.ImageEnhance", |
| 83 | + "PIL.ImageOps", |
| 84 | + ] |
| 85 | + |
| 86 | + for module_name in modules_to_reload: |
| 87 | + sys.modules.pop(module_name, None) |
| 88 | + |
| 89 | + # Remove any modules that imported the mocked modules so they get re-imported fresh |
| 90 | + modules_to_reload = [ |
| 91 | + "idp_common.extraction.agentic_idp", |
| 92 | + "idp_common.extraction.service", |
| 93 | + ] |
| 94 | + |
| 95 | + for module_name in modules_to_reload: |
| 96 | + sys.modules.pop(module_name, None) |
| 97 | + |
| 98 | + # Remove modules that depend on the unmocked modules so they get re-imported fresh |
| 99 | + for module_name in modules_to_reload: |
| 100 | + sys.modules.pop(module_name, None) |
| 101 | + |
| 102 | + # Remove modules that depend on the unmocked modules |
| 103 | + for module_name in modules_to_reload: |
| 104 | + sys.modules.pop(module_name, None) |
0 commit comments