184 lines
5.7 KiB
Python
184 lines
5.7 KiB
Python
"""Tests for Pydantic models validation."""
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
from app.models.auth import LoginRequest, UserResponse, LoginResponse
|
|
from app.models.query import QuestionRequest, BenchQueryRequest, BackendQueryRequest, QueryResponse
|
|
from app.models.settings import EnvironmentSettings, UserSettingsUpdate
|
|
|
|
|
|
class TestAuthModels:
|
|
"""Tests for authentication models."""
|
|
|
|
def test_login_request_valid(self):
|
|
"""Test valid LoginRequest."""
|
|
request = LoginRequest(
|
|
login="12345678",
|
|
client_ip="192.168.1.1"
|
|
)
|
|
|
|
assert request.login == "12345678"
|
|
assert request.client_ip == "192.168.1.1"
|
|
|
|
def test_login_request_invalid_format(self):
|
|
"""Test LoginRequest with invalid login format."""
|
|
|
|
with pytest.raises(ValidationError):
|
|
LoginRequest(login="1234567", client_ip="192.168.1.1")
|
|
|
|
|
|
with pytest.raises(ValidationError):
|
|
LoginRequest(login="abcd1234", client_ip="192.168.1.1")
|
|
|
|
def test_user_response(self):
|
|
"""Test UserResponse model."""
|
|
user = UserResponse(
|
|
user_id="user-123",
|
|
login="12345678",
|
|
last_login_at="2024-01-01T00:00:00Z",
|
|
created_at="2024-01-01T00:00:00Z"
|
|
)
|
|
|
|
assert user.user_id == "user-123"
|
|
assert user.login == "12345678"
|
|
|
|
def test_login_response(self):
|
|
"""Test LoginResponse model."""
|
|
user = UserResponse(
|
|
user_id="user-123",
|
|
login="12345678",
|
|
last_login_at="2024-01-01T00:00:00Z",
|
|
created_at="2024-01-01T00:00:00Z"
|
|
)
|
|
|
|
response = LoginResponse(
|
|
access_token="token123",
|
|
token_type="bearer",
|
|
user=user
|
|
)
|
|
|
|
assert response.access_token == "token123"
|
|
assert response.token_type == "bearer"
|
|
assert response.user.user_id == "user-123"
|
|
|
|
|
|
class TestQueryModels:
|
|
"""Tests for query models."""
|
|
|
|
def test_question_request_valid(self):
|
|
"""Test valid QuestionRequest."""
|
|
question = QuestionRequest(
|
|
body="What is the weather?",
|
|
with_docs=True
|
|
)
|
|
|
|
assert question.body == "What is the weather?"
|
|
assert question.with_docs is True
|
|
|
|
def test_question_request_default_with_docs(self):
|
|
"""Test QuestionRequest with default with_docs."""
|
|
question = QuestionRequest(body="Test question")
|
|
|
|
assert question.with_docs is True
|
|
|
|
def test_bench_query_request_valid(self):
|
|
"""Test valid BenchQueryRequest."""
|
|
request = BenchQueryRequest(
|
|
environment="ift",
|
|
questions=[
|
|
QuestionRequest(body="Q1", with_docs=True),
|
|
QuestionRequest(body="Q2", with_docs=False)
|
|
]
|
|
)
|
|
|
|
assert request.environment == "ift"
|
|
assert len(request.questions) == 2
|
|
assert request.questions[0].body == "Q1"
|
|
|
|
def test_backend_query_request_valid(self):
|
|
"""Test valid BackendQueryRequest."""
|
|
request = BackendQueryRequest(
|
|
environment="psi",
|
|
questions=[
|
|
QuestionRequest(body="Q1", with_docs=True)
|
|
],
|
|
reset_session=True
|
|
)
|
|
|
|
assert request.environment == "psi"
|
|
assert len(request.questions) == 1
|
|
assert request.reset_session is True
|
|
|
|
def test_backend_query_request_default_reset(self):
|
|
"""Test BackendQueryRequest with default reset_session."""
|
|
request = BackendQueryRequest(
|
|
environment="prod",
|
|
questions=[QuestionRequest(body="Q1")]
|
|
)
|
|
|
|
assert request.reset_session is True
|
|
|
|
def test_query_response(self):
|
|
"""Test QueryResponse model."""
|
|
response = QueryResponse(
|
|
request_id="req-123",
|
|
timestamp="2024-01-01T00:00:00Z",
|
|
environment="ift",
|
|
response={"answers": []}
|
|
)
|
|
|
|
assert response.request_id == "req-123"
|
|
assert response.environment == "ift"
|
|
assert isinstance(response.response, dict)
|
|
|
|
|
|
class TestSettingsModels:
|
|
"""Tests for settings models."""
|
|
|
|
def test_environment_settings_valid(self):
|
|
"""Test valid EnvironmentSettings."""
|
|
settings = EnvironmentSettings(
|
|
apiMode="bench",
|
|
bearerToken="token123",
|
|
systemPlatform="platform",
|
|
systemPlatformUser="user",
|
|
platformUserId="user-123",
|
|
platformId="platform-123",
|
|
withClassify=True,
|
|
resetSessionMode=False
|
|
)
|
|
|
|
assert settings.apiMode == "bench"
|
|
assert settings.bearerToken == "token123"
|
|
assert settings.withClassify is True
|
|
assert settings.resetSessionMode is False
|
|
|
|
def test_environment_settings_defaults(self):
|
|
"""Test EnvironmentSettings with default values."""
|
|
settings = EnvironmentSettings(apiMode="backend")
|
|
|
|
assert settings.apiMode == "backend"
|
|
assert settings.bearerToken == ""
|
|
assert settings.withClassify is False
|
|
assert settings.resetSessionMode is True
|
|
|
|
def test_user_settings_update(self):
|
|
"""Test UserSettingsUpdate model."""
|
|
update = UserSettingsUpdate(
|
|
settings={
|
|
"ift": EnvironmentSettings(apiMode="bench"),
|
|
"psi": EnvironmentSettings(apiMode="backend")
|
|
}
|
|
)
|
|
|
|
assert "ift" in update.settings
|
|
assert "psi" in update.settings
|
|
assert update.settings["ift"].apiMode == "bench"
|
|
assert update.settings["psi"].apiMode == "backend"
|
|
|
|
def test_user_settings_update_empty(self):
|
|
"""Test UserSettingsUpdate with empty settings."""
|
|
update = UserSettingsUpdate(settings={})
|
|
|
|
assert update.settings == {}
|