69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
import pytest
|
|
import json
|
|
from llm.chains import validate_response
|
|
from llm.models import AnalysisFlags
|
|
|
|
def test_validate_response_valid():
|
|
"""Test validation with valid response"""
|
|
response = {
|
|
"hasMultipleEscalations": False,
|
|
"customerSentiment": "neutral"
|
|
}
|
|
assert validate_response(response) is True
|
|
|
|
def test_validate_response_valid_json_string():
|
|
"""Test validation with valid JSON string"""
|
|
response = json.dumps({
|
|
"hasMultipleEscalations": True,
|
|
"customerSentiment": "frustrated"
|
|
})
|
|
assert validate_response(response) is True
|
|
|
|
def test_validate_response_invalid_json_string():
|
|
"""Test validation with invalid JSON string"""
|
|
response = "not a valid json"
|
|
assert validate_response(response) is False
|
|
|
|
def test_validate_response_missing_field():
|
|
"""Test validation with missing required field"""
|
|
response = {
|
|
"hasMultipleEscalations": False
|
|
}
|
|
assert validate_response(response) is False
|
|
|
|
def test_validate_response_invalid_type():
|
|
"""Test validation with invalid field type"""
|
|
response = {
|
|
"hasMultipleEscalations": "not a boolean",
|
|
"customerSentiment": "neutral"
|
|
}
|
|
assert validate_response(response) is False
|
|
|
|
def test_validate_response_null_sentiment():
|
|
"""Test validation with null sentiment"""
|
|
response = {
|
|
"hasMultipleEscalations": True,
|
|
"customerSentiment": None
|
|
}
|
|
assert validate_response(response) is True
|
|
|
|
def test_validate_response_invalid_structure():
|
|
"""Test validation with invalid JSON structure"""
|
|
response = "not a dictionary"
|
|
assert validate_response(response) is False
|
|
|
|
def test_validate_response_complex_error():
|
|
"""Test validation with multiple errors"""
|
|
response = {
|
|
"hasMultipleEscalations": "invalid",
|
|
"customerSentiment": 123
|
|
}
|
|
assert validate_response(response) is False
|
|
|
|
def test_validate_response_model_validation():
|
|
"""Test validation using Pydantic model"""
|
|
response = {
|
|
"hasMultipleEscalations": True,
|
|
"customerSentiment": "calm"
|
|
}
|
|
assert AnalysisFlags.model_validate(response) is not None |