Ejemplo n.º 1
0
 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))
Ejemplo n.º 2
0
  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)
Ejemplo n.º 3
0
 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
Ejemplo n.º 4
0
  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)
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
  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))
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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))
Ejemplo n.º 9
0
  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)
Ejemplo n.º 10
0
 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))
Ejemplo n.º 11
0
  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))
Ejemplo n.º 12
0
 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))
Ejemplo n.º 13
0
  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)
Ejemplo n.º 14
0
  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))
Ejemplo n.º 15
0
  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)
Ejemplo n.º 16
0
 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))
Ejemplo n.º 17
0
 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)
Ejemplo n.º 18
0
  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)
Ejemplo n.º 19
0
 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))
Ejemplo n.º 20
0
  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)
Ejemplo n.º 21
0
  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")
Ejemplo n.º 22
0
 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))
Ejemplo n.º 23
0
  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)
Ejemplo n.º 24
0
  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)
Ejemplo n.º 25
0
 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)
Ejemplo n.º 26
0
 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())
Ejemplo n.º 27
0
 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"))
Ejemplo n.º 28
0
 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"))
Ejemplo n.º 29
0
 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)
Ejemplo n.º 30
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)