1- import subprocess
1+ import importlib . util
22import sys
33from pathlib import Path
44
55import pytest
6+ import responses
67
78
89def get_example_files () -> list [str ]:
@@ -21,19 +22,64 @@ def get_example_files() -> list[str]:
2122
2223EXAMPLES = get_example_files ()
2324
25+ # Map of example files to required optional packages
26+ OPTIONAL_DEPENDENCIES = {
27+ "openai_integration.py" : ["openai" ],
28+ "langchain_integration.py" : ["langchain_openai" ],
29+ "crewai_integration.py" : ["crewai" ],
30+ }
2431
25- def test_example_files_exist ():
32+
33+ def test_example_files_exist () -> None :
2634 """Verify that we found example files to test"""
2735 assert len (EXAMPLES ) > 0 , "No example files found"
2836 print (f"Found { len (EXAMPLES )} examples" )
2937
3038
3139@pytest .mark .parametrize ("example_file" , EXAMPLES )
32- def test_run_example (example_file ):
40+ @responses .activate
41+ def test_run_example (example_file : str ) -> None :
3342 """Run each example file directly using python"""
43+ # Skip if optional dependencies are not available
44+ if example_file in OPTIONAL_DEPENDENCIES :
45+ for module in OPTIONAL_DEPENDENCIES [example_file ]:
46+ try :
47+ __import__ (module )
48+ except ImportError :
49+ pytest .skip (f"Skipping { example_file } : { module } not installed" )
50+
51+ # Setup mock responses for examples that need them
52+ if example_file in ["index.py" , "file_uploads.py" ]:
53+ # Mock employee list endpoint
54+ responses .add (
55+ responses .GET ,
56+ "https://api.stackone.com/unified/hris/employees" ,
57+ json = {
58+ "data" : [
59+ {
60+ "id" : "test-employee-1" ,
61+ "first_name" : "John" ,
62+ "last_name" : "Doe" ,
63+ "email" : "john.doe@example.com"
64+ }
65+ ]
66+ },
67+ status = 200
68+ )
69+
70+ # Mock document upload endpoint
71+ responses .add (
72+ responses .POST ,
73+ "https://api.stackone.com/unified/hris/employees/c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA/documents/upload" ,
74+ json = {"success" : True , "document_id" : "test-doc-123" },
75+ status = 200
76+ )
77+
3478 example_path = Path (__file__ ).parent / example_file
35- result = subprocess .run ([sys .executable , str (example_path )], capture_output = True , text = True )
36- if result .returncode != 0 :
37- print (f"stdout: { result .stdout } " )
38- print (f"stderr: { result .stderr } " )
39- pytest .fail (f"Example { example_file } failed with return code { result .returncode } " )
79+
80+ # Import and run the example module directly
81+ spec = importlib .util .spec_from_file_location ("example" , example_path )
82+ if spec and spec .loader :
83+ module = importlib .util .module_from_spec (spec )
84+ sys .modules ["example" ] = module
85+ spec .loader .exec_module (module )
0 commit comments