ALwrity version 0.5.5
This commit is contained in:
145
docs/alwrity_test_scripts/test_ai_integration.py
Normal file
145
docs/alwrity_test_scripts/test_ai_integration.py
Normal file
@@ -0,0 +1,145 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for AI Integration
|
||||
Verifies that the AI Engine Service is working with real AI calls.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# Add the backend directory to the Python path
|
||||
sys.path.append(str(Path(__file__).parent / "backend"))
|
||||
|
||||
from services.content_gap_analyzer.ai_engine_service import AIEngineService
|
||||
from loguru import logger
|
||||
|
||||
async def test_ai_integration():
|
||||
"""Test the AI integration functionality."""
|
||||
|
||||
print("🤖 Testing AI Integration...")
|
||||
|
||||
# Initialize the AI Engine Service
|
||||
ai_service = AIEngineService()
|
||||
|
||||
# Test data
|
||||
test_analysis_summary = {
|
||||
'target_url': 'https://example.com',
|
||||
'industry': 'Technology',
|
||||
'serp_opportunities': 15,
|
||||
'expanded_keywords_count': 50,
|
||||
'competitors_analyzed': 5,
|
||||
'dominant_themes': {
|
||||
'artificial_intelligence': 0.3,
|
||||
'machine_learning': 0.25,
|
||||
'data_science': 0.2,
|
||||
'automation': 0.15,
|
||||
'innovation': 0.1
|
||||
}
|
||||
}
|
||||
|
||||
test_market_data = {
|
||||
'industry': 'Technology',
|
||||
'competitors': [
|
||||
{
|
||||
'url': 'competitor1.com',
|
||||
'content_count': 150,
|
||||
'avg_quality_score': 8.5,
|
||||
'top_keywords': ['AI', 'ML', 'Data Science']
|
||||
},
|
||||
{
|
||||
'url': 'competitor2.com',
|
||||
'content_count': 200,
|
||||
'avg_quality_score': 7.8,
|
||||
'top_keywords': ['Automation', 'Innovation', 'Tech']
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
try:
|
||||
print("\n1. Testing Content Gap Analysis...")
|
||||
content_gaps = await ai_service.analyze_content_gaps(test_analysis_summary)
|
||||
print(f"✅ Content Gap Analysis completed: {len(content_gaps.get('strategic_insights', []))} insights generated")
|
||||
|
||||
print("\n2. Testing Market Position Analysis...")
|
||||
market_position = await ai_service.analyze_market_position(test_market_data)
|
||||
print(f"✅ Market Position Analysis completed: {len(market_position.get('strategic_recommendations', []))} recommendations generated")
|
||||
|
||||
print("\n3. Testing Content Recommendations...")
|
||||
recommendations = await ai_service.generate_content_recommendations(test_analysis_summary)
|
||||
print(f"✅ Content Recommendations completed: {len(recommendations)} recommendations generated")
|
||||
|
||||
print("\n4. Testing Performance Predictions...")
|
||||
predictions = await ai_service.predict_content_performance(test_analysis_summary)
|
||||
print(f"✅ Performance Predictions completed: {predictions.get('traffic_predictions', {}).get('confidence_level', 'N/A')} confidence")
|
||||
|
||||
print("\n5. Testing Strategic Insights...")
|
||||
insights = await ai_service.generate_strategic_insights(test_analysis_summary)
|
||||
print(f"✅ Strategic Insights completed: {len(insights)} insights generated")
|
||||
|
||||
print("\n6. Testing Health Check...")
|
||||
health = await ai_service.health_check()
|
||||
print(f"✅ Health Check completed: {health.get('status', 'unknown')} status")
|
||||
print(f" AI Integration Status: {health.get('capabilities', {}).get('ai_integration', 'unknown')}")
|
||||
|
||||
print("\n🎉 All AI Integration Tests Passed!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ AI Integration Test Failed: {str(e)}")
|
||||
logger.error(f"AI Integration test failed: {str(e)}")
|
||||
return False
|
||||
|
||||
async def test_ai_fallback():
|
||||
"""Test the fallback functionality when AI fails."""
|
||||
|
||||
print("\n🔄 Testing AI Fallback Functionality...")
|
||||
|
||||
# Initialize the AI Engine Service
|
||||
ai_service = AIEngineService()
|
||||
|
||||
# Test with minimal data to trigger fallback
|
||||
minimal_data = {'test': 'data'}
|
||||
|
||||
try:
|
||||
print("Testing fallback with minimal data...")
|
||||
result = await ai_service.analyze_content_gaps(minimal_data)
|
||||
|
||||
if result and 'strategic_insights' in result:
|
||||
print("✅ Fallback functionality working correctly")
|
||||
return True
|
||||
else:
|
||||
print("❌ Fallback functionality failed")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Fallback test failed: {str(e)}")
|
||||
return False
|
||||
|
||||
async def main():
|
||||
"""Main test function."""
|
||||
print("🚀 Starting AI Integration Tests...")
|
||||
print("=" * 50)
|
||||
|
||||
# Test 1: AI Integration
|
||||
ai_success = await test_ai_integration()
|
||||
|
||||
# Test 2: Fallback Functionality
|
||||
fallback_success = await test_ai_fallback()
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
print("📊 Test Results Summary:")
|
||||
print(f"AI Integration: {'✅ PASSED' if ai_success else '❌ FAILED'}")
|
||||
print(f"Fallback Functionality: {'✅ PASSED' if fallback_success else '❌ FAILED'}")
|
||||
|
||||
if ai_success and fallback_success:
|
||||
print("\n🎉 All tests passed! AI Integration is working correctly.")
|
||||
return 0
|
||||
else:
|
||||
print("\n⚠️ Some tests failed. Please check the AI configuration.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit_code = asyncio.run(main())
|
||||
sys.exit(exit_code)
|
||||
127
docs/alwrity_test_scripts/test_ai_service_debug.py
Normal file
127
docs/alwrity_test_scripts/test_ai_service_debug.py
Normal file
@@ -0,0 +1,127 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to debug AI analytics service issues.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import traceback
|
||||
from datetime import datetime
|
||||
|
||||
# Add backend to path
|
||||
sys.path.append('backend')
|
||||
|
||||
async def test_ai_analytics_service():
|
||||
"""Test the AI analytics service directly."""
|
||||
try:
|
||||
print("🧪 Testing AI Analytics Service Directly")
|
||||
print("=" * 50)
|
||||
|
||||
# Import the service
|
||||
from services.ai_analytics_service import AIAnalyticsService
|
||||
|
||||
print("✅ AI Analytics Service imported successfully")
|
||||
|
||||
# Create service instance
|
||||
ai_service = AIAnalyticsService()
|
||||
print("✅ AI Analytics Service instantiated")
|
||||
|
||||
# Test performance trends analysis
|
||||
print("\n🧪 Testing performance trends analysis...")
|
||||
try:
|
||||
performance_analysis = await ai_service.analyze_performance_trends(
|
||||
strategy_id=1,
|
||||
metrics=['engagement_rate', 'reach', 'conversion_rate']
|
||||
)
|
||||
print(f"✅ Performance analysis completed: {len(performance_analysis)} keys")
|
||||
print(f" - Keys: {list(performance_analysis.keys())}")
|
||||
|
||||
if 'trend_analysis' in performance_analysis:
|
||||
print(f" - Trend analysis: {len(performance_analysis['trend_analysis'])} metrics")
|
||||
else:
|
||||
print(" - No trend_analysis found")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Performance analysis failed: {e}")
|
||||
print(f" - Error type: {type(e).__name__}")
|
||||
traceback.print_exc()
|
||||
|
||||
# Test strategic intelligence
|
||||
print("\n🧪 Testing strategic intelligence...")
|
||||
try:
|
||||
strategic_intelligence = await ai_service.generate_strategic_intelligence(
|
||||
strategy_id=1
|
||||
)
|
||||
print(f"✅ Strategic intelligence completed: {len(strategic_intelligence)} keys")
|
||||
print(f" - Keys: {list(strategic_intelligence.keys())}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Strategic intelligence failed: {e}")
|
||||
print(f" - Error type: {type(e).__name__}")
|
||||
traceback.print_exc()
|
||||
|
||||
# Test content evolution
|
||||
print("\n🧪 Testing content evolution...")
|
||||
try:
|
||||
evolution_analysis = await ai_service.analyze_content_evolution(
|
||||
strategy_id=1,
|
||||
time_period="30d"
|
||||
)
|
||||
print(f"✅ Content evolution completed: {len(evolution_analysis)} keys")
|
||||
print(f" - Keys: {list(evolution_analysis.keys())}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Content evolution failed: {e}")
|
||||
print(f" - Error type: {type(e).__name__}")
|
||||
traceback.print_exc()
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
print("📊 AI Service Debug Complete")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ AI service test failed: {e}")
|
||||
traceback.print_exc()
|
||||
|
||||
async def test_ai_engine_service():
|
||||
"""Test the AI engine service that AI analytics depends on."""
|
||||
try:
|
||||
print("\n🧪 Testing AI Engine Service")
|
||||
print("=" * 30)
|
||||
|
||||
from services.content_gap_analyzer.ai_engine_service import AIEngineService
|
||||
|
||||
print("✅ AI Engine Service imported successfully")
|
||||
|
||||
# Create service instance
|
||||
ai_engine = AIEngineService()
|
||||
print("✅ AI Engine Service instantiated")
|
||||
|
||||
# Test a simple AI call
|
||||
print("\n🧪 Testing simple AI call...")
|
||||
try:
|
||||
# Test with a simple prompt
|
||||
result = await ai_engine.generate_recommendations(
|
||||
website_analysis={"content_types": ["blog", "video"]},
|
||||
competitor_analysis={"top_performers": ["competitor1.com"]},
|
||||
gap_analysis={"content_gaps": ["AI content"]},
|
||||
keyword_analysis={"high_value_keywords": ["AI marketing"]}
|
||||
)
|
||||
print(f"✅ AI engine call completed: {type(result)}")
|
||||
print(f" - Result: {result}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ AI engine call failed: {e}")
|
||||
print(f" - Error type: {type(e).__name__}")
|
||||
traceback.print_exc()
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ AI engine test failed: {e}")
|
||||
traceback.print_exc()
|
||||
|
||||
async def main():
|
||||
"""Run all AI service tests."""
|
||||
await test_ai_analytics_service()
|
||||
await test_ai_engine_service()
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
512
docs/alwrity_test_scripts/test_api_database_integration.py
Normal file
512
docs/alwrity_test_scripts/test_api_database_integration.py
Normal file
@@ -0,0 +1,512 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for API Database Integration
|
||||
Verifies that all API endpoints with database integration are working correctly.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
import requests
|
||||
import json
|
||||
from pathlib import Path
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
# Add the backend directory to the Python path
|
||||
sys.path.append(str(Path(__file__).parent / "backend"))
|
||||
|
||||
from services.database import init_database, get_db_session
|
||||
from services.content_planning_db import ContentPlanningDBService
|
||||
from loguru import logger
|
||||
|
||||
# API base URL
|
||||
API_BASE_URL = "http://localhost:8000"
|
||||
|
||||
def test_database_initialization():
|
||||
"""Test database initialization."""
|
||||
|
||||
print("🗄️ Testing Database Initialization...")
|
||||
|
||||
try:
|
||||
# Initialize database
|
||||
init_database()
|
||||
print("✅ Database initialized successfully")
|
||||
|
||||
# Test database session
|
||||
db_session = get_db_session()
|
||||
if db_session:
|
||||
print("✅ Database session created successfully")
|
||||
db_session.close()
|
||||
return True
|
||||
else:
|
||||
print("❌ Failed to create database session")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Database initialization failed: {str(e)}")
|
||||
return False
|
||||
|
||||
def test_api_health_check():
|
||||
"""Test API health check endpoints."""
|
||||
|
||||
print("\n🏥 Testing API Health Checks...")
|
||||
|
||||
# Test content planning health check
|
||||
try:
|
||||
response = requests.get(f"{API_BASE_URL}/api/content-planning/health")
|
||||
if response.status_code == 200:
|
||||
health_data = response.json()
|
||||
print(f"✅ Content planning health check: {health_data['status']}")
|
||||
else:
|
||||
print(f"❌ Content planning health check failed: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Content planning health check error: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test database health check
|
||||
try:
|
||||
response = requests.get(f"{API_BASE_URL}/api/content-planning/database/health")
|
||||
if response.status_code == 200:
|
||||
health_data = response.json()
|
||||
print(f"✅ Database health check: {health_data['status']}")
|
||||
else:
|
||||
print(f"❌ Database health check failed: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Database health check error: {str(e)}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def test_content_strategy_api():
|
||||
"""Test content strategy API endpoints."""
|
||||
|
||||
print("\n📋 Testing Content Strategy API...")
|
||||
|
||||
# Test 1: Create content strategy
|
||||
print("\n📝 Test 1: Create Content Strategy")
|
||||
strategy_data = {
|
||||
"user_id": 1,
|
||||
"name": "Test Content Strategy",
|
||||
"industry": "technology",
|
||||
"target_audience": {
|
||||
"demographics": "25-45 years old",
|
||||
"interests": ["technology", "innovation"]
|
||||
},
|
||||
"content_pillars": [
|
||||
{"name": "AI", "description": "Artificial Intelligence content"},
|
||||
{"name": "Machine Learning", "description": "ML tutorials and guides"}
|
||||
],
|
||||
"ai_recommendations": {
|
||||
"strategic_insights": ["Focus on educational content"],
|
||||
"content_recommendations": ["Create comprehensive guides"]
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{API_BASE_URL}/api/content-planning/strategies/",
|
||||
json=strategy_data
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
strategy = response.json()
|
||||
print(f"✅ Content strategy created: {strategy['id']}")
|
||||
strategy_id = strategy['id']
|
||||
else:
|
||||
print(f"❌ Failed to create content strategy: {response.status_code}")
|
||||
print(f"Response: {response.text}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating content strategy: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Get content strategy
|
||||
print("\n📖 Test 2: Get Content Strategy")
|
||||
try:
|
||||
response = requests.get(f"{API_BASE_URL}/api/content-planning/strategies/{strategy_id}")
|
||||
|
||||
if response.status_code == 200:
|
||||
strategy = response.json()
|
||||
print(f"✅ Content strategy retrieved: {strategy['name']}")
|
||||
else:
|
||||
print(f"❌ Failed to retrieve content strategy: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error retrieving content strategy: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 3: Get user strategies
|
||||
print("\n👤 Test 3: Get User Content Strategies")
|
||||
try:
|
||||
response = requests.get(f"{API_BASE_URL}/api/content-planning/strategies/?user_id=1")
|
||||
|
||||
if response.status_code == 200:
|
||||
strategies = response.json()
|
||||
print(f"✅ Retrieved {len(strategies)} user strategies")
|
||||
else:
|
||||
print(f"❌ Failed to get user strategies: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting user strategies: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 4: Update content strategy
|
||||
print("\n✏️ Test 4: Update Content Strategy")
|
||||
update_data = {
|
||||
"name": "Updated Test Content Strategy",
|
||||
"industry": "artificial_intelligence"
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.put(
|
||||
f"{API_BASE_URL}/api/content-planning/strategies/{strategy_id}",
|
||||
json=update_data
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
strategy = response.json()
|
||||
print(f"✅ Content strategy updated: {strategy['name']}")
|
||||
else:
|
||||
print(f"❌ Failed to update content strategy: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error updating content strategy: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 5: Delete content strategy
|
||||
print("\n🗑️ Test 5: Delete Content Strategy")
|
||||
try:
|
||||
response = requests.delete(f"{API_BASE_URL}/api/content-planning/strategies/{strategy_id}")
|
||||
|
||||
if response.status_code == 200:
|
||||
print("✅ Content strategy deleted successfully")
|
||||
else:
|
||||
print(f"❌ Failed to delete content strategy: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error deleting content strategy: {str(e)}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def test_calendar_event_api():
|
||||
"""Test calendar event API endpoints."""
|
||||
|
||||
print("\n📅 Testing Calendar Event API...")
|
||||
|
||||
# First create a strategy for the event
|
||||
strategy_data = {
|
||||
"user_id": 1,
|
||||
"name": "Test Strategy for Events",
|
||||
"industry": "technology"
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{API_BASE_URL}/api/content-planning/strategies/",
|
||||
json=strategy_data
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
strategy = response.json()
|
||||
strategy_id = strategy['id']
|
||||
else:
|
||||
print(f"❌ Failed to create test strategy: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating test strategy: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 1: Create calendar event
|
||||
print("\n📝 Test 1: Create Calendar Event")
|
||||
event_data = {
|
||||
"strategy_id": strategy_id,
|
||||
"title": "Test Blog Post",
|
||||
"description": "A comprehensive guide to AI",
|
||||
"content_type": "blog_post",
|
||||
"platform": "website",
|
||||
"scheduled_date": (datetime.utcnow() + timedelta(days=7)).isoformat(),
|
||||
"ai_recommendations": {
|
||||
"keywords": ["AI", "machine learning"],
|
||||
"estimated_performance": "High engagement expected"
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{API_BASE_URL}/api/content-planning/calendar-events/",
|
||||
json=event_data
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
event = response.json()
|
||||
print(f"✅ Calendar event created: {event['id']}")
|
||||
event_id = event['id']
|
||||
else:
|
||||
print(f"❌ Failed to create calendar event: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating calendar event: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Get calendar event
|
||||
print("\n📖 Test 2: Get Calendar Event")
|
||||
try:
|
||||
response = requests.get(f"{API_BASE_URL}/api/content-planning/calendar-events/{event_id}")
|
||||
|
||||
if response.status_code == 200:
|
||||
event = response.json()
|
||||
print(f"✅ Calendar event retrieved: {event['title']}")
|
||||
else:
|
||||
print(f"❌ Failed to retrieve calendar event: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error retrieving calendar event: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 3: Get strategy events
|
||||
print("\n📋 Test 3: Get Strategy Calendar Events")
|
||||
try:
|
||||
response = requests.get(f"{API_BASE_URL}/api/content-planning/calendar-events/?strategy_id={strategy_id}")
|
||||
|
||||
if response.status_code == 200:
|
||||
events = response.json()
|
||||
print(f"✅ Retrieved {len(events)} strategy events")
|
||||
else:
|
||||
print(f"❌ Failed to get strategy events: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting strategy events: {str(e)}")
|
||||
return False
|
||||
|
||||
# Clean up
|
||||
try:
|
||||
requests.delete(f"{API_BASE_URL}/api/content-planning/strategies/{strategy_id}")
|
||||
except:
|
||||
pass
|
||||
|
||||
return True
|
||||
|
||||
def test_content_gap_analysis_api():
|
||||
"""Test content gap analysis API endpoints."""
|
||||
|
||||
print("\n🔍 Testing Content Gap Analysis API...")
|
||||
|
||||
# Test 1: Create content gap analysis
|
||||
print("\n📝 Test 1: Create Content Gap Analysis")
|
||||
analysis_data = {
|
||||
"user_id": 1,
|
||||
"website_url": "https://example.com",
|
||||
"competitor_urls": ["https://competitor1.com", "https://competitor2.com"],
|
||||
"target_keywords": ["AI", "machine learning", "data science"],
|
||||
"industry": "technology",
|
||||
"analysis_results": {
|
||||
"content_gaps": ["Video tutorials", "Case studies"],
|
||||
"opportunities": ["Educational content", "Expert interviews"]
|
||||
},
|
||||
"recommendations": {
|
||||
"strategic_insights": ["Focus on educational content"],
|
||||
"content_recommendations": ["Create comprehensive guides"]
|
||||
},
|
||||
"opportunities": {
|
||||
"high_priority": ["Video tutorials"],
|
||||
"medium_priority": ["Case studies"]
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{API_BASE_URL}/api/content-planning/gap-analysis/",
|
||||
json=analysis_data
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
analysis = response.json()
|
||||
print(f"✅ Content gap analysis created: {analysis['id']}")
|
||||
analysis_id = analysis['id']
|
||||
else:
|
||||
print(f"❌ Failed to create content gap analysis: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating content gap analysis: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Get content gap analysis
|
||||
print("\n📖 Test 2: Get Content Gap Analysis")
|
||||
try:
|
||||
response = requests.get(f"{API_BASE_URL}/api/content-planning/gap-analysis/{analysis_id}")
|
||||
|
||||
if response.status_code == 200:
|
||||
analysis = response.json()
|
||||
print(f"✅ Content gap analysis retrieved: {analysis['website_url']}")
|
||||
else:
|
||||
print(f"❌ Failed to retrieve content gap analysis: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error retrieving content gap analysis: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 3: Get user analyses
|
||||
print("\n👤 Test 3: Get User Content Gap Analyses")
|
||||
try:
|
||||
response = requests.get(f"{API_BASE_URL}/api/content-planning/gap-analysis/?user_id=1")
|
||||
|
||||
if response.status_code == 200:
|
||||
analyses = response.json()
|
||||
print(f"✅ Retrieved {len(analyses)} user analyses")
|
||||
else:
|
||||
print(f"❌ Failed to get user analyses: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting user analyses: {str(e)}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def test_advanced_api_endpoints():
|
||||
"""Test advanced API endpoints."""
|
||||
|
||||
print("\n🚀 Testing Advanced API Endpoints...")
|
||||
|
||||
# Create a test strategy first
|
||||
strategy_data = {
|
||||
"user_id": 1,
|
||||
"name": "Advanced Test Strategy",
|
||||
"industry": "technology"
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{API_BASE_URL}/api/content-planning/strategies/",
|
||||
json=strategy_data
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
strategy = response.json()
|
||||
strategy_id = strategy['id']
|
||||
else:
|
||||
print(f"❌ Failed to create test strategy: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating test strategy: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 1: Get strategy analytics
|
||||
print("\n📊 Test 1: Get Strategy Analytics")
|
||||
try:
|
||||
response = requests.get(f"{API_BASE_URL}/api/content-planning/strategies/{strategy_id}/analytics")
|
||||
|
||||
if response.status_code == 200:
|
||||
analytics = response.json()
|
||||
print(f"✅ Strategy analytics retrieved: {analytics['analytics_count']} records")
|
||||
else:
|
||||
print(f"❌ Failed to get strategy analytics: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting strategy analytics: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Get strategy events
|
||||
print("\n📅 Test 2: Get Strategy Events")
|
||||
try:
|
||||
response = requests.get(f"{API_BASE_URL}/api/content-planning/strategies/{strategy_id}/events")
|
||||
|
||||
if response.status_code == 200:
|
||||
events = response.json()
|
||||
print(f"✅ Strategy events retrieved: {events['events_count']} events")
|
||||
else:
|
||||
print(f"❌ Failed to get strategy events: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting strategy events: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 3: Get user recommendations
|
||||
print("\n💡 Test 3: Get User Recommendations")
|
||||
try:
|
||||
response = requests.get(f"{API_BASE_URL}/api/content-planning/users/1/recommendations")
|
||||
|
||||
if response.status_code == 200:
|
||||
recommendations = response.json()
|
||||
print(f"✅ User recommendations retrieved: {recommendations['recommendations_count']} recommendations")
|
||||
else:
|
||||
print(f"❌ Failed to get user recommendations: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting user recommendations: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 4: Get strategy summary
|
||||
print("\n📋 Test 4: Get Strategy Summary")
|
||||
try:
|
||||
response = requests.get(f"{API_BASE_URL}/api/content-planning/strategies/{strategy_id}/summary")
|
||||
|
||||
if response.status_code == 200:
|
||||
summary = response.json()
|
||||
print(f"✅ Strategy summary retrieved successfully")
|
||||
else:
|
||||
print(f"❌ Failed to get strategy summary: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting strategy summary: {str(e)}")
|
||||
return False
|
||||
|
||||
# Clean up
|
||||
try:
|
||||
requests.delete(f"{API_BASE_URL}/api/content-planning/strategies/{strategy_id}")
|
||||
except:
|
||||
pass
|
||||
|
||||
return True
|
||||
|
||||
def main():
|
||||
"""Main test function."""
|
||||
print("🚀 Starting API Database Integration Tests...")
|
||||
print("=" * 60)
|
||||
|
||||
# Test 1: Database Initialization
|
||||
db_init_success = test_database_initialization()
|
||||
|
||||
# Test 2: API Health Checks
|
||||
health_success = test_api_health_check()
|
||||
|
||||
# Test 3: Content Strategy API
|
||||
strategy_success = test_content_strategy_api()
|
||||
|
||||
# Test 4: Calendar Event API
|
||||
event_success = test_calendar_event_api()
|
||||
|
||||
# Test 5: Content Gap Analysis API
|
||||
analysis_success = test_content_gap_analysis_api()
|
||||
|
||||
# Test 6: Advanced API Endpoints
|
||||
advanced_success = test_advanced_api_endpoints()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("📊 Test Results Summary:")
|
||||
print(f"Database Initialization: {'✅ PASSED' if db_init_success else '❌ FAILED'}")
|
||||
print(f"API Health Checks: {'✅ PASSED' if health_success else '❌ FAILED'}")
|
||||
print(f"Content Strategy API: {'✅ PASSED' if strategy_success else '❌ FAILED'}")
|
||||
print(f"Calendar Event API: {'✅ PASSED' if event_success else '❌ FAILED'}")
|
||||
print(f"Content Gap Analysis API: {'✅ PASSED' if analysis_success else '❌ FAILED'}")
|
||||
print(f"Advanced API Endpoints: {'✅ PASSED' if advanced_success else '❌ FAILED'}")
|
||||
|
||||
if db_init_success and health_success and strategy_success and event_success and analysis_success and advanced_success:
|
||||
print("\n🎉 All API database integration tests passed!")
|
||||
print("\n✅ API Database Integration Achievements:")
|
||||
print(" - Database models integrated with API endpoints")
|
||||
print(" - All CRUD operations working via API")
|
||||
print(" - Health checks for both services and database")
|
||||
print(" - Advanced query endpoints functional")
|
||||
print(" - Error handling and validation working")
|
||||
print(" - RESTful API design implemented")
|
||||
return 0
|
||||
else:
|
||||
print("\n⚠️ Some API database integration tests failed. Please check the API server and database configuration.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit_code = main()
|
||||
sys.exit(exit_code)
|
||||
637
docs/alwrity_test_scripts/test_database_integration.py
Normal file
637
docs/alwrity_test_scripts/test_database_integration.py
Normal file
@@ -0,0 +1,637 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for Database Integration
|
||||
Verifies that all database operations are working correctly.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
|
||||
# Add the backend directory to the Python path
|
||||
sys.path.append(str(Path(__file__).parent / "backend"))
|
||||
|
||||
from services.database import get_db_session, init_database
|
||||
from services.content_planning_db import ContentPlanningDBService
|
||||
from loguru import logger
|
||||
|
||||
async def test_database_initialization():
|
||||
"""Test database initialization."""
|
||||
|
||||
print("🗄️ Testing Database Initialization...")
|
||||
|
||||
try:
|
||||
# Initialize database
|
||||
init_database()
|
||||
print("✅ Database initialized successfully")
|
||||
|
||||
# Test database session
|
||||
db_session = get_db_session()
|
||||
if db_session:
|
||||
print("✅ Database session created successfully")
|
||||
db_session.close()
|
||||
return True
|
||||
else:
|
||||
print("❌ Failed to create database session")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Database initialization failed: {str(e)}")
|
||||
return False
|
||||
|
||||
async def test_content_strategy_operations():
|
||||
"""Test content strategy database operations."""
|
||||
|
||||
print("\n📋 Testing Content Strategy Operations...")
|
||||
|
||||
db_session = get_db_session()
|
||||
if not db_session:
|
||||
print("❌ No database session available")
|
||||
return False
|
||||
|
||||
db_service = ContentPlanningDBService(db_session)
|
||||
|
||||
# Test 1: Create content strategy
|
||||
print("\n📝 Test 1: Create Content Strategy")
|
||||
strategy_data = {
|
||||
'user_id': 1,
|
||||
'name': 'Test Content Strategy',
|
||||
'industry': 'technology',
|
||||
'target_audience': {
|
||||
'demographics': '25-45 years old',
|
||||
'interests': ['technology', 'innovation']
|
||||
},
|
||||
'content_pillars': ['AI', 'Machine Learning', 'Data Science'],
|
||||
'ai_recommendations': {
|
||||
'strategic_insights': ['Focus on educational content'],
|
||||
'content_recommendations': ['Create comprehensive guides']
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
strategy = await db_service.create_content_strategy(strategy_data)
|
||||
if strategy:
|
||||
print(f"✅ Content strategy created: {strategy.id}")
|
||||
strategy_id = strategy.id
|
||||
else:
|
||||
print("❌ Failed to create content strategy")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating content strategy: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Get content strategy
|
||||
print("\n📖 Test 2: Get Content Strategy")
|
||||
try:
|
||||
retrieved_strategy = await db_service.get_content_strategy(strategy_id)
|
||||
if retrieved_strategy:
|
||||
print(f"✅ Content strategy retrieved: {retrieved_strategy.name}")
|
||||
else:
|
||||
print("❌ Failed to retrieve content strategy")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error retrieving content strategy: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 3: Update content strategy
|
||||
print("\n✏️ Test 3: Update Content Strategy")
|
||||
update_data = {
|
||||
'name': 'Updated Test Content Strategy',
|
||||
'industry': 'artificial_intelligence'
|
||||
}
|
||||
|
||||
try:
|
||||
updated_strategy = await db_service.update_content_strategy(strategy_id, update_data)
|
||||
if updated_strategy:
|
||||
print(f"✅ Content strategy updated: {updated_strategy.name}")
|
||||
else:
|
||||
print("❌ Failed to update content strategy")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error updating content strategy: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 4: Get user strategies
|
||||
print("\n👤 Test 4: Get User Content Strategies")
|
||||
try:
|
||||
user_strategies = await db_service.get_user_content_strategies(1)
|
||||
print(f"✅ Retrieved {len(user_strategies)} user strategies")
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting user strategies: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 5: Delete content strategy
|
||||
print("\n🗑️ Test 5: Delete Content Strategy")
|
||||
try:
|
||||
deleted = await db_service.delete_content_strategy(strategy_id)
|
||||
if deleted:
|
||||
print("✅ Content strategy deleted successfully")
|
||||
else:
|
||||
print("❌ Failed to delete content strategy")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error deleting content strategy: {str(e)}")
|
||||
return False
|
||||
|
||||
db_session.close()
|
||||
return True
|
||||
|
||||
async def test_calendar_event_operations():
|
||||
"""Test calendar event database operations."""
|
||||
|
||||
print("\n📅 Testing Calendar Event Operations...")
|
||||
|
||||
db_session = get_db_session()
|
||||
if not db_session:
|
||||
print("❌ No database session available")
|
||||
return False
|
||||
|
||||
db_service = ContentPlanningDBService(db_session)
|
||||
|
||||
# First create a strategy for the event
|
||||
strategy_data = {
|
||||
'user_id': 1,
|
||||
'name': 'Test Strategy for Events',
|
||||
'industry': 'technology'
|
||||
}
|
||||
strategy = await db_service.create_content_strategy(strategy_data)
|
||||
if not strategy:
|
||||
print("❌ Failed to create test strategy")
|
||||
return False
|
||||
|
||||
# Test 1: Create calendar event
|
||||
print("\n📝 Test 1: Create Calendar Event")
|
||||
event_data = {
|
||||
'strategy_id': strategy.id,
|
||||
'title': 'Test Blog Post',
|
||||
'description': 'A comprehensive guide to AI',
|
||||
'content_type': 'blog_post',
|
||||
'platform': 'website',
|
||||
'scheduled_date': datetime.utcnow(),
|
||||
'status': 'draft',
|
||||
'ai_recommendations': {
|
||||
'keywords': ['AI', 'machine learning'],
|
||||
'estimated_performance': 'High engagement expected'
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
event = await db_service.create_calendar_event(event_data)
|
||||
if event:
|
||||
print(f"✅ Calendar event created: {event.id}")
|
||||
event_id = event.id
|
||||
else:
|
||||
print("❌ Failed to create calendar event")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating calendar event: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Get calendar event
|
||||
print("\n📖 Test 2: Get Calendar Event")
|
||||
try:
|
||||
retrieved_event = await db_service.get_calendar_event(event_id)
|
||||
if retrieved_event:
|
||||
print(f"✅ Calendar event retrieved: {retrieved_event.title}")
|
||||
else:
|
||||
print("❌ Failed to retrieve calendar event")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error retrieving calendar event: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 3: Get strategy events
|
||||
print("\n📋 Test 3: Get Strategy Calendar Events")
|
||||
try:
|
||||
strategy_events = await db_service.get_strategy_calendar_events(strategy.id)
|
||||
print(f"✅ Retrieved {len(strategy_events)} strategy events")
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting strategy events: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 4: Update calendar event
|
||||
print("\n✏️ Test 4: Update Calendar Event")
|
||||
update_data = {
|
||||
'title': 'Updated Test Blog Post',
|
||||
'status': 'scheduled'
|
||||
}
|
||||
|
||||
try:
|
||||
updated_event = await db_service.update_calendar_event(event_id, update_data)
|
||||
if updated_event:
|
||||
print(f"✅ Calendar event updated: {updated_event.title}")
|
||||
else:
|
||||
print("❌ Failed to update calendar event")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error updating calendar event: {str(e)}")
|
||||
return False
|
||||
|
||||
# Clean up
|
||||
await db_service.delete_content_strategy(strategy.id)
|
||||
db_session.close()
|
||||
return True
|
||||
|
||||
async def test_content_gap_analysis_operations():
|
||||
"""Test content gap analysis database operations."""
|
||||
|
||||
print("\n🔍 Testing Content Gap Analysis Operations...")
|
||||
|
||||
db_session = get_db_session()
|
||||
if not db_session:
|
||||
print("❌ No database session available")
|
||||
return False
|
||||
|
||||
db_service = ContentPlanningDBService(db_session)
|
||||
|
||||
# Test 1: Create content gap analysis
|
||||
print("\n📝 Test 1: Create Content Gap Analysis")
|
||||
analysis_data = {
|
||||
'user_id': 1,
|
||||
'website_url': 'https://example.com',
|
||||
'competitor_urls': ['https://competitor1.com', 'https://competitor2.com'],
|
||||
'target_keywords': ['AI', 'machine learning', 'data science'],
|
||||
'analysis_results': {
|
||||
'content_gaps': ['Video tutorials', 'Case studies'],
|
||||
'opportunities': ['Educational content', 'Expert interviews']
|
||||
},
|
||||
'recommendations': {
|
||||
'strategic_insights': ['Focus on educational content'],
|
||||
'content_recommendations': ['Create comprehensive guides']
|
||||
},
|
||||
'opportunities': {
|
||||
'high_priority': ['Video tutorials'],
|
||||
'medium_priority': ['Case studies']
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
analysis = await db_service.create_content_gap_analysis(analysis_data)
|
||||
if analysis:
|
||||
print(f"✅ Content gap analysis created: {analysis.id}")
|
||||
analysis_id = analysis.id
|
||||
else:
|
||||
print("❌ Failed to create content gap analysis")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating content gap analysis: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Get content gap analysis
|
||||
print("\n📖 Test 2: Get Content Gap Analysis")
|
||||
try:
|
||||
retrieved_analysis = await db_service.get_content_gap_analysis(analysis_id)
|
||||
if retrieved_analysis:
|
||||
print(f"✅ Content gap analysis retrieved: {retrieved_analysis.website_url}")
|
||||
else:
|
||||
print("❌ Failed to retrieve content gap analysis")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error retrieving content gap analysis: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 3: Get user analyses
|
||||
print("\n👤 Test 3: Get User Content Gap Analyses")
|
||||
try:
|
||||
user_analyses = await db_service.get_user_content_gap_analyses(1)
|
||||
print(f"✅ Retrieved {len(user_analyses)} user analyses")
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting user analyses: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 4: Update content gap analysis
|
||||
print("\n✏️ Test 4: Update Content Gap Analysis")
|
||||
update_data = {
|
||||
'website_url': 'https://updated-example.com',
|
||||
'analysis_results': {
|
||||
'content_gaps': ['Video tutorials', 'Case studies', 'Webinars'],
|
||||
'opportunities': ['Educational content', 'Expert interviews', 'Interactive content']
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
updated_analysis = await db_service.update_content_gap_analysis(analysis_id, update_data)
|
||||
if updated_analysis:
|
||||
print(f"✅ Content gap analysis updated: {updated_analysis.website_url}")
|
||||
else:
|
||||
print("❌ Failed to update content gap analysis")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error updating content gap analysis: {str(e)}")
|
||||
return False
|
||||
|
||||
# Clean up
|
||||
await db_service.delete_content_gap_analysis(analysis_id)
|
||||
db_session.close()
|
||||
return True
|
||||
|
||||
async def test_content_recommendation_operations():
|
||||
"""Test content recommendation database operations."""
|
||||
|
||||
print("\n💡 Testing Content Recommendation Operations...")
|
||||
|
||||
db_session = get_db_session()
|
||||
if not db_session:
|
||||
print("❌ No database session available")
|
||||
return False
|
||||
|
||||
db_service = ContentPlanningDBService(db_session)
|
||||
|
||||
# Test 1: Create content recommendation
|
||||
print("\n📝 Test 1: Create Content Recommendation")
|
||||
recommendation_data = {
|
||||
'user_id': 1,
|
||||
'recommendation_type': 'blog_post',
|
||||
'title': 'Complete Guide to AI Implementation',
|
||||
'description': 'A comprehensive guide for implementing AI in business',
|
||||
'target_keywords': ['AI implementation', 'business AI', 'AI strategy'],
|
||||
'estimated_length': '2000-3000 words',
|
||||
'priority': 'high',
|
||||
'platforms': ['website', 'linkedin'],
|
||||
'estimated_performance': 'High engagement expected',
|
||||
'status': 'pending'
|
||||
}
|
||||
|
||||
try:
|
||||
recommendation = await db_service.create_content_recommendation(recommendation_data)
|
||||
if recommendation:
|
||||
print(f"✅ Content recommendation created: {recommendation.id}")
|
||||
recommendation_id = recommendation.id
|
||||
else:
|
||||
print("❌ Failed to create content recommendation")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating content recommendation: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Get content recommendation
|
||||
print("\n📖 Test 2: Get Content Recommendation")
|
||||
try:
|
||||
retrieved_recommendation = await db_service.get_content_recommendation(recommendation_id)
|
||||
if retrieved_recommendation:
|
||||
print(f"✅ Content recommendation retrieved: {retrieved_recommendation.title}")
|
||||
else:
|
||||
print("❌ Failed to retrieve content recommendation")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error retrieving content recommendation: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 3: Get user recommendations
|
||||
print("\n👤 Test 3: Get User Content Recommendations")
|
||||
try:
|
||||
user_recommendations = await db_service.get_user_content_recommendations(1)
|
||||
print(f"✅ Retrieved {len(user_recommendations)} user recommendations")
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting user recommendations: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 4: Update content recommendation
|
||||
print("\n✏️ Test 4: Update Content Recommendation")
|
||||
update_data = {
|
||||
'title': 'Updated Complete Guide to AI Implementation',
|
||||
'status': 'accepted',
|
||||
'priority': 'medium'
|
||||
}
|
||||
|
||||
try:
|
||||
updated_recommendation = await db_service.update_content_recommendation(recommendation_id, update_data)
|
||||
if updated_recommendation:
|
||||
print(f"✅ Content recommendation updated: {updated_recommendation.title}")
|
||||
else:
|
||||
print("❌ Failed to update content recommendation")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error updating content recommendation: {str(e)}")
|
||||
return False
|
||||
|
||||
# Clean up
|
||||
await db_service.delete_content_recommendation(recommendation_id)
|
||||
db_session.close()
|
||||
return True
|
||||
|
||||
async def test_analytics_operations():
|
||||
"""Test analytics database operations."""
|
||||
|
||||
print("\n📊 Testing Analytics Operations...")
|
||||
|
||||
db_session = get_db_session()
|
||||
if not db_session:
|
||||
print("❌ No database session available")
|
||||
return False
|
||||
|
||||
db_service = ContentPlanningDBService(db_session)
|
||||
|
||||
# Create test strategy and event for analytics
|
||||
strategy_data = {
|
||||
'user_id': 1,
|
||||
'name': 'Test Strategy for Analytics',
|
||||
'industry': 'technology'
|
||||
}
|
||||
strategy = await db_service.create_content_strategy(strategy_data)
|
||||
|
||||
event_data = {
|
||||
'strategy_id': strategy.id,
|
||||
'title': 'Test Event for Analytics',
|
||||
'content_type': 'blog_post',
|
||||
'platform': 'website',
|
||||
'scheduled_date': datetime.utcnow(),
|
||||
'status': 'published'
|
||||
}
|
||||
event = await db_service.create_calendar_event(event_data)
|
||||
|
||||
# Test 1: Create content analytics
|
||||
print("\n📝 Test 1: Create Content Analytics")
|
||||
analytics_data = {
|
||||
'event_id': event.id,
|
||||
'strategy_id': strategy.id,
|
||||
'platform': 'website',
|
||||
'metrics': {
|
||||
'page_views': 1500,
|
||||
'unique_visitors': 800,
|
||||
'time_on_page': 180,
|
||||
'bounce_rate': 0.25,
|
||||
'social_shares': 45
|
||||
},
|
||||
'performance_score': 8.5,
|
||||
'recorded_at': datetime.utcnow()
|
||||
}
|
||||
|
||||
try:
|
||||
analytics = await db_service.create_content_analytics(analytics_data)
|
||||
if analytics:
|
||||
print(f"✅ Content analytics created: {analytics.id}")
|
||||
analytics_id = analytics.id
|
||||
else:
|
||||
print("❌ Failed to create content analytics")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating content analytics: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Get event analytics
|
||||
print("\n📖 Test 2: Get Event Analytics")
|
||||
try:
|
||||
event_analytics = await db_service.get_event_analytics(event.id)
|
||||
print(f"✅ Retrieved {len(event_analytics)} event analytics")
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting event analytics: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 3: Get strategy analytics
|
||||
print("\n📋 Test 3: Get Strategy Analytics")
|
||||
try:
|
||||
strategy_analytics = await db_service.get_strategy_analytics(strategy.id)
|
||||
print(f"✅ Retrieved {len(strategy_analytics)} strategy analytics")
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting strategy analytics: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 4: Get platform analytics
|
||||
print("\n🌐 Test 4: Get Platform Analytics")
|
||||
try:
|
||||
platform_analytics = await db_service.get_analytics_by_platform('website')
|
||||
print(f"✅ Retrieved {len(platform_analytics)} platform analytics")
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting platform analytics: {str(e)}")
|
||||
return False
|
||||
|
||||
# Clean up
|
||||
await db_service.delete_content_strategy(strategy.id)
|
||||
db_session.close()
|
||||
return True
|
||||
|
||||
async def test_advanced_operations():
|
||||
"""Test advanced database operations."""
|
||||
|
||||
print("\n🚀 Testing Advanced Operations...")
|
||||
|
||||
db_session = get_db_session()
|
||||
if not db_session:
|
||||
print("❌ No database session available")
|
||||
return False
|
||||
|
||||
db_service = ContentPlanningDBService(db_session)
|
||||
|
||||
# Create test data
|
||||
strategy_data = {
|
||||
'user_id': 1,
|
||||
'name': 'Advanced Test Strategy',
|
||||
'industry': 'technology'
|
||||
}
|
||||
strategy = await db_service.create_content_strategy(strategy_data)
|
||||
|
||||
# Create multiple events
|
||||
events_data = [
|
||||
{
|
||||
'strategy_id': strategy.id,
|
||||
'title': 'Event 1',
|
||||
'content_type': 'blog_post',
|
||||
'platform': 'website',
|
||||
'scheduled_date': datetime.utcnow(),
|
||||
'status': 'published'
|
||||
},
|
||||
{
|
||||
'strategy_id': strategy.id,
|
||||
'title': 'Event 2',
|
||||
'content_type': 'video',
|
||||
'platform': 'youtube',
|
||||
'scheduled_date': datetime.utcnow(),
|
||||
'status': 'draft'
|
||||
}
|
||||
]
|
||||
|
||||
for event_data in events_data:
|
||||
await db_service.create_calendar_event(event_data)
|
||||
|
||||
# Test 1: Get strategies with analytics
|
||||
print("\n📊 Test 1: Get Strategies with Analytics")
|
||||
try:
|
||||
strategies_with_analytics = await db_service.get_strategies_with_analytics(1)
|
||||
print(f"✅ Retrieved {len(strategies_with_analytics)} strategies with analytics")
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting strategies with analytics: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Get events by status
|
||||
print("\n📋 Test 2: Get Events by Status")
|
||||
try:
|
||||
published_events = await db_service.get_events_by_status(strategy.id, 'published')
|
||||
draft_events = await db_service.get_events_by_status(strategy.id, 'draft')
|
||||
print(f"✅ Retrieved {len(published_events)} published events and {len(draft_events)} draft events")
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting events by status: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 3: Health check
|
||||
print("\n🏥 Test 3: Database Health Check")
|
||||
try:
|
||||
health_status = await db_service.health_check()
|
||||
print(f"✅ Health check completed: {health_status['status']}")
|
||||
print(f" - Tables: {len(health_status['tables'])}")
|
||||
except Exception as e:
|
||||
print(f"❌ Error in health check: {str(e)}")
|
||||
return False
|
||||
|
||||
# Clean up
|
||||
await db_service.delete_content_strategy(strategy.id)
|
||||
db_session.close()
|
||||
return True
|
||||
|
||||
async def main():
|
||||
"""Main test function."""
|
||||
print("🚀 Starting Database Integration Tests...")
|
||||
print("=" * 60)
|
||||
|
||||
# Test 1: Database Initialization
|
||||
db_init_success = await test_database_initialization()
|
||||
|
||||
# Test 2: Content Strategy Operations
|
||||
strategy_success = await test_content_strategy_operations()
|
||||
|
||||
# Test 3: Calendar Event Operations
|
||||
event_success = await test_calendar_event_operations()
|
||||
|
||||
# Test 4: Content Gap Analysis Operations
|
||||
analysis_success = await test_content_gap_analysis_operations()
|
||||
|
||||
# Test 5: Content Recommendation Operations
|
||||
recommendation_success = await test_content_recommendation_operations()
|
||||
|
||||
# Test 6: Analytics Operations
|
||||
analytics_success = await test_analytics_operations()
|
||||
|
||||
# Test 7: Advanced Operations
|
||||
advanced_success = await test_advanced_operations()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("📊 Test Results Summary:")
|
||||
print(f"Database Initialization: {'✅ PASSED' if db_init_success else '❌ FAILED'}")
|
||||
print(f"Content Strategy Operations: {'✅ PASSED' if strategy_success else '❌ FAILED'}")
|
||||
print(f"Calendar Event Operations: {'✅ PASSED' if event_success else '❌ FAILED'}")
|
||||
print(f"Content Gap Analysis Operations: {'✅ PASSED' if analysis_success else '❌ FAILED'}")
|
||||
print(f"Content Recommendation Operations: {'✅ PASSED' if recommendation_success else '❌ FAILED'}")
|
||||
print(f"Analytics Operations: {'✅ PASSED' if analytics_success else '❌ FAILED'}")
|
||||
print(f"Advanced Operations: {'✅ PASSED' if advanced_success else '❌ FAILED'}")
|
||||
|
||||
if (db_init_success and strategy_success and event_success and
|
||||
analysis_success and recommendation_success and analytics_success and advanced_success):
|
||||
print("\n🎉 All database integration tests passed!")
|
||||
print("\n✅ Database Integration Achievements:")
|
||||
print(" - Database models integrated successfully")
|
||||
print(" - All CRUD operations working correctly")
|
||||
print(" - Relationships and foreign keys functional")
|
||||
print(" - Error handling and rollback mechanisms working")
|
||||
print(" - Session management and connection handling operational")
|
||||
print(" - Advanced queries and analytics working")
|
||||
print(" - Health monitoring and status checks functional")
|
||||
return 0
|
||||
else:
|
||||
print("\n⚠️ Some database integration tests failed. Please check the database configuration.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit_code = asyncio.run(main())
|
||||
sys.exit(exit_code)
|
||||
118
docs/alwrity_test_scripts/test_endpoint_fixes.py
Normal file
118
docs/alwrity_test_scripts/test_endpoint_fixes.py
Normal file
@@ -0,0 +1,118 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify the endpoint fixes for 422 errors.
|
||||
"""
|
||||
|
||||
import requests
|
||||
import json
|
||||
import sys
|
||||
|
||||
def test_strategies_endpoint():
|
||||
"""Test the strategies endpoint that was causing 422 errors."""
|
||||
try:
|
||||
print("🧪 Testing strategies endpoint...")
|
||||
|
||||
# Test without user_id (should now work)
|
||||
response = requests.get("http://localhost:8000/api/content-planning/strategies/", timeout=10)
|
||||
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if isinstance(data, list) and len(data) > 0:
|
||||
print("✅ Strategies endpoint: PASSED")
|
||||
print(f" - Status: {response.status_code}")
|
||||
print(f" - Found {len(data)} strategies")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ Strategies endpoint: FAILED (Invalid response format: {data})")
|
||||
return False
|
||||
else:
|
||||
print(f"❌ Strategies endpoint: FAILED (Status: {response.status_code})")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Strategies endpoint: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def test_gap_analysis_endpoint():
|
||||
"""Test the gap analysis endpoint that was causing 422 errors."""
|
||||
try:
|
||||
print("🧪 Testing gap analysis endpoint...")
|
||||
|
||||
# Test without user_id (should now work)
|
||||
response = requests.get("http://localhost:8000/api/content-planning/gap-analysis/", timeout=10)
|
||||
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if isinstance(data, list) and len(data) > 0:
|
||||
print("✅ Gap analysis endpoint: PASSED")
|
||||
print(f" - Status: {response.status_code}")
|
||||
print(f" - Found {len(data)} analyses")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ Gap analysis endpoint: FAILED (Invalid response format: {data})")
|
||||
return False
|
||||
else:
|
||||
print(f"❌ Gap analysis endpoint: FAILED (Status: {response.status_code})")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Gap analysis endpoint: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def test_ai_analytics_endpoint():
|
||||
"""Test the AI analytics endpoint."""
|
||||
try:
|
||||
print("🧪 Testing AI analytics endpoint...")
|
||||
|
||||
response = requests.get("http://localhost:8000/api/content-planning/ai-analytics/", timeout=10)
|
||||
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if "insights" in data and "recommendations" in data:
|
||||
print("✅ AI analytics endpoint: PASSED")
|
||||
print(f" - Status: {response.status_code}")
|
||||
print(f" - Found {len(data['insights'])} insights")
|
||||
print(f" - Found {len(data['recommendations'])} recommendations")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ AI analytics endpoint: FAILED (Missing expected fields)")
|
||||
return False
|
||||
else:
|
||||
print(f"❌ AI analytics endpoint: FAILED (Status: {response.status_code})")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ AI analytics endpoint: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""Run all endpoint tests."""
|
||||
print("🧪 Testing Endpoint Fixes")
|
||||
print("=" * 50)
|
||||
|
||||
tests = [
|
||||
test_strategies_endpoint,
|
||||
test_gap_analysis_endpoint,
|
||||
test_ai_analytics_endpoint
|
||||
]
|
||||
|
||||
passed = 0
|
||||
total = len(tests)
|
||||
|
||||
for test in tests:
|
||||
if test():
|
||||
passed += 1
|
||||
print()
|
||||
|
||||
print("=" * 50)
|
||||
print(f"📊 Test Results: {passed}/{total} tests passed")
|
||||
|
||||
if passed == total:
|
||||
print("🎉 All endpoint tests passed! The 422 errors are fixed.")
|
||||
return 0
|
||||
else:
|
||||
print("⚠️ Some endpoint tests failed. Please check the backend.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
154
docs/alwrity_test_scripts/test_final_ai_integration.py
Normal file
154
docs/alwrity_test_scripts/test_final_ai_integration.py
Normal file
@@ -0,0 +1,154 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Final test to verify real AI integration is working.
|
||||
"""
|
||||
|
||||
import requests
|
||||
import json
|
||||
import sys
|
||||
|
||||
def test_ai_analytics_real_data():
|
||||
"""Test that AI analytics endpoint returns real AI insights."""
|
||||
try:
|
||||
print("🧪 Testing AI Analytics Real Data")
|
||||
print("=" * 40)
|
||||
|
||||
response = requests.get("http://localhost:8000/api/content-planning/ai-analytics/", timeout=30)
|
||||
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
|
||||
print(f"✅ AI Analytics endpoint: PASSED")
|
||||
print(f" - Status: {response.status_code}")
|
||||
print(f" - AI Service Status: {data.get('ai_service_status', 'unknown')}")
|
||||
print(f" - Total Insights: {data.get('total_insights', 0)}")
|
||||
print(f" - Total Recommendations: {data.get('total_recommendations', 0)}")
|
||||
|
||||
# Check if we have real AI insights
|
||||
insights = data.get('insights', [])
|
||||
if len(insights) > 0:
|
||||
print(f" - Real AI Insights Found: {len(insights)}")
|
||||
for i, insight in enumerate(insights[:2]): # Show first 2 insights
|
||||
print(f" {i+1}. {insight.get('title', 'No title')} ({insight.get('type', 'unknown')})")
|
||||
print(f" Priority: {insight.get('priority', 'unknown')}")
|
||||
print(f" Description: {insight.get('description', 'No description')[:80]}...")
|
||||
else:
|
||||
print(" - No insights found")
|
||||
|
||||
# Check recommendations
|
||||
recommendations = data.get('recommendations', [])
|
||||
if len(recommendations) > 0:
|
||||
print(f" - Real AI Recommendations Found: {len(recommendations)}")
|
||||
for i, rec in enumerate(recommendations[:2]): # Show first 2 recommendations
|
||||
print(f" {i+1}. {rec.get('title', 'No title')} (Confidence: {rec.get('confidence', 0)}%)")
|
||||
else:
|
||||
print(" - No recommendations found")
|
||||
|
||||
# Verify it's not mock data
|
||||
if data.get('ai_service_status') == 'operational':
|
||||
print("✅ Real AI Integration: CONFIRMED")
|
||||
return True
|
||||
else:
|
||||
print("❌ Still using fallback/mock data")
|
||||
return False
|
||||
|
||||
else:
|
||||
print(f"❌ AI Analytics endpoint: FAILED (Status: {response.status_code})")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ AI Analytics test failed: {e}")
|
||||
return False
|
||||
|
||||
def test_strategies_endpoint():
|
||||
"""Test that strategies endpoint works without user_id."""
|
||||
try:
|
||||
print("\n🧪 Testing Strategies Endpoint")
|
||||
print("=" * 35)
|
||||
|
||||
response = requests.get("http://localhost:8000/api/content-planning/strategies/", timeout=10)
|
||||
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
print(f"✅ Strategies endpoint: PASSED")
|
||||
print(f" - Status: {response.status_code}")
|
||||
print(f" - Strategies found: {len(data)}")
|
||||
|
||||
if len(data) > 0:
|
||||
strategy = data[0]
|
||||
print(f" - Strategy name: {strategy.get('name', 'Unknown')}")
|
||||
print(f" - Industry: {strategy.get('industry', 'Unknown')}")
|
||||
print(f" - Content pillars: {len(strategy.get('content_pillars', []))}")
|
||||
|
||||
return True
|
||||
else:
|
||||
print(f"❌ Strategies endpoint: FAILED (Status: {response.status_code})")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Strategies test failed: {e}")
|
||||
return False
|
||||
|
||||
def test_gap_analysis_endpoint():
|
||||
"""Test that gap analysis endpoint works without user_id."""
|
||||
try:
|
||||
print("\n🧪 Testing Gap Analysis Endpoint")
|
||||
print("=" * 35)
|
||||
|
||||
response = requests.get("http://localhost:8000/api/content-planning/gap-analysis/", timeout=10)
|
||||
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
print(f"✅ Gap analysis endpoint: PASSED")
|
||||
print(f" - Status: {response.status_code}")
|
||||
print(f" - Analyses found: {len(data)}")
|
||||
|
||||
if len(data) > 0:
|
||||
analysis = data[0]
|
||||
print(f" - Website: {analysis.get('website_url', 'Unknown')}")
|
||||
print(f" - Competitors: {len(analysis.get('competitor_urls', []))}")
|
||||
print(f" - Keywords: {len(analysis.get('target_keywords', []))}")
|
||||
|
||||
return True
|
||||
else:
|
||||
print(f"❌ Gap analysis endpoint: FAILED (Status: {response.status_code})")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Gap analysis test failed: {e}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""Run all final tests."""
|
||||
print("🧪 Final AI Integration Test")
|
||||
print("=" * 50)
|
||||
|
||||
tests = [
|
||||
test_ai_analytics_real_data,
|
||||
test_strategies_endpoint,
|
||||
test_gap_analysis_endpoint
|
||||
]
|
||||
|
||||
passed = 0
|
||||
total = len(tests)
|
||||
|
||||
for test in tests:
|
||||
if test():
|
||||
passed += 1
|
||||
print()
|
||||
|
||||
print("=" * 50)
|
||||
print(f"📊 Final Test Results: {passed}/{total} tests passed")
|
||||
|
||||
if passed == total:
|
||||
print("🎉 SUCCESS: All endpoints working with real AI integration!")
|
||||
print("✅ 422 errors fixed")
|
||||
print("✅ Real AI insights being generated")
|
||||
print("✅ UI should now show real data instead of mock data")
|
||||
return 0
|
||||
else:
|
||||
print("⚠️ Some tests failed. Please check the implementation.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
95
docs/alwrity_test_scripts/test_fixes.py
Normal file
95
docs/alwrity_test_scripts/test_fixes.py
Normal file
@@ -0,0 +1,95 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify the fixes for the Content Planning Dashboard.
|
||||
"""
|
||||
|
||||
import requests
|
||||
import json
|
||||
import sys
|
||||
|
||||
def test_backend_health():
|
||||
"""Test if the backend is responding."""
|
||||
try:
|
||||
response = requests.get("http://localhost:8000/health", timeout=5)
|
||||
if response.status_code == 200:
|
||||
print("✅ Backend health check: PASSED")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ Backend health check: FAILED (Status: {response.status_code})")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Backend health check: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def test_ai_analytics_endpoint():
|
||||
"""Test if the AI analytics endpoint is working."""
|
||||
try:
|
||||
response = requests.get("http://localhost:8000/api/content-planning/ai-analytics/", timeout=10)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if 'insights' in data and 'recommendations' in data:
|
||||
print("✅ AI Analytics endpoint: PASSED")
|
||||
print(f" - Found {len(data['insights'])} insights")
|
||||
print(f" - Found {len(data['recommendations'])} recommendations")
|
||||
return True
|
||||
else:
|
||||
print("❌ AI Analytics endpoint: FAILED (Missing expected fields)")
|
||||
return False
|
||||
else:
|
||||
print(f"❌ AI Analytics endpoint: FAILED (Status: {response.status_code})")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ AI Analytics endpoint: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def test_content_planning_health():
|
||||
"""Test if the content planning health endpoint is working."""
|
||||
try:
|
||||
response = requests.get("http://localhost:8000/api/content-planning/health", timeout=10)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if 'status' in data:
|
||||
print("✅ Content Planning health check: PASSED")
|
||||
print(f" - Status: {data['status']}")
|
||||
return True
|
||||
else:
|
||||
print("❌ Content Planning health check: FAILED (Missing status field)")
|
||||
return False
|
||||
else:
|
||||
print(f"❌ Content Planning health check: FAILED (Status: {response.status_code})")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Content Planning health check: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""Run all tests."""
|
||||
print("🧪 Testing Content Planning Dashboard Fixes")
|
||||
print("=" * 50)
|
||||
|
||||
tests = [
|
||||
test_backend_health,
|
||||
test_ai_analytics_endpoint,
|
||||
test_content_planning_health
|
||||
]
|
||||
|
||||
passed = 0
|
||||
total = len(tests)
|
||||
|
||||
for test in tests:
|
||||
if test():
|
||||
passed += 1
|
||||
print()
|
||||
|
||||
print("=" * 50)
|
||||
print(f"📊 Test Results: {passed}/{total} tests passed")
|
||||
|
||||
if passed == total:
|
||||
print("🎉 All tests passed! The fixes are working correctly.")
|
||||
return 0
|
||||
else:
|
||||
print("⚠️ Some tests failed. Please check the backend logs.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
119
docs/alwrity_test_scripts/test_gemini_fix.py
Normal file
119
docs/alwrity_test_scripts/test_gemini_fix.py
Normal file
@@ -0,0 +1,119 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify the Gemini provider fixes.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add the backend directory to the path
|
||||
sys.path.append(str(Path(__file__).parent / 'backend'))
|
||||
|
||||
from llm_providers.gemini_provider import gemini_text_response, gemini_pro_text_gen, test_gemini_api_key
|
||||
|
||||
def test_gemini_text_response():
|
||||
"""Test the basic text response function."""
|
||||
try:
|
||||
print("🧪 Testing Gemini text response...")
|
||||
|
||||
# Test with a simple prompt
|
||||
prompt = "Hello, how are you today?"
|
||||
response = gemini_text_response(prompt, temperature=0.1, max_tokens=50)
|
||||
|
||||
if response and len(response) > 0:
|
||||
print("✅ Gemini text response: PASSED")
|
||||
print(f" - Response: {response[:100]}...")
|
||||
return True
|
||||
else:
|
||||
print("❌ Gemini text response: FAILED (Empty response)")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Gemini text response: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def test_gemini_pro_text_gen():
|
||||
"""Test the legacy text generation function."""
|
||||
try:
|
||||
print("🧪 Testing Gemini Pro text generation...")
|
||||
|
||||
# Test with a simple prompt
|
||||
prompt = "What is the capital of France?"
|
||||
response = gemini_pro_text_gen(prompt, temperature=0.1, max_tokens=50)
|
||||
|
||||
if response and len(response) > 0 and not response.startswith("Error"):
|
||||
print("✅ Gemini Pro text generation: PASSED")
|
||||
print(f" - Response: {response[:100]}...")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ Gemini Pro text generation: FAILED (Response: {response})")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Gemini Pro text generation: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
async def test_gemini_api_key_validation():
|
||||
"""Test the API key validation function."""
|
||||
try:
|
||||
print("🧪 Testing Gemini API key validation...")
|
||||
|
||||
# Get API key from environment
|
||||
api_key = os.getenv('GEMINI_API_KEY')
|
||||
if not api_key:
|
||||
print("❌ Gemini API key validation: FAILED (No API key found)")
|
||||
return False
|
||||
|
||||
# Test the API key
|
||||
is_valid, message = await test_gemini_api_key(api_key)
|
||||
|
||||
if is_valid:
|
||||
print("✅ Gemini API key validation: PASSED")
|
||||
print(f" - Message: {message}")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ Gemini API key validation: FAILED (Message: {message})")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Gemini API key validation: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
async def main():
|
||||
"""Run all Gemini tests."""
|
||||
print("🧪 Testing Gemini Provider Fixes")
|
||||
print("=" * 50)
|
||||
|
||||
tests = [
|
||||
test_gemini_text_response,
|
||||
test_gemini_pro_text_gen,
|
||||
test_gemini_api_key_validation
|
||||
]
|
||||
|
||||
passed = 0
|
||||
total = len(tests)
|
||||
|
||||
for test in tests:
|
||||
if test == test_gemini_api_key_validation:
|
||||
result = await test()
|
||||
else:
|
||||
result = test()
|
||||
|
||||
if result:
|
||||
passed += 1
|
||||
print()
|
||||
|
||||
print("=" * 50)
|
||||
print(f"📊 Test Results: {passed}/{total} tests passed")
|
||||
|
||||
if passed == total:
|
||||
print("🎉 All Gemini tests passed! The fixes are working correctly.")
|
||||
return 0
|
||||
else:
|
||||
print("⚠️ Some Gemini tests failed. Please check the API key and configuration.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
import asyncio
|
||||
sys.exit(asyncio.run(main()))
|
||||
86
docs/alwrity_test_scripts/test_gemini_real.py
Normal file
86
docs/alwrity_test_scripts/test_gemini_real.py
Normal file
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify the Gemini provider is working with real API calls.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add the backend directory to the path
|
||||
sys.path.append(str(Path(__file__).parent / 'backend'))
|
||||
|
||||
from llm_providers.gemini_provider import gemini_text_response, gemini_pro_text_gen
|
||||
|
||||
def test_gemini_real_call():
|
||||
"""Test a real Gemini API call."""
|
||||
try:
|
||||
print("🧪 Testing real Gemini API call...")
|
||||
|
||||
# Test with a simple prompt
|
||||
prompt = "What is the capital of France? Answer in one sentence."
|
||||
response = gemini_text_response(prompt, temperature=0.1, max_tokens=50)
|
||||
|
||||
if response and len(response) > 0:
|
||||
print("✅ Real Gemini API call: PASSED")
|
||||
print(f" - Response: {response}")
|
||||
return True
|
||||
else:
|
||||
print("❌ Real Gemini API call: FAILED (Empty response)")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Real Gemini API call: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def test_gemini_pro_real_call():
|
||||
"""Test the legacy function with real API call."""
|
||||
try:
|
||||
print("🧪 Testing Gemini Pro real API call...")
|
||||
|
||||
# Test with a simple prompt
|
||||
prompt = "What is 2 + 2? Answer in one word."
|
||||
response = gemini_pro_text_gen(prompt, temperature=0.1, max_tokens=10)
|
||||
|
||||
if response and len(response) > 0 and not response.startswith("Error"):
|
||||
print("✅ Gemini Pro real API call: PASSED")
|
||||
print(f" - Response: {response}")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ Gemini Pro real API call: FAILED (Response: {response})")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Gemini Pro real API call: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""Run all real API tests."""
|
||||
print("🧪 Testing Gemini Provider Real API Calls")
|
||||
print("=" * 50)
|
||||
|
||||
tests = [
|
||||
test_gemini_real_call,
|
||||
test_gemini_pro_real_call
|
||||
]
|
||||
|
||||
passed = 0
|
||||
total = len(tests)
|
||||
|
||||
for test in tests:
|
||||
if test():
|
||||
passed += 1
|
||||
print()
|
||||
|
||||
print("=" * 50)
|
||||
print(f"📊 Test Results: {passed}/{total} tests passed")
|
||||
|
||||
if passed == total:
|
||||
print("🎉 All real API tests passed! The Gemini provider is working correctly.")
|
||||
return 0
|
||||
else:
|
||||
print("⚠️ Some real API tests failed. Please check the API key and configuration.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
159
docs/alwrity_test_scripts/test_gemini_structure.py
Normal file
159
docs/alwrity_test_scripts/test_gemini_structure.py
Normal file
@@ -0,0 +1,159 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify the Gemini provider structure is correct.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add the backend directory to the path
|
||||
sys.path.append(str(Path(__file__).parent / 'backend'))
|
||||
|
||||
def test_gemini_import():
|
||||
"""Test that the Gemini provider can be imported without errors."""
|
||||
try:
|
||||
print("🧪 Testing Gemini provider import...")
|
||||
|
||||
# Test import
|
||||
from llm_providers.gemini_provider import (
|
||||
gemini_text_response,
|
||||
gemini_pro_text_gen,
|
||||
test_gemini_api_key,
|
||||
gemini_structured_json_response
|
||||
)
|
||||
|
||||
print("✅ Gemini provider import: PASSED")
|
||||
print(" - All functions imported successfully")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Gemini provider import: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def test_gemini_function_signatures():
|
||||
"""Test that the function signatures are correct."""
|
||||
try:
|
||||
print("🧪 Testing Gemini function signatures...")
|
||||
|
||||
from llm_providers.gemini_provider import (
|
||||
gemini_text_response,
|
||||
gemini_pro_text_gen,
|
||||
test_gemini_api_key,
|
||||
gemini_structured_json_response
|
||||
)
|
||||
|
||||
# Test function signatures
|
||||
import inspect
|
||||
|
||||
# Check gemini_text_response
|
||||
sig = inspect.signature(gemini_text_response)
|
||||
expected_params = ['prompt', 'temperature', 'top_p', 'n', 'max_tokens', 'system_prompt']
|
||||
actual_params = list(sig.parameters.keys())
|
||||
|
||||
if all(param in actual_params for param in expected_params):
|
||||
print("✅ gemini_text_response signature: PASSED")
|
||||
else:
|
||||
print(f"❌ gemini_text_response signature: FAILED")
|
||||
print(f" - Expected: {expected_params}")
|
||||
print(f" - Actual: {actual_params}")
|
||||
return False
|
||||
|
||||
# Check gemini_pro_text_gen
|
||||
sig = inspect.signature(gemini_pro_text_gen)
|
||||
expected_params = ['prompt', 'temperature', 'top_p', 'top_k', 'max_tokens']
|
||||
actual_params = list(sig.parameters.keys())
|
||||
|
||||
if all(param in actual_params for param in expected_params):
|
||||
print("✅ gemini_pro_text_gen signature: PASSED")
|
||||
else:
|
||||
print(f"❌ gemini_pro_text_gen signature: FAILED")
|
||||
print(f" - Expected: {expected_params}")
|
||||
print(f" - Actual: {actual_params}")
|
||||
return False
|
||||
|
||||
# Check gemini_structured_json_response
|
||||
sig = inspect.signature(gemini_structured_json_response)
|
||||
expected_params = ['prompt', 'schema', 'temperature', 'top_p', 'top_k', 'max_tokens', 'system_prompt']
|
||||
actual_params = list(sig.parameters.keys())
|
||||
|
||||
if all(param in actual_params for param in expected_params):
|
||||
print("✅ gemini_structured_json_response signature: PASSED")
|
||||
else:
|
||||
print(f"❌ gemini_structured_json_response signature: FAILED")
|
||||
print(f" - Expected: {expected_params}")
|
||||
print(f" - Actual: {actual_params}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Gemini function signatures: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def test_gemini_api_key_handling():
|
||||
"""Test that the API key handling is correct."""
|
||||
try:
|
||||
print("🧪 Testing Gemini API key handling...")
|
||||
|
||||
from llm_providers.gemini_provider import gemini_text_response
|
||||
|
||||
# Test with no API key (should raise ValueError)
|
||||
original_key = os.environ.get('GEMINI_API_KEY')
|
||||
if 'GEMINI_API_KEY' in os.environ:
|
||||
del os.environ['GEMINI_API_KEY']
|
||||
|
||||
try:
|
||||
gemini_text_response("test", max_tokens=10)
|
||||
print("❌ API key handling: FAILED (Should have raised ValueError)")
|
||||
return False
|
||||
except ValueError as e:
|
||||
if "Gemini API key not found" in str(e):
|
||||
print("✅ API key handling: PASSED")
|
||||
print(" - Correctly raises ValueError when API key is missing")
|
||||
else:
|
||||
print(f"❌ API key handling: FAILED (Unexpected error: {e})")
|
||||
return False
|
||||
finally:
|
||||
# Restore original key if it existed
|
||||
if original_key:
|
||||
os.environ['GEMINI_API_KEY'] = original_key
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Gemini API key handling: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""Run all structure tests."""
|
||||
print("🧪 Testing Gemini Provider Structure")
|
||||
print("=" * 50)
|
||||
|
||||
tests = [
|
||||
test_gemini_import,
|
||||
test_gemini_function_signatures,
|
||||
test_gemini_api_key_handling
|
||||
]
|
||||
|
||||
passed = 0
|
||||
total = len(tests)
|
||||
|
||||
for test in tests:
|
||||
if test():
|
||||
passed += 1
|
||||
print()
|
||||
|
||||
print("=" * 50)
|
||||
print(f"📊 Test Results: {passed}/{total} tests passed")
|
||||
|
||||
if passed == total:
|
||||
print("🎉 All structure tests passed! The Gemini provider is correctly structured.")
|
||||
print("💡 To test with real API calls, set the GEMINI_API_KEY environment variable.")
|
||||
return 0
|
||||
else:
|
||||
print("⚠️ Some structure tests failed. Please check the implementation.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
135
docs/alwrity_test_scripts/test_json_compatibility.py
Normal file
135
docs/alwrity_test_scripts/test_json_compatibility.py
Normal file
@@ -0,0 +1,135 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify the JSON compatibility fix.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
# Add the backend directory to the path
|
||||
sys.path.append(str(Path(__file__).parent / 'backend'))
|
||||
|
||||
from llm_providers.gemini_provider import gemini_structured_json_response
|
||||
|
||||
def test_json_string_return():
|
||||
"""Test that the function returns JSON string instead of dict."""
|
||||
try:
|
||||
print("🧪 Testing JSON string return...")
|
||||
|
||||
# Simple schema for testing
|
||||
test_schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {"type": "string"},
|
||||
"age": {"type": "integer"},
|
||||
"city": {"type": "string"}
|
||||
},
|
||||
"required": ["name", "age"]
|
||||
}
|
||||
|
||||
# Test prompt
|
||||
prompt = "Create a person profile with name John, age 30, and city New York."
|
||||
|
||||
response = gemini_structured_json_response(
|
||||
prompt=prompt,
|
||||
schema=test_schema,
|
||||
temperature=0.1,
|
||||
max_tokens=100
|
||||
)
|
||||
|
||||
# Check that response is a JSON string
|
||||
if isinstance(response, str):
|
||||
# Try to parse it as JSON
|
||||
parsed = json.loads(response)
|
||||
if isinstance(parsed, dict) and "name" in parsed and "age" in parsed:
|
||||
print("✅ JSON string return: PASSED")
|
||||
print(f" - Response type: {type(response)}")
|
||||
print(f" - Parsed content: {parsed}")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ JSON string return: FAILED (Invalid JSON content: {parsed})")
|
||||
return False
|
||||
else:
|
||||
print(f"❌ JSON string return: FAILED (Expected string, got {type(response)})")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ JSON string return: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def test_json_compatibility():
|
||||
"""Test that the response can be parsed by calling code."""
|
||||
try:
|
||||
print("🧪 Testing JSON compatibility...")
|
||||
|
||||
# Simple schema for testing
|
||||
test_schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"result": {"type": "string"},
|
||||
"status": {"type": "string"}
|
||||
},
|
||||
"required": ["result", "status"]
|
||||
}
|
||||
|
||||
# Test prompt
|
||||
prompt = "Return a simple result with status success."
|
||||
|
||||
response = gemini_structured_json_response(
|
||||
prompt=prompt,
|
||||
schema=test_schema,
|
||||
temperature=0.1,
|
||||
max_tokens=50
|
||||
)
|
||||
|
||||
# Simulate what calling code would do
|
||||
try:
|
||||
parsed_response = json.loads(response)
|
||||
if isinstance(parsed_response, dict):
|
||||
print("✅ JSON compatibility: PASSED")
|
||||
print(f" - Successfully parsed by calling code")
|
||||
print(f" - Parsed content: {parsed_response}")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ JSON compatibility: FAILED (Parsed result not dict: {parsed_response})")
|
||||
return False
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"❌ JSON compatibility: FAILED (JSON decode error: {e})")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ JSON compatibility: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""Run all JSON compatibility tests."""
|
||||
print("🧪 Testing JSON Compatibility Fix")
|
||||
print("=" * 50)
|
||||
|
||||
tests = [
|
||||
test_json_string_return,
|
||||
test_json_compatibility
|
||||
]
|
||||
|
||||
passed = 0
|
||||
total = len(tests)
|
||||
|
||||
for test in tests:
|
||||
if test():
|
||||
passed += 1
|
||||
print()
|
||||
|
||||
print("=" * 50)
|
||||
print(f"📊 Test Results: {passed}/{total} tests passed")
|
||||
|
||||
if passed == total:
|
||||
print("🎉 All JSON compatibility tests passed!")
|
||||
return 0
|
||||
else:
|
||||
print("⚠️ Some JSON compatibility tests failed.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
202
docs/alwrity_test_scripts/test_phase2_ai_integration.py
Normal file
202
docs/alwrity_test_scripts/test_phase2_ai_integration.py
Normal file
@@ -0,0 +1,202 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for Phase 2 AI Integration
|
||||
Verifies that the Keyword Researcher and Competitor Analyzer are working with real AI calls.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# Add the backend directory to the Python path
|
||||
sys.path.append(str(Path(__file__).parent / "backend"))
|
||||
|
||||
from services.content_gap_analyzer.keyword_researcher import KeywordResearcher
|
||||
from services.content_gap_analyzer.competitor_analyzer import CompetitorAnalyzer
|
||||
from loguru import logger
|
||||
|
||||
async def test_keyword_researcher_ai():
|
||||
"""Test the Keyword Researcher AI integration."""
|
||||
|
||||
print("🔍 Testing Keyword Researcher AI Integration...")
|
||||
|
||||
# Initialize the Keyword Researcher
|
||||
keyword_researcher = KeywordResearcher()
|
||||
|
||||
# Test data
|
||||
test_industry = "Technology"
|
||||
test_url = "https://example.com"
|
||||
test_keywords = ["artificial intelligence", "machine learning", "data science"]
|
||||
|
||||
try:
|
||||
print("\n1. Testing Keyword Analysis...")
|
||||
keyword_analysis = await keyword_researcher.analyze_keywords(test_industry, test_url, test_keywords)
|
||||
print(f"✅ Keyword Analysis completed: {len(keyword_analysis.get('insights', []))} insights generated")
|
||||
|
||||
print("\n2. Testing Keyword Expansion...")
|
||||
keyword_expansion = await keyword_researcher.expand_keywords(test_keywords, test_industry)
|
||||
print(f"✅ Keyword Expansion completed: {len(keyword_expansion.get('expanded_keywords', []))} keywords expanded")
|
||||
|
||||
print("\n3. Testing Search Intent Analysis...")
|
||||
intent_analysis = await keyword_researcher.analyze_search_intent(test_keywords)
|
||||
print(f"✅ Search Intent Analysis completed: {len(intent_analysis.get('intent_categories', {}))} intent categories")
|
||||
|
||||
print("\n4. Testing Content Format Suggestions...")
|
||||
# Create mock AI insights for testing
|
||||
mock_ai_insights = {
|
||||
'keywords': test_keywords,
|
||||
'industry': test_industry,
|
||||
'trends': {'ai': 'rising', 'ml': 'stable'}
|
||||
}
|
||||
content_formats = await keyword_researcher._suggest_content_formats(mock_ai_insights)
|
||||
print(f"✅ Content Format Suggestions completed: {len(content_formats)} formats suggested")
|
||||
|
||||
print("\n5. Testing Topic Clustering...")
|
||||
topic_clusters = await keyword_researcher._create_topic_clusters(mock_ai_insights)
|
||||
print(f"✅ Topic Clustering completed: {len(topic_clusters.get('topic_clusters', []))} clusters created")
|
||||
|
||||
print("\n🎉 All Keyword Researcher AI Tests Passed!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Keyword Researcher AI Test Failed: {str(e)}")
|
||||
logger.error(f"Keyword Researcher AI test failed: {str(e)}")
|
||||
return False
|
||||
|
||||
async def test_competitor_analyzer_ai():
|
||||
"""Test the Competitor Analyzer AI integration."""
|
||||
|
||||
print("\n🏢 Testing Competitor Analyzer AI Integration...")
|
||||
|
||||
# Initialize the Competitor Analyzer
|
||||
competitor_analyzer = CompetitorAnalyzer()
|
||||
|
||||
# Test data
|
||||
test_competitor_urls = [
|
||||
"https://competitor1.com",
|
||||
"https://competitor2.com",
|
||||
"https://competitor3.com"
|
||||
]
|
||||
test_industry = "Technology"
|
||||
|
||||
try:
|
||||
print("\n1. Testing Competitor Analysis...")
|
||||
competitor_analysis = await competitor_analyzer.analyze_competitors(test_competitor_urls, test_industry)
|
||||
print(f"✅ Competitor Analysis completed: {len(competitor_analysis.get('competitors', []))} competitors analyzed")
|
||||
|
||||
print("\n2. Testing Market Position Evaluation...")
|
||||
# Create mock competitor data for testing
|
||||
mock_competitors = [
|
||||
{
|
||||
'url': 'competitor1.com',
|
||||
'analysis': {
|
||||
'content_count': 150,
|
||||
'avg_quality_score': 8.5,
|
||||
'top_keywords': ['AI', 'ML', 'Data Science']
|
||||
}
|
||||
},
|
||||
{
|
||||
'url': 'competitor2.com',
|
||||
'analysis': {
|
||||
'content_count': 200,
|
||||
'avg_quality_score': 7.8,
|
||||
'top_keywords': ['Automation', 'Innovation', 'Tech']
|
||||
}
|
||||
}
|
||||
]
|
||||
market_position = await competitor_analyzer._evaluate_market_position(mock_competitors, test_industry)
|
||||
print(f"✅ Market Position Evaluation completed: {len(market_position.get('strategic_recommendations', []))} recommendations")
|
||||
|
||||
print("\n3. Testing Content Gap Identification...")
|
||||
content_gaps = await competitor_analyzer._identify_content_gaps(mock_competitors)
|
||||
print(f"✅ Content Gap Identification completed: {len(content_gaps)} gaps identified")
|
||||
|
||||
print("\n4. Testing Competitive Insights Generation...")
|
||||
# Create mock analysis results for testing
|
||||
mock_analysis_results = {
|
||||
'competitors': mock_competitors,
|
||||
'market_position': market_position,
|
||||
'content_gaps': content_gaps,
|
||||
'industry': test_industry
|
||||
}
|
||||
competitive_insights = await competitor_analyzer._generate_competitive_insights(mock_analysis_results)
|
||||
print(f"✅ Competitive Insights Generation completed: {len(competitive_insights)} insights generated")
|
||||
|
||||
print("\n🎉 All Competitor Analyzer AI Tests Passed!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Competitor Analyzer AI Test Failed: {str(e)}")
|
||||
logger.error(f"Competitor Analyzer AI test failed: {str(e)}")
|
||||
return False
|
||||
|
||||
async def test_ai_fallback_functionality():
|
||||
"""Test the fallback functionality when AI fails."""
|
||||
|
||||
print("\n🔄 Testing AI Fallback Functionality...")
|
||||
|
||||
# Initialize services
|
||||
keyword_researcher = KeywordResearcher()
|
||||
competitor_analyzer = CompetitorAnalyzer()
|
||||
|
||||
# Test with minimal data to trigger fallback
|
||||
minimal_data = {'test': 'data'}
|
||||
|
||||
try:
|
||||
print("Testing Keyword Researcher fallback...")
|
||||
keyword_result = await keyword_researcher._analyze_keyword_trends("test", [])
|
||||
|
||||
if keyword_result and 'trends' in keyword_result:
|
||||
print("✅ Keyword Researcher fallback working correctly")
|
||||
else:
|
||||
print("❌ Keyword Researcher fallback failed")
|
||||
return False
|
||||
|
||||
print("Testing Competitor Analyzer fallback...")
|
||||
competitor_result = await competitor_analyzer._evaluate_market_position([], "test")
|
||||
|
||||
if competitor_result and 'market_leader' in competitor_result:
|
||||
print("✅ Competitor Analyzer fallback working correctly")
|
||||
else:
|
||||
print("❌ Competitor Analyzer fallback failed")
|
||||
return False
|
||||
|
||||
print("✅ All fallback functionality working correctly")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Fallback test failed: {str(e)}")
|
||||
return False
|
||||
|
||||
async def main():
|
||||
"""Main test function."""
|
||||
print("🚀 Starting Phase 2 AI Integration Tests...")
|
||||
print("=" * 60)
|
||||
|
||||
# Test 1: Keyword Researcher AI Integration
|
||||
keyword_success = await test_keyword_researcher_ai()
|
||||
|
||||
# Test 2: Competitor Analyzer AI Integration
|
||||
competitor_success = await test_competitor_analyzer_ai()
|
||||
|
||||
# Test 3: Fallback Functionality
|
||||
fallback_success = await test_ai_fallback_functionality()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("📊 Phase 2 Test Results Summary:")
|
||||
print(f"Keyword Researcher AI: {'✅ PASSED' if keyword_success else '❌ FAILED'}")
|
||||
print(f"Competitor Analyzer AI: {'✅ PASSED' if competitor_success else '❌ FAILED'}")
|
||||
print(f"Fallback Functionality: {'✅ PASSED' if fallback_success else '❌ FAILED'}")
|
||||
|
||||
if keyword_success and competitor_success and fallback_success:
|
||||
print("\n🎉 All Phase 2 tests passed! AI Integration is working correctly.")
|
||||
print("✅ Phase 2: Advanced AI Features COMPLETED")
|
||||
return 0
|
||||
else:
|
||||
print("\n⚠️ Some tests failed. Please check the AI configuration.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit_code = asyncio.run(main())
|
||||
sys.exit(exit_code)
|
||||
263
docs/alwrity_test_scripts/test_phase3_ai_optimization.py
Normal file
263
docs/alwrity_test_scripts/test_phase3_ai_optimization.py
Normal file
@@ -0,0 +1,263 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for Phase 3 AI Prompt Optimization
|
||||
Verifies that the AI Prompt Optimizer is working with advanced prompts and schemas.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# Add the backend directory to the Python path
|
||||
sys.path.append(str(Path(__file__).parent / "backend"))
|
||||
|
||||
from services.ai_prompt_optimizer import AIPromptOptimizer
|
||||
from services.content_gap_analyzer.ai_engine_service import AIEngineService
|
||||
from loguru import logger
|
||||
|
||||
async def test_ai_prompt_optimizer():
|
||||
"""Test the AI Prompt Optimizer functionality."""
|
||||
|
||||
print("🔧 Testing AI Prompt Optimizer...")
|
||||
|
||||
# Initialize the AI Prompt Optimizer
|
||||
ai_optimizer = AIPromptOptimizer()
|
||||
|
||||
# Test 1: Strategic Content Gap Analysis
|
||||
print("\n📊 Test 1: Strategic Content Gap Analysis")
|
||||
analysis_data = {
|
||||
'target_url': 'example.com',
|
||||
'industry': 'technology',
|
||||
'serp_opportunities': 25,
|
||||
'expanded_keywords_count': 150,
|
||||
'competitors_analyzed': 5,
|
||||
'content_quality_score': 8.5,
|
||||
'competition_level': 'high',
|
||||
'dominant_themes': {
|
||||
'artificial_intelligence': 0.3,
|
||||
'machine_learning': 0.25,
|
||||
'data_science': 0.2,
|
||||
'automation': 0.15,
|
||||
'innovation': 0.1
|
||||
},
|
||||
'competitive_landscape': {
|
||||
'market_leader': 'competitor1.com',
|
||||
'content_leader': 'competitor2.com',
|
||||
'quality_leader': 'competitor3.com'
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
result = await ai_optimizer.generate_strategic_content_gap_analysis(analysis_data)
|
||||
print(f"✅ Strategic content gap analysis completed")
|
||||
print(f" - Strategic insights: {len(result.get('strategic_insights', []))}")
|
||||
print(f" - Content recommendations: {len(result.get('content_recommendations', []))}")
|
||||
print(f" - Keyword strategy: {bool(result.get('keyword_strategy'))}")
|
||||
except Exception as e:
|
||||
print(f"❌ Strategic content gap analysis failed: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Advanced Market Position Analysis
|
||||
print("\n🏢 Test 2: Advanced Market Position Analysis")
|
||||
market_data = {
|
||||
'industry': 'technology',
|
||||
'competitors': [
|
||||
{
|
||||
'url': 'competitor1.com',
|
||||
'content_score': 8.5,
|
||||
'quality_score': 9.0,
|
||||
'frequency': 'high'
|
||||
},
|
||||
{
|
||||
'url': 'competitor2.com',
|
||||
'content_score': 7.8,
|
||||
'quality_score': 8.2,
|
||||
'frequency': 'medium'
|
||||
}
|
||||
],
|
||||
'market_size': 'Large',
|
||||
'growth_rate': '15%',
|
||||
'key_trends': ['AI adoption', 'Cloud migration', 'Digital transformation']
|
||||
}
|
||||
|
||||
try:
|
||||
result = await ai_optimizer.generate_advanced_market_position_analysis(market_data)
|
||||
print(f"✅ Advanced market position analysis completed")
|
||||
print(f" - Market leader: {result.get('market_leader', 'N/A')}")
|
||||
print(f" - Market gaps: {len(result.get('market_gaps', []))}")
|
||||
print(f" - Opportunities: {len(result.get('opportunities', []))}")
|
||||
print(f" - Strategic recommendations: {len(result.get('strategic_recommendations', []))}")
|
||||
except Exception as e:
|
||||
print(f"❌ Advanced market position analysis failed: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 3: Advanced Keyword Analysis
|
||||
print("\n🔍 Test 3: Advanced Keyword Analysis")
|
||||
keyword_data = {
|
||||
'industry': 'technology',
|
||||
'target_keywords': ['artificial intelligence', 'machine learning', 'data science'],
|
||||
'search_volume_data': {
|
||||
'artificial intelligence': 50000,
|
||||
'machine learning': 35000,
|
||||
'data science': 25000
|
||||
},
|
||||
'competition_analysis': {
|
||||
'artificial intelligence': 'high',
|
||||
'machine learning': 'medium',
|
||||
'data science': 'low'
|
||||
},
|
||||
'trend_analysis': {
|
||||
'artificial intelligence': 'rising',
|
||||
'machine learning': 'stable',
|
||||
'data science': 'rising'
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
result = await ai_optimizer.generate_advanced_keyword_analysis(keyword_data)
|
||||
print(f"✅ Advanced keyword analysis completed")
|
||||
print(f" - Keyword opportunities: {len(result.get('keyword_opportunities', []))}")
|
||||
print(f" - Keyword clusters: {len(result.get('keyword_clusters', []))}")
|
||||
except Exception as e:
|
||||
print(f"❌ Advanced keyword analysis failed: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 4: Health Check
|
||||
print("\n🏥 Test 4: Health Check")
|
||||
try:
|
||||
health_status = await ai_optimizer.health_check()
|
||||
print(f"✅ Health check completed")
|
||||
print(f" - Service status: {health_status.get('status')}")
|
||||
print(f" - Prompts loaded: {health_status.get('prompts_loaded')}")
|
||||
print(f" - Schemas loaded: {health_status.get('schemas_loaded')}")
|
||||
print(f" - AI integration: {health_status.get('capabilities', {}).get('ai_integration')}")
|
||||
except Exception as e:
|
||||
print(f"❌ Health check failed: {str(e)}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
async def test_ai_engine_integration():
|
||||
"""Test the AI Engine Service integration with prompt optimizer."""
|
||||
|
||||
print("\n🤖 Testing AI Engine Service Integration...")
|
||||
|
||||
# Initialize the AI Engine Service
|
||||
ai_engine = AIEngineService()
|
||||
|
||||
# Test 1: Content Gap Analysis with Advanced Prompts
|
||||
print("\n📊 Test 1: Content Gap Analysis with Advanced Prompts")
|
||||
analysis_summary = {
|
||||
'target_url': 'example.com',
|
||||
'industry': 'technology',
|
||||
'serp_opportunities': 25,
|
||||
'expanded_keywords_count': 150,
|
||||
'competitors_analyzed': 5,
|
||||
'dominant_themes': {
|
||||
'artificial_intelligence': 0.3,
|
||||
'machine_learning': 0.25,
|
||||
'data_science': 0.2
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
result = await ai_engine.analyze_content_gaps(analysis_summary)
|
||||
print(f"✅ Content gap analysis with advanced prompts completed")
|
||||
print(f" - Strategic insights: {len(result.get('strategic_insights', []))}")
|
||||
print(f" - Content recommendations: {len(result.get('content_recommendations', []))}")
|
||||
except Exception as e:
|
||||
print(f"❌ Content gap analysis failed: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Market Position Analysis with Advanced Prompts
|
||||
print("\n🏢 Test 2: Market Position Analysis with Advanced Prompts")
|
||||
market_data = {
|
||||
'industry': 'technology',
|
||||
'competitors': [
|
||||
{
|
||||
'url': 'competitor1.com',
|
||||
'content_score': 8.5,
|
||||
'quality_score': 9.0
|
||||
},
|
||||
{
|
||||
'url': 'competitor2.com',
|
||||
'content_score': 7.8,
|
||||
'quality_score': 8.2
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
try:
|
||||
result = await ai_engine.analyze_market_position(market_data)
|
||||
print(f"✅ Market position analysis with advanced prompts completed")
|
||||
print(f" - Market leader: {result.get('market_leader', 'N/A')}")
|
||||
print(f" - Market gaps: {len(result.get('market_gaps', []))}")
|
||||
print(f" - Strategic recommendations: {len(result.get('strategic_recommendations', []))}")
|
||||
except Exception as e:
|
||||
print(f"❌ Market position analysis failed: {str(e)}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
async def test_ai_fallback_functionality():
|
||||
"""Test the fallback functionality when AI fails."""
|
||||
|
||||
print("\n🛡️ Testing AI Fallback Functionality...")
|
||||
|
||||
# Initialize the AI Prompt Optimizer
|
||||
ai_optimizer = AIPromptOptimizer()
|
||||
|
||||
# Test with invalid data to trigger fallback
|
||||
print("\n📊 Test: Fallback for Strategic Content Gap Analysis")
|
||||
invalid_data = {
|
||||
'invalid_field': 'invalid_value'
|
||||
}
|
||||
|
||||
try:
|
||||
result = await ai_optimizer.generate_strategic_content_gap_analysis(invalid_data)
|
||||
print(f"✅ Fallback functionality working")
|
||||
print(f" - Strategic insights: {len(result.get('strategic_insights', []))}")
|
||||
print(f" - Content recommendations: {len(result.get('content_recommendations', []))}")
|
||||
except Exception as e:
|
||||
print(f"❌ Fallback functionality failed: {str(e)}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
async def main():
|
||||
"""Main test function."""
|
||||
print("🚀 Starting Phase 3 AI Prompt Optimization Tests...")
|
||||
print("=" * 60)
|
||||
|
||||
# Test 1: AI Prompt Optimizer
|
||||
ai_optimizer_success = await test_ai_prompt_optimizer()
|
||||
|
||||
# Test 2: AI Engine Integration
|
||||
ai_engine_success = await test_ai_engine_integration()
|
||||
|
||||
# Test 3: Fallback Functionality
|
||||
fallback_success = await test_ai_fallback_functionality()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("📊 Test Results Summary:")
|
||||
print(f"AI Prompt Optimizer: {'✅ PASSED' if ai_optimizer_success else '❌ FAILED'}")
|
||||
print(f"AI Engine Integration: {'✅ PASSED' if ai_engine_success else '❌ FAILED'}")
|
||||
print(f"Fallback Functionality: {'✅ PASSED' if fallback_success else '❌ FAILED'}")
|
||||
|
||||
if ai_optimizer_success and ai_engine_success and fallback_success:
|
||||
print("\n🎉 All Phase 3 tests passed! AI Prompt Optimization is working correctly.")
|
||||
print("\n✅ Phase 3 Achievements:")
|
||||
print(" - Advanced AI prompts implemented")
|
||||
print(" - Comprehensive JSON schemas created")
|
||||
print(" - Expert-level AI instructions optimized")
|
||||
print(" - Robust error handling and fallbacks")
|
||||
print(" - AI engine service integration completed")
|
||||
return 0
|
||||
else:
|
||||
print("\n⚠️ Some Phase 3 tests failed. Please check the AI configuration.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit_code = asyncio.run(main())
|
||||
sys.exit(exit_code)
|
||||
330
docs/alwrity_test_scripts/test_phase4_ai_service_integration.py
Normal file
330
docs/alwrity_test_scripts/test_phase4_ai_service_integration.py
Normal file
@@ -0,0 +1,330 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for Phase 4 AI Service Integration
|
||||
Verifies that the AI Service Manager is working with centralized management and performance monitoring.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# Add the backend directory to the Python path
|
||||
sys.path.append(str(Path(__file__).parent / "backend"))
|
||||
|
||||
from services.ai_service_manager import AIServiceManager
|
||||
from services.content_gap_analyzer.ai_engine_service import AIEngineService
|
||||
from loguru import logger
|
||||
|
||||
async def test_ai_service_manager():
|
||||
"""Test the AI Service Manager functionality."""
|
||||
|
||||
print("🔧 Testing AI Service Manager...")
|
||||
|
||||
# Initialize the AI Service Manager
|
||||
ai_manager = AIServiceManager()
|
||||
|
||||
# Test 1: Content Gap Analysis
|
||||
print("\n📊 Test 1: Content Gap Analysis")
|
||||
analysis_data = {
|
||||
'target_url': 'example.com',
|
||||
'industry': 'technology',
|
||||
'serp_opportunities': 25,
|
||||
'expanded_keywords_count': 150,
|
||||
'competitors_analyzed': 5,
|
||||
'content_quality_score': 8.5,
|
||||
'competition_level': 'high',
|
||||
'dominant_themes': {
|
||||
'artificial_intelligence': 0.3,
|
||||
'machine_learning': 0.25,
|
||||
'data_science': 0.2,
|
||||
'automation': 0.15,
|
||||
'innovation': 0.1
|
||||
},
|
||||
'competitive_landscape': {
|
||||
'market_leader': 'competitor1.com',
|
||||
'content_leader': 'competitor2.com',
|
||||
'quality_leader': 'competitor3.com'
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
result = await ai_manager.generate_content_gap_analysis(analysis_data)
|
||||
print(f"✅ Content gap analysis completed")
|
||||
print(f" - Strategic insights: {len(result.get('strategic_insights', []))}")
|
||||
print(f" - Content recommendations: {len(result.get('content_recommendations', []))}")
|
||||
except Exception as e:
|
||||
print(f"❌ Content gap analysis failed: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Market Position Analysis
|
||||
print("\n🏢 Test 2: Market Position Analysis")
|
||||
market_data = {
|
||||
'industry': 'technology',
|
||||
'competitors': [
|
||||
{
|
||||
'url': 'competitor1.com',
|
||||
'content_score': 8.5,
|
||||
'quality_score': 9.0,
|
||||
'frequency': 'high'
|
||||
},
|
||||
{
|
||||
'url': 'competitor2.com',
|
||||
'content_score': 7.8,
|
||||
'quality_score': 8.2,
|
||||
'frequency': 'medium'
|
||||
}
|
||||
],
|
||||
'market_size': 'Large',
|
||||
'growth_rate': '15%',
|
||||
'key_trends': ['AI adoption', 'Cloud migration', 'Digital transformation']
|
||||
}
|
||||
|
||||
try:
|
||||
result = await ai_manager.generate_market_position_analysis(market_data)
|
||||
print(f"✅ Market position analysis completed")
|
||||
print(f" - Market leader: {result.get('market_leader', 'N/A')}")
|
||||
print(f" - Market gaps: {len(result.get('market_gaps', []))}")
|
||||
print(f" - Opportunities: {len(result.get('opportunities', []))}")
|
||||
print(f" - Strategic recommendations: {len(result.get('strategic_recommendations', []))}")
|
||||
except Exception as e:
|
||||
print(f"❌ Market position analysis failed: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 3: Keyword Analysis
|
||||
print("\n🔍 Test 3: Keyword Analysis")
|
||||
keyword_data = {
|
||||
'industry': 'technology',
|
||||
'target_keywords': ['artificial intelligence', 'machine learning', 'data science'],
|
||||
'search_volume_data': {
|
||||
'artificial intelligence': 50000,
|
||||
'machine learning': 35000,
|
||||
'data science': 25000
|
||||
},
|
||||
'competition_analysis': {
|
||||
'artificial intelligence': 'high',
|
||||
'machine learning': 'medium',
|
||||
'data science': 'low'
|
||||
},
|
||||
'trend_analysis': {
|
||||
'artificial intelligence': 'rising',
|
||||
'machine learning': 'stable',
|
||||
'data science': 'rising'
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
result = await ai_manager.generate_keyword_analysis(keyword_data)
|
||||
print(f"✅ Keyword analysis completed")
|
||||
print(f" - Keyword opportunities: {len(result.get('keyword_opportunities', []))}")
|
||||
except Exception as e:
|
||||
print(f"❌ Keyword analysis failed: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 4: Performance Metrics
|
||||
print("\n📈 Test 4: Performance Metrics")
|
||||
try:
|
||||
performance_metrics = ai_manager.get_performance_metrics()
|
||||
print(f"✅ Performance metrics retrieved")
|
||||
print(f" - Total calls: {performance_metrics.get('total_calls', 0)}")
|
||||
print(f" - Success rate: {performance_metrics.get('success_rate', 0):.1f}%")
|
||||
print(f" - Average response time: {performance_metrics.get('average_response_time', 0):.2f}s")
|
||||
print(f" - Service breakdown: {len(performance_metrics.get('service_breakdown', {}))} services")
|
||||
except Exception as e:
|
||||
print(f"❌ Performance metrics failed: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 5: Health Check
|
||||
print("\n🏥 Test 5: Health Check")
|
||||
try:
|
||||
health_status = await ai_manager.health_check()
|
||||
print(f"✅ Health check completed")
|
||||
print(f" - Service status: {health_status.get('status')}")
|
||||
print(f" - Prompts loaded: {health_status.get('prompts_loaded')}")
|
||||
print(f" - Schemas loaded: {health_status.get('schemas_loaded')}")
|
||||
print(f" - AI integration: {health_status.get('capabilities', {}).get('ai_integration')}")
|
||||
print(f" - Configuration: {len(health_status.get('configuration', {}))} settings")
|
||||
except Exception as e:
|
||||
print(f"❌ Health check failed: {str(e)}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
async def test_ai_engine_integration():
|
||||
"""Test the AI Engine Service integration with AI Service Manager."""
|
||||
|
||||
print("\n🤖 Testing AI Engine Service Integration...")
|
||||
|
||||
# Initialize the AI Engine Service
|
||||
ai_engine = AIEngineService()
|
||||
|
||||
# Test 1: Content Gap Analysis with AI Service Manager
|
||||
print("\n📊 Test 1: Content Gap Analysis with AI Service Manager")
|
||||
analysis_summary = {
|
||||
'target_url': 'example.com',
|
||||
'industry': 'technology',
|
||||
'serp_opportunities': 25,
|
||||
'expanded_keywords_count': 150,
|
||||
'competitors_analyzed': 5,
|
||||
'dominant_themes': {
|
||||
'artificial_intelligence': 0.3,
|
||||
'machine_learning': 0.25,
|
||||
'data_science': 0.2
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
result = await ai_engine.analyze_content_gaps(analysis_summary)
|
||||
print(f"✅ Content gap analysis with AI Service Manager completed")
|
||||
print(f" - Strategic insights: {len(result.get('strategic_insights', []))}")
|
||||
print(f" - Content recommendations: {len(result.get('content_recommendations', []))}")
|
||||
except Exception as e:
|
||||
print(f"❌ Content gap analysis failed: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Market Position Analysis with AI Service Manager
|
||||
print("\n🏢 Test 2: Market Position Analysis with AI Service Manager")
|
||||
market_data = {
|
||||
'industry': 'technology',
|
||||
'competitors': [
|
||||
{
|
||||
'url': 'competitor1.com',
|
||||
'content_score': 8.5,
|
||||
'quality_score': 9.0
|
||||
},
|
||||
{
|
||||
'url': 'competitor2.com',
|
||||
'content_score': 7.8,
|
||||
'quality_score': 8.2
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
try:
|
||||
result = await ai_engine.analyze_market_position(market_data)
|
||||
print(f"✅ Market position analysis with AI Service Manager completed")
|
||||
print(f" - Market leader: {result.get('market_leader', 'N/A')}")
|
||||
print(f" - Market gaps: {len(result.get('market_gaps', []))}")
|
||||
print(f" - Strategic recommendations: {len(result.get('strategic_recommendations', []))}")
|
||||
except Exception as e:
|
||||
print(f"❌ Market position analysis failed: {str(e)}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
async def test_performance_monitoring():
|
||||
"""Test the performance monitoring functionality."""
|
||||
|
||||
print("\n📊 Testing Performance Monitoring...")
|
||||
|
||||
# Initialize the AI Service Manager
|
||||
ai_manager = AIServiceManager()
|
||||
|
||||
# Make multiple AI calls to generate performance data
|
||||
print("\n🔄 Making multiple AI calls to generate performance data...")
|
||||
|
||||
test_data = {
|
||||
'target_url': 'test.com',
|
||||
'industry': 'technology',
|
||||
'serp_opportunities': 10,
|
||||
'expanded_keywords_count': 50,
|
||||
'competitors_analyzed': 3,
|
||||
'dominant_themes': {'test': 1.0},
|
||||
'competitive_landscape': {'test': 'test'}
|
||||
}
|
||||
|
||||
# Make several calls to generate metrics
|
||||
for i in range(3):
|
||||
try:
|
||||
await ai_manager.generate_content_gap_analysis(test_data)
|
||||
print(f" - Call {i+1} completed")
|
||||
except Exception as e:
|
||||
print(f" - Call {i+1} failed: {str(e)}")
|
||||
|
||||
# Test performance metrics
|
||||
print("\n📈 Testing Performance Metrics...")
|
||||
try:
|
||||
metrics = ai_manager.get_performance_metrics()
|
||||
print(f"✅ Performance metrics analysis:")
|
||||
print(f" - Total calls: {metrics.get('total_calls', 0)}")
|
||||
print(f" - Success rate: {metrics.get('success_rate', 0):.1f}%")
|
||||
print(f" - Average response time: {metrics.get('average_response_time', 0):.2f}s")
|
||||
|
||||
# Service breakdown
|
||||
service_breakdown = metrics.get('service_breakdown', {})
|
||||
print(f" - Service breakdown:")
|
||||
for service, data in service_breakdown.items():
|
||||
print(f" * {service}: {data.get('total_calls', 0)} calls, {data.get('success_rate', 0):.1f}% success")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Performance metrics failed: {str(e)}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
async def test_configuration_management():
|
||||
"""Test the configuration management functionality."""
|
||||
|
||||
print("\n⚙️ Testing Configuration Management...")
|
||||
|
||||
# Initialize the AI Service Manager
|
||||
ai_manager = AIServiceManager()
|
||||
|
||||
# Test configuration access
|
||||
try:
|
||||
config = ai_manager.config
|
||||
print(f"✅ Configuration retrieved:")
|
||||
print(f" - Max retries: {config.get('max_retries')}")
|
||||
print(f" - Timeout seconds: {config.get('timeout_seconds')}")
|
||||
print(f" - Temperature: {config.get('temperature')}")
|
||||
print(f" - Max tokens: {config.get('max_tokens')}")
|
||||
print(f" - Enable caching: {config.get('enable_caching')}")
|
||||
print(f" - Performance monitoring: {config.get('performance_monitoring')}")
|
||||
print(f" - Fallback enabled: {config.get('fallback_enabled')}")
|
||||
except Exception as e:
|
||||
print(f"❌ Configuration test failed: {str(e)}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
async def main():
|
||||
"""Main test function."""
|
||||
print("🚀 Starting Phase 4 AI Service Integration Tests...")
|
||||
print("=" * 70)
|
||||
|
||||
# Test 1: AI Service Manager
|
||||
ai_manager_success = await test_ai_service_manager()
|
||||
|
||||
# Test 2: AI Engine Integration
|
||||
ai_engine_success = await test_ai_engine_integration()
|
||||
|
||||
# Test 3: Performance Monitoring
|
||||
performance_success = await test_performance_monitoring()
|
||||
|
||||
# Test 4: Configuration Management
|
||||
config_success = await test_configuration_management()
|
||||
|
||||
print("\n" + "=" * 70)
|
||||
print("📊 Test Results Summary:")
|
||||
print(f"AI Service Manager: {'✅ PASSED' if ai_manager_success else '❌ FAILED'}")
|
||||
print(f"AI Engine Integration: {'✅ PASSED' if ai_engine_success else '❌ FAILED'}")
|
||||
print(f"Performance Monitoring: {'✅ PASSED' if performance_success else '❌ FAILED'}")
|
||||
print(f"Configuration Management: {'✅ PASSED' if config_success else '❌ FAILED'}")
|
||||
|
||||
if ai_manager_success and ai_engine_success and performance_success and config_success:
|
||||
print("\n🎉 All Phase 4 tests passed! AI Service Integration is working correctly.")
|
||||
print("\n✅ Phase 4 Achievements:")
|
||||
print(" - Centralized AI service management implemented")
|
||||
print(" - Performance monitoring with metrics tracking")
|
||||
print(" - Service breakdown by AI type")
|
||||
print(" - Configuration management with timeout settings")
|
||||
print(" - Health monitoring and error handling")
|
||||
print(" - All services integrated with AI Service Manager")
|
||||
return 0
|
||||
else:
|
||||
print("\n⚠️ Some Phase 4 tests failed. Please check the AI configuration.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit_code = asyncio.run(main())
|
||||
sys.exit(exit_code)
|
||||
173
docs/alwrity_test_scripts/test_schema_fixes.py
Normal file
173
docs/alwrity_test_scripts/test_schema_fixes.py
Normal file
@@ -0,0 +1,173 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify the schema validation fixes.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add the backend directory to the path
|
||||
sys.path.append(str(Path(__file__).parent / 'backend'))
|
||||
|
||||
from llm_providers.gemini_provider import _clean_schema_for_gemini, _validate_and_fix_schema
|
||||
|
||||
def test_empty_object_fix():
|
||||
"""Test fixing empty object properties."""
|
||||
try:
|
||||
print("🧪 Testing empty object property fix...")
|
||||
|
||||
# Test schema with empty object properties (like the one causing errors)
|
||||
test_schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"trends": {
|
||||
"type": "object",
|
||||
"properties": {} # This causes the error
|
||||
},
|
||||
"analysis": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"score": {"type": "number"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Clean the schema
|
||||
cleaned_schema = _clean_schema_for_gemini(test_schema)
|
||||
fixed_schema = _validate_and_fix_schema(cleaned_schema)
|
||||
|
||||
# Check that empty object properties are converted to strings
|
||||
assert fixed_schema["properties"]["trends"]["type"] == "string"
|
||||
assert fixed_schema["properties"]["analysis"]["type"] == "object"
|
||||
assert "score" in fixed_schema["properties"]["analysis"]["properties"]
|
||||
|
||||
print("✅ Empty object property fix: PASSED")
|
||||
print(f" - Trends type: {fixed_schema['properties']['trends']['type']}")
|
||||
print(f" - Analysis type: {fixed_schema['properties']['analysis']['type']}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Empty object property fix: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def test_complex_schema_validation():
|
||||
"""Test complex schema validation."""
|
||||
try:
|
||||
print("🧪 Testing complex schema validation...")
|
||||
|
||||
# Test schema with nested empty objects
|
||||
test_schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"metrics": {
|
||||
"type": "object",
|
||||
"properties": {} # Empty properties
|
||||
},
|
||||
"summary": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"total": {"type": "integer"},
|
||||
"average": {"type": "number"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Clean and validate the schema
|
||||
cleaned_schema = _clean_schema_for_gemini(test_schema)
|
||||
fixed_schema = _validate_and_fix_schema(cleaned_schema)
|
||||
|
||||
# Check that empty nested objects are fixed
|
||||
assert fixed_schema["properties"]["data"]["properties"]["metrics"]["type"] == "string"
|
||||
assert fixed_schema["properties"]["data"]["properties"]["summary"]["type"] == "object"
|
||||
assert "total" in fixed_schema["properties"]["data"]["properties"]["summary"]["properties"]
|
||||
|
||||
print("✅ Complex schema validation: PASSED")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Complex schema validation: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def test_unsupported_properties_removal():
|
||||
"""Test removal of unsupported properties."""
|
||||
try:
|
||||
print("🧪 Testing unsupported properties removal...")
|
||||
|
||||
# Test schema with unsupported properties
|
||||
test_schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"maxLength": 100,
|
||||
"pattern": "^[a-zA-Z0-9 ]+$"
|
||||
},
|
||||
"content": {
|
||||
"type": "string",
|
||||
"format": "text"
|
||||
}
|
||||
},
|
||||
"additionalProperties": False
|
||||
}
|
||||
|
||||
# Clean the schema
|
||||
cleaned_schema = _clean_schema_for_gemini(test_schema)
|
||||
|
||||
# Check that unsupported properties are removed
|
||||
assert "additionalProperties" not in cleaned_schema
|
||||
assert "minLength" not in cleaned_schema["properties"]["title"]
|
||||
assert "maxLength" not in cleaned_schema["properties"]["title"]
|
||||
assert "pattern" not in cleaned_schema["properties"]["title"]
|
||||
assert "format" not in cleaned_schema["properties"]["content"]
|
||||
|
||||
# Check that supported properties remain
|
||||
assert "type" in cleaned_schema
|
||||
assert "properties" in cleaned_schema
|
||||
|
||||
print("✅ Unsupported properties removal: PASSED")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Unsupported properties removal: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""Run all schema validation tests."""
|
||||
print("🧪 Testing Schema Validation Fixes")
|
||||
print("=" * 50)
|
||||
|
||||
tests = [
|
||||
test_empty_object_fix,
|
||||
test_complex_schema_validation,
|
||||
test_unsupported_properties_removal
|
||||
]
|
||||
|
||||
passed = 0
|
||||
total = len(tests)
|
||||
|
||||
for test in tests:
|
||||
if test():
|
||||
passed += 1
|
||||
print()
|
||||
|
||||
print("=" * 50)
|
||||
print(f"📊 Test Results: {passed}/{total} tests passed")
|
||||
|
||||
if passed == total:
|
||||
print("🎉 All schema validation tests passed!")
|
||||
return 0
|
||||
else:
|
||||
print("⚠️ Some schema validation tests failed.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
435
docs/alwrity_test_scripts/test_service_integration.py
Normal file
435
docs/alwrity_test_scripts/test_service_integration.py
Normal file
@@ -0,0 +1,435 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for Phase 3: Service Integration
|
||||
Verifies that content planning service integrates with database and AI services correctly.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
# Add the backend directory to the Python path
|
||||
sys.path.append(str(Path(__file__).parent / "backend"))
|
||||
|
||||
from services.database import init_database, get_db_session
|
||||
from services.content_planning_service import ContentPlanningService
|
||||
from services.content_planning_db import ContentPlanningDBService
|
||||
from loguru import logger
|
||||
|
||||
async def test_database_initialization():
|
||||
"""Test database initialization."""
|
||||
|
||||
print("🗄️ Testing Database Initialization...")
|
||||
|
||||
try:
|
||||
# Initialize database
|
||||
init_database()
|
||||
print("✅ Database initialized successfully")
|
||||
|
||||
# Test database session
|
||||
db_session = get_db_session()
|
||||
if db_session:
|
||||
print("✅ Database session created successfully")
|
||||
db_session.close()
|
||||
return True
|
||||
else:
|
||||
print("❌ Failed to create database session")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Database initialization failed: {str(e)}")
|
||||
return False
|
||||
|
||||
async def test_service_initialization():
|
||||
"""Test content planning service initialization."""
|
||||
|
||||
print("\n🔧 Testing Service Initialization...")
|
||||
|
||||
try:
|
||||
# Test service initialization with database session
|
||||
db_session = get_db_session()
|
||||
if not db_session:
|
||||
print("❌ No database session available")
|
||||
return False
|
||||
|
||||
service = ContentPlanningService(db_session)
|
||||
|
||||
if service.db_service:
|
||||
print("✅ Content planning service initialized with database service")
|
||||
else:
|
||||
print("❌ Database service not initialized")
|
||||
return False
|
||||
|
||||
if service.ai_manager:
|
||||
print("✅ AI service manager initialized")
|
||||
else:
|
||||
print("❌ AI service manager not initialized")
|
||||
return False
|
||||
|
||||
db_session.close()
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Service initialization failed: {str(e)}")
|
||||
return False
|
||||
|
||||
async def test_content_strategy_with_ai():
|
||||
"""Test content strategy creation with AI integration."""
|
||||
|
||||
print("\n📋 Testing Content Strategy with AI...")
|
||||
|
||||
db_session = get_db_session()
|
||||
if not db_session:
|
||||
print("❌ No database session available")
|
||||
return False
|
||||
|
||||
service = ContentPlanningService(db_session)
|
||||
|
||||
# Test 1: Create content strategy with AI
|
||||
print("\n📝 Test 1: Create Content Strategy with AI")
|
||||
strategy_data = {
|
||||
'user_id': 1,
|
||||
'name': 'AI-Enhanced Content Strategy',
|
||||
'industry': 'technology',
|
||||
'target_audience': {
|
||||
'demographics': '25-45 years old',
|
||||
'interests': ['technology', 'innovation', 'AI']
|
||||
},
|
||||
'content_preferences': {
|
||||
'formats': ['blog_posts', 'videos', 'social_media'],
|
||||
'frequency': 'weekly',
|
||||
'platforms': ['website', 'linkedin', 'youtube']
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
strategy = await service.create_content_strategy_with_ai(
|
||||
user_id=strategy_data['user_id'],
|
||||
strategy_data=strategy_data
|
||||
)
|
||||
|
||||
if strategy:
|
||||
print(f"✅ Content strategy created with AI: {strategy.id}")
|
||||
strategy_id = strategy.id
|
||||
else:
|
||||
print("❌ Failed to create content strategy with AI")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating content strategy with AI: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Get content strategy from database
|
||||
print("\n📖 Test 2: Get Content Strategy from Database")
|
||||
try:
|
||||
retrieved_strategy = await service.get_content_strategy(
|
||||
user_id=strategy_data['user_id'],
|
||||
strategy_id=strategy_id
|
||||
)
|
||||
|
||||
if retrieved_strategy:
|
||||
print(f"✅ Content strategy retrieved: {retrieved_strategy.name}")
|
||||
print(f" - Industry: {retrieved_strategy.industry}")
|
||||
print(f" - AI Recommendations: {len(retrieved_strategy.ai_recommendations) if retrieved_strategy.ai_recommendations else 0} items")
|
||||
else:
|
||||
print("❌ Failed to retrieve content strategy")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error retrieving content strategy: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 3: Analyze content strategy with AI
|
||||
print("\n🤖 Test 3: Analyze Content Strategy with AI")
|
||||
try:
|
||||
ai_strategy = await service.analyze_content_strategy_with_ai(
|
||||
industry='artificial_intelligence',
|
||||
target_audience={
|
||||
'demographics': '30-50 years old',
|
||||
'interests': ['AI', 'machine learning', 'data science']
|
||||
},
|
||||
business_goals=['thought leadership', 'lead generation'],
|
||||
content_preferences={
|
||||
'formats': ['blog_posts', 'webinars', 'case_studies'],
|
||||
'frequency': 'bi-weekly'
|
||||
},
|
||||
user_id=2
|
||||
)
|
||||
|
||||
if ai_strategy:
|
||||
print(f"✅ AI-analyzed strategy created: {ai_strategy.id}")
|
||||
print(f" - Name: {ai_strategy.name}")
|
||||
print(f" - Industry: {ai_strategy.industry}")
|
||||
else:
|
||||
print("❌ Failed to create AI-analyzed strategy")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error analyzing content strategy with AI: {str(e)}")
|
||||
return False
|
||||
|
||||
db_session.close()
|
||||
return True
|
||||
|
||||
async def test_calendar_events_with_ai():
|
||||
"""Test calendar event creation with AI integration."""
|
||||
|
||||
print("\n📅 Testing Calendar Events with AI...")
|
||||
|
||||
db_session = get_db_session()
|
||||
if not db_session:
|
||||
print("❌ No database session available")
|
||||
return False
|
||||
|
||||
service = ContentPlanningService(db_session)
|
||||
|
||||
# First create a strategy for the events
|
||||
strategy_data = {
|
||||
'user_id': 1,
|
||||
'name': 'Test Strategy for Events',
|
||||
'industry': 'technology'
|
||||
}
|
||||
|
||||
try:
|
||||
strategy = await service.create_content_strategy_with_ai(
|
||||
user_id=strategy_data['user_id'],
|
||||
strategy_data=strategy_data
|
||||
)
|
||||
|
||||
if not strategy:
|
||||
print("❌ Failed to create test strategy")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating test strategy: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 1: Create calendar event with AI
|
||||
print("\n📝 Test 1: Create Calendar Event with AI")
|
||||
event_data = {
|
||||
'strategy_id': strategy.id,
|
||||
'title': 'AI Marketing Trends 2024',
|
||||
'description': 'Comprehensive analysis of AI marketing trends and strategies',
|
||||
'content_type': 'blog_post',
|
||||
'platform': 'website',
|
||||
'scheduled_date': datetime.utcnow() + timedelta(days=7)
|
||||
}
|
||||
|
||||
try:
|
||||
event = await service.create_calendar_event_with_ai(event_data)
|
||||
|
||||
if event:
|
||||
print(f"✅ Calendar event created with AI: {event.id}")
|
||||
print(f" - Title: {event.title}")
|
||||
print(f" - Platform: {event.platform}")
|
||||
print(f" - AI Recommendations: {len(event.ai_recommendations) if event.ai_recommendations else 0} items")
|
||||
event_id = event.id
|
||||
else:
|
||||
print("❌ Failed to create calendar event with AI")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating calendar event with AI: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Get calendar events from database
|
||||
print("\n📖 Test 2: Get Calendar Events from Database")
|
||||
try:
|
||||
events = await service.get_calendar_events(strategy_id=strategy.id)
|
||||
|
||||
if events:
|
||||
print(f"✅ Retrieved {len(events)} calendar events")
|
||||
for event in events:
|
||||
print(f" - {event.title} ({event.content_type})")
|
||||
else:
|
||||
print("❌ No calendar events found")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error getting calendar events: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 3: Track content performance with AI
|
||||
print("\n📊 Test 3: Track Content Performance with AI")
|
||||
try:
|
||||
performance = await service.track_content_performance_with_ai(event_id)
|
||||
|
||||
if performance:
|
||||
print(f"✅ Performance tracking completed: {performance['analytics_id']}")
|
||||
print(f" - Performance Score: {performance['performance_score']}")
|
||||
print(f" - Engagement Prediction: {performance['engagement_prediction']}")
|
||||
else:
|
||||
print("❌ Failed to track content performance")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error tracking content performance: {str(e)}")
|
||||
return False
|
||||
|
||||
db_session.close()
|
||||
return True
|
||||
|
||||
async def test_content_gap_analysis_with_ai():
|
||||
"""Test content gap analysis with AI integration."""
|
||||
|
||||
print("\n🔍 Testing Content Gap Analysis with AI...")
|
||||
|
||||
db_session = get_db_session()
|
||||
if not db_session:
|
||||
print("❌ No database session available")
|
||||
return False
|
||||
|
||||
service = ContentPlanningService(db_session)
|
||||
|
||||
# Test 1: Analyze content gaps with AI
|
||||
print("\n📝 Test 1: Analyze Content Gaps with AI")
|
||||
try:
|
||||
analysis = await service.analyze_content_gaps_with_ai(
|
||||
website_url='https://example.com',
|
||||
competitor_urls=['https://competitor1.com', 'https://competitor2.com'],
|
||||
user_id=1,
|
||||
target_keywords=['AI marketing', 'digital transformation', 'content strategy']
|
||||
)
|
||||
|
||||
if analysis:
|
||||
print(f"✅ Content gap analysis completed: {analysis['analysis_id']}")
|
||||
print(f" - Stored at: {analysis['stored_at']}")
|
||||
print(f" - Results: {len(analysis['results']) if analysis['results'] else 0} items")
|
||||
else:
|
||||
print("❌ Failed to analyze content gaps with AI")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error analyzing content gaps with AI: {str(e)}")
|
||||
return False
|
||||
|
||||
# Test 2: Generate content recommendations with AI
|
||||
print("\n💡 Test 2: Generate Content Recommendations with AI")
|
||||
try:
|
||||
# First create a strategy for recommendations
|
||||
strategy_data = {
|
||||
'user_id': 1,
|
||||
'name': 'Recommendation Test Strategy',
|
||||
'industry': 'technology'
|
||||
}
|
||||
|
||||
strategy = await service.create_content_strategy_with_ai(
|
||||
user_id=strategy_data['user_id'],
|
||||
strategy_data=strategy_data
|
||||
)
|
||||
|
||||
if strategy:
|
||||
recommendations = await service.generate_content_recommendations_with_ai(strategy.id)
|
||||
|
||||
if recommendations:
|
||||
print(f"✅ Generated {len(recommendations)} content recommendations")
|
||||
for i, rec in enumerate(recommendations[:3], 1):
|
||||
print(f" {i}. {rec.get('title', 'Untitled')} ({rec.get('type', 'content')})")
|
||||
else:
|
||||
print("❌ No content recommendations generated")
|
||||
return False
|
||||
else:
|
||||
print("❌ Failed to create strategy for recommendations")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error generating content recommendations: {str(e)}")
|
||||
return False
|
||||
|
||||
db_session.close()
|
||||
return True
|
||||
|
||||
async def test_ai_analytics_storage():
|
||||
"""Test AI analytics storage functionality."""
|
||||
|
||||
print("\n📊 Testing AI Analytics Storage...")
|
||||
|
||||
db_session = get_db_session()
|
||||
if not db_session:
|
||||
print("❌ No database session available")
|
||||
return False
|
||||
|
||||
service = ContentPlanningService(db_session)
|
||||
|
||||
# Test 1: Create strategy and verify AI analytics storage
|
||||
print("\n📝 Test 1: Verify AI Analytics Storage")
|
||||
try:
|
||||
strategy_data = {
|
||||
'user_id': 1,
|
||||
'name': 'Analytics Test Strategy',
|
||||
'industry': 'technology',
|
||||
'target_audience': {'demographics': '25-45 years old'},
|
||||
'content_preferences': {'formats': ['blog_posts']}
|
||||
}
|
||||
|
||||
strategy = await service.create_content_strategy_with_ai(
|
||||
user_id=strategy_data['user_id'],
|
||||
strategy_data=strategy_data
|
||||
)
|
||||
|
||||
if strategy:
|
||||
print(f"✅ Strategy created with AI analytics: {strategy.id}")
|
||||
|
||||
# Check if AI analytics were stored
|
||||
db_service = service._get_db_service()
|
||||
analytics = await db_service.get_strategy_analytics(strategy.id)
|
||||
|
||||
if analytics:
|
||||
print(f"✅ AI analytics stored: {len(analytics)} records")
|
||||
for analytic in analytics:
|
||||
print(f" - Type: {analytic.analysis_type}")
|
||||
print(f" - Performance Score: {analytic.performance_score}")
|
||||
else:
|
||||
print("⚠️ No AI analytics found (this might be expected)")
|
||||
else:
|
||||
print("❌ Failed to create strategy for analytics test")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Error testing AI analytics storage: {str(e)}")
|
||||
return False
|
||||
|
||||
db_session.close()
|
||||
return True
|
||||
|
||||
async def main():
|
||||
"""Main test function."""
|
||||
print("🚀 Starting Phase 3: Service Integration Tests...")
|
||||
print("=" * 60)
|
||||
|
||||
# Test 1: Database Initialization
|
||||
db_init_success = await test_database_initialization()
|
||||
|
||||
# Test 2: Service Initialization
|
||||
service_init_success = await test_service_initialization()
|
||||
|
||||
# Test 3: Content Strategy with AI
|
||||
strategy_success = await test_content_strategy_with_ai()
|
||||
|
||||
# Test 4: Calendar Events with AI
|
||||
events_success = await test_calendar_events_with_ai()
|
||||
|
||||
# Test 5: Content Gap Analysis with AI
|
||||
analysis_success = await test_content_gap_analysis_with_ai()
|
||||
|
||||
# Test 6: AI Analytics Storage
|
||||
analytics_success = await test_ai_analytics_storage()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("📊 Test Results Summary:")
|
||||
print(f"Database Initialization: {'✅ PASSED' if db_init_success else '❌ FAILED'}")
|
||||
print(f"Service Initialization: {'✅ PASSED' if service_init_success else '❌ FAILED'}")
|
||||
print(f"Content Strategy with AI: {'✅ PASSED' if strategy_success else '❌ FAILED'}")
|
||||
print(f"Calendar Events with AI: {'✅ PASSED' if events_success else '❌ FAILED'}")
|
||||
print(f"Content Gap Analysis with AI: {'✅ PASSED' if analysis_success else '❌ FAILED'}")
|
||||
print(f"AI Analytics Storage: {'✅ PASSED' if analytics_success else '❌ FAILED'}")
|
||||
|
||||
if db_init_success and service_init_success and strategy_success and events_success and analysis_success and analytics_success:
|
||||
print("\n🎉 All Phase 3 service integration tests passed!")
|
||||
print("\n✅ Phase 3 Service Integration Achievements:")
|
||||
print(" - Content planning service integrated with database operations")
|
||||
print(" - AI services integrated with database storage")
|
||||
print(" - Data persistence for AI results implemented")
|
||||
print(" - Service database integration tested and functional")
|
||||
print(" - AI analytics tracking and storage working")
|
||||
print(" - Comprehensive error handling and logging")
|
||||
return 0
|
||||
else:
|
||||
print("\n⚠️ Some Phase 3 service integration tests failed. Please check the service configuration.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit_code = asyncio.run(main())
|
||||
sys.exit(exit_code)
|
||||
121
docs/alwrity_test_scripts/test_structured_output.py
Normal file
121
docs/alwrity_test_scripts/test_structured_output.py
Normal file
@@ -0,0 +1,121 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify the structured output functionality.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add the backend directory to the path
|
||||
sys.path.append(str(Path(__file__).parent / 'backend'))
|
||||
|
||||
from llm_providers.gemini_provider import gemini_structured_json_response, _clean_schema_for_gemini
|
||||
|
||||
def test_schema_cleaning():
|
||||
"""Test the schema cleaning function."""
|
||||
try:
|
||||
print("🧪 Testing schema cleaning...")
|
||||
|
||||
# Test schema with unsupported properties
|
||||
test_schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {"type": "string", "minLength": 1, "maxLength": 100},
|
||||
"description": {"type": "string", "pattern": "^[a-zA-Z0-9 ]+$"},
|
||||
"tags": {"type": "array", "items": {"type": "string"}}
|
||||
},
|
||||
"additionalProperties": False,
|
||||
"required": ["title"]
|
||||
}
|
||||
|
||||
cleaned_schema = _clean_schema_for_gemini(test_schema)
|
||||
|
||||
# Check that unsupported properties are removed
|
||||
assert "additionalProperties" not in cleaned_schema
|
||||
assert "minLength" not in cleaned_schema["properties"]["title"]
|
||||
assert "maxLength" not in cleaned_schema["properties"]["title"]
|
||||
assert "pattern" not in cleaned_schema["properties"]["description"]
|
||||
|
||||
# Check that supported properties remain
|
||||
assert "type" in cleaned_schema
|
||||
assert "properties" in cleaned_schema
|
||||
assert "required" in cleaned_schema
|
||||
|
||||
print("✅ Schema cleaning: PASSED")
|
||||
print(f" - Original schema keys: {list(test_schema.keys())}")
|
||||
print(f" - Cleaned schema keys: {list(cleaned_schema.keys())}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Schema cleaning: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def test_structured_output():
|
||||
"""Test structured JSON output."""
|
||||
try:
|
||||
print("🧪 Testing structured JSON output...")
|
||||
|
||||
# Simple schema for testing
|
||||
test_schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {"type": "string"},
|
||||
"age": {"type": "integer"},
|
||||
"city": {"type": "string"}
|
||||
},
|
||||
"required": ["name", "age"]
|
||||
}
|
||||
|
||||
# Test prompt
|
||||
prompt = "Create a person profile with name John, age 30, and city New York."
|
||||
|
||||
response = gemini_structured_json_response(
|
||||
prompt=prompt,
|
||||
schema=test_schema,
|
||||
temperature=0.1,
|
||||
max_tokens=100
|
||||
)
|
||||
|
||||
if isinstance(response, dict) and "name" in response and "age" in response:
|
||||
print("✅ Structured JSON output: PASSED")
|
||||
print(f" - Response: {response}")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ Structured JSON output: FAILED (Response: {response})")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Structured JSON output: FAILED (Error: {e})")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""Run all structured output tests."""
|
||||
print("🧪 Testing Structured Output Functionality")
|
||||
print("=" * 50)
|
||||
|
||||
tests = [
|
||||
test_schema_cleaning,
|
||||
test_structured_output
|
||||
]
|
||||
|
||||
passed = 0
|
||||
total = len(tests)
|
||||
|
||||
for test in tests:
|
||||
if test():
|
||||
passed += 1
|
||||
print()
|
||||
|
||||
print("=" * 50)
|
||||
print(f"📊 Test Results: {passed}/{total} tests passed")
|
||||
|
||||
if passed == total:
|
||||
print("🎉 All structured output tests passed!")
|
||||
return 0
|
||||
else:
|
||||
print("⚠️ Some structured output tests failed.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user