Example #1
0
def test_type_path_constraint():
    """where(type='successor').​resource
    where(resolve() is Patient)
    where(system='email')
    where(type='predecessor').​resource
    """
    path_ = ElementPath("Patient.telecom.where(system='email')")
    assert path_._where is not None
    assert path_._where.value == "email"
    assert path_._where.name == "system"
    assert path_._where.subpath is None
    assert path_._where.type == WhereConstraintType.T1

    path_ = ElementPath("CarePlan.subject.where(resolve() is Patient)")
    assert path_._where.value == "Patient"
    assert path_._where.name is None
    assert path_._where.subpath is None
    assert path_._where.type == WhereConstraintType.T2

    path_ = ElementPath(
        "ActivityDefinition.relatedArtifact.where(type='composed-of').resource"
    )
    assert path_._where.value == "composed-of"
    assert path_._where.name is None
    assert path_._where.subpath == "resource"
    assert path_._where.type == WhereConstraintType.T3
Example #2
0
def test_type_path_element(engine):
    """ """
    path_ = ElementPath("Patient.name")
    path_.finalize(engine)
    path_ = path_ / "firstname"

    assert path_.path == "Patient.name.firstname"
Example #3
0
def test_path_constraint_as():
    """Condition.​abatement.​as(Range)
    Condition.​abatement.​as(dateTime)
    Condition.​abatement.​as(Period)
    """
    path_ = ElementPath("Condition.abatement.as(Range)")
    assert path_._path == "Condition.abatementRange"
    path_ = ElementPath("Condition.abatement.as(dateTime)")
    assert path_._path == "Condition.abatementDateTime"
Example #4
0
def test_path_constains_function():
    """ """
    path_ = ElementPath("Organization.address.first()")
    assert path_._path == "Organization.address"

    path_ = ElementPath("Organization.telecom.Take(1)")
    assert path_._path == "Organization.telecom"

    path_ = ElementPath("Organization.telecom.Skip(0)")
    assert path_._path == "Organization.telecom"
Example #5
0
    def extract_references(
            self, search_param: SearchParameter) -> Dict[str, List[str]]:
        """Takes a search parameter as input and extract all targeted references

        Returns a dict like:
        {"Patient": ["list", "of", "referenced", "patient", "ids"], "Observation": []}
        """
        if search_param.type != "reference":
            raise ValueError(
                "You cannot extract a reference for a search parameter "
                "that is not of type reference.")
        if not isinstance(search_param.expression, str):
            raise ValueError(
                f"'expression' is not defined for search parameter {search_param.name}"
            )

        ids: Dict = defaultdict(list)

        # use ElementPath to parse fhirpath expressions like .where()
        path_element = ElementPath(search_param.expression)

        def browse(node, path):
            parts = path.split(".", 1)

            if len(parts) == 0:
                return node
            elif parts[0] not in node:
                return None
            elif len(parts) == 1:
                return node[parts[0]]
            else:
                return browse(node[parts[0]], parts[1])

        def append_ref(ref_attr):
            # if the searchparam expression contains .where() statement, skip references
            # that do not match the required resource type
            if (path_element._where
                    and path_element._where.type == WhereConstraintType.T2):
                ref_target_type = ref_attr["reference"].split("/")[0]
                if path_element._where.value != ref_target_type:
                    return

            if "reference" not in ref_attr:
                return

            # FIXME: this does not work with references using absolute URLs
            referenced_resource, _id = ref_attr["reference"].split("/")
            ids[referenced_resource].append(_id)

        # remove the resource type from the path
        _, path = path_element._path.split(".", 1)
        for row in self.body:
            resource = row[0]
            ref_attribute = browse(resource, path)

            if ref_attribute is None:
                continue
            elif isinstance(ref_attribute, list):
                for r in ref_attribute:
                    append_ref(r)
            else:
                append_ref(ref_attribute)

        return ids
Example #6
0
def test_path_constains_index():
    """ """
    path_ = ElementPath("Organization.address[0].line[1]")
    assert path_._path == "Organization.address"
Example #7
0
def test_path_constraint_as_complex():
    """ """
    path_ = ElementPath("MedicationRequest.medication as CodeableConcept")
    assert path_._path == "MedicationRequest.medicationCodeableConcept"