Esempio n. 1
0
    def testClearFhirExtensions_withMultipleExtensions_succeeds(self):
        """Tests ClearFhirExtensions when a message has multiple extensions."""
        arbitrary_string = datatypes_pb2.String()
        arbitrary_string.extension.add(
            url=datatypes_pb2.Uri(value='first'),
            value=datatypes_pb2.Extension.ValueX(boolean=datatypes_pb2.Boolean(
                value=True)))
        arbitrary_string.extension.add(
            url=datatypes_pb2.Uri(value='second'),
            value=datatypes_pb2.Extension.ValueX(boolean=datatypes_pb2.Boolean(
                value=True)))
        arbitrary_string.extension.add(
            url=datatypes_pb2.Uri(value='third'),
            value=datatypes_pb2.Extension.ValueX(boolean=datatypes_pb2.Boolean(
                value=True)))
        self.assertLen(extensions.get_fhir_extensions(arbitrary_string), 3)

        # Remove middle extension
        extensions.clear_fhir_extensions_with_url(arbitrary_string, 'second')
        remaining_extensions = extensions.get_fhir_extensions(arbitrary_string)
        self.assertLen(remaining_extensions, 2)

        remaining_urls = [
            extension.url.value for extension in remaining_extensions
        ]
        self.assertEqual(remaining_urls, ['first', 'third'])
def validate_primitive_without_value(fhir_primitive: message.Message):
    """Validates a Message which has the PrimitiveWithoutValue extension.

  Given that there is a PrimitiveWithoutValue extension present, there must be
  at least one other extension. Otherwise, there is truly no value set other
  than id and/or extension (non-value fields).

  Args:
    fhir_primitive: The FHIR primitive Message to validate.

  Raises:
    fhir_errors.InvalidFhirError: In the event that there is less than one
    extension present, or there are values set other than id and/or extension.
  """
    name = fhir_primitive.DESCRIPTOR.full_name

    # There must be at least one other FHIR extension
    if len(extensions.get_fhir_extensions(fhir_primitive)) < 2:
        raise fhir_errors.InvalidFhirError(
            f'{name!r} must have either extensions or a value present.')

    # ... Else truly no value set other than id and/or extension
    for field, _ in fhir_primitive.ListFields():  # Iterate set fields only
        if field.name not in extensions.NON_VALUE_FIELDS:
            raise fhir_errors.InvalidFhirError(
                f'{name!r} contains PrimitiveHasNoValue but {field.name!r} is set.'
            )
Esempio n. 3
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)
    def has_value(self) -> bool:
        """Returns True if the wrapped primitive has a value.

    This method checks for the PrimitiveHasNoValue extension,
    *not* for the actual presence of a value field.
    """
        extensions_list = cast(List[Any],
                               extensions.get_fhir_extensions(self.wrapped))
        for extension in extensions_list:
            if (extension.url.value == extensions.PRIMITIVE_HAS_NO_VALUE_URL
                    and extension.value.boolean.value):
                return False
        return True
    def has_element(self) -> bool:
        """Returns True if the wrapped primitive has data in its `Element` fields.

    This method checks whether or not the wrapped primitive contains data in
    fields defined on the root Element type, like `id` or `extension`.
    """
        if proto_utils.field_is_set(self.wrapped, 'id'):
            return True

        # Return True if there exists an extension on the wrapped primitive whose
        # structure definition URL is something other than a conversion-only URL
        extensions_list = cast(List[Any],
                               extensions.get_fhir_extensions(self.wrapped))
        for extension in extensions_list:
            if extension.url.value not in extensions.CONVERSION_ONLY_EXTENSION_URLS:
                return True
        return False
Esempio n. 6
0
  def get_element(self) -> Optional[message.Message]:
    """Returns the raw Element underlying the wrapped primitive.

    Note that conversion-only extensions are removed prior to returning.
    """
    if not (proto_utils.field_is_set(self.wrapped, 'id') or
            proto_utils.field_is_set(self.wrapped, 'extension')):
      return None  # Early-exit if we can't populate an Element

    element = proto_utils.create_message_from_descriptor(
        self.wrapped.DESCRIPTOR)
    if proto_utils.field_is_set(self.wrapped, 'id'):
      proto_utils.copy_common_field(self.wrapped, element, 'id')

    extensions_list = cast(List[Any],
                           extensions.get_fhir_extensions(self.wrapped))
    for extension in extensions_list:
      if extension.url.value not in extensions.CONVERSION_ONLY_EXTENSION_URLS:
        proto_utils.append_value_at_field(element, 'extension', extension)

    return element
Esempio n. 7
0
 def testGetFhirExtensions_withNoExtensions_returnsEmptyList(self):
     """Tests get_fhir_extensions returns an empty list with no extensions."""
     patient = patient_pb2.Patient()
     self.assertEmpty(extensions.get_fhir_extensions(patient))