def testGetFixedCodingSystem_withInvalidMessage_returnsNone(self): """Test get_fixed_coding_system functionality with no annotation present.""" boolean = datatypes_pb2.Boolean() self.assertIsNone(annotation_utils.get_fixed_coding_system(boolean)) code = datatypes_pb2.Code() self.assertIsNone(annotation_utils.get_fixed_coding_system(code))
def testGetFixedCodingSystem_withValidFixedCodingSystem_returnsValue(self): """Test get_fixed_coding_system functionality when annotation is present.""" expected_system = 'http://hl7.org/fhir/metric-color' coding = (test_pb2.TestPatient.CodeableConceptForMaritalStatus. ColorCoding.FixedCode()) self.assertEqual(annotation_utils.get_fixed_coding_system(coding), expected_system) self.assertEqual( annotation_utils.get_fixed_coding_system(coding.DESCRIPTOR), expected_system)
def get_system_for_code(code: message.Message) -> str: """Returns the code system associated with the provided Code.""" system_field = code.DESCRIPTOR.fields_by_name.get('system') if system_field is not None: return proto_utils.get_value_at_field(code, system_field) fixed_coding_system = annotation_utils.get_fixed_coding_system(code) if fixed_coding_system is not None: # The entire profiled coding can only be from a single system. Use that. return fixed_coding_system # There is no single system for the whole coding. Look for the coding system # annotation on the enum. enum_field = code.DESCRIPTOR.fields_by_name.get('value') if (enum_field is None or enum_field.type != descriptor.FieldDescriptor.TYPE_ENUM): raise fhir_errors.InvalidFhirError( f'Invalid profiled Coding: {code.DESCRIPTOR.full_name}; missing system ' 'information on string code.') enum_value = proto_utils.get_value_at_field(code, enum_field) enum_value_descriptor = enum_field.enum_type.values_by_number[enum_value] if not annotation_utils.has_source_code_system(enum_value_descriptor): raise fhir_errors.InvalidFhirError( f'Invalid profiled Coding: {code.DESCRIPTOR.full_name}; missing system ' 'information on enum code') return cast(str, annotation_utils.get_source_code_system(enum_value_descriptor))