Spaces:
Sleeping
Sleeping
File size: 10,939 Bytes
be1b5d2 |
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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
#!/usr/bin/env python3
"""
Unit tests for ImprovedClassificationPromptManager.
Tests the implementation of updated classification prompts with new red flag definitions
and explicit indicators based on medical professional feedback.
Requirements: 4.1, 4.2, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6
"""
import pytest
from src.core.improved_classification_prompt_manager import (
ImprovedClassificationPromptManager,
ClassificationResult,
create_improved_classification_prompt_manager
)
class TestImprovedClassificationPromptManager:
"""Test suite for ImprovedClassificationPromptManager."""
def setup_method(self):
"""Set up test fixtures."""
self.manager = create_improved_classification_prompt_manager()
def test_updated_red_flag_definition(self):
"""
Test updated red flag definition - Requirements 4.1, 4.2.
Verifies that the new definition is broader and includes active distress,
not just crisis situations.
"""
definition = self.manager.get_updated_red_flag_definition()
# Check that definition contains key phrases from requirements
assert "caused by or actively causing" in definition
assert "emotional or spiritual distress" in definition
# Ensure it's not empty
assert len(definition.strip()) > 0
def test_explicit_red_indicators_completeness(self):
"""
Test explicit red indicators - Requirements 5.2, 5.3, 5.4, 5.5, 5.6.
Verifies that all five required explicit indicators are present.
"""
indicators = self.manager.get_explicit_red_indicators()
# Check that all 5 required indicators are present
expected_indicators = [
"Complex grief",
"Loss of a loved one",
"Doubt about meaning of life",
"Doubt about meaning of suffering",
"Doubt about personal dignity"
]
assert len(indicators) == 5
for expected in expected_indicators:
assert expected in indicators
def test_yellow_flag_criteria(self):
"""
Test yellow flag criteria - Requirements 6.1, 6.2, 6.3, 6.4, 6.5.
Verifies that yellow flag criteria include appropriate situations
requiring further investigation.
"""
criteria = self.manager.get_yellow_flag_criteria()
# Check that criteria are not empty
assert len(criteria) > 0
# Check for key criteria from requirements
criteria_text = " ".join(criteria).lower()
assert "sadness without additional context" in criteria_text
assert "further clarification" in criteria_text or "further investigation" in criteria_text
def test_enhanced_prompt_structure(self):
"""
Test enhanced prompt generation - Requirements 5.1, 5.7.
Verifies that the prompt contains all required sections and indicators.
"""
prompt = self.manager.build_enhanced_classification_prompt()
# Check that prompt contains all required sections
required_sections = [
"<system_role>",
"<updated_red_flag_definition>",
"<explicit_red_indicators>",
"<yellow_flag_criteria>",
"<classification_logic>",
"<output_format>"
]
for section in required_sections:
assert section in prompt
# Check that all explicit indicators are in the prompt
red_indicators = self.manager.get_explicit_red_indicators()
for indicator in red_indicators:
assert indicator in prompt
def test_classification_validation_valid_red(self):
"""
Test classification validation for valid red classification.
Requirements: 5.7, 4.1, 4.2
"""
# Test valid red classification with explicit indicator
valid_red = ClassificationResult(
classification="red",
confidence=0.9,
indicators=["complex grief", "loss"],
reasoning="Complex grief detected",
red_flag_indicators=["Complex grief"],
yellow_flag_indicators=[],
is_valid=False
)
assert self.manager.validate_classification_consistency(valid_red) == True
def test_classification_validation_invalid_simple_sadness(self):
"""
Test classification validation rejects simple sadness as red.
Requirements: 3.1, 6.1
"""
# Test invalid classification (simple sadness as red without context)
invalid_red = ClassificationResult(
classification="red",
confidence=0.8,
indicators=["feeling sad"],
reasoning="Sadness",
red_flag_indicators=[],
yellow_flag_indicators=[],
is_valid=False
)
assert self.manager.validate_classification_consistency(invalid_red) == False
def test_classification_validation_meaning_loss_should_be_red(self):
"""
Test that loss of meaning/purpose should be classified as red.
Requirements: 3.2
"""
# Test loss of meaning should be red
meaning_loss = ClassificationResult(
classification="yellow",
confidence=0.7,
indicators=["loss of meaning in life"],
reasoning="Loss of meaning",
red_flag_indicators=[],
yellow_flag_indicators=["loss of meaning"],
is_valid=False
)
assert self.manager.validate_classification_consistency(meaning_loss) == False
def test_classification_validation_invalid_classification_type(self):
"""Test validation rejects invalid classification types."""
invalid_classification = ClassificationResult(
classification="blue", # Invalid
confidence=0.8,
indicators=["test"],
reasoning="Test",
red_flag_indicators=[],
yellow_flag_indicators=[],
is_valid=False
)
assert self.manager.validate_classification_consistency(invalid_classification) == False
def test_classification_validation_invalid_confidence(self):
"""Test validation rejects invalid confidence values."""
invalid_confidence = ClassificationResult(
classification="red",
confidence=1.5, # Invalid - should be 0.0-1.0
indicators=["test"],
reasoning="Test",
red_flag_indicators=[],
yellow_flag_indicators=[],
is_valid=False
)
assert self.manager.validate_classification_consistency(invalid_confidence) == False
def test_classification_guidelines(self):
"""Test classification guidelines content."""
guidelines = self.manager.get_classification_guidelines()
# Check that all classification types are present
assert "red" in guidelines
assert "yellow" in guidelines
assert "green" in guidelines
# Check content quality
assert len(guidelines["red"]) > 0
assert len(guidelines["yellow"]) > 0
assert len(guidelines["green"]) > 0
# Check for key terms
assert "spiritual" in guidelines["red"]
assert "further investigation" in guidelines["yellow"]
assert "No signs" in guidelines["green"]
def test_create_classification_result(self):
"""Test classification result creation with validation."""
result = self.manager.create_classification_result(
classification="red",
confidence=0.9,
indicators=["complex grief"],
reasoning="Complex grief detected",
red_flag_indicators=["Complex grief"]
)
assert result.classification == "red"
assert result.confidence == 0.9
assert result.indicators == ["complex grief"]
assert result.reasoning == "Complex grief detected"
assert result.red_flag_indicators == ["Complex grief"]
assert result.yellow_flag_indicators == []
assert result.is_valid == True
def test_create_classification_result_with_validation_failure(self):
"""Test classification result creation that fails validation."""
result = self.manager.create_classification_result(
classification="red",
confidence=0.8,
indicators=["почуття смутку"], # Simple sadness without context
reasoning="Смуток",
red_flag_indicators=[],
yellow_flag_indicators=[]
)
assert result.classification == "red"
assert result.is_valid == False # Should fail validation
def test_factory_function(self):
"""Test factory function creates proper instance."""
manager = create_improved_classification_prompt_manager()
assert isinstance(manager, ImprovedClassificationPromptManager)
# Test that it works properly
definition = manager.get_updated_red_flag_definition()
assert len(definition) > 0
indicators = manager.get_explicit_red_indicators()
assert len(indicators) == 5
class TestClassificationResult:
"""Test suite for ClassificationResult data class."""
def test_classification_result_creation(self):
"""Test ClassificationResult creation."""
result = ClassificationResult(
classification="red",
confidence=0.9,
indicators=["test indicator"],
reasoning="test reasoning",
red_flag_indicators=["red indicator"],
yellow_flag_indicators=["yellow indicator"],
is_valid=True
)
assert result.classification == "red"
assert result.confidence == 0.9
assert result.indicators == ["test indicator"]
assert result.reasoning == "test reasoning"
assert result.red_flag_indicators == ["red indicator"]
assert result.yellow_flag_indicators == ["yellow indicator"]
assert result.is_valid == True
def test_classification_result_defaults(self):
"""Test ClassificationResult with default values."""
result = ClassificationResult(
classification="green",
confidence=0.7,
indicators=[],
reasoning="No indicators found",
red_flag_indicators=[],
yellow_flag_indicators=[],
is_valid=True
)
assert result.red_flag_indicators == []
assert result.yellow_flag_indicators == []
if __name__ == "__main__":
pytest.main([__file__]) |