Spaces:
Sleeping
Sleeping
File size: 1,891 Bytes
d623240 | 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 | import pytest
from pydantic import ValidationError
from app.schemas.auth import LoginRequest, RegisterRequest
from app.schemas.prediction import PredictionInput
VALID_PAYLOAD = {
"age": 35,
"workclass": "Private",
"fnlwgt": 150000,
"education": "Bachelors",
"education.num": 13,
"marital.status": "Married-civ-spouse",
"occupation": "Tech-support",
"relationship": "Husband",
"race": "White",
"sex": "Male",
"capital.gain": 5000,
"capital.loss": 0,
"hours.per.week": 40,
"native.country": "United-States",
}
def test_prediction_input_accepts_aliases() -> None:
payload = PredictionInput(**VALID_PAYLOAD)
assert payload.education_num == 13
assert payload.capital_gain == 5000
assert payload.marital_status == "Married-civ-spouse"
@pytest.mark.parametrize(
"field_name, invalid_value",
[
("age", 16),
("education.num", 0),
("hours.per.week", 100),
("workclass", ""),
("native.country", "x" * 65),
("sex", "Unknown"),
],
)
def test_prediction_input_rejects_invalid_values(field_name: str, invalid_value) -> None:
payload = dict(VALID_PAYLOAD)
payload[field_name] = invalid_value
with pytest.raises(ValidationError):
PredictionInput(**payload)
def test_prediction_input_rejects_unknown_fields() -> None:
payload = dict(VALID_PAYLOAD)
payload["unexpected"] = "boom"
with pytest.raises(ValidationError):
PredictionInput(**payload)
def test_register_request_enforces_password_strength() -> None:
with pytest.raises(ValidationError):
RegisterRequest(email="user@example.com", full_name="User Test", password="weakpassword")
def test_login_request_normalizes_email() -> None:
payload = LoginRequest(email="USER@EXAMPLE.COM", password="StrongPass!123")
assert payload.email == "user@example.com"
|