def test_getRootEntity_allPlaceholders_raises(self) -> None: placeholder_incarceration_period = schema.StateIncarcerationPeriod() placeholder_incarceration_sentence = schema.StateIncarcerationSentence( incarceration_periods=[placeholder_incarceration_period]) placeholder_sentence_group = schema.StateSentenceGroup( incarceration_sentences=[placeholder_incarceration_sentence]) placeholder_person = schema.StatePerson( sentence_groups=[placeholder_sentence_group]) with pytest.raises(EntityMatchingError): get_root_entity_cls([placeholder_person])
def test_getRootEntity_allPlaceholders_raises(self): placeholder_incarceration_period = \ StateIncarcerationPeriod.new_with_defaults() placeholder_incarceration_sentence = \ StateIncarcerationSentence.new_with_defaults( incarceration_periods=[placeholder_incarceration_period]) placeholder_sentence_group = StateSentenceGroup.new_with_defaults( incarceration_sentences=[placeholder_incarceration_sentence]) placeholder_person = StatePerson.new_with_defaults( sentence_groups=[placeholder_sentence_group]) with pytest.raises(EntityMatchingError): get_root_entity_cls([placeholder_person])
def _match_persons( *, ingested_persons: List[StatePerson], db_persons: List[StatePerson]) \ -> MatchedEntities: """Attempts to match all persons from |ingested_persons| with the provided |db_persons|. Results are returned in the MatchedEntities object which contains all successfully matched and merged persons as well as an error count that is incremented every time an error is raised matching an ingested person. """ db_person_trees = [ EntityTree(entity=db_person, ancestor_chain=[]) for db_person in db_persons ] ingested_person_trees = [ EntityTree(entity=ingested_person, ancestor_chain=[]) for ingested_person in ingested_persons ] root_entity_cls = get_root_entity_cls(ingested_persons) total_root_entities = get_total_entities_of_cls(ingested_persons, root_entity_cls) persons_match_results = _match_entity_trees( ingested_entity_trees=ingested_person_trees, db_entity_trees=db_person_trees, root_entity_cls=root_entity_cls) updated_persons = [] for match_result in persons_match_results.individual_match_results: if not match_result.merged_entity_trees: updated_persons.append(match_result.ingested_entity_tree.entity) else: # It is possible that multiple ingested people match to the same # DB person, in which case we should only keep one reference to # that object. for merged_person_tree in match_result.merged_entity_trees: if merged_person_tree.entity not in updated_persons: updated_persons.append(merged_person_tree.entity) # The only database persons that are unmatched that we potentially want to # update are placeholder persons. These may have had children removed as # a part of the matching process and therefore would need updating. for db_person in persons_match_results.unmatched_db_entities: if is_placeholder(db_person): updated_persons.append(db_person) return MatchedEntities(people=updated_persons, error_count=persons_match_results.error_count, total_root_entities=total_root_entities)
def test_getRootEntity(self): incarceration_incident = StateIncarcerationIncident.new_with_defaults( external_id=_EXTERNAL_ID) placeholder_incarceration_period = \ StateIncarcerationPeriod.new_with_defaults( incarceration_incidents=[incarceration_incident]) placeholder_incarceration_sentence = \ StateIncarcerationSentence.new_with_defaults( incarceration_periods=[placeholder_incarceration_period]) placeholder_sentence_group = StateSentenceGroup.new_with_defaults( sentence_group_id=None, incarceration_sentences=[placeholder_incarceration_sentence]) person = StatePerson.new_with_defaults( sentence_groups=[placeholder_sentence_group]) self.assertEqual(StateIncarcerationIncident, get_root_entity_cls([person]))
def test_getRootEntity(self) -> None: # Arrange incarceration_incident = schema.StateIncarcerationIncident( external_id=_EXTERNAL_ID) placeholder_incarceration_period = schema.StateIncarcerationPeriod( incarceration_incidents=[incarceration_incident]) placeholder_incarceration_sentence = schema.StateIncarcerationSentence( incarceration_periods=[placeholder_incarceration_period]) placeholder_sentence_group = schema.StateSentenceGroup( sentence_group_id=None, incarceration_sentences=[placeholder_incarceration_sentence], ) person = schema.StatePerson( sentence_groups=[placeholder_sentence_group]) # Act root_entity_cls = get_root_entity_cls([person]) # Assert self.assertEqual(schema.StateIncarcerationIncident, root_entity_cls)
def test_getRootEntity_emptyList_raises(self): with pytest.raises(EntityMatchingError): get_root_entity_cls([])