kaauh_ats/test_job_reminders.py

170 lines
4.8 KiB
Python

#!/usr/bin/env python
"""
Test script for job closing reminder system
"""
import os
import sys
import django
from datetime import datetime, timedelta
from django.utils import timezone
# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'NorahUniversity.settings')
django.setup()
from recruitment.models import JobPosting, Application
from django_q.models import Schedule
from django.contrib.auth import get_user_model
User = get_user_model()
def test_reminder_scheduling():
"""Test that reminders are properly scheduled when a job is created/updated"""
print("🧪 Testing Job Closing Reminder System")
print("=" * 50)
# Get or create a test user
test_user, created = User.objects.get_or_create(
username='test_recruiter',
defaults={
'email': 'test@example.com',
'is_staff': True
}
)
# Create a test job with deadline in 2 days
deadline = timezone.now() + timedelta(days=2)
print(f"📅 Creating test job with deadline: {deadline}")
job = JobPosting.objects.create(
title='Test Software Engineer Position',
description='Test job for reminder system',
qualifications='Python, Django, PostgreSQL',
status='ACTIVE',
application_deadline=deadline,
assigned_to=test_user,
created_by='Test User'
)
print(f"✅ Created job: {job.title} (ID: {job.pk})")
# Check if schedules were created
schedules = Schedule.objects.filter(
name__in=[
f"one_day_reminder_{job.pk}",
f"fifteen_min_reminder_{job.pk}",
f"job_closing_{job.pk}"
]
)
print(f"\n📋 Scheduled Tasks ({schedules.count()} found):")
for schedule in schedules:
print(f" - {schedule.name}: {schedule.next_run}")
# Test deadline update
print(f"\n🔄 Testing deadline update...")
new_deadline = deadline + timedelta(hours=6)
job.application_deadline = new_deadline
job.save()
# Check if schedules were updated
updated_schedules = Schedule.objects.filter(
name__in=[
f"one_day_reminder_{job.pk}",
f"fifteen_min_reminder_{job.pk}",
f"job_closing_{job.pk}"
]
)
print(f"📋 Updated Scheduled Tasks ({updated_schedules.count()} found):")
for schedule in updated_schedules:
print(f" - {schedule.name}: {schedule.next_run}")
# Test job deactivation
print(f"\n🛑 Testing job deactivation...")
job.status = 'CLOSED'
job.save()
remaining_schedules = Schedule.objects.filter(
name__in=[
f"one_day_reminder_{job.pk}",
f"fifteen_min_reminder_{job.pk}",
f"job_closing_{job.pk}"
]
)
print(f"📋 Remaining Scheduled Tasks after deactivation: {remaining_schedules.count()}")
# Cleanup
print(f"\n🧹 Cleaning up test data...")
job.delete()
print("✅ Test completed successfully!")
def test_email_functions():
"""Test the email functions directly"""
print("\n🧪 Testing Email Functions")
print("=" * 50)
# Create a test job
test_user, _ = User.objects.get_or_create(
username='test_recruiter_email',
defaults={'email': 'test_email@example.com', 'is_staff': True}
)
job = JobPosting.objects.create(
title='Test Email Job',
description='Test job for email functions',
status='ACTIVE',
application_deadline=timezone.now() + timedelta(hours=1),
assigned_to=test_user,
created_by='Test User'
)
print(f"📧 Testing email functions for job: {job.pk}")
# Import and test email functions
from recruitment.tasks import (
send_one_day_reminder,
send_fifteen_minute_reminder,
send_job_closed_notification
)
try:
print("📤 Testing 1-day reminder...")
send_one_day_reminder(job.pk)
print("✅ 1-day reminder sent successfully")
except Exception as e:
print(f"❌ 1-day reminder failed: {e}")
try:
print("📤 Testing 15-minute reminder...")
send_fifteen_minute_reminder(job.pk)
print("✅ 15-minute reminder sent successfully")
except Exception as e:
print(f"❌ 15-minute reminder failed: {e}")
try:
print("📤 Testing job closed notification...")
send_job_closed_notification(job.pk)
print("✅ Job closed notification sent successfully")
except Exception as e:
print(f"❌ Job closed notification failed: {e}")
# Cleanup
job.delete()
print("🧹 Test data cleaned up")
if __name__ == '__main__':
try:
test_reminder_scheduling()
test_email_functions()
print("\n🎉 All tests completed!")
except Exception as e:
print(f"\n❌ Test failed with error: {e}")
import traceback
traceback.print_exc()