-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_examples.py
More file actions
63 lines (49 loc) · 1.97 KB
/
test_examples.py
File metadata and controls
63 lines (49 loc) · 1.97 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
import importlib.util
import sys
from pathlib import Path
import pytest
def get_example_files() -> list[str]:
"""Get all example files from the directory"""
examples_dir = Path(__file__).parent
examples = []
for file in examples_dir.glob("*.py"):
# Skip __init__.py and test files
if file.name.startswith("__") or file.name.startswith("test_"):
continue
examples.append(file.name)
return examples
EXAMPLES = get_example_files()
# Map of example files to required optional packages
# Note: All examples now require MCP extra for fetch_tools()
OPTIONAL_DEPENDENCIES = {
"openai_integration.py": ["openai", "mcp"],
"langchain_integration.py": ["langchain_openai", "mcp"],
"crewai_integration.py": ["crewai", "mcp"],
"index.py": ["mcp"],
"file_uploads.py": ["mcp"],
"stackone_account_ids.py": ["mcp"],
"search_tool_example.py": ["mcp"],
"semantic_search_example.py": ["mcp"],
"mcp_server.py": ["mcp"],
}
def test_example_files_exist() -> None:
"""Verify that we found example files to test"""
assert len(EXAMPLES) > 0, "No example files found"
print(f"Found {len(EXAMPLES)} examples")
@pytest.mark.parametrize("example_file", EXAMPLES)
def test_run_example(example_file: str) -> None:
"""Run each example file directly using python"""
# Skip if optional dependencies are not available
if example_file in OPTIONAL_DEPENDENCIES:
for module in OPTIONAL_DEPENDENCIES[example_file]:
try:
__import__(module)
except ImportError:
pytest.skip(f"Skipping {example_file}: {module} not installed")
example_path = Path(__file__).parent / example_file
# Import and run the example module directly
spec = importlib.util.spec_from_file_location("example", example_path)
if spec and spec.loader:
module = importlib.util.module_from_spec(spec)
sys.modules["example"] = module
spec.loader.exec_module(module)