コード例 #1
0
def _move_incidents_onto_periods_helper(
        *,
        placeholder_periods: List[schema.StateIncarcerationPeriod],
        non_placeholder_periods: List[schema.StateIncarcerationPeriod]):
    """Moves all StateIncarcerationIncidents on any of the provided
    |placeholder_periods| onto periods in |non_placeholder_periods|, if a
    matching non-placeholder period exists.
    """
    for placeholder_period in placeholder_periods:
        incidents_to_remove = []
        for incident in placeholder_period.incarceration_incidents:
            match = _find_matching_period(
                incident, non_placeholder_periods)
            if match:
                incidents_to_remove.append((match, incident))

        # Remove incidents from placeholder parent after looping through all
        # incidents.
        for match_period, incident in incidents_to_remove:
            add_child_to_entity(
                entity=match_period,
                child_field_name='incarceration_incidents',
                child_to_add=incident)
            remove_child_from_entity(
                entity=placeholder_period,
                child_field_name='incarceration_incidents',
                child_to_remove=incident)
コード例 #2
0
def _replace_entity(*, entity: Entity, to_replace: Entity,
                    linked_parents: List[_LinkedParents]):
    """For all parent entities in |to_replace_parents|, replaces |to_replace|
    with |entity|.
    """
    for linked_parent in linked_parents:
        remove_child_from_entity(entity=linked_parent.parent,
                                 child_field_name=linked_parent.field_name,
                                 child_to_remove=to_replace)
        add_child_to_entity(entity=linked_parent.parent,
                            child_field_name=linked_parent.field_name,
                            child_to_add=entity)
コード例 #3
0
    def test_removeChildFromEntity(self):
        fine = StateFine.new_with_defaults(fine_id=_ID)
        fine_another = StateFine.new_with_defaults(fine_id=_ID_2)
        sentence_group = StateSentenceGroup.new_with_defaults(
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            state_code=_STATE_CODE,
            fines=[fine, fine_another],
            sentence_group_id=_ID)

        expected_sentence_group = attr.evolve(sentence_group, fines=[fine])
        remove_child_from_entity(entity=sentence_group,
                                 child_field_name='fines',
                                 child_to_remove=fine_another)
        self.assertEqual(expected_sentence_group, sentence_group)
コード例 #4
0
    def test_removeChildFromEntity(self) -> None:
        fine = schema.StateFine(state_code=_STATE_CODE, fine_id=_ID)
        fine_another = schema.StateFine(state_code=_STATE_CODE, fine_id=_ID_2)
        sentence_group = schema.StateSentenceGroup(
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            state_code=_STATE_CODE,
            fines=[fine, fine_another],
            sentence_group_id=_ID,
        )

        remove_child_from_entity(
            entity=sentence_group,
            child_field_name="fines",
            child_to_remove=fine_another,
        )
        self.assertEqual(sentence_group.fines, [fine])
コード例 #5
0
    def resolve_child_match_result():
        """Resolves any child matches by moving matched children off of their DB
        placeholder parent and onto the ingested, unmatched entity.
        """
        if not child_field_name or not child_match_result:
            raise EntityMatchingError(
                f"Expected child_field_name and child_match_result to be set, "
                f"but instead got {child_field_name} and {child_match_result} "
                f"respectively.",
                ingested_unmatched_entity_tree.entity.get_entity_name())

        # If child is unmatched, keep track of unchanged child
        if not child_match_result.merged_entity_trees:
            updated_child_trees.append(child_match_result.ingested_entity_tree)
        else:
            # For each matched child, remove child from the DB placeholder and
            # keep track of merged child(ren).
            for merged_child_tree in child_match_result.merged_entity_trees:
                updated_child_trees.append(merged_child_tree)
                placeholder_tree = merged_child_tree.generate_parent_tree()
                remove_child_from_entity(
                    entity=placeholder_tree.entity,
                    child_field_name=child_field_name,
                    child_to_remove=merged_child_tree.entity)

                # For now we only handle the case where all placeholders with
                # matched children have the same parent chain. If they do not,
                # we throw an error.
                if ancestor_chain_updated:
                    if ancestor_chain_updated != \
                            placeholder_tree.ancestor_chain:
                        raise EntityMatchingError(
                            f"Expected all placeholder DB entities matched to "
                            f"an ingested unmatched entity to have the same "
                            f"ancestor chain, but they did not. Found "
                            f"conflicting ancestor chains: "
                            f"{ancestor_chain_updated} and "
                            f"{placeholder_tree.ancestor_chain}",
                            ingested_entity.get_entity_name())
                else:
                    ancestor_chain_updated.extend(
                        placeholder_tree.ancestor_chain)