Spiritual_Health_Project / tests /unit /test_coherent_summary_formatter.py
DocUA's picture
feat: Fix classification logic and remove redundant spiritual care message functionality
be1b5d2
# test_coherent_summary_formatter.py
"""
Unit tests for Coherent Summary Formatter functionality.
Tests the new coherent paragraph formatting functionality added to
ProviderSummaryGenerator for requirements 2.1-2.8.
"""
import pytest
from unittest.mock import Mock
from datetime import datetime
from src.core.provider_summary_generator import ProviderSummaryGenerator, ProviderSummary
class TestCoherentSummaryFormatter:
"""Test cases for coherent summary formatting functionality."""
def setup_method(self):
"""Set up test fixtures."""
self.generator = ProviderSummaryGenerator()
def test_format_coherent_paragraph_basic(self):
"""Test basic coherent paragraph formatting."""
# Create sample provider summary
summary = ProviderSummary(
patient_name="John Doe",
patient_phone="(555) 123-4567",
indicators=["Loss of faith", "Spiritual distress"],
situation_description="existential crisis",
medical_context={
"age": 45,
"gender": "male",
"conditions": ["Diabetes", "Hypertension"]
}
)
result = self.generator.format_coherent_paragraph(summary)
# Check that all required elements are present (Requirements 2.2, 2.3, 2.4, 2.5)
assert "John Doe is a 45-year-old male" in result
assert "clinical history of Diabetes and Hypertension" in result
assert "Loss of faith and Spiritual distress" in result
assert "existential crisis" in result
assert "RED FLAG" in result
assert "(555) 123-4567" in result
# Check that it's a single paragraph (Requirement 2.1)
lines = result.split('\n')
main_paragraph_lines = [line for line in lines if line.strip() and not line.startswith('Patient reported:')]
assert len(main_paragraph_lines) == 1
def test_format_coherent_paragraph_no_medical_history(self):
"""Test coherent paragraph with no medical history."""
summary = ProviderSummary(
patient_name="Jane Smith",
patient_phone="(555) 987-6543",
indicators=["Anxiety"],
situation_description="mild distress",
medical_context={"age": 30, "gender": "female"}
)
result = self.generator.format_coherent_paragraph(summary)
assert "Jane Smith is a 30-year-old female" in result
assert "no significant medical history documented" in result
assert "Anxiety" in result
assert "mild distress" in result
def test_format_coherent_paragraph_single_condition(self):
"""Test coherent paragraph with single medical condition."""
summary = ProviderSummary(
patient_name="Bob Wilson",
patient_phone="(555) 456-7890",
indicators=["Depression"],
situation_description="emotional distress",
medical_context={
"age": 55,
"gender": "male",
"conditions": ["Cancer"]
}
)
result = self.generator.format_coherent_paragraph(summary)
assert "clinical history of Cancer" in result
assert "Depression" in result
def test_format_coherent_paragraph_multiple_conditions(self):
"""Test coherent paragraph with multiple medical conditions."""
summary = ProviderSummary(
patient_name="Alice Brown",
patient_phone="(555) 321-0987",
indicators=["Fear", "Uncertainty", "Loss of hope"],
situation_description="complex medical situation",
medical_context={
"age": 67,
"gender": "female",
"conditions": ["Heart Disease", "Diabetes", "Arthritis", "COPD"]
}
)
result = self.generator.format_coherent_paragraph(summary)
# Should format as "X, Y, Z, and W"
assert "clinical history of Heart Disease, Diabetes, Arthritis, and COPD" in result
assert "Fear, Uncertainty, and Loss of hope" in result
def test_format_coherent_paragraph_no_phone(self):
"""Test coherent paragraph with no phone number."""
summary = ProviderSummary(
patient_name="Charlie Davis",
patient_phone="",
indicators=["Spiritual questioning"],
situation_description="seeking meaning",
medical_context={"age": 40, "gender": "male"}
)
result = self.generator.format_coherent_paragraph(summary)
assert "No contact number is currently available" in result
def test_format_coherent_paragraph_with_conversation_context(self):
"""Test coherent paragraph with patient quote from conversation."""
summary = ProviderSummary(
patient_name="Diana Evans",
patient_phone="(555) 654-3210",
indicators=["Existential questioning"],
situation_description="spiritual crisis",
medical_context={"age": 35, "gender": "female"},
conversation_context="I don't understand why this is happening to me and my family"
)
result = self.generator.format_coherent_paragraph(summary)
# Should include patient quote as separate line (Requirement 2.8)
assert 'Patient reported: "I don\'t understand why this is happening to me and my family"' in result
# Should have main paragraph and quote separated
parts = result.split('\n\n')
assert len(parts) == 2
def test_format_coherent_paragraph_no_indicators(self):
"""Test coherent paragraph with no specific indicators."""
summary = ProviderSummary(
patient_name="Frank Green",
patient_phone="(555) 789-0123",
indicators=[],
situation_description="general concern",
medical_context={"age": 50, "gender": "male"}
)
result = self.generator.format_coherent_paragraph(summary)
assert "general distress" in result
assert "general concern" in result
def test_format_coherent_paragraph_unknown_gender(self):
"""Test coherent paragraph with unknown gender."""
summary = ProviderSummary(
patient_name="Taylor Smith",
patient_phone="(555) 111-2222",
indicators=["Anxiety"],
situation_description="stress",
medical_context={"age": 28} # No gender specified
)
result = self.generator.format_coherent_paragraph(summary)
assert "Taylor Smith is a 28-year-old individual" in result
def test_format_coherent_paragraph_no_age(self):
"""Test coherent paragraph with no age specified."""
summary = ProviderSummary(
patient_name="Sam Johnson",
patient_phone="(555) 333-4444",
indicators=["Worry"],
situation_description="concern",
medical_context={"gender": "non-binary"}
)
result = self.generator.format_coherent_paragraph(summary)
assert "Sam Johnson is a unknown age non-binary" in result
def test_format_coherent_paragraph_minimal_data(self):
"""Test coherent paragraph with minimal data."""
summary = ProviderSummary(
patient_name="Minimal Patient",
patient_phone="",
indicators=[],
situation_description="",
medical_context=None
)
result = self.generator.format_coherent_paragraph(summary)
# Should still produce a valid paragraph
assert "Minimal Patient is a unknown age individual" in result
assert "no significant medical history documented" in result
assert "general distress" in result
assert "spiritual or emotional distress" in result
assert "No contact number is currently available" in result
def test_format_coherent_paragraph_sentence_structure(self):
"""Test that coherent paragraph has proper sentence structure."""
summary = ProviderSummary(
patient_name="Structure Test",
patient_phone="(555) 555-5555",
indicators=["Test indicator"],
situation_description="test situation",
medical_context={"age": 40, "gender": "male", "conditions": ["Test condition"]}
)
result = self.generator.format_coherent_paragraph(summary)
# Should end with period
main_paragraph = result.split('\n\n')[0]
assert main_paragraph.endswith('.')
# Should be properly connected sentences
sentences = main_paragraph.split('. ')
assert len(sentences) >= 4 # Should have multiple connected sentences
def test_format_coherent_paragraph_integration_with_existing(self):
"""Test that coherent paragraph doesn't break existing functionality."""
summary = ProviderSummary(
patient_name="Integration Test",
patient_phone="(555) 999-8888",
indicators=["Integration test"],
situation_description="testing integration"
)
# Test that existing format_for_display still works
structured_result = self.generator.format_for_display(summary)
assert "PROVIDER SUMMARY - SPIRITUAL CARE REFERRAL" in structured_result
# Test that new coherent format works
coherent_result = self.generator.format_coherent_paragraph(summary)
assert "Integration Test is a unknown age individual" in coherent_result
# Results should be different formats
assert structured_result != coherent_result
if __name__ == "__main__":
pytest.main([__file__])