File size: 7,498 Bytes
d5a003e
 
 
1084ca5
 
 
 
 
f5b66ec
1084ca5
f9a80bc
 
1084ca5
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
f9a80bc
1084ca5
 
f9a80bc
1084ca5
 
 
 
 
f9a80bc
1084ca5
 
f9a80bc
 
 
 
1084ca5
f9a80bc
1084ca5
 
 
 
 
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
 
 
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
 
f9a80bc
 
1084ca5
 
 
 
 
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
 
 
 
 
 
 
f9a80bc
 
1084ca5
 
 
 
 
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
f9a80bc
 
1084ca5
 
 
 
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
 
 
 
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
 
 
 
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
 
 
 
 
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
 
 
f9a80bc
1084ca5
 
 
 
f9a80bc
1084ca5
 
 
f9a80bc
1084ca5
 
 
 
 
f9a80bc
1084ca5
 
f9a80bc
 
 
1084ca5
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
 
 
 
f9a80bc
1084ca5
 
 
 
f9a80bc
1084ca5
 
f9a80bc
1084ca5
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""
Unit tests for the context_acquisition module.
"""

import unittest
import tempfile
import os
from unittest.mock import patch, MagicMock
from functions import linkedin_resume as ca

# pylint: disable=protected-access


class TestCleanExtractedText(unittest.TestCase):
    """Test cases for the _clean_extracted_text function."""

    def test_normalize_multiple_newlines(self):
        """Test normalization of multiple newlines."""

        raw = "Line 1\n\nLine 2\n\n\nLine 3"
        expected = "Line 1\nLine 2\nLine 3"
        self.assertEqual(ca._clean_extracted_text(raw), expected)

    def test_remove_artifacts(self):
        """Test removal of PDF artifacts."""

        raw = "  123  \n|---|\nSome text\n"
        expected = "Some text"
        self.assertEqual(ca._clean_extracted_text(raw), expected)

    def test_normalize_spaces(self):
        """Test normalization of multiple spaces."""

        raw = "A  B   C"
        expected = "A B C"
        self.assertEqual(ca._clean_extracted_text(raw), expected)

    def test_empty_string(self):
        """Test handling of empty string."""

        self.assertEqual(ca._clean_extracted_text(""), "")

    def test_none_input(self):
        """Test handling of None input."""

        self.assertEqual(ca._clean_extracted_text(None), "")


class TestStructureResumeText(unittest.TestCase):
    """Test cases for the _structure_resume_text function."""

    def test_basic_structure(self):
        """Test basic resume text structuring."""

        text = "Contact Info\nJohn Doe\nSummary\nExperienced dev" + \
               "\nExperience\nCompany X\nEducation\nMIT\nSkills\nPython, C++"

        result = ca._structure_resume_text(text)

        self.assertIn("contact_info", result["sections"])
        self.assertIn("summary", result["sections"])
        self.assertIn("experience", result["sections"])
        self.assertIn("education", result["sections"])
        self.assertIn("skills", result["sections"])
        self.assertGreater(result["word_count"], 0)
        self.assertGreaterEqual(result["section_count"], 5)

    def test_empty_text(self):
        """Test handling of empty text."""

        result = ca._structure_resume_text("")
        self.assertEqual(result["sections"], {})
        self.assertEqual(result["full_text"], "")
        self.assertEqual(result["word_count"], 0)
        self.assertEqual(result["section_count"], 0)

    def test_contains_required_fields(self):
        """Test that result contains all required fields."""

        text = "Some basic text"
        result = ca._structure_resume_text(text)

        required_fields = ["sections", "full_text", "llm_formatted", "summary",
                          "format", "word_count", "section_count"]
        for field in required_fields:
            self.assertIn(field, result)


class TestFormatForLLM(unittest.TestCase):
    """Test cases for the _format_for_llm function."""

    def test_section_formatting(self):
        """Test proper formatting of sections for LLM."""

        sections = {
            "summary": "A summary.",
            "contact_info": "Contact details.",
            "experience": "Work exp.",
            "education": "School info.",
            "skills": "Python, C++"
        }
        formatted = ca._format_for_llm(sections)

        self.assertIn("[SUMMARY]", formatted)
        self.assertIn("[CONTACT INFO]", formatted)
        self.assertIn("[EXPERIENCE]", formatted)
        self.assertIn("[EDUCATION]", formatted)
        self.assertIn("[SKILLS]", formatted)
        self.assertTrue(formatted.startswith("=== RESUME CONTENT ==="))
        self.assertTrue(formatted.endswith("=== END RESUME ==="))

    def test_empty_sections(self):
        """Test handling of empty sections."""

        sections = {}
        formatted = ca._format_for_llm(sections)

        self.assertTrue(formatted.startswith("=== RESUME CONTENT ==="))
        self.assertTrue(formatted.endswith("=== END RESUME ==="))


class TestGetLLMContextFromResume(unittest.TestCase):
    """Test cases for the get_llm_context_from_resume function."""

    def test_success_with_llm_formatted(self):
        """Test successful extraction with LLM formatted text."""

        extraction_result = {
            "status": "success",
            "structured_text": {"llm_formatted": "LLM text", "full_text": "Full text"}
        }
        result = ca.get_llm_context_from_resume(extraction_result)
        self.assertEqual(result, "LLM text")

    def test_fallback_to_full_text(self):
        """Test fallback to full text when LLM formatted not available."""

        extraction_result = {
            "status": "success",
            "structured_text": {"full_text": "Full text"}
        }
        result = ca.get_llm_context_from_resume(extraction_result)
        self.assertEqual(result, "Full text")

    def test_error_status(self):
        """Test handling of error status."""

        extraction_result = {"status": "error"}
        result = ca.get_llm_context_from_resume(extraction_result)
        self.assertEqual(result, "")

    def test_missing_structured_text(self):
        """Test handling of missing structured_text."""

        extraction_result = {"status": "success"}
        result = ca.get_llm_context_from_resume(extraction_result)
        self.assertEqual(result, "")


class TestExtractTextFromLinkedInPDF(unittest.TestCase):
    """Test cases for the extract_text_from_linkedin_pdf function."""

    def test_none_input(self):
        """Test handling of None input."""

        result = ca.extract_text_from_linkedin_pdf(None)
        self.assertEqual(result["status"], "error")
        self.assertIn("No PDF file provided", result["message"])

    @patch('PyPDF2.PdfReader')
    @patch('builtins.open')
    def test_successful_extraction(self, mock_open, mock_pdf_reader):
        """Test successful PDF text extraction with mocked PyPDF2."""

        # Create a temporary file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
            tmp_path = tmp.name

        try:
            # Mock file reading
            mock_file = MagicMock()
            mock_file.read.return_value = b"fake pdf content"
            mock_open.return_value.__enter__.return_value = mock_file

            # Mock PDF reader and page
            mock_page = MagicMock()
            mock_page.extract_text.return_value = "Contact Info\nJohn Doe\nSummary" + \
                                                   "\nDeveloper\nExperience\nCompany X"

            mock_reader_instance = MagicMock()
            mock_reader_instance.pages = [mock_page]
            mock_pdf_reader.return_value = mock_reader_instance

            # Test the function
            result = ca.extract_text_from_linkedin_pdf(tmp_path)

            self.assertEqual(result["status"], "success")
            self.assertIn("structured_text", result)
            self.assertIn("metadata", result)
            self.assertIn("contact_info", result["structured_text"]["sections"])

        finally:
            # Clean up
            if os.path.exists(tmp_path):
                os.remove(tmp_path)

    def test_nonexistent_file(self):
        """Test handling of non-existent file."""

        result = ca.extract_text_from_linkedin_pdf("/nonexistent/path.pdf")
        self.assertEqual(result["status"], "error")
        self.assertIn("Failed to extract text from PDF", result["message"])


if __name__ == '__main__':
    unittest.main()