#!/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()