def testAreSameMessageType_withSameMessageType_returnsTrue(self): """Test are_same_message_type with the same message types.""" patient_a = patient_pb2.Patient() patient_b = patient_pb2.Patient() self.assertTrue( proto_utils.are_same_message_type(patient_a.DESCRIPTOR, patient_b.DESCRIPTOR)) self.assertTrue( proto_utils.are_same_message_type(patient_pb2.Patient.DESCRIPTOR, patient_pb2.Patient.DESCRIPTOR))
def testCopyCommonField_notPresentInBothMessages_raisesException(self): """Tests copy_common_field with an invalid descriptor raises.""" first_patient = patient_pb2.Patient( active=datatypes_pb2.Boolean(value=True)) second_patient = patient_pb2.Patient( active=datatypes_pb2.Boolean(value=False)) with self.assertRaises(ValueError) as ve: proto_utils.copy_common_field(first_patient, second_patient, "value") self.assertIsInstance(ve.exception, ValueError)
def _create_patient_with_names(self, names: List[str]) -> patient_pb2.Patient: patient = patient_pb2.Patient() for name in names: patient.name.append( datatypes_pb2.HumanName(text=datatypes_pb2.String(value=name))) return patient
def testGetValueAtFieldIndex_invalidIndex_raisesException(self): """Test get_value_at_field_index with an invalid index.""" patient = patient_pb2.Patient(active=datatypes_pb2.Boolean(value=True)) with self.assertRaises(ValueError) as ve: proto_utils.get_value_at_field_index(patient, "active", 1) self.assertIsInstance(ve.exception, ValueError)
def testGetStructureDefinitionUrl_withFhirType_returnsValue(self): """Test get_structure_definition_url functionality on FHIR types.""" boolean = datatypes_pb2.Boolean() boolean_descriptor_proto = self._descriptor_proto_for_descriptor( boolean.DESCRIPTOR) code = datatypes_pb2.Code() code_descriptor_proto = self._descriptor_proto_for_descriptor( code.DESCRIPTOR) patient = patient_pb2.Patient() patient_descriptor_proto = self._descriptor_proto_for_descriptor( patient.DESCRIPTOR) self.assertEqual( annotation_utils.get_structure_definition_url(boolean), _BOOLEAN_STRUCTURE_DEFINITION_URL) self.assertEqual( annotation_utils.get_structure_definition_url(boolean.DESCRIPTOR), _BOOLEAN_STRUCTURE_DEFINITION_URL) self.assertEqual( annotation_utils.get_structure_definition_url(code), _CODE_STRUCTURE_DEFINITION_URL) self.assertEqual( annotation_utils.get_structure_definition_url(patient), _PATIENT_STRUCTURE_DEFINITION_URL) self.assertEqual( annotation_utils.get_structure_definition_url(patient.DESCRIPTOR), _PATIENT_STRUCTURE_DEFINITION_URL) self.assertEqual( annotation_utils.get_structure_definition_url(boolean_descriptor_proto), _BOOLEAN_STRUCTURE_DEFINITION_URL) self.assertEqual( annotation_utils.get_structure_definition_url(code_descriptor_proto), _CODE_STRUCTURE_DEFINITION_URL) self.assertEqual( annotation_utils.get_structure_definition_url(patient_descriptor_proto), _PATIENT_STRUCTURE_DEFINITION_URL)
def testMessageIsType_withActualMessageType_returnsTrue(self): """Test MessageIsType functionality against the proper FHIR type.""" patient = patient_pb2.Patient() self.assertTrue(proto_utils.is_message_type(patient, patient_pb2.Patient)) boolean = datatypes_pb2.Boolean() self.assertTrue(proto_utils.is_message_type(boolean, datatypes_pb2.Boolean))
def testGetFhirExtensions_withExtensions_returnsList(self): """Tests get_fhir_extensions returns a non-empty list with extensions.""" patient = patient_pb2.Patient() patient.extension.add(url=datatypes_pb2.Uri(value='abcd'), value=datatypes_pb2.Extension.ValueX( boolean=datatypes_pb2.Boolean(value=True))) self.assertLen(extensions.get_fhir_extensions(patient), 1)
def testIsResource_withPatient_returnsTrue(self): """Test is_resource functionality on non-primitive input.""" patient = patient_pb2.Patient() patient_descriptor_proto = self._descriptor_proto_for_descriptor( patient.DESCRIPTOR) self.assertTrue(annotation_utils.is_resource(patient)) self.assertTrue(annotation_utils.is_resource(patient.DESCRIPTOR)) self.assertTrue(annotation_utils.is_resource(patient_descriptor_proto))
def testSetValueAtField_singleCompositeValue_setsValue(self): """Test set_value_at_field with a singular compositie type.""" patient = patient_pb2.Patient(active=datatypes_pb2.Boolean(value=False)) self.assertFalse(patient.active.value) proto_utils.set_value_at_field(patient, "active", datatypes_pb2.Boolean(value=True)) self.assertTrue(patient.active.value)
def testGetValueRegexForPrimitiveType_withCompound_returnsNone(self): """Test get_value_regex_for_primitive_type on non-primitives.""" patient = patient_pb2.Patient() self.assertIsNone( annotation_utils.get_value_regex_for_primitive_type(patient)) self.assertIsNone( annotation_utils.get_value_regex_for_primitive_type( patient.DESCRIPTOR))
def testMessageIsType_withDifferentMessageType_returnsFalse(self): """Test MessageIsType functionality against a different FHIR type.""" patient = patient_pb2.Patient() self.assertFalse( proto_utils.is_message_type(patient, datatypes_pb2.Boolean)) boolean = datatypes_pb2.Boolean() self.assertFalse(proto_utils.is_message_type(boolean, patient_pb2.Patient))
def testIsPrimitiveType_withPatient_returnsFalse(self): """Test is_primitive_type functionality on non-primitive input.""" patient = patient_pb2.Patient() patient_descriptor_proto = self._descriptor_proto_for_descriptor( patient.DESCRIPTOR) self.assertFalse(annotation_utils.is_primitive_type(patient)) self.assertFalse(annotation_utils.is_primitive_type(patient.DESCRIPTOR)) self.assertFalse( annotation_utils.is_primitive_type(patient_descriptor_proto))
def testAppendValueAtField_singularCompositeValue_raises(self): """Test append_value_at_field with a singular composite type.""" patient = patient_pb2.Patient() active = datatypes_pb2.Boolean(value=True) with self.assertRaises(ValueError) as ve: proto_utils.append_value_at_field(patient, "active", active) self.assertIsInstance(ve.exception, ValueError)
def testIsChoiceType_withInvalidChoiceType_returnsFalse(self): """Test is_choice_type functionality on invalid input.""" boolean = datatypes_pb2.Boolean() value_fd = boolean.DESCRIPTOR.fields_by_name['value'] self.assertFalse(annotation_utils.is_choice_type_field(value_fd)) patient = patient_pb2.Patient() text_fd = patient.DESCRIPTOR.fields_by_name['text'] self.assertFalse(annotation_utils.is_choice_type_field(text_fd))
def testCreateMessageFromDescriptor_withArguments_returnsMessage(self): """Tests that the correct class is instantiated with kwargs.""" patient_name = datatypes_pb2.HumanName( text=datatypes_pb2.String(value="Foo"), family=datatypes_pb2.String(value="Bar")) expected_patient = patient_pb2.Patient(name=[patient_name]) actual_patient = proto_utils.create_message_from_descriptor( patient_pb2.Patient.DESCRIPTOR, name=[patient_name]) self.assertEqual(expected_patient, actual_patient)
def testAreSameMessageType_withDifferentMessageType_returnsFalse(self): """Test are_same_message_type with two different message types.""" patient = patient_pb2.Patient() uscore_patient_profile = uscore_pb2.USCorePatientProfile() self.assertFalse( proto_utils.are_same_message_type(patient.DESCRIPTOR, uscore_patient_profile.DESCRIPTOR)) self.assertFalse( proto_utils.are_same_message_type(patient_pb2.Patient.DESCRIPTOR, uscore_patient_profile.DESCRIPTOR))
def testGetValueAtField_withRepeatedComposite_returnsList(self): """Test get_value_at_field with a repeated composite field.""" patient_names = [ datatypes_pb2.HumanName(text=datatypes_pb2.String(value="Foo")), datatypes_pb2.HumanName(text=datatypes_pb2.String(value="Bar")), datatypes_pb2.HumanName(text=datatypes_pb2.String(value="Bats")), ] patient = patient_pb2.Patient(name=patient_names) result = proto_utils.get_value_at_field(patient, "name") self.assertEqual(result, patient_names)
def testSetValueAtFieldIndex_singleCompositeField_setsValue(self): """Test set_value_at_field_index with a singular composite type.""" known_gender = patient_pb2.Patient.GenderCode( value=codes_pb2.AdministrativeGenderCode.MALE) unknown_gender = patient_pb2.Patient.GenderCode( value=codes_pb2.AdministrativeGenderCode.UNKNOWN) patient = patient_pb2.Patient(gender=unknown_gender) self.assertEqual(patient.gender, unknown_gender) proto_utils.set_value_at_field_index(patient, "gender", 0, known_gender) self.assertEqual(patient.gender, known_gender)
def testIsProfileOf_withInvalidProfile_returnsFalse(self): """Tests is_profile_of functionality with an invalid profile of Patient.""" patient = patient_pb2.Patient() observation = observation_pb2.Observation() self.assertFalse(annotation_utils.is_profile_of(patient, observation)) self.assertFalse( annotation_utils.is_profile_of(patient, observation.DESCRIPTOR)) self.assertFalse( annotation_utils.is_profile_of(patient.DESCRIPTOR, observation)) self.assertFalse( annotation_utils.is_profile_of(patient.DESCRIPTOR, observation.DESCRIPTOR))
def testSetInParentOrAdd_withSingularComposite_returnsMessage(self): """Test set_in_parent_or_add with a singlular composite field.""" patient = patient_pb2.Patient() self.assertFalse(proto_utils.field_is_set(patient, "active")) active_set_in_parent = proto_utils.set_in_parent_or_add(patient, "active") self.assertTrue(proto_utils.field_is_set(patient, "active")) self.assertFalse(active_set_in_parent.value) self.assertFalse(patient.active.value) active_set_in_parent.value = True self.assertTrue(active_set_in_parent.value) self.assertTrue(patient.active.value)
def testSetInParentOrAdd_withRepeatedComposite_returnsMessage(self): """Test set_in_parent_or_add with repeated composite field.""" patient = patient_pb2.Patient() self.assertFalse(proto_utils.field_is_set(patient, "name")) name_set_in_parent = proto_utils.set_in_parent_or_add(patient, "name") self.assertTrue(proto_utils.field_is_set(patient, "name")) self.assertEmpty(name_set_in_parent.text.value) self.assertEmpty(patient.name[0].text.value) name_set_in_parent.text.value = "Foo" self.assertEqual(name_set_in_parent.text.value, "Foo") self.assertEqual(patient.name[0].text.value, "Foo")
def testIsProfileOf_withValidProfile_returnsTrue(self): """Tests is_profile_of functionality with a valid Patient profile.""" patient = patient_pb2.Patient() uscore_patient_profile = uscore_pb2.USCorePatientProfile() self.assertTrue( annotation_utils.is_profile_of(patient, uscore_patient_profile)) self.assertTrue( annotation_utils.is_profile_of(patient.DESCRIPTOR, uscore_patient_profile)) self.assertTrue( annotation_utils.is_profile_of(patient, uscore_patient_profile.DESCRIPTOR)) self.assertTrue( annotation_utils.is_profile_of(patient.DESCRIPTOR, uscore_patient_profile.DESCRIPTOR))
def testAppendValueAtField_repeatedCompositeValue_appendsValue(self): """Test append_value_at_field with a repeated composite type.""" patient = patient_pb2.Patient() patient_names = [ datatypes_pb2.HumanName(text=datatypes_pb2.String(value="Foo")), datatypes_pb2.HumanName(text=datatypes_pb2.String(value="Bar")), datatypes_pb2.HumanName(text=datatypes_pb2.String(value="Bats")), ] self.assertEqual(proto_utils.field_content_length(patient, "name"), 0) for name in patient_names: proto_utils.append_value_at_field(patient, "name", name) self.assertEqual(proto_utils.field_content_length(patient, "name"), 3) self.assertEqual(patient.name[:], patient_names)
def testSetValueAtField_repeatedCompositeValue_setsList(self): """Test set_value_at_field with a repeated composite type.""" old_names = [ datatypes_pb2.HumanName(text=datatypes_pb2.String(value="A")), datatypes_pb2.HumanName(text=datatypes_pb2.String(value="B")), datatypes_pb2.HumanName(text=datatypes_pb2.String(value="C")), ] patient = patient_pb2.Patient(name=old_names) self.assertEqual(patient.name[:], old_names) new_names = [ datatypes_pb2.HumanName(text=datatypes_pb2.String(value="Foo")), datatypes_pb2.HumanName(text=datatypes_pb2.String(value="Bar")), datatypes_pb2.HumanName(text=datatypes_pb2.String(value="Bats")), ] proto_utils.set_value_at_field(patient, "name", new_names) self.assertEqual(patient.name[:], new_names)
def testGetFhirVersion_withValidInput_returnsCorrectVersion(self): """Tests get_fhir_version to ensure that the correct version is returned.""" patient = patient_pb2.Patient() uscore_patient = uscore_pb2.USCorePatientProfile() self.assertEqual( annotation_utils.get_fhir_version(patient), _R4_FHIR_VERSION) self.assertEqual( annotation_utils.get_fhir_version(uscore_patient), _R4_FHIR_VERSION) self.assertEqual( annotation_utils.get_fhir_version(patient.DESCRIPTOR), _R4_FHIR_VERSION) self.assertEqual( annotation_utils.get_fhir_version(uscore_patient.DESCRIPTOR), _R4_FHIR_VERSION) self.assertEqual( annotation_utils.get_fhir_version(patient.DESCRIPTOR.file), _R4_FHIR_VERSION) self.assertEqual( annotation_utils.get_fhir_version(uscore_patient.DESCRIPTOR.file), _R4_FHIR_VERSION)
def testCreateMessageFromDescriptor_returnsMessage(self): """Tests that the correct class is returned for a message.""" self.assertEqual( proto_utils.create_message_from_descriptor( patient_pb2.Patient.DESCRIPTOR), patient_pb2.Patient())
def testFieldIsSet_withUnsetField_returnsFalse(self): """Test field_is_set with an unset field.""" default_patient = patient_pb2.Patient() # Leave active unset self.assertFalse(proto_utils.field_is_set(default_patient, "active"))
def testFieldIsSet_withSetField_returnsTrue(self): """Test field_is_set with a set field.""" patient = patient_pb2.Patient(active=datatypes_pb2.Boolean(value=True)) self.assertTrue(proto_utils.field_is_set(patient, "active"))
def testFieldContentLength_withNonExistentField_returnsZero(self): """Test field_content_length functionality on non-existent field input.""" default_patient = patient_pb2.Patient() # Leave active unset self.assertEqual( proto_utils.field_content_length(default_patient, "active"), 0)
def testFieldContentLength_withSingularField_returnsSingleCount(self): """Test field_content_length functionality on singular field input.""" patient = patient_pb2.Patient(active=datatypes_pb2.Boolean(value=True)) self.assertEqual(proto_utils.field_content_length(patient, "active"), 1)