test_server.pyā¢5.18 kB
"""
Quick test script to validate the FHIR MCP server functionality
"""
def test_discovery():
"""Test progressive discovery functions"""
print("š Testing Progressive Discovery...")
# Import after ensuring dependencies are installed
from fhir_mcp_server import (
discover_resource_groups,
explore_resource_group,
list_all_resource_groups,
RESOURCE_GROUPS,
)
# Test 1: Discover groups by intent
print("\n1ļøā£ Testing discover_resource_groups with intent 'patient care'")
result = discover_resource_groups("patient care")
print(f" Found {len(result['discovered_groups'])} relevant groups")
for group in result["discovered_groups"][:3]:
print(f" - {group['name']}: {group['resource_count']} resources")
# Test 2: List all groups
print("\n2ļøā£ Testing list_all_resource_groups")
result = list_all_resource_groups()
print(f" Total groups: {result['total_groups']}")
# Test 3: Explore specific group
print("\n3ļøā£ Testing explore_resource_group('clinical')")
result = explore_resource_group("clinical")
print(f" Group: {result['name']}")
print(f" Resources: {', '.join(result['resources'][:5])}...")
print("\nā
Discovery tests passed!")
def test_resource_operations():
"""Test FHIR resource operations"""
print("\nš„ Testing FHIR Resource Operations...")
from fhir_mcp_server import get_resource_schema, create_resource, validate_resource
# Test 1: Get schema
print("\n1ļøā£ Testing get_resource_schema('Patient')")
result = get_resource_schema("Patient")
if "schema" in result:
print(f" Schema retrieved for {result['resource_type']}")
print(f" FHIR version: {result.get('fhir_version', 'unknown')}")
print(f" Required fields: {result.get('required_fields', [])[:3]}...")
else:
print(f" ā ļø Error: {result.get('error', 'Unknown error')}")
# Test 2: Create a Patient resource
print("\n2ļøā£ Testing create_resource (Patient)")
patient_data = {
"resourceType": "Patient",
"active": True,
"name": [{"use": "official", "family": "TestPatient", "given": ["John"]}],
"gender": "male",
"birthDate": "1990-01-01",
}
result = create_resource("Patient", patient_data)
if result.get("success"):
print(f" ā
Patient created successfully")
print(f" Resource ID: {result.get('id', 'N/A')}")
else:
print(f" ā ļø Error: {result.get('error', 'Unknown error')}")
# Test 3: Validate a resource
print("\n3ļøā£ Testing validate_resource")
import json
patient_json = json.dumps(patient_data)
result = validate_resource(patient_json)
if result.get("valid"):
print(f" ā
Resource is valid FHIR")
else:
print(f" ā ļø Validation failed: {result.get('error', 'Unknown error')}")
print("\nā
Resource operations tests passed!")
def test_format_conversion():
"""Test format conversion"""
print("\nš Testing Format Conversion...")
from fhir_mcp_server import convert_between_formats
import json
patient_json = json.dumps(
{"resourceType": "Patient", "id": "example", "gender": "male", "birthDate": "1990-01-01"}
)
print("\n1ļøā£ Testing JSON to XML conversion")
result = convert_between_formats(
resource_data=patient_json, input_format="json", output_format="xml"
)
if result.get("success"):
print(f" ā
Converted {result['resource_type']} to XML")
print(f" Output length: {len(result['output'])} chars")
else:
print(f" ā ļø Error: {result.get('error', 'Unknown error')}")
print("\nā
Format conversion tests passed!")
def test_utility_functions():
"""Test utility functions"""
print("\nš ļø Testing Utility Functions...")
from fhir_mcp_server import get_fhir_version_info
print("\n1ļøā£ Testing get_fhir_version_info")
result = get_fhir_version_info()
if "error" not in result:
print(f" Default version: {result.get('default_version', 'unknown')}")
print(f" Supported versions: {result.get('supported_versions', [])}")
print(f" Features: {len(result.get('features', []))} features")
else:
print(f" ā ļø Error: {result.get('error', 'Unknown error')}")
print("\nā
Utility tests passed!")
if __name__ == "__main__":
print("=" * 60)
print("FHIR Progressive Discovery MCP Server - Test Suite")
print("=" * 60)
try:
test_discovery()
test_resource_operations()
test_format_conversion()
test_utility_functions()
print("\n" + "=" * 60)
print("š ALL TESTS PASSED!")
print("=" * 60)
print("\nš” Next steps:")
print(" 1. Run the server: python3 fhir_mcp_server.py")
print(" 2. Configure your MCP client to connect")
print(" 3. Start using progressive discovery!")
except Exception as e:
print(f"\nā Test failed with error: {e}")
import traceback
traceback.print_exc()