Spaces:
Configuration error
Configuration error
| #!/usr/bin/env python3 | |
| """ | |
| Test PDF generation with actual model structure. | |
| """ | |
| from datetime import datetime | |
| import uuid | |
| from pdf_generator import create_pdf_resume | |
| from utils import log_info, log_error | |
| # Test with mock objects that mimic the model structure | |
| class MockWorkExperience: | |
| def __init__(self, title, organization, start_month, start_year, end_month=None, end_year=None, remarks=""): | |
| self.title = title | |
| self.organization = organization | |
| self.start_month = start_month | |
| self.start_year = start_year | |
| self.end_month = end_month | |
| self.end_year = end_year | |
| self.remarks = remarks | |
| class MockProject: | |
| def __init__(self, title, organization, start_month, start_year, end_month=None, end_year=None, remarks=""): | |
| self.title = title | |
| self.organization = organization | |
| self.start_month = start_month | |
| self.start_year = start_year | |
| self.end_month = end_month | |
| self.end_year = end_year | |
| self.remarks = remarks | |
| class MockEducation: | |
| def __init__(self, title, organization, start_month, start_year, end_month=None, end_year=None, remarks=""): | |
| self.title = title | |
| self.organization = organization | |
| self.start_month = start_month | |
| self.start_year = start_year | |
| self.end_month = end_month | |
| self.end_year = end_year | |
| self.remarks = remarks | |
| class MockSkill: | |
| def __init__(self, skill): | |
| self.skill = skill | |
| class MockAchievement: | |
| def __init__(self, achievement): | |
| self.achievement = achievement | |
| def test_with_mock_models(): | |
| """Test PDF generation with mock model objects.""" | |
| log_info("Testing PDF generation with mock model structure...") | |
| # Create mock data | |
| work_experiences = [ | |
| MockWorkExperience("Software Engineer", "Tech Corp", 1, 2022, None, None, "Developing web applications"), | |
| MockWorkExperience("Junior Developer", "StartupXYZ", 6, 2020, 12, 2021, "Built mobile apps") | |
| ] | |
| projects = [ | |
| MockProject("E-commerce Platform", "Personal", 3, 2023, 5, 2023, "Full-stack development") | |
| ] | |
| educations = [ | |
| MockEducation("BS Computer Science", "University", 9, 2016, 5, 2020, "Graduated with honors") | |
| ] | |
| skills = [ | |
| MockSkill("Python"), | |
| MockSkill("JavaScript"), | |
| MockSkill("React"), | |
| MockSkill("Node.js") | |
| ] | |
| achievements = [ | |
| MockAchievement("Employee of the Year 2023"), | |
| MockAchievement("Best Project Award") | |
| ] | |
| # Test data conversion (same as in app.py) | |
| import calendar | |
| def format_date(month, year): | |
| """Format month and year as 'Month Year'""" | |
| if month and year: | |
| try: | |
| month_name = calendar.month_name[int(month)] | |
| return f"{month_name[:3]} {year}" | |
| except: | |
| return f"{month}/{year}" | |
| return "" | |
| # Prepare data for PDF generation | |
| work_exp_list = [] | |
| for exp in work_experiences: | |
| start_date = format_date(exp.start_month, exp.start_year) | |
| end_date = "Present" if not exp.end_month or not exp.end_year else format_date(exp.end_month, exp.end_year) | |
| work_exp_list.append({ | |
| 'title': exp.title, | |
| 'organization': exp.organization, | |
| 'start_date': start_date, | |
| 'end_date': end_date, | |
| 'remarks': exp.remarks or '' | |
| }) | |
| projects_list = [] | |
| for proj in projects: | |
| start_date = format_date(proj.start_month, proj.start_year) | |
| end_date = "Present" if not proj.end_month or not proj.end_year else format_date(proj.end_month, proj.end_year) | |
| projects_list.append({ | |
| 'title': proj.title, | |
| 'organization': proj.organization, | |
| 'start_date': start_date, | |
| 'end_date': end_date, | |
| 'remarks': proj.remarks or '' | |
| }) | |
| education_list = [] | |
| for edu in educations: | |
| start_date = format_date(edu.start_month, edu.start_year) | |
| end_date = "Present" if not edu.end_month or not edu.end_year else format_date(edu.end_month, edu.end_year) | |
| education_list.append({ | |
| 'title': edu.title, | |
| 'organization': edu.organization, | |
| 'start_date': start_date, | |
| 'end_date': end_date, | |
| 'remarks': edu.remarks or '' | |
| }) | |
| # Convert skills and achievements to comma-separated strings | |
| skills_text = ', '.join([skill.skill for skill in skills]) if skills else '' | |
| achievements_text = ', '.join([achievement.achievement for achievement in achievements]) if achievements else '' | |
| # Create data dictionary | |
| data = { | |
| 'name': 'John Doe', | |
| 'email': '[email protected]', | |
| 'phone': '+1 (555) 123-4567', | |
| 'linkedin': 'johndoe', | |
| 'github': 'johndoe', | |
| 'website': 'https://johndoe.com', | |
| 'summary': 'Experienced software developer with expertise in full-stack development.', | |
| 'work_experience': work_exp_list, | |
| 'projects': projects_list, | |
| 'education': education_list, | |
| 'skills': skills_text, | |
| 'achievements': achievements_text, | |
| 'sections_order': ['work_experience', 'projects', 'education', 'skills', 'achievements'] | |
| } | |
| # Test PDF generation | |
| try: | |
| log_info("Generating standard PDF...") | |
| pdf_bytes = create_pdf_resume(data, "standard") | |
| if pdf_bytes: | |
| with open('test_mock_standard.pdf', 'wb') as f: | |
| f.write(pdf_bytes) | |
| log_info("β Standard PDF generated successfully: test_mock_standard.pdf") | |
| else: | |
| log_info("β Failed to generate standard PDF") | |
| log_info("Generating modern PDF...") | |
| pdf_bytes = create_pdf_resume(data, "modern") | |
| if pdf_bytes: | |
| with open('test_mock_modern.pdf', 'wb') as f: | |
| f.write(pdf_bytes) | |
| log_info("β Modern PDF generated successfully: test_mock_modern.pdf") | |
| else: | |
| log_info("β Failed to generate modern PDF") | |
| except Exception as e: | |
| log_error(f"Test failed: {str(e)}", e) | |
| if __name__ == "__main__": | |
| test_with_mock_models() |