Exemple #1
0
def _get_match_results_for_all_children(
        ingested_entity_tree: EntityTree, db_entity_trees: List[EntityTree],
        root_entity_cls) \
        -> List[Tuple[str, MatchResults]]:
    """Attempts to match all children of the |ingested_entity_tree| to children
    of the |db_entity_trees|. Matching for each child is independent and can
    match to different DB parents.

    Returns a list of tuples with the following values:
    - str: the string name of the child field
    - MatchResult: the result of matching this child field to children of the
        provided |db_entity_trees|
    """
    results = []
    ingested_entity = ingested_entity_tree.entity
    set_child_fields = get_set_entity_field_names(ingested_entity,
                                                  EntityFieldType.FORWARD_EDGE)

    for child_field_name in set_child_fields:
        ingested_child_field = get_field(ingested_entity, child_field_name)
        db_child_trees = generate_child_entity_trees(child_field_name,
                                                     db_entity_trees)
        if isinstance(ingested_child_field, list):
            ingested_child_list = ingested_child_field
        else:
            ingested_child_list = [ingested_child_field]

        ingested_child_trees = \
            ingested_entity_tree.generate_child_trees(ingested_child_list)
        match_results = _match_entity_trees(
            ingested_entity_trees=ingested_child_trees,
            db_entity_trees=db_child_trees,
            root_entity_cls=root_entity_cls)
        results.append((child_field_name, match_results))
    return results
    def test_generateChildEntitiesWithAncestorChain(self):
        fine = StateFine.new_with_defaults(fine_id=_ID)
        fine_another = StateFine.new_with_defaults(fine_id=_ID_2)
        person = StatePerson.new_with_defaults(person_id=_ID)
        sentence_group = StateSentenceGroup.new_with_defaults(
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            state_code=_STATE_CODE,
            fines=[fine, fine_another],
            person=[person],
            sentence_group_id=_ID)
        sentence_group_tree = EntityTree(entity=sentence_group,
                                         ancestor_chain=[person])

        expected_child_trees = [
            EntityTree(entity=fine, ancestor_chain=[person, sentence_group]),
            EntityTree(entity=fine_another,
                       ancestor_chain=[person, sentence_group]),
        ]

        self.assertEqual(
            expected_child_trees,
            generate_child_entity_trees('fines', [sentence_group_tree]))
    def test_generateChildEntitiesWithAncestorChain(self) -> None:
        fine = schema.StateFine(state_code=_STATE_CODE, fine_id=_ID)
        fine_another = schema.StateFine(state_code=_STATE_CODE, fine_id=_ID_2)
        person = schema.StatePerson(state_code=_STATE_CODE, person_id=_ID)
        sentence_group = schema.StateSentenceGroup(
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            state_code=_STATE_CODE,
            fines=[fine, fine_another],
            person=person,
            sentence_group_id=_ID,
        )
        sentence_group_tree = EntityTree(entity=sentence_group,
                                         ancestor_chain=[person])

        expected_child_trees = [
            EntityTree(entity=fine, ancestor_chain=[person, sentence_group]),
            EntityTree(entity=fine_another,
                       ancestor_chain=[person, sentence_group]),
        ]

        self.assertEqual(
            expected_child_trees,
            generate_child_entity_trees("fines", [sentence_group_tree]),
        )