Spaces:
Configuration error
Configuration error
""" | |
Unit tests for the gradio module. | |
""" | |
import unittest | |
from pathlib import Path | |
from unittest.mock import patch | |
from functions import gradio | |
class TestProcessInputs(unittest.TestCase): | |
"""Test cases for the process_inputs function.""" | |
def test_process_inputs_with_real_pdf(self): | |
"""Test process_inputs with the actual test PDF file.""" | |
# Get path to the test PDF file | |
test_pdf_path = Path(__file__).parent / "test_data" / "linkedin_profile.pdf" | |
# Skip test if PDF doesn't exist (optional test data) | |
if not test_pdf_path.exists(): | |
self.skipTest(f"Test PDF file not found: {test_pdf_path}") | |
with patch('functions.gradio.extract_text') as mock_extract, \ | |
patch('functions.gradio.get_github_repositories') as mock_github: | |
mock_extract.return_value = {"test": "data"} | |
mock_github.return_value = [{"name": "test-repo"}] | |
result = gradio.process_inputs( | |
linkedin_pdf_path=str(test_pdf_path), | |
github_username="testuser", | |
job_post_text="Software engineer position", | |
user_instructions="Custom instructions" | |
) | |
# Function currently returns empty string | |
self.assertEqual(result, "") | |
def test_process_inputs_with_pdf_path_mocked(self, mock_github, mock_extract): | |
"""Test process_inputs with a PDF file path (mocked for controlled testing).""" | |
# Mock successful LinkedIn text extraction | |
mock_extract.return_value = { | |
"contact_info": "John Doe, [email protected]", | |
"summary": "Experienced software engineer", | |
"experience": "Software Engineer at Company" | |
} | |
mock_github.return_value = [{"name": "test-repo"}] | |
result = gradio.process_inputs( | |
linkedin_pdf_path="/path/to/resume.pdf", | |
github_username="testuser", | |
job_post_text="Software engineer position", | |
user_instructions="Custom instructions" | |
) | |
# Verify extract_text was called with the correct path | |
mock_extract.assert_called_once_with("/path/to/resume.pdf") | |
# Verify get_github_repositories was called with username | |
mock_github.assert_called_once_with("testuser") | |
# Function currently returns empty string | |
self.assertEqual(result, "") | |
def test_process_inputs_extraction_failure(self, mock_github, mock_extract): | |
"""Test process_inputs when LinkedIn extraction fails.""" | |
# Mock failed LinkedIn text extraction | |
mock_extract.return_value = None | |
mock_github.return_value = None | |
result = gradio.process_inputs( | |
linkedin_pdf_path="/path/to/resume.pdf", | |
github_username="testuser", | |
job_post_text="Software engineer position", | |
user_instructions="Custom instructions" | |
) | |
# Verify extract_text was called | |
mock_extract.assert_called_once_with("/path/to/resume.pdf") | |
mock_github.assert_called_once_with("testuser") | |
# Function currently returns empty string | |
self.assertEqual(result, "") | |
def test_process_inputs_no_pdf_path(self, mock_github, mock_extract): | |
"""Test process_inputs with no PDF path provided.""" | |
mock_extract.return_value = None | |
mock_github.return_value = [] | |
result = gradio.process_inputs( | |
linkedin_pdf_path=None, | |
github_username="testuser", | |
job_post_text="Software engineer position", | |
user_instructions="Custom instructions" | |
) | |
# extract_text should be called with None | |
mock_extract.assert_called_once_with(None) | |
mock_github.assert_called_once_with("testuser") | |
# Function currently returns empty string | |
self.assertEqual(result, "") | |
def test_process_inputs_with_long_job_post(self, mock_github, mock_extract): | |
"""Test process_inputs with a long job post text (for logging truncation).""" | |
mock_extract.return_value = { | |
"summary": "Test summary" | |
} | |
mock_github.return_value = [] | |
long_job_post = "This is a very long job posting " * 50 # Make it longer than 100 chars | |
result = gradio.process_inputs( | |
linkedin_pdf_path="/path/to/resume.pdf", | |
github_username="testuser", | |
job_post_text=long_job_post, | |
user_instructions="Custom instructions" | |
) | |
# Verify extract_text was called | |
mock_extract.assert_called_once_with("/path/to/resume.pdf") | |
mock_github.assert_called_once_with("testuser") | |
# Function currently returns empty string | |
self.assertEqual(result, "") | |
def test_process_inputs_github_username_whitespace(self, mock_github, mock_extract): | |
"""Test that github_username is properly stripped of whitespace.""" | |
mock_extract.return_value = None | |
mock_github.return_value = [] | |
result = gradio.process_inputs( | |
linkedin_pdf_path=None, | |
github_username=" testuser ", | |
job_post_text="", | |
user_instructions="" | |
) | |
# Verify get_github_repositories was called with stripped username | |
mock_github.assert_called_once_with("testuser") | |
self.assertEqual(result, "") | |
def test_logging_calls(self, mock_get_logger, mock_github, mock_extract): | |
"""Test that appropriate logging calls are made.""" | |
mock_logger = mock_get_logger.return_value | |
mock_extract.return_value = {"test": "data"} | |
mock_github.return_value = [{"name": "repo"}] | |
gradio.process_inputs( | |
linkedin_pdf_path="/path/to/resume.pdf", | |
github_username="testuser", | |
job_post_text="Job description here", | |
user_instructions="Custom instructions" | |
) | |
# Verify logging calls were made | |
mock_logger.info.assert_called() | |
def test_process_inputs_none_github_username(self, mock_github, mock_extract): | |
"""Test process_inputs with None github_username (should handle gracefully).""" | |
mock_extract.return_value = None | |
mock_github.return_value = None | |
# This should raise a TypeError due to the bug in gradio.py | |
# where it tries to slice job_post_text[:100] when job_post_text is None | |
with self.assertRaises(TypeError): | |
gradio.process_inputs( | |
linkedin_pdf_path=None, | |
github_username=None, | |
job_post_text=None, | |
user_instructions=None | |
) | |
if __name__ == '__main__': | |
unittest.main() | |