Example #1
0
    def test_readPlaceholderPeople(self) -> None:
        placeholder_person = schema.StatePerson(person_id=1,
                                                state_code=_STATE_CODE)
        person = schema.StatePerson(person_id=2, state_code=_STATE_CODE)
        person_external_id = schema.StatePersonExternalId(
            person_external_id_id=1,
            external_id=_EXTERNAL_ID,
            id_type=external_id_types.US_ND_SID,
            state_code=_STATE_CODE,
            person=person,
        )
        person.external_ids = [person_external_id]

        session = SessionFactory.for_schema_base(StateBase)
        session.add(placeholder_person)
        session.add(person)
        session.commit()

        # Act
        people = dao.read_placeholder_persons(session, _STATE_CODE)

        # Assert
        expected_people = [placeholder_person]

        self.assertCountEqual(people, expected_people)
Example #2
0
def read_persons_by_root_entity_cls(
    session: Session,
    region: str,
    ingested_people: List[schema.StatePerson],
    allowed_root_entity_classes: Optional[List[Type[DatabaseEntity]]],
) -> List[schema.StatePerson]:
    """Looks up all people necessary for entity matching based on the provided
    |region| and |ingested_people|.

    If |allowed_root_entity_classes| is provided, throw an error if any
    unexpected root entity class is found.
    """
    root_entity_cls = get_root_entity_cls(ingested_people)
    if (allowed_root_entity_classes
            and root_entity_cls not in allowed_root_entity_classes):
        raise ValueError(
            f"For region [{region}] found unexpected root_entity_cls: [{root_entity_cls.__name__}]. "
            f"Allowed classes: [{allowed_root_entity_classes}]")
    root_external_ids = get_external_ids_of_cls(ingested_people,
                                                root_entity_cls)
    logging.info(
        "[Entity Matching] Reading [%s] external ids of class [%s]",
        len(root_external_ids),
        root_entity_cls.__name__,
    )
    persons_by_root_entity = dao.read_people_by_cls_external_ids(
        session, region, root_entity_cls, root_external_ids)
    placeholder_persons = dao.read_placeholder_persons(session, region)

    # When the |root_entity_cls| is not StatePerson, it is possible for both
    # persons_by_root_entity and placeholder_persons to contain the same
    # placeholder person(s). For this reason, we dedup people across both lists
    # before returning.
    deduped_people = []
    seen_person_ids: Set[int] = set()
    for person in persons_by_root_entity + placeholder_persons:
        if person.person_id not in seen_person_ids:
            deduped_people.append(person)
            seen_person_ids.add(person.person_id)

    return deduped_people