File size: 2,348 Bytes
9387654
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
import fitz  # PyMuPDF
import re
from io import BytesIO
import torch
from unittest.mock import MagicMock, patch
from config import SUPABASE_URL, SUPABASE_KEY, HF_API_TOKEN, HF_HEADERS, supabase, HF_MODELS, query, embedding_model
from utils import (
    evaluate_resumes, parse_resume, extract_email, score_candidate, 
    summarize_resume, store_in_supabase, generate_pdf_report
)

def test_parse_resume():
    pdf_content = BytesIO()
    doc = fitz.open()
    page = doc.new_page()
    page.insert_text((50, 50), "Sample Resume Text")
    doc.save(pdf_content)
    pdf_content.seek(0)
    
    extracted_text = parse_resume(pdf_content)
    assert "Sample Resume Text" in extracted_text

def test_extract_email():
    text = "Contact me at test@example.com for details."
    assert extract_email(text) == "test@example.com"
    
    text_no_email = "No email here."
    assert extract_email(text_no_email) is None

@patch("test_module.embedding_model.encode")
def test_score_candidate(mock_encode):
    # Mock embeddings as tensors with the same shape as actual embeddings
    mock_encode.return_value = torch.tensor([0.1, 0.2, 0.3])  

    score = score_candidate("Resume text", "Job description")

    assert isinstance(score, float)
    assert 0 <= score <= 1  # Ensure score is within valid range

@patch("test_module.query")
def test_summarize_resume(mock_query):
    mock_query.return_value = [{"generated_text": "This is a summary."}]
    summary = summarize_resume("Long resume text")
    assert summary == "This is a summary."
    
    mock_query.return_value = None
    assert summarize_resume("Long resume text") == "Summary could not be generated."

@patch("test_module.supabase.table")
def test_store_in_supabase(mock_table):
    mock_insert = mock_table.return_value.insert.return_value.execute
    mock_insert.return_value = {"status": "success"}
    
    response = store_in_supabase("Resume text", 0.8, "John Doe", "john@example.com", "Summary")
    assert response["status"] == "success"

def test_generate_pdf_report():
    candidates = [{
        "name": "John Doe",
        "email": "john@example.com",
        "score": 0.9,
        "summary": "A skilled developer."
    }]
    pdf = generate_pdf_report(candidates)
    assert isinstance(pdf, BytesIO)
    assert pdf.getvalue()  # Check that the PDF is not empty