|
| 1 | +import json |
| 2 | +from collections.abc import Generator |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pydantic import ValidationError |
| 7 | + |
| 8 | +from canvas_generated.messages.effects_pb2 import EffectType |
| 9 | +from canvas_sdk.effects.ccda import CreateCCDA, DocumentType |
| 10 | + |
| 11 | +SAMPLE_XML_CONTENT = """<?xml version="1.0" encoding="UTF-8"?> |
| 12 | +<ClinicalDocument xmlns="urn:hl7-org:v3"> |
| 13 | + <typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040"/> |
| 14 | +</ClinicalDocument>""" |
| 15 | + |
| 16 | +INVALID_XML_CONTENT = """<ClinicalDocument> |
| 17 | + <unclosed_tag> |
| 18 | +</ClinicalDocument>""" |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def mock_patient_exists() -> Generator[MagicMock]: |
| 23 | + """Mock Patient.objects to return that the patient exists.""" |
| 24 | + with patch("canvas_sdk.effects.ccda.ccda_export.Patient.objects") as mock_patient: |
| 25 | + mock_patient.filter.return_value.exists.return_value = True |
| 26 | + yield mock_patient |
| 27 | + |
| 28 | + |
| 29 | +def test_values_with_all_fields() -> None: |
| 30 | + """Test that values property returns all fields correctly.""" |
| 31 | + effect = CreateCCDA( |
| 32 | + patient_id="patient-key-123", |
| 33 | + content=SAMPLE_XML_CONTENT, |
| 34 | + document_type=DocumentType.CCD, |
| 35 | + ) |
| 36 | + |
| 37 | + assert effect.values == { |
| 38 | + "patient_id": "patient-key-123", |
| 39 | + "content": SAMPLE_XML_CONTENT, |
| 40 | + "document_type": "CCD", |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +def test_values_with_minimal_fields() -> None: |
| 45 | + """Test that values property returns correctly with required fields and default document_type.""" |
| 46 | + effect = CreateCCDA( |
| 47 | + patient_id="patient-key-123", |
| 48 | + content=SAMPLE_XML_CONTENT, |
| 49 | + ) |
| 50 | + |
| 51 | + assert effect.values == { |
| 52 | + "patient_id": "patient-key-123", |
| 53 | + "content": SAMPLE_XML_CONTENT, |
| 54 | + "document_type": "CCD", |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +def test_values_with_referral_document_type() -> None: |
| 59 | + """Test that values property works with Referral document type.""" |
| 60 | + effect = CreateCCDA( |
| 61 | + patient_id="patient-key-123", |
| 62 | + content=SAMPLE_XML_CONTENT, |
| 63 | + document_type=DocumentType.REFERRAL, |
| 64 | + ) |
| 65 | + |
| 66 | + assert effect.values["document_type"] == "Referral" |
| 67 | + |
| 68 | + |
| 69 | +def test_document_type_accepts_string_value() -> None: |
| 70 | + """Test that document_type accepts plain strings via strict=False.""" |
| 71 | + effect = CreateCCDA( |
| 72 | + patient_id="patient-key-123", |
| 73 | + content=SAMPLE_XML_CONTENT, |
| 74 | + document_type="Referral", # type: ignore[arg-type] |
| 75 | + ) |
| 76 | + |
| 77 | + assert effect.document_type == DocumentType.REFERRAL |
| 78 | + assert effect.values["document_type"] == "Referral" |
| 79 | + |
| 80 | + |
| 81 | +def test_effect_payload_structure() -> None: |
| 82 | + """Test that effect_payload has correct structure.""" |
| 83 | + effect = CreateCCDA( |
| 84 | + patient_id="patient-key-123", |
| 85 | + content=SAMPLE_XML_CONTENT, |
| 86 | + document_type=DocumentType.CCD, |
| 87 | + ) |
| 88 | + |
| 89 | + payload = effect.effect_payload |
| 90 | + assert "data" in payload |
| 91 | + assert payload["data"]["patient_id"] == "patient-key-123" |
| 92 | + assert payload["data"]["content"] == SAMPLE_XML_CONTENT |
| 93 | + assert payload["data"]["document_type"] == "CCD" |
| 94 | + |
| 95 | + |
| 96 | +def test_construction_raises_error_without_patient_id() -> None: |
| 97 | + """Test that construction raises validation error when patient_id is missing.""" |
| 98 | + with pytest.raises(ValidationError) as exc_info: |
| 99 | + CreateCCDA(content=SAMPLE_XML_CONTENT) # type: ignore[call-arg] |
| 100 | + |
| 101 | + assert "patient_id" in str(exc_info.value) |
| 102 | + |
| 103 | + |
| 104 | +def test_construction_raises_error_with_empty_patient_id() -> None: |
| 105 | + """Test that construction raises validation error when patient_id is empty.""" |
| 106 | + with pytest.raises(ValidationError) as exc_info: |
| 107 | + CreateCCDA(patient_id="", content=SAMPLE_XML_CONTENT) |
| 108 | + |
| 109 | + assert "patient_id" in str(exc_info.value) |
| 110 | + |
| 111 | + |
| 112 | +def test_construction_raises_error_without_content() -> None: |
| 113 | + """Test that construction raises validation error when content is missing.""" |
| 114 | + with pytest.raises(ValidationError) as exc_info: |
| 115 | + CreateCCDA(patient_id="patient-key-123") # type: ignore[call-arg] |
| 116 | + |
| 117 | + assert "content" in str(exc_info.value) |
| 118 | + |
| 119 | + |
| 120 | +def test_construction_raises_error_with_empty_content() -> None: |
| 121 | + """Test that construction raises validation error when content is empty.""" |
| 122 | + with pytest.raises(ValidationError) as exc_info: |
| 123 | + CreateCCDA(patient_id="patient-key-123", content="") |
| 124 | + |
| 125 | + assert "content" in str(exc_info.value) |
| 126 | + |
| 127 | + |
| 128 | +def test_default_document_type_is_ccd() -> None: |
| 129 | + """Test that document_type defaults to CCD.""" |
| 130 | + effect = CreateCCDA(patient_id="patient-key-123", content=SAMPLE_XML_CONTENT) |
| 131 | + assert effect.document_type == DocumentType.CCD |
| 132 | + |
| 133 | + |
| 134 | +def test_effect_payload_serialization() -> None: |
| 135 | + """Test that effect_payload contains correctly serialized data.""" |
| 136 | + effect = CreateCCDA( |
| 137 | + patient_id="test-patient-key", |
| 138 | + content=SAMPLE_XML_CONTENT, |
| 139 | + document_type=DocumentType.REFERRAL, |
| 140 | + ) |
| 141 | + |
| 142 | + payload = effect.effect_payload |
| 143 | + assert payload["data"]["patient_id"] == "test-patient-key" |
| 144 | + assert payload["data"]["content"] == SAMPLE_XML_CONTENT |
| 145 | + assert payload["data"]["document_type"] == "Referral" |
| 146 | + |
| 147 | + |
| 148 | +def test_document_type_enum_values() -> None: |
| 149 | + """Test DocumentType enum has expected values.""" |
| 150 | + assert DocumentType.CCD.value == "CCD" |
| 151 | + assert DocumentType.REFERRAL.value == "Referral" |
| 152 | + |
| 153 | + |
| 154 | +def test_apply_succeeds_with_valid_data(mock_patient_exists: MagicMock) -> None: |
| 155 | + """Test that apply returns an Effect with correct type and payload.""" |
| 156 | + effect = CreateCCDA( |
| 157 | + patient_id="patient-key-123", |
| 158 | + content=SAMPLE_XML_CONTENT, |
| 159 | + document_type=DocumentType.CCD, |
| 160 | + ) |
| 161 | + |
| 162 | + applied = effect.apply() |
| 163 | + |
| 164 | + assert applied.type == EffectType.CREATE_CCDA |
| 165 | + payload = json.loads(applied.payload) |
| 166 | + assert payload["data"]["patient_id"] == "patient-key-123" |
| 167 | + assert payload["data"]["content"] == SAMPLE_XML_CONTENT |
| 168 | + assert payload["data"]["document_type"] == "CCD" |
| 169 | + |
| 170 | + |
| 171 | +def test_apply_raises_error_for_nonexistent_patient() -> None: |
| 172 | + """Test that apply raises validation error when patient does not exist.""" |
| 173 | + with patch("canvas_sdk.effects.ccda.ccda_export.Patient.objects") as mock_patient: |
| 174 | + mock_patient.filter.return_value.exists.return_value = False |
| 175 | + |
| 176 | + effect = CreateCCDA( |
| 177 | + patient_id="nonexistent-patient", |
| 178 | + content=SAMPLE_XML_CONTENT, |
| 179 | + ) |
| 180 | + |
| 181 | + with pytest.raises(ValidationError) as exc_info: |
| 182 | + effect.apply() |
| 183 | + |
| 184 | + assert "does not exist" in str(exc_info.value) |
| 185 | + |
| 186 | + |
| 187 | +def test_apply_raises_error_for_invalid_xml(mock_patient_exists: MagicMock) -> None: |
| 188 | + """Test that apply raises validation error when content is invalid XML.""" |
| 189 | + effect = CreateCCDA( |
| 190 | + patient_id="patient-key-123", |
| 191 | + content=INVALID_XML_CONTENT, |
| 192 | + ) |
| 193 | + |
| 194 | + with pytest.raises(ValidationError) as exc_info: |
| 195 | + effect.apply() |
| 196 | + |
| 197 | + assert "Invalid XML content" in str(exc_info.value) |
0 commit comments