-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_enhanced.py
More file actions
414 lines (328 loc) · 14.8 KB
/
test_enhanced.py
File metadata and controls
414 lines (328 loc) · 14.8 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/env python3
"""
Modern Test Suite for LinkedIn Profile Analyzer
Tests all components: modern scraping, agents, and frontend integration
"""
import os
import sys
import json
import time
from datetime import datetime
from typing import Dict, List
# Add current directory to path for imports
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from dotenv import load_dotenv
load_dotenv()
# Import our modern modules
from agent_modern import ModernLinkedInAgent, generate_profile_summary_and_facts_single_step
from scraper_modern import scrape_linkedin_profile_modern, test_modern_scraper
from cache import get as cache_get, set as cache_set, clear_cache
class ModernTestSuite:
"""Comprehensive test suite for the modern LinkedIn analyzer"""
def __init__(self):
self.results = []
self.start_time = None
def log_test(self, test_name: str, success: bool, message: str = "", data: Dict = None):
"""Log test results"""
result = {
"test": test_name,
"success": success,
"message": message,
"timestamp": datetime.now().isoformat(),
"data": data
}
self.results.append(result)
status = "✅ PASS" if success else "❌ FAIL"
print(f"{status} {test_name}: {message}")
def run_all_tests(self):
"""Run all tests in the suite"""
print("🧪 Starting Modern LinkedIn Analyzer Test Suite")
print("=" * 60)
self.start_time = time.time()
# Test 1: Environment Setup
self.test_environment()
# Test 2: Cache System
self.test_cache_system()
# Test 3: Modern Scraper
self.test_modern_scraper()
# Test 4: LinkedIn URL Tool
self.test_linkedin_url_tool()
# Test 5: Agent Analysis
self.test_agent_analysis()
# Test 6: Full Integration
self.test_full_integration()
# Test 7: Performance
self.test_performance()
# Test 8: Error Handling
self.test_error_handling()
# Generate report
self.generate_report()
def test_environment(self):
"""Test environment setup and API keys"""
try:
# Check required environment variables
required_vars = ["OPENAI_API_KEY", "TAVILY_API_KEY"]
missing_vars = []
for var in required_vars:
if not os.environ.get(var):
missing_vars.append(var)
if missing_vars:
self.log_test(
"Environment Setup",
False,
f"Missing environment variables: {', '.join(missing_vars)}"
)
else:
self.log_test(
"Environment Setup",
True,
"All required environment variables are set"
)
except Exception as e:
self.log_test("Environment Setup", False, f"Error: {str(e)}")
def test_cache_system(self):
"""Test caching functionality"""
try:
# Test cache operations
test_key = "test_profile_url"
test_data = {"name": "Test User", "timestamp": datetime.now().isoformat()}
# Clear any existing cache
clear_cache()
# Test cache miss
cached_data, age, from_cache = cache_get(test_key)
if cached_data is None:
self.log_test("Cache Miss", True, "Cache correctly returns None for non-existent key")
else:
self.log_test("Cache Miss", False, "Cache should return None for non-existent key")
return
# Test cache set
cache_set(test_key, test_data)
# Test cache hit
cached_data, age, from_cache = cache_get(test_key)
if cached_data and cached_data.get("name") == "Test User":
self.log_test("Cache Hit", True, f"Cache correctly retrieved data (age: {age} min)")
else:
self.log_test("Cache Hit", False, "Cache failed to retrieve stored data")
except Exception as e:
self.log_test("Cache System", False, f"Error: {str(e)}")
def test_modern_scraper(self):
"""Test modern scraper functionality"""
try:
test_url = "https://www.linkedin.com/in/williamhgates/"
print("🔍 Testing modern scraper...")
result = scrape_linkedin_profile_modern(test_url)
if result and isinstance(result, dict):
scraping_info = result.get("_scraping_info", {})
method = scraping_info.get("method", "unknown")
success = scraping_info.get("success", False)
if success:
self.log_test(
"Modern Scraper",
True,
f"Successfully scraped using method: {method}",
{"method": method, "name": result.get("full_name", "N/A")}
)
else:
self.log_test(
"Modern Scraper",
False,
f"Scraping failed with method: {method}"
)
else:
self.log_test("Modern Scraper", False, "Invalid response format")
except Exception as e:
self.log_test("Modern Scraper", False, f"Error: {str(e)}")
def test_linkedin_url_tool(self):
"""Test LinkedIn URL search tool"""
try:
from linkedin_url import linkedin_url
test_name = "Satya Nadella"
print(f"🔍 Testing LinkedIn URL search for: {test_name}")
result = linkedin_url.func(test_name)
if result and isinstance(result, str):
if "linkedin.com" in result.lower():
self.log_test(
"LinkedIn URL Tool",
True,
f"Found LinkedIn URL: {result[:50]}...",
{"url": result}
)
else:
self.log_test(
"LinkedIn URL Tool",
False,
"No LinkedIn URL found in search results"
)
else:
self.log_test("LinkedIn URL Tool", False, "Invalid search response")
except Exception as e:
self.log_test("LinkedIn URL Tool", False, f"Error: {str(e)}")
def test_agent_analysis(self):
"""Test AI agent analysis"""
try:
test_name = "Tim Cook"
print(f"🤖 Testing agent analysis for: {test_name}")
# Test modern agent
agent = ModernLinkedInAgent()
result = agent.analyze_profile(test_name)
if result and isinstance(result, dict):
full_name = result.get("full_name", "")
has_summary = bool(result.get("summary", ""))
has_facts = bool(result.get("interesting_facts", []))
self.log_test(
"Agent Analysis",
True,
f"Analyzed: {full_name}, Summary: {has_summary}, Facts: {has_facts}",
{"name": full_name, "has_summary": has_summary, "has_facts": has_facts}
)
else:
self.log_test("Agent Analysis", False, "Invalid agent response")
except Exception as e:
self.log_test("Agent Analysis", False, f"Error: {str(e)}")
def test_full_integration(self):
"""Test full integration with all components"""
try:
test_cases = [
"Elon Musk",
"Sundar Pichai",
"Reid Hoffman"
]
successful_tests = 0
for test_case in test_cases:
try:
print(f"🔄 Full integration test: {test_case}")
# Use the main analysis function
result_json = generate_profile_summary_and_facts_single_step(test_case)
result = json.loads(result_json) if isinstance(result_json, str) else result_json
if result and isinstance(result, dict) and not result.get("error"):
successful_tests += 1
except Exception as e:
print(f" ❌ Failed: {str(e)}")
success_rate = (successful_tests / len(test_cases)) * 100
self.log_test(
"Full Integration",
successful_tests > 0,
f"Success rate: {success_rate:.1f}% ({successful_tests}/{len(test_cases)})",
{"success_rate": success_rate, "successful_tests": successful_tests}
)
except Exception as e:
self.log_test("Full Integration", False, f"Error: {str(e)}")
def test_performance(self):
"""Test performance metrics"""
try:
test_name = "Reid Hoffman"
print(f"⚡ Performance test for: {test_name}")
start_time = time.time()
# Run analysis
result_json = generate_profile_summary_and_facts_single_step(test_name)
result = json.loads(result_json) if isinstance(result_json, str) else result_json
end_time = time.time()
duration = end_time - start_time
# Performance thresholds
excellent_threshold = 30 # seconds
good_threshold = 60 # seconds
if duration < excellent_threshold:
performance_rating = "Excellent"
elif duration < good_threshold:
performance_rating = "Good"
else:
performance_rating = "Needs Improvement"
self.log_test(
"Performance",
duration < good_threshold,
f"Analysis completed in {duration:.2f}s - {performance_rating}",
{"duration": duration, "rating": performance_rating}
)
except Exception as e:
self.log_test("Performance", False, f"Error: {str(e)}")
def test_error_handling(self):
"""Test error handling with invalid inputs"""
try:
test_cases = [
"", # Empty string
"NonExistentPerson12345", # Non-existent person
"https://invalid-url.com", # Invalid URL
]
handled_errors = 0
for test_case in test_cases:
try:
print(f"🔄 Error handling test: '{test_case}'")
result_json = generate_profile_summary_and_facts_single_step(test_case)
result = json.loads(result_json) if isinstance(result_json, str) else result_json
# Should handle gracefully without crashing
if result and isinstance(result, dict):
handled_errors += 1
except Exception as e:
print(f" ❌ Unhandled error: {str(e)}")
success_rate = (handled_errors / len(test_cases)) * 100
self.log_test(
"Error Handling",
handled_errors == len(test_cases),
f"Handled {handled_errors}/{len(test_cases)} error cases gracefully",
{"success_rate": success_rate}
)
except Exception as e:
self.log_test("Error Handling", False, f"Error: {str(e)}")
def generate_report(self):
"""Generate comprehensive test report"""
total_time = time.time() - self.start_time
print("\n" + "=" * 60)
print("📊 MODERN TEST SUITE REPORT")
print("=" * 60)
# Summary statistics
total_tests = len(self.results)
passed_tests = sum(1 for r in self.results if r["success"])
failed_tests = total_tests - passed_tests
success_rate = (passed_tests / total_tests) * 100 if total_tests > 0 else 0
print(f"📈 Total Tests: {total_tests}")
print(f"✅ Passed: {passed_tests}")
print(f"❌ Failed: {failed_tests}")
print(f"📊 Success Rate: {success_rate:.1f}%")
print(f"⏱️ Total Time: {total_time:.2f}s")
# Detailed results
print(f"\n📋 Detailed Results:")
print("-" * 60)
for result in self.results:
status = "✅" if result["success"] else "❌"
print(f"{status} {result['test']}: {result['message']}")
# System status
print(f"\n🔧 System Status:")
print("-" * 60)
if failed_tests == 0:
print("🎉 All tests passed! Your modern LinkedIn analyzer is working perfectly.")
print("✨ Features available:")
print(" • Modern multi-method scraping")
print(" • AI-powered profile analysis")
print(" • Intelligent caching system")
print(" • Robust error handling")
else:
print("⚠️ Some tests failed. Check the following:")
for result in self.results:
if not result["success"]:
print(f" • {result['test']}: {result['message']}")
# Save report to file
report_data = {
"timestamp": datetime.now().isoformat(),
"version": "modern_v1.0",
"summary": {
"total_tests": total_tests,
"passed_tests": passed_tests,
"failed_tests": failed_tests,
"success_rate": success_rate,
"total_time": total_time
},
"results": self.results
}
with open("test_report_modern.json", "w") as f:
json.dump(report_data, f, indent=2)
print(f"\n💾 Report saved to: test_report_modern.json")
def main():
"""Main test runner"""
print("🚀 Modern LinkedIn Profile Analyzer - Test Suite")
print("🔧 Testing modern scraping, AI agents, and integrations...")
print()
# Run the test suite
suite = ModernTestSuite()
suite.run_all_tests()
if __name__ == "__main__":
main()