-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathtest_setup.py
141 lines (123 loc) · 4.27 KB
/
test_setup.py
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
import asyncio
import os
import logging
import sys
from anthropic import Anthropic
import aiohttp
from dotenv import load_dotenv
# Add parent directory to Python path for imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from src.config import (
ANTHROPIC_API_KEY,
DEBUG,
LOG_FORMAT,
INPUT_DIR,
RAW_DIR,
SECTIONS_DIR,
REPORTS_DIR,
LOGS_DIR,
CLAUDE_MODEL
)
# Configure logging
logging.basicConfig(
level=logging.DEBUG if DEBUG else logging.INFO,
format=LOG_FORMAT,
handlers=[
logging.FileHandler(os.path.join(LOGS_DIR, "setup_test.log")),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
async def test_directories():
"""Test if all required directories exist and are writable"""
logger.info("Testing directory structure...")
directories = [INPUT_DIR, RAW_DIR, SECTIONS_DIR, REPORTS_DIR, LOGS_DIR]
for directory in directories:
if not os.path.exists(directory):
logger.error(f"Directory missing: {directory}")
return False
# Test write permissions
test_file = os.path.join(directory, ".test")
try:
with open(test_file, "w") as f:
f.write("test")
os.remove(test_file)
logger.info(f"Directory OK: {directory}")
except Exception as e:
logger.error(f"Cannot write to directory {directory}: {str(e)}")
return False
return True
async def test_claude():
"""Test Claude API connection"""
logger.info("Testing Claude API connection...")
try:
client = Anthropic(api_key=ANTHROPIC_API_KEY)
response = client.messages.create(
model=CLAUDE_MODEL,
max_tokens=100,
messages=[{"role": "user", "content": "Say hello!"}]
)
# Access the response content
if response.content and len(response.content) > 0:
logger.info(f"Claude response: {response.content[0].text}")
logger.info("Claude API test successful!")
return True
else:
logger.error("No content in Claude response")
return False
except Exception as e:
logger.error(f"Claude API test failed: {str(e)}")
return False
async def test_web_access():
"""Test web access to case study URLs"""
logger.info("Testing web access...")
test_url = "https://aws.amazon.com/solutions/case-studies/airbnb-case-study/"
try:
async with aiohttp.ClientSession() as session:
async with session.get(test_url) as response:
if response.status == 200:
logger.info("Web access test successful!")
return True
else:
logger.error(f"Web access test failed with status: {response.status}")
return False
except Exception as e:
logger.error(f"Web access test failed: {str(e)}")
return False
async def main():
"""Run all tests"""
load_dotenv()
# Test environment variables
logger.info("\nChecking environment variables...")
required_vars = ["ANTHROPIC_API_KEY"]
missing_vars = [var for var in required_vars if not os.getenv(var)]
if missing_vars:
logger.error(f"Missing environment variables: {', '.join(missing_vars)}")
return
logger.info("Environment variables loaded successfully!")
# Run tests
tests = [
("Directory Structure", test_directories()),
("Claude API", test_claude()),
("Web Access", test_web_access())
]
results = []
for test_name, test_coro in tests:
print(f"\nRunning test: {test_name}")
result = await test_coro
results.append((test_name, result))
# Print summary
print("\nTest Summary:")
print("-" * 40)
for test_name, result in results:
status = "✅ PASSED" if result else "❌ FAILED"
print(f"{test_name}: {status}")
# Exit with appropriate status code
if all(result for _, result in results):
logger.info("All tests passed successfully!")
sys.exit(0)
else:
logger.error("Some tests failed!")
sys.exit(1)
if __name__ == "__main__":
asyncio.run(main())