|
""" |
|
TDD Tests for FHIR Validation |
|
Focus on healthcare-grade FHIR R4 compliance |
|
""" |
|
|
|
import pytest |
|
import json |
|
from unittest.mock import Mock, patch |
|
from typing import Dict, Any |
|
|
|
|
|
try: |
|
from src.fhir_validator import FhirValidator |
|
except ImportError: |
|
FhirValidator = None |
|
|
|
|
|
class TestFhirValidatorTDD: |
|
"""TDD tests for FHIR validation - healthcare grade""" |
|
|
|
def setup_method(self): |
|
"""Setup test FHIR bundles""" |
|
self.valid_fhir_bundle = { |
|
"resourceType": "Bundle", |
|
"id": "test-bundle", |
|
"type": "document", |
|
"timestamp": "2025-06-03T00:00:00Z", |
|
"entry": [ |
|
{ |
|
"resource": { |
|
"resourceType": "Patient", |
|
"id": "test-patient", |
|
"identifier": [{"value": "123456789"}], |
|
"name": [{"given": ["John"], "family": "Doe"}], |
|
"birthDate": "1980-01-01" |
|
} |
|
}, |
|
{ |
|
"resource": { |
|
"resourceType": "Observation", |
|
"id": "test-observation", |
|
"status": "final", |
|
"code": { |
|
"coding": [{ |
|
"system": "http://loinc.org", |
|
"code": "85354-9", |
|
"display": "Blood pressure" |
|
}] |
|
}, |
|
"subject": {"reference": "Patient/test-patient"}, |
|
"valueString": "140/90 mmHg" |
|
} |
|
} |
|
] |
|
} |
|
|
|
self.invalid_fhir_bundle = { |
|
"resourceType": "InvalidType", |
|
"entry": [] |
|
} |
|
|
|
@pytest.mark.unit |
|
def test_fhir_validator_initialization(self): |
|
"""Test: FHIR validator initializes correctly""" |
|
|
|
|
|
validator = FhirValidator() |
|
|
|
|
|
assert validator is not None |
|
assert validator.validation_level == 'healthcare_grade' |
|
assert validator.fhir_version == 'R4' |
|
|
|
@pytest.mark.unit |
|
def test_validate_valid_fhir_bundle(self): |
|
"""Test: Valid FHIR bundle passes validation""" |
|
|
|
validator = FhirValidator() |
|
bundle = self.valid_fhir_bundle |
|
|
|
|
|
result = validator.validate_bundle(bundle) |
|
|
|
|
|
assert result['is_valid'] is True |
|
assert result['compliance_score'] > 0.9 |
|
assert len(result['errors']) == 0 |
|
assert result['fhir_r4_compliant'] is True |
|
|
|
@pytest.mark.unit |
|
def test_validate_invalid_fhir_bundle(self): |
|
"""Test: Invalid FHIR bundle fails validation""" |
|
|
|
validator = FhirValidator() |
|
bundle = self.invalid_fhir_bundle |
|
|
|
|
|
result = validator.validate_bundle(bundle) |
|
|
|
|
|
assert result['is_valid'] is False |
|
assert result['compliance_score'] < 0.5 |
|
assert len(result['errors']) > 0 |
|
assert result['fhir_r4_compliant'] is False |
|
|
|
@pytest.mark.unit |
|
def test_validate_fhir_structure(self): |
|
"""Test: FHIR structure validation""" |
|
|
|
validator = FhirValidator() |
|
|
|
|
|
result = validator.validate_structure(self.valid_fhir_bundle) |
|
|
|
|
|
assert result['structure_valid'] is True |
|
assert 'Bundle' in result['detected_resources'] |
|
assert 'Patient' in result['detected_resources'] |
|
assert 'Observation' in result['detected_resources'] |
|
|
|
@pytest.mark.unit |
|
def test_validate_medical_terminology(self): |
|
"""Test: Medical terminology validation (LOINC, SNOMED CT)""" |
|
|
|
validator = FhirValidator() |
|
bundle = self.valid_fhir_bundle |
|
|
|
|
|
result = validator.validate_terminology(bundle) |
|
|
|
|
|
assert result['terminology_valid'] is True |
|
assert result['loinc_codes_valid'] is True |
|
assert 'validated_codes' in result |
|
assert len(result['validated_codes']) > 0 |
|
|
|
@pytest.mark.unit |
|
def test_validate_hipaa_compliance(self): |
|
"""Test: HIPAA compliance validation""" |
|
|
|
validator = FhirValidator() |
|
bundle = self.valid_fhir_bundle |
|
|
|
|
|
result = validator.validate_hipaa_compliance(bundle) |
|
|
|
|
|
assert result['hipaa_compliant'] is True |
|
assert result['phi_protection'] is True |
|
assert result['security_tags_present'] is False |
|
|
|
@pytest.mark.unit |
|
def test_calculate_compliance_score(self): |
|
"""Test: Compliance score calculation""" |
|
|
|
validator = FhirValidator() |
|
validation_data = { |
|
'structure_valid': True, |
|
'terminology_valid': True, |
|
'hipaa_compliant': True, |
|
'fhir_r4_compliant': True |
|
} |
|
|
|
|
|
score = validator.calculate_compliance_score(validation_data) |
|
|
|
|
|
assert score >= 0.95 |
|
assert isinstance(score, float) |
|
assert 0.0 <= score <= 1.0 |
|
|
|
@pytest.mark.unit |
|
def test_validate_with_healthcare_grade_level(self): |
|
"""Test: Healthcare-grade validation level""" |
|
|
|
validator = FhirValidator(validation_level='healthcare_grade') |
|
bundle = self.valid_fhir_bundle |
|
|
|
|
|
result = validator.validate_bundle(bundle, validation_level='healthcare_grade') |
|
|
|
|
|
assert result['validation_level'] == 'healthcare_grade' |
|
assert result['strict_mode'] is True |
|
assert result['medical_coding_validated'] is True |
|
assert result['interoperability_score'] > 0.9 |