Skip to content

Commit 8a23073

Browse files
committed
agentic idp test case
1 parent a108144 commit 8a23073

File tree

6 files changed

+427
-105
lines changed

6 files changed

+427
-105
lines changed

lib/idp_common_pkg/idp_common/extraction/service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,8 +1342,8 @@ def process_document_section(self, document: Document, section_id: str) -> Docum
13421342
else:
13431343
message_prompt = content
13441344
logger.info("Using Agentic extraction")
1345-
logger.info(f"Using input: {str(message_prompt)}")
1346-
structured_data, response_with_metering = structured_output(
1345+
logger.debug(f"Using input: {str(message_prompt)}")
1346+
structured_data, response_with_metering = structured_output( # pyright: ignore[reportPossiblyUnboundVariable]
13471347
model_id=model_id,
13481348
data_format=dynamic_model,
13491349
prompt=message_prompt, # pyright: ignore[reportArgumentType]

lib/idp_common_pkg/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,6 @@ agentic-extraction = [
150150

151151
[dependency-groups]
152152
dev = [
153+
"moto>=5.1.8",
153154
"pytest-asyncio>=1.1.0",
154155
]
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)