subul / backend /tests /test_application_scores.py
Kaadan's picture
initial commit
358dfff
import json
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models.assessment import Assessment
from models.job import Job
from models.user import User
from models.application import Application
from models.base import Base
from config import settings
from schemas.assessment import AssessmentQuestion, AssessmentQuestionOption
from schemas.enums import QuestionType
from uuid import uuid4
def test_application_list_returns_scores():
"""Test that the application list endpoint returns scores as required by technical requirements"""
print("Testing that application list returns scores...")
# Create a test database session
engine = create_engine(settings.database_url, connect_args={"check_same_thread": False})
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Create tables if they don't exist
Base.metadata.create_all(bind=engine)
# Create a test session
db = TestingSessionLocal()
try:
# Create a test job
test_job = Job(
id=str(uuid4()),
title="Software Engineer",
seniority="mid",
description="Test job for assessment",
skill_categories='["programming", "python", "fastapi"]'
)
db.add(test_job)
db.commit()
# Create a test assessment with questions
test_questions = [
{
"id": str(uuid4()),
"text": "What is Python?",
"weight": 3,
"skill_categories": ["programming", "python"],
"type": "choose_one",
"options": [
{"text": "A snake", "value": "a"},
{"text": "A programming language", "value": "b"},
{"text": "An IDE", "value": "c"}
],
"correct_options": ["b"]
},
{
"id": str(uuid4()),
"text": "What is 2+2?",
"weight": 2,
"skill_categories": ["math"],
"type": "choose_one",
"options": [
{"text": "3", "value": "a"},
{"text": "4", "value": "b"},
{"text": "5", "value": "c"}
],
"correct_options": ["b"]
}
]
test_assessment = Assessment(
id=str(uuid4()),
job_id=test_job.id,
title="Programming Skills Assessment",
passing_score=70,
questions=json.dumps(test_questions)
)
db.add(test_assessment)
db.commit()
# Create a test user
test_user = User(
id=str(uuid4()),
first_name="John",
last_name="Doe",
email=f"test_{str(uuid4())[:8]}@example.com",
role="applicant"
)
test_user.set_password("password123")
db.add(test_user)
db.commit()
# Create an application with correct answers
test_answers = [
{
"question_id": test_questions[0]['id'],
"text": "",
"options": ["b"] # Correct answer for question 1
},
{
"question_id": test_questions[1]['id'],
"text": "",
"options": ["b"] # Correct answer for question 2
}
]
test_application = Application(
id=str(uuid4()),
job_id=test_job.id,
assessment_id=test_assessment.id,
user_id=test_user.id,
answers=json.dumps(test_answers)
)
db.add(test_application)
db.commit()
# Now test the application list functionality
from services.application_service import get_applications_by_job_and_assessment, calculate_application_score
# Get applications for the job and assessment
applications = get_applications_by_job_and_assessment(db, test_job.id, test_assessment.id)
print(f"Retrieved {len(applications)} applications")
# Calculate scores for each application
for app in applications:
score = calculate_application_score(db, app.id)
print(f"Application ID: {app.id}, Score: {score}%")
# Verify that we can get a valid score
assert score >= 0 and score <= 100, f"Score {score} is not within valid range [0, 100]"
# For our test case with all correct answers, score should be 100%
if app.id == test_application.id:
expected_total_points = 3 + 2 # weights of both questions
expected_earned_points = 3 + 2 # both answers are correct
expected_percentage = (expected_earned_points / expected_total_points) * 100
assert score == expected_percentage, f"Expected {expected_percentage}%, got {score}%"
print(" [PASS] Application list returns valid scores")
# Test with an application that has incorrect answers
test_answers_incorrect = [
{
"question_id": test_questions[0]['id'],
"text": "",
"options": ["a"] # Wrong answer for question 1
},
{
"question_id": test_questions[1]['id'],
"text": "",
"options": ["b"] # Correct answer for question 2
}
]
test_application_incorrect = Application(
id=str(uuid4()),
job_id=test_job.id,
assessment_id=test_assessment.id,
user_id=test_user.id,
answers=json.dumps(test_answers_incorrect)
)
db.add(test_application_incorrect)
db.commit()
# Calculate score for the incorrect application
incorrect_score = calculate_application_score(db, test_application_incorrect.id)
print(f"Incorrect application score: {incorrect_score}%")
# Expected: 2 points earned (question 2) out of 5 total points = 40%
expected_incorrect_percentage = (2 / 5) * 100 # 40%
assert incorrect_score == expected_incorrect_percentage, f"Expected {expected_incorrect_percentage}%, got {incorrect_score}%"
print(" [PASS] Application with incorrect answers returns appropriate score")
print("\n[PASS] Application list endpoint returns scores as required by technical requirements!")
print("According to the technical requirements, the /jobs/:jid/assessments/:aid/applications GET endpoint")
print("should return: { ..., score: number, passing_score: number, ... }")
print("This functionality is now properly implemented.")
finally:
db.close()
if __name__ == "__main__":
test_application_list_returns_scores()