132 lines
4.7 KiB
Python
132 lines
4.7 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
Test script to verify CSV export functionality with updated JSON structure
|
|
"""
|
|
import os
|
|
import sys
|
|
import django
|
|
|
|
# Setup Django environment
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'NorahUniversity.settings')
|
|
django.setup()
|
|
|
|
from recruitment.models import Candidate, JobPosting
|
|
from recruitment.views_frontend import export_candidates_csv
|
|
from django.test import RequestFactory
|
|
from django.contrib.auth.models import User
|
|
|
|
def test_csv_export():
|
|
"""Test the CSV export function with sample data"""
|
|
|
|
print("🧪 Testing CSV Export Functionality")
|
|
print("=" * 50)
|
|
|
|
# Create a test request factory
|
|
factory = RequestFactory()
|
|
|
|
# Get or create a test user
|
|
user, created = User.objects.get_or_create(
|
|
username='testuser',
|
|
defaults={'email': 'test@example.com', 'is_staff': True}
|
|
)
|
|
|
|
# Get a sample job
|
|
job = JobPosting.objects.first()
|
|
if not job:
|
|
print("❌ No jobs found in database. Please create a job first.")
|
|
return False
|
|
|
|
print(f"📋 Using job: {job.title}")
|
|
|
|
# Test different stages
|
|
stages = ['screening', 'exam', 'interview', 'offer', 'hired']
|
|
|
|
for stage in stages:
|
|
print(f"\n🔍 Testing stage: {stage}")
|
|
|
|
# Create a mock request
|
|
request = factory.get(f'/export/{job.slug}/{stage}/')
|
|
request.user = user
|
|
request.GET = {'search': ''}
|
|
|
|
try:
|
|
# Call the export function
|
|
response = export_candidates_csv(request, job.slug, stage)
|
|
|
|
# Check if response is successful
|
|
if response.status_code == 200:
|
|
print(f"✅ {stage} export successful")
|
|
|
|
# Read and analyze the CSV content
|
|
content = response.content.decode('utf-8-sig')
|
|
lines = content.split('\n')
|
|
|
|
if len(lines) > 1:
|
|
headers = lines[0].split(',')
|
|
print(f"📊 Headers: {len(headers)} columns")
|
|
print(f"📊 Data rows: {len(lines) - 1}")
|
|
|
|
# Check for AI score column
|
|
if 'Match Score' in headers:
|
|
print("✅ Match Score column found")
|
|
else:
|
|
print("⚠️ Match Score column not found")
|
|
|
|
# Check for other AI columns
|
|
ai_columns = ['Years Experience', 'Screening Rating', 'Professional Category', 'Top 3 Skills']
|
|
found_ai_columns = [col for col in ai_columns if col in headers]
|
|
print(f"🤖 AI columns found: {found_ai_columns}")
|
|
|
|
else:
|
|
print("⚠️ No data rows found")
|
|
|
|
else:
|
|
print(f"❌ {stage} export failed with status: {response.status_code}")
|
|
|
|
except Exception as e:
|
|
print(f"❌ {stage} export error: {str(e)}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
# Test with actual candidate data
|
|
print(f"\n🔍 Testing with actual candidate data")
|
|
candidates = Candidate.objects.filter(job=job)
|
|
print(f"📊 Total candidates for job: {candidates.count()}")
|
|
|
|
if candidates.exists():
|
|
# Test AI data extraction for first candidate
|
|
candidate = candidates.first()
|
|
print(f"\n🧪 Testing AI data extraction for: {candidate.name}")
|
|
|
|
try:
|
|
# Test the model properties
|
|
print(f"📊 Match Score: {candidate.match_score}")
|
|
print(f"📊 Years Experience: {candidate.years_of_experience}")
|
|
print(f"📊 Screening Rating: {candidate.screening_stage_rating}")
|
|
print(f"📊 Professional Category: {candidate.professional_category}")
|
|
print(f"📊 Top 3 Skills: {candidate.top_3_keywords}")
|
|
print(f"📊 Strengths: {candidate.strengths}")
|
|
print(f"📊 Weaknesses: {candidate.weaknesses}")
|
|
|
|
# Test AI analysis data structure
|
|
if candidate.ai_analysis_data:
|
|
print(f"📊 AI Analysis Data keys: {list(candidate.ai_analysis_data.keys())}")
|
|
if 'analysis_data' in candidate.ai_analysis_data:
|
|
analysis_keys = list(candidate.ai_analysis_data['analysis_data'].keys())
|
|
print(f"📊 Analysis Data keys: {analysis_keys}")
|
|
else:
|
|
print("⚠️ 'analysis_data' key not found in ai_analysis_data")
|
|
else:
|
|
print("⚠️ No AI analysis data found")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error extracting AI data: {str(e)}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
print("\n🎉 CSV Export Test Complete!")
|
|
return True
|
|
|
|
if __name__ == '__main__':
|
|
test_csv_export()
|