File size: 8,767 Bytes
358dfff |
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
from schemas.assessment import AssessmentCreate, AssessmentQuestion, AssessmentQuestionOption
from schemas.enums import QuestionType
from pydantic import ValidationError
import json
def test_assessment_validation():
"""Test assessment schema validation"""
print("Testing Assessment Schema Validation...")
# Test valid assessment
try:
valid_question = AssessmentQuestion(
id="test-id",
text="Sample question?",
weight=3,
skill_categories=["python", "programming"],
type=QuestionType.choose_one,
options=[
AssessmentQuestionOption(text="Option A", value="a"),
AssessmentQuestionOption(text="Option B", value="b")
],
correct_options=["a"]
)
valid_assessment = AssessmentCreate(
title="Valid Assessment",
passing_score=70,
questions=[valid_question]
)
print("[PASS] Valid assessment creation succeeded")
except ValidationError as e:
print(f"[FAIL] Valid assessment creation failed: {e}")
# Test invalid weight (too low)
try:
invalid_question_low_weight = AssessmentQuestion(
id="test-id",
text="Sample question?",
weight=0, # Invalid: below minimum of 1
skill_categories=["python", "programming"],
type=QuestionType.choose_one
)
print("[FAIL] Invalid weight (too low) should have failed validation")
except ValidationError:
print("[PASS] Invalid weight (too low) correctly failed validation")
# Test invalid weight (too high)
try:
invalid_question_high_weight = AssessmentQuestion(
id="test-id",
text="Sample question?",
weight=6, # Invalid: above maximum of 5
skill_categories=["python", "programming"],
type=QuestionType.choose_one
)
print("[FAIL] Invalid weight (too high) should have failed validation")
except ValidationError:
print("[PASS] Invalid weight (too high) correctly failed validation")
# Test invalid passing score (too low)
try:
valid_question = AssessmentQuestion(
id="test-id",
text="Sample question?",
weight=3,
skill_categories=["python", "programming"],
type=QuestionType.choose_one
)
invalid_assessment_low_score = AssessmentCreate(
title="Invalid Assessment",
passing_score=10, # Invalid: below minimum of 20
questions=[valid_question]
)
print("[FAIL] Invalid passing score (too low) should have failed validation")
except ValidationError:
print("[PASS] Invalid passing score (too low) correctly failed validation")
# Test invalid passing score (too high)
try:
valid_question = AssessmentQuestion(
id="test-id",
text="Sample question?",
weight=3,
skill_categories=["python", "programming"],
type=QuestionType.choose_one
)
invalid_assessment_high_score = AssessmentCreate(
title="Invalid Assessment",
passing_score=90, # Invalid: above maximum of 80
questions=[valid_question]
)
print("[FAIL] Invalid passing score (too high) should have failed validation")
except ValidationError:
print("[PASS] Invalid passing score (too high) correctly failed validation")
# Test title length validation
try:
too_long_title = "x" * 201 # Exceeds max length of 200
invalid_assessment_title = AssessmentCreate(
title=too_long_title,
passing_score=70,
questions=[valid_question]
)
print("[FAIL] Invalid title length should have failed validation")
except ValidationError:
print("[PASS] Invalid title length correctly failed validation")
def test_user_validation():
"""Test user schema validation"""
print("\nTesting User Schema Validation...")
from schemas.user import UserCreate
from schemas.enums import UserRole
# Test valid user
try:
valid_user = UserCreate(
first_name="John",
last_name="Doe",
email="john.doe@example.com",
role=UserRole.hr,
password="securepassword123"
)
print("[PASS] Valid user creation succeeded")
except ValidationError as e:
print(f"[FAIL] Valid user creation failed: {e}")
# Test invalid first name (too long)
try:
invalid_user_long_name = UserCreate(
first_name="x" * 51, # Exceeds max length of 50
last_name="Doe",
email="john.doe@example.com",
role=UserRole.hr,
password="securepassword123"
)
print("[FAIL] Invalid first name length should have failed validation")
except ValidationError:
print("[PASS] Invalid first name length correctly failed validation")
# Test invalid last name (too short, empty)
try:
invalid_user_empty_name = UserCreate(
first_name="John",
last_name="", # Invalid: empty
email="john.doe@example.com",
role=UserRole.hr,
password="securepassword123"
)
print("[FAIL] Invalid last name (empty) should have failed validation")
except ValidationError:
print("[PASS] Invalid last name (empty) correctly failed validation")
def test_job_validation():
"""Test job schema validation"""
print("\nTesting Job Schema Validation...")
from schemas.job import JobCreate
from schemas.enums import JobSeniority
# Test valid job
try:
valid_job = JobCreate(
title="Software Engineer",
seniority=JobSeniority.mid,
description="Develop software solutions"
)
print("[PASS] Valid job creation succeeded")
except ValidationError as e:
print(f"[FAIL] Valid job creation failed: {e}")
# Test invalid title (too long)
try:
invalid_job_long_title = JobCreate(
title="x" * 201, # Exceeds max length of 200
seniority=JobSeniority.junior,
description="Develop software solutions"
)
print("[FAIL] Invalid job title length should have failed validation")
except ValidationError:
print("[PASS] Invalid job title length correctly failed validation")
# Test invalid description (too long)
try:
invalid_job_long_desc = JobCreate(
title="Software Engineer",
seniority=JobSeniority.junior,
description="x" * 1001 # Exceeds max length of 1000
)
print("[FAIL] Invalid job description length should have failed validation")
except ValidationError:
print("[PASS] Invalid job description length correctly failed validation")
def test_application_validation():
"""Test application schema validation"""
print("\nTesting Application Schema Validation...")
from schemas.application import ApplicationAnswer, ApplicationCreate
# Test valid application
try:
valid_answer = ApplicationAnswer(
question_id="question-1",
text="Sample answer text",
options=["option1", "option2"]
)
valid_application = ApplicationCreate(
job_id="job-1",
assessment_id="assessment-1",
user_id="user-1",
answers=[valid_answer]
)
print("[PASS] Valid application creation succeeded")
except ValidationError as e:
print(f"[FAIL] Valid application creation failed: {e}")
# Test invalid question_id (empty)
try:
invalid_answer = ApplicationAnswer(
question_id="", # Invalid: empty
text="Sample answer text"
)
print("[FAIL] Invalid question_id should have failed validation")
except ValidationError:
print("[PASS] Invalid question_id correctly failed validation")
# Test invalid answer text (too long)
try:
invalid_answer_long_text = ApplicationAnswer(
question_id="question-1",
text="x" * 5001 # Exceeds max length of 5000
)
print("[FAIL] Invalid answer text length should have failed validation")
except ValidationError:
print("[PASS] Invalid answer text length correctly failed validation")
if __name__ == "__main__":
test_assessment_validation()
test_user_validation()
test_job_validation()
test_application_validation()
print("\nAll validation tests completed!") |