-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
250 lines (204 loc) · 7.98 KB
/
Copy pathconftest.py
File metadata and controls
250 lines (204 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
"""
Pytest configuration and fixtures for Case File Analysis tests.
"""
import pytest
import sys
import os
import warnings
# Add src to path for imports
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
# Suppress warnings for cleaner test output
warnings.filterwarnings("ignore", category=UserWarning)
warnings.filterwarnings("ignore", category=DeprecationWarning)
@pytest.fixture(scope="session")
def sample_case_text():
"""Fixture providing sample case file text for testing."""
return """
Case No: 2024-CR-001
Status: Active
Date of Incident: March 15, 2024
Time of Incident: 10:30 PM
Location: 123 Main Street, Downtown
Crime: Burglary
John Smith reported that Tom Wilson broke into his house.
The suspect stole $2,500 in cash and jewelry worth $15,000.
Mary Johnson witnessed the incident.
"""
@pytest.fixture(scope="session")
def complex_case_text():
"""Fixture providing complex case file text for integration testing."""
return """
Case No: 2024-BG-001
Status: Under Investigation
Date of Incident: March 15, 2024
Time of Incident: 2:30 AM
Location: 123 Main Street, Downtown
Crime Type: Burglary
Incident Report:
On the above date and time, victim John Smith (DOB: 01/15/1980) reported a burglary
at his residence located at 123 Main Street, Downtown. The suspect, identified as
Tom Wilson (DOB: 05/22/1985), allegedly broke into the residence through the back door
using a crowbar.
Stolen Items:
- Gold necklace valued at $2,500.00
- Cash totaling $500.00
- iPhone 13 Pro valued at $800.00
- Wedding ring worth $1,200.00
Witnesses:
- Mary Johnson (neighbor) observed Wilson near the property at approximately 2:15 AM
- Security camera footage shows suspect vehicle (License: ABC-123)
Evidence Collected:
- Fingerprints on crowbar
- Footprints in backyard
- Security footage from 2:00-3:00 AM
Investigating Officer: Detective Sarah Connor
Report Filed: March 15, 2024 at 8:30 AM
Follow-up Actions:
- Interview additional neighbors
- Process forensic evidence
- Issue warrant for suspect arrest
"""
@pytest.fixture(scope="function")
def temp_directory():
"""Fixture providing temporary directory for test files."""
import tempfile
import shutil
temp_dir = tempfile.mkdtemp()
yield temp_dir
shutil.rmtree(temp_dir)
@pytest.fixture(scope="session")
def parser_minimal():
"""Fixture providing LangGraphParser with minimal configuration for fast testing."""
try:
from casefile_parser.LangGraphParser import LangGraphParser
parser = LangGraphParser(
max_enrichment_steps=3,
model_config={
"model_id": "claude-3-haiku-20240307",
"temperature": 0.0,
"max_tokens": 4096
}
)
return parser
except Exception as e:
pytest.skip(f"Could not initialize LangGraphParser: {e}")
@pytest.fixture(scope="session", autouse=True)
def setup_test_environment():
"""Automatically set up test environment."""
# Set environment variables for testing
os.environ['TESTING'] = 'true'
# Reduce logging verbosity during tests
import logging
logging.getLogger().setLevel(logging.ERROR)
yield
# Cleanup after all tests
if 'TESTING' in os.environ:
del os.environ['TESTING']
@pytest.fixture
def mock_nlp():
"""Fixture providing mocked spaCy nlp object."""
from unittest.mock import MagicMock
mock_nlp = MagicMock()
mock_doc = MagicMock()
mock_nlp.return_value = mock_doc
mock_doc.ents = []
mock_doc.sents = []
return mock_nlp
# Pytest markers for test categorization
pytest_plugins = []
def pytest_configure(config):
"""Configure pytest with custom markers."""
config.addinivalue_line(
"markers", "slow: mark test as slow running (integration tests)"
)
config.addinivalue_line(
"markers", "integration: mark test as integration test"
)
config.addinivalue_line(
"markers", "unit: mark test as unit test"
)
config.addinivalue_line(
"markers", "parser: mark test as parser-related"
)
config.addinivalue_line(
"markers", "entities: mark test as entity extraction related"
)
config.addinivalue_line(
"markers", "relations: mark test as relation extraction related"
)
config.addinivalue_line(
"markers", "utilities: mark test as utility function related"
)
def pytest_collection_modifyitems(config, items):
"""Modify test collection to add markers automatically."""
for item in items:
# Add markers based on test file names
if "test_integration" in item.nodeid:
item.add_marker(pytest.mark.integration)
item.add_marker(pytest.mark.slow)
elif "test_casefile_parser" in item.nodeid:
item.add_marker(pytest.mark.parser)
item.add_marker(pytest.mark.unit)
elif "test_entity_extractor" in item.nodeid:
item.add_marker(pytest.mark.entities)
item.add_marker(pytest.mark.unit)
elif "test_relational_inference" in item.nodeid:
item.add_marker(pytest.mark.relations)
item.add_marker(pytest.mark.unit)
elif "test_utilities" in item.nodeid:
item.add_marker(pytest.mark.utilities)
item.add_marker(pytest.mark.unit)
# Add slow marker for tests that might take longer
if any(keyword in item.nodeid.lower() for keyword in ['performance', 'large', 'batch']):
item.add_marker(pytest.mark.slow)
@pytest.fixture
def skip_if_no_spacy():
"""Skip tests if spaCy model is not available (not autouse to avoid slow imports)."""
try:
import spacy
spacy.load("en_core_web_sm")
except (ImportError, OSError):
pytest.skip("spaCy en_core_web_sm model not available")
# Pytest hooks for better test reporting
def pytest_runtest_setup(item):
"""Setup for each test run."""
# Clear any cached imports or state
pass
def pytest_runtest_teardown(item, nextitem):
"""Teardown after each test run."""
# Clean up any test artifacts
pass
# Custom assertion helpers
class CaseFileAssertions:
"""Custom assertions for case file testing."""
@staticmethod
def assert_valid_entity_result(result):
"""Assert that entity extraction result is valid."""
assert isinstance(result, dict), "Result should be a dictionary"
for category, spans in result.items():
assert isinstance(category, str), "Category should be string"
assert isinstance(spans, list), "Spans should be a list"
for span in spans:
assert isinstance(span, tuple), "Span should be tuple"
assert len(span) == 2, "Span should have start and end"
assert isinstance(span[0], int), "Start should be integer"
assert isinstance(span[1], int), "End should be integer"
assert span[0] <= span[1], "Start should be <= end"
@staticmethod
def assert_valid_relations_result(relations):
"""Assert that relation extraction result is valid."""
assert isinstance(relations, list), "Relations should be a list"
for relation in relations:
assert isinstance(relation, tuple), "Relation should be tuple"
assert len(relation) == 3, "Relation should have head, tail, info"
head, tail, info = relation
assert isinstance(head, str), "Head should be string"
assert isinstance(tail, str), "Tail should be string"
assert isinstance(info, dict), "Info should be dictionary"
assert 'relation' in info, "Info should contain relation type"
# Make assertions available to all test files
@pytest.fixture
def assertions():
"""Fixture providing custom assertions."""
return CaseFileAssertions()