kaauh_ats/test_agency_crud.py

205 lines
7.0 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
"""
Test script to verify Agency CRUD functionality
"""
import os
import sys
import django
# Add the project directory to the Python path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# Set up Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'NorahUniversity.settings')
django.setup()
from django.test import Client
from django.contrib.auth.models import User
from recruitment.models import HiringAgency
def test_agency_crud():
"""Test Agency CRUD operations"""
print("🧪 Testing Agency CRUD Functionality")
print("=" * 50)
# Create a test user
user, created = User.objects.get_or_create(
username='testuser',
defaults={'email': 'test@example.com', 'is_staff': True, 'is_superuser': True}
)
if created:
user.set_password('testpass123')
user.save()
print("✅ Created test user")
else:
print(" Using existing test user")
# Create test client
client = Client()
# Login the user
client.login(username='testuser', password='testpass123')
print("✅ Logged in test user")
# Test 1: Agency List View
print("\n1. Testing Agency List View...")
response = client.get('/recruitment/agencies/')
if response.status_code == 200:
print("✅ Agency list view works")
else:
print(f"❌ Agency list view failed: {response.status_code}")
return False
# Test 2: Agency Create View (GET)
print("\n2. Testing Agency Create View (GET)...")
response = client.get('/recruitment/agencies/create/')
if response.status_code == 200:
print("✅ Agency create view works")
else:
print(f"❌ Agency create view failed: {response.status_code}")
return False
# Test 3: Agency Create (POST)
print("\n3. Testing Agency Create (POST)...")
agency_data = {
'name': 'Test Agency',
'contact_person': 'John Doe',
'email': 'test@agency.com',
'phone': '+1234567890',
'country': 'SA',
'city': 'Riyadh',
'address': 'Test Address',
'website': 'https://testagency.com',
'description': 'Test agency description'
}
response = client.post('/recruitment/agencies/create/', agency_data)
if response.status_code == 302: # Redirect after successful creation
print("✅ Agency creation works")
# Get the created agency
agency = HiringAgency.objects.filter(name='Test Agency').first()
if agency:
print(f"✅ Agency created with ID: {agency.id}")
# Test 4: Agency Detail View
print("\n4. Testing Agency Detail View...")
response = client.get(f'/recruitment/agencies/{agency.slug}/')
if response.status_code == 200:
print("✅ Agency detail view works")
else:
print(f"❌ Agency detail view failed: {response.status_code}")
return False
# Test 5: Agency Update View (GET)
print("\n5. Testing Agency Update View (GET)...")
response = client.get(f'/recruitment/agencies/{agency.slug}/update/')
if response.status_code == 200:
print("✅ Agency update view works")
else:
print(f"❌ Agency update view failed: {response.status_code}")
return False
# Test 6: Agency Update (POST)
print("\n6. Testing Agency Update (POST)...")
update_data = agency_data.copy()
update_data['name'] = 'Updated Test Agency'
response = client.post(f'/recruitment/agencies/{agency.slug}/update/', update_data)
if response.status_code == 302:
print("✅ Agency update works")
# Verify the update
agency.refresh_from_db()
if agency.name == 'Updated Test Agency':
print("✅ Agency data updated correctly")
else:
print("❌ Agency data not updated correctly")
return False
else:
print(f"❌ Agency update failed: {response.status_code}")
return False
# Test 7: Agency Delete View (GET)
print("\n7. Testing Agency Delete View (GET)...")
response = client.get(f'/recruitment/agencies/{agency.slug}/delete/')
if response.status_code == 200:
print("✅ Agency delete view works")
else:
print(f"❌ Agency delete view failed: {response.status_code}")
return False
# Test 8: Agency Delete (POST)
print("\n8. Testing Agency Delete (POST)...")
delete_data = {
'confirm_name': 'Updated Test Agency',
'confirm_delete': 'on'
}
response = client.post(f'/recruitment/agencies/{agency.slug}/delete/', delete_data)
if response.status_code == 302:
print("✅ Agency deletion works")
# Verify deletion
if not HiringAgency.objects.filter(name='Updated Test Agency').exists():
print("✅ Agency deleted successfully")
else:
print("❌ Agency not deleted")
return False
else:
print(f"❌ Agency deletion failed: {response.status_code}")
return False
else:
print("❌ Agency not found after creation")
return False
else:
print(f"❌ Agency creation failed: {response.status_code}")
print(f"Response content: {response.content.decode()}")
return False
# Test 9: URL patterns
print("\n9. Testing URL patterns...")
try:
from django.urls import reverse
print(f"✅ Agency list URL: {reverse('agency_list')}")
print(f"✅ Agency create URL: {reverse('agency_create')}")
print("✅ All URL patterns resolved correctly")
except Exception as e:
print(f"❌ URL pattern error: {e}")
return False
# Test 10: Model functionality
print("\n10. Testing Model functionality...")
try:
# Test model creation
test_agency = HiringAgency.objects.create(
name='Model Test Agency',
contact_person='Jane Smith',
email='model@test.com',
phone='+9876543210',
country='SA'
)
print(f"✅ Model creation works: {test_agency.name}")
print(f"✅ Slug generation works: {test_agency.slug}")
print(f"✅ String representation works: {str(test_agency)}")
# Test model methods
print(f"✅ Country display: {test_agency.get_country_display()}")
# Clean up
test_agency.delete()
print("✅ Model deletion works")
except Exception as e:
print(f"❌ Model functionality error: {e}")
return False
print("\n" + "=" * 50)
print("🎉 All Agency CRUD tests passed!")
return True
if __name__ == '__main__':
success = test_agency_crud()
sys.exit(0 if success else 1)