Ejemplo n.º 1
0
    def test_add_object_conflicting_external_id_no_flush(
            self, generate_func) -> None:
        # Arrange
        arrange_session = SessionFactory.for_schema_base(StateBase)

        db_external_id = generate_external_id(
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_1,
            id_type=self.ID_TYPE_1,
        )
        db_person = generate_person(state_code=self.state_code,
                                    external_ids=[db_external_id])
        db_object_new = generate_func(db_person,
                                      external_id=self.EXTERNAL_ID_1,
                                      state_code=self.state_code)

        arrange_session.add(db_object_new)
        arrange_session.commit()

        db_external_id = generate_external_id(
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_2,
            id_type=self.ID_TYPE_1,
        )
        db_person = generate_person(state_code=self.state_code,
                                    external_ids=[db_external_id])
        db_object_new = generate_func(db_person,
                                      external_id=self.EXTERNAL_ID_1,
                                      state_code=self.state_code)

        # Act
        session = SessionFactory.for_schema_base(StateBase)
        session.add(db_object_new)
        with self.assertRaises(sqlalchemy.exc.IntegrityError):
            session.commit()
Ejemplo n.º 2
0
    def test_add_person_conflicting_external_id_different_state(self) -> None:
        # Arrange
        arrange_session = SessionFactory.for_schema_base(StateBase)

        db_external_id = generate_external_id(
            state_code="OTHER_STATE_CODE",
            external_id=self.EXTERNAL_ID_1,
            id_type=self.ID_TYPE_1,
        )

        db_person = generate_person(state_code="OTHER_STATE_CODE",
                                    external_ids=[db_external_id])

        arrange_session.add(db_person)
        arrange_session.commit()

        db_external_id_duplicated = generate_external_id(
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_1,
            id_type=self.ID_TYPE_1,
        )

        db_person_new = generate_person(
            state_code=self.state_code,
            external_ids=[db_external_id_duplicated])

        # Act
        session = SessionFactory.for_schema_base(StateBase)

        session.add(db_person_new)
        session.flush()
        session.commit()
Ejemplo n.º 3
0
    def test_add_person_conflicting_external_id_same_session(self) -> None:
        # Arrange
        db_external_id = generate_external_id(
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_1,
            id_type=self.ID_TYPE_1,
        )

        db_person = generate_person(state_code=self.state_code,
                                    external_ids=[db_external_id])

        db_external_id_duplicated = generate_external_id(
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_1,
            id_type=self.ID_TYPE_1,
        )

        db_person_2 = generate_person(state_code=self.state_code,
                                      external_ids=[db_external_id_duplicated])

        # Act
        session = SessionFactory.for_schema_base(StateBase)

        session.add(db_person)
        session.add(db_person_2)
        session.flush()

        with self.assertRaises(sqlalchemy.exc.IntegrityError):
            session.commit()
Ejemplo n.º 4
0
    def test_add_object_conflicting_external_id_no_flush(
            self, generate_func) -> None:
        # Arrange
        with SessionFactory.using_database(
                self.database_key) as arrange_session:
            db_external_id = generate_external_id(
                state_code=self.state_code,
                external_id=self.EXTERNAL_ID_1,
                id_type=self.ID_TYPE_1,
            )
            db_person = generate_person(state_code=self.state_code,
                                        external_ids=[db_external_id])
            db_object_new = generate_func(db_person,
                                          external_id=self.EXTERNAL_ID_1,
                                          state_code=self.state_code)

            arrange_session.add(db_object_new)

        db_external_id = generate_external_id(
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_2,
            id_type=self.ID_TYPE_1,
        )
        db_person = generate_person(state_code=self.state_code,
                                    external_ids=[db_external_id])
        db_object_new = generate_func(db_person,
                                      external_id=self.EXTERNAL_ID_1,
                                      state_code=self.state_code)

        # Act
        with SessionFactory.using_database(self.database_key,
                                           autocommit=False) as session:
            session.add(db_object_new)
            with self.assertRaises(sqlalchemy.exc.IntegrityError):
                session.commit()
Ejemplo n.º 5
0
    def test_add_person_conflicting_external_id_different_state(self) -> None:
        # Arrange
        with SessionFactory.using_database(
                self.database_key, autocommit=False) as arrange_session:
            db_external_id = generate_external_id(
                state_code="OTHER_STATE_CODE",
                external_id=self.EXTERNAL_ID_1,
                id_type=self.ID_TYPE_1,
            )

            db_person = generate_person(state_code="OTHER_STATE_CODE",
                                        external_ids=[db_external_id])

            arrange_session.add(db_person)
            arrange_session.commit()

        db_external_id_duplicated = generate_external_id(
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_1,
            id_type=self.ID_TYPE_1,
        )

        db_person_new = generate_person(
            state_code=self.state_code,
            external_ids=[db_external_id_duplicated])

        # Act
        with SessionFactory.using_database(self.database_key) as session:
            session.add(db_person_new)
            session.flush()
    def test_add_two_people_same_id_type(self) -> None:
        # Arrange
        db_external_id = generate_external_id(state_code=self.state_code,
                                              external_id=EXTERNAL_ID_1,
                                              id_type=ID_TYPE_1)

        db_person = generate_person(state_code=self.state_code,
                                    external_ids=[db_external_id])

        db_external_id_2 = generate_external_id(state_code=self.state_code,
                                                external_id=EXTERNAL_ID_2,
                                                id_type=ID_TYPE_1)

        db_person_2 = generate_person(state_code=self.state_code,
                                      external_ids=[db_external_id_2])

        # Act
        session = SessionFactory.for_schema_base(StateBase)

        session.add(db_person)
        session.add(db_person_2)
        session.flush()

        output_people = [db_person, db_person_2]

        errors = validate_invariants(session, self.system_level,
                                     self.state_code, output_people)

        # Assert
        self.assertEqual(0, errors)
        session.commit()
Ejemplo n.º 7
0
    def test_add_person_two_ids_same_type_us_pa(self) -> None:
        with SessionFactory.using_database(
            self.database_key, autocommit=False
        ) as session:
            # Arrange
            self.state_code = "US_PA"

            db_external_id = generate_external_id(
                state_code=self.state_code, external_id=EXTERNAL_ID_1, id_type=ID_TYPE_1
            )

            db_external_id_2 = generate_external_id(
                state_code=self.state_code, external_id=EXTERNAL_ID_2, id_type=ID_TYPE_1
            )

            db_person = generate_person(
                state_code=self.state_code,
                external_ids=[db_external_id, db_external_id_2],
            )

            session.add(db_person)
            session.flush()

            output_people = [db_person]

            # Act
            errors = validate_invariants(
                session, self.system_level, self.state_code, output_people
            )

            # Assert
            self.assertEqual(0, errors)
Ejemplo n.º 8
0
    def test_add_person_simple_no_flush(self) -> None:
        # Arrange
        session = SessionFactory.for_schema_base(StateBase)

        db_external_id = generate_external_id(state_code=self.state_code,
                                              external_id=EXTERNAL_ID_1,
                                              id_type=ID_TYPE_1)

        db_person = generate_person(state_code=self.state_code,
                                    external_ids=[db_external_id])

        session.add(db_person)

        output_people = [db_person]

        # Act
        with self.assertRaises(SessionIsDirtyError) as e:
            _ = validate_invariants(session, self.system_level,
                                    self.state_code, output_people)

        # Assert
        self.assertEqual(
            str(e.exception),
            "Session unexpectedly dirty - flush before querying the database.",
        )
    def test_addSupervisingOfficerToOpenSupervisionPeriods(self):
        # Arrange
        supervising_officer = generate_agent(agent_id=_ID,
                                             external_id=_EXTERNAL_ID,
                                             state_code=_STATE_CODE)
        person = generate_person(person_id=_ID,
                                 supervising_officer=supervising_officer)
        external_id = generate_external_id(person_external_id_id=_ID,
                                           external_id=_EXTERNAL_ID,
                                           state_code=_STATE_CODE,
                                           id_type=_ID_TYPE)
        open_supervision_period = generate_supervision_period(
            person=person,
            supervision_period_id=_ID,
            external_id=_EXTERNAL_ID,
            start_date=_DATE_1,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO.value,
            state_code=_STATE_CODE)
        placeholder_supervision_period = generate_supervision_period(
            person=person,
            supervision_period_id=_ID_2,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO.value,
            state_code=_STATE_CODE)
        closed_supervision_period = generate_supervision_period(
            person=person,
            supervision_period_id=_ID_3,
            external_id=_EXTERNAL_ID_3,
            start_date=_DATE_3,
            termination_date=_DATE_4,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO.value,
            state_code=_STATE_CODE)
        supervision_sentence = generate_supervision_sentence(
            person=person,
            state_code=_STATE_CODE,
            external_id=_EXTERNAL_ID,
            supervision_sentence_id=_ID,
            supervision_periods=[
                open_supervision_period, placeholder_supervision_period,
                closed_supervision_period
            ])
        sentence_group = generate_sentence_group(
            external_id=_EXTERNAL_ID,
            state_code=_STATE_CODE,
            sentence_group_id=_ID,
            supervision_sentences=[supervision_sentence])
        person.external_ids = [external_id]
        person.sentence_groups = [sentence_group]

        # Act
        add_supervising_officer_to_open_supervision_periods([person])

        # Assert
        self.assertEqual(open_supervision_period.supervising_officer,
                         supervising_officer)
        self.assertIsNone(placeholder_supervision_period.supervising_officer,
                          supervising_officer)
        self.assertIsNone(closed_supervision_period.supervising_officer,
                          supervising_officer)
Ejemplo n.º 10
0
    def test_add_object_diff_external_id_same_state(self,
                                                    generate_func) -> None:
        # Arrange
        arrange_session = SessionFactory.for_schema_base(StateBase)

        db_external_id = generate_external_id(
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_2,
            id_type=self.ID_TYPE_1,
        )

        db_person = generate_person(state_code=self.state_code,
                                    external_ids=[db_external_id])

        db_object = generate_func(
            db_person,
            external_id=self.EXTERNAL_ID_2,
            state_code=self.state_code,
        )
        arrange_session.add(db_object)
        arrange_session.commit()

        db_external_id_duplicated = generate_external_id(
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_1,
            id_type=self.ID_TYPE_1,
        )

        db_person_new = generate_person(
            state_code=self.state_code,
            external_ids=[db_external_id_duplicated])
        db_object_new = generate_func(
            db_person_new,
            external_id=self.EXTERNAL_ID_1,
            state_code=self.state_code,
        )

        # Act
        session = SessionFactory.for_schema_base(StateBase)

        session.add(db_object_new)
        session.flush()
        session.commit()
Ejemplo n.º 11
0
    def test_add_object_diff_external_id_same_state(self,
                                                    generate_func) -> None:
        # Arrange
        with SessionFactory.using_database(
                self.database_key) as arrange_session:
            db_external_id = generate_external_id(
                state_code=self.state_code,
                external_id=self.EXTERNAL_ID_2,
                id_type=self.ID_TYPE_1,
            )

            db_person = generate_person(state_code=self.state_code,
                                        external_ids=[db_external_id])

            db_object = generate_func(
                db_person,
                external_id=self.EXTERNAL_ID_2,
                state_code=self.state_code,
            )
            arrange_session.add(db_object)

        db_external_id_duplicated = generate_external_id(
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_1,
            id_type=self.ID_TYPE_1,
        )

        db_person_new = generate_person(
            state_code=self.state_code,
            external_ids=[db_external_id_duplicated])
        db_object_new = generate_func(
            db_person_new,
            external_id=self.EXTERNAL_ID_1,
            state_code=self.state_code,
        )

        # Act
        with SessionFactory.using_database(self.database_key) as session:
            session.add(db_object_new)
            session.flush()
Ejemplo n.º 12
0
    def test_add_person_update_with_new_id(self) -> None:
        with SessionFactory.using_database(
            self.database_key, autocommit=False
        ) as arrange_session:
            # Arrange
            db_external_id = generate_external_id(
                state_code=self.state_code, external_id=EXTERNAL_ID_1, id_type=ID_TYPE_1
            )

            db_person = generate_person(
                state_code=self.state_code, external_ids=[db_external_id]
            )

            arrange_session.add(db_person)
            arrange_session.commit()

            db_external_id_2 = generate_external_id(
                state_code=self.state_code, external_id=EXTERNAL_ID_2, id_type=ID_TYPE_1
            )

        # Act
        with SessionFactory.using_database(
            self.database_key, autocommit=False
        ) as session:
            result = session.query(schema.StatePerson).all()

            person_to_update = one(result)

            person_to_update.external_ids.append(db_external_id_2)
            session.flush()

            output_people = [person_to_update]

            errors = validate_invariants(
                session, self.system_level, self.state_code, output_people
            )

            # Assert
            self.assertEqual(1, errors)
Ejemplo n.º 13
0
    def test_ssvrFlatFieldMatchingRevocationTypeChanges(self) -> None:
        db_person = generate_person(state_code=_US_MO)
        db_supervision_violation_response = generate_supervision_violation_response(
            person=db_person,
            state_code=_US_MO,
            response_type=StateSupervisionViolationResponseType.
            PERMANENT_DECISION.value,
            response_type_raw_text=StateSupervisionViolationResponseType.
            PERMANENT_DECISION.value,
            deciding_body_type=StateSupervisionViolationResponseDecidingBodyType
            .PAROLE_BOARD.value,
            deciding_body_type_raw_text=
            StateSupervisionViolationResponseDecidingBodyType.PAROLE_BOARD.
            value,
            revocation_type=StateSupervisionViolationResponseRevocationType.
            REINCARCERATION.value,
            revocation_type_raw_text="S",
        )
        db_incarceration_period = generate_incarceration_period(
            person=db_person,
            state_code=_US_MO,
            external_id=_EXTERNAL_ID,
            admission_reason=StateIncarcerationPeriodAdmissionReason.
            PAROLE_REVOCATION.value,
            source_supervision_violation_response=
            db_supervision_violation_response,
        )
        db_incarceration_sentence = generate_incarceration_sentence(
            person=db_person,
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value,
            state_code=_US_MO,
            external_id=_EXTERNAL_ID,
            incarceration_periods=[db_incarceration_period],
        )
        db_sentence_group = generate_sentence_group(
            person=db_person,
            state_code=_US_MO,
            external_id=_EXTERNAL_ID_WITH_SUFFIX,
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value,
            incarceration_sentences=[db_incarceration_sentence],
            supervision_sentences=[],
        )
        db_external_id = generate_external_id(
            person=db_person,
            state_code=_US_MO,
            id_type=_ID_TYPE,
            external_id=_EXTERNAL_ID,
        )
        db_person.external_ids = [db_external_id]
        db_person.sentence_groups = [db_sentence_group]

        self._commit_to_db(db_person)

        # Even though the revocation type has changed, we still allow a match
        supervision_violation_response = StateSupervisionViolationResponse.new_with_defaults(
            state_code=_US_MO,
            response_type=StateSupervisionViolationResponseType.
            PERMANENT_DECISION,
            response_type_raw_text=StateSupervisionViolationResponseType.
            PERMANENT_DECISION.value,
            deciding_body_type=StateSupervisionViolationResponseDecidingBodyType
            .PAROLE_BOARD,
            deciding_body_type_raw_text=
            StateSupervisionViolationResponseDecidingBodyType.PAROLE_BOARD.
            value,
            revocation_type=StateSupervisionViolationResponseRevocationType.
            TREATMENT_IN_PRISON,
            revocation_type_raw_text="L",
        )
        incarceration_period = StateIncarcerationPeriod.new_with_defaults(
            state_code=_US_MO,
            external_id=_EXTERNAL_ID,
            admission_reason=StateIncarcerationPeriodAdmissionReason.
            PAROLE_REVOCATION,
            source_supervision_violation_response=
            supervision_violation_response,
            status=StateIncarcerationPeriodStatus.PRESENT_WITHOUT_INFO,
        )
        incarceration_sentence = StateIncarcerationSentence.new_with_defaults(
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            state_code=_US_MO,
            external_id=_EXTERNAL_ID,
            incarceration_periods=[incarceration_period],
        )
        sentence_group = StateSentenceGroup.new_with_defaults(
            state_code=_US_MO,
            external_id=_EXTERNAL_ID_WITH_SUFFIX,
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            incarceration_sentences=[incarceration_sentence],
            supervision_sentences=[],
        )
        external_id = StatePersonExternalId.new_with_defaults(
            state_code=_US_MO, id_type=_ID_TYPE, external_id=_EXTERNAL_ID)
        person = StatePerson.new_with_defaults(
            sentence_groups=[sentence_group],
            external_ids=[external_id],
            state_code=_US_MO,
        )

        expected_person = self.to_entity(db_person)
        expected_ip = (expected_person.sentence_groups[0].
                       incarceration_sentences[0].incarceration_periods[0])
        expected_ssvr = expected_ip.source_supervision_violation_response
        expected_ssvr.revocation_type = (
            StateSupervisionViolationResponseRevocationType.TREATMENT_IN_PRISON
        )
        expected_ssvr.revocation_type_raw_text = "L"

        # Act 1 - Match
        session = self._session()
        matched_entities = entity_matching.match(session,
                                                 _US_MO,
                                                 ingested_people=[person])

        self.assert_people_match_pre_and_post_commit([expected_person],
                                                     matched_entities.people,
                                                     session)
        self.assertEqual(0, matched_entities.error_count)
        self.assertEqual(1, matched_entities.total_root_entities)
Ejemplo n.º 14
0
    def test_setCurrentSupervisingOfficerFromSupervision_periods(self) -> None:
        # Arrange
        person = generate_person(person_id=_ID)
        external_id = generate_external_id(
            person_external_id_id=_ID,
            external_id=_EXTERNAL_ID,
            state_code=_STATE_CODE,
            id_type=_ID_TYPE,
        )

        supervising_officer = generate_agent(agent_id=_ID,
                                             external_id=_EXTERNAL_ID,
                                             state_code=_STATE_CODE)
        supervising_officer_2 = generate_agent(agent_id=_ID_2,
                                               external_id=_EXTERNAL_ID_2,
                                               state_code=_STATE_CODE)

        open_supervision_period = generate_supervision_period(
            person=person,
            supervision_period_id=_ID,
            external_id=_EXTERNAL_ID,
            start_date=_DATE,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO.value,
            state_code=_STATE_CODE,
            supervising_officer=supervising_officer_2,
        )
        placeholder_supervision_period = generate_supervision_period(
            person=person,
            supervision_period_id=_ID_2,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO.value,
            state_code=_STATE_CODE,
        )
        closed_supervision_period = generate_supervision_period(
            person=person,
            supervision_period_id=_ID_3,
            external_id=_EXTERNAL_ID_3,
            start_date=_DATE_3,
            termination_date=_DATE_4,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO.value,
            state_code=_STATE_CODE,
            supervising_officer=supervising_officer,
        )
        supervision_sentence = generate_supervision_sentence(
            person=person,
            state_code=_STATE_CODE,
            external_id=_EXTERNAL_ID,
            supervision_sentence_id=_ID,
            supervision_periods=[
                open_supervision_period,
                placeholder_supervision_period,
                closed_supervision_period,
            ],
        )
        sentence_group = generate_sentence_group(
            external_id=_EXTERNAL_ID,
            state_code=_STATE_CODE,
            sentence_group_id=_ID,
            supervision_sentences=[supervision_sentence],
        )
        person.external_ids = [external_id]
        person.sentence_groups = [sentence_group]

        # Act
        set_current_supervising_officer_from_supervision_periods([person])

        # Assert
        self.assertEqual(closed_supervision_period.supervising_officer,
                         supervising_officer)
        self.assertEqual(open_supervision_period.supervising_officer,
                         supervising_officer_2)
        self.assertIsNone(placeholder_supervision_period.supervising_officer)
        self.assertEqual(person.supervising_officer, supervising_officer_2)
Ejemplo n.º 15
0
    def test_ssvrFlatFieldMatchingWithSomeNullValues(self) -> None:
        db_person = generate_person(state_code=_US_MO)
        db_supervision_violation_response = generate_supervision_violation_response(
            person=db_person,
            state_code=_US_MO,
            response_type=StateSupervisionViolationResponseType.
            PERMANENT_DECISION.value,
            response_type_raw_text=StateSupervisionViolationResponseType.
            PERMANENT_DECISION.value,
            deciding_body_type=StateSupervisionViolationResponseDecidingBodyType
            .PAROLE_BOARD.value,
            deciding_body_type_raw_text=
            StateSupervisionViolationResponseDecidingBodyType.PAROLE_BOARD.
            value,
        )
        db_incarceration_period = generate_incarceration_period(
            person=db_person,
            state_code=_US_MO,
            external_id=_EXTERNAL_ID,
            admission_reason=StateIncarcerationPeriodAdmissionReason.
            PAROLE_REVOCATION.value,
            source_supervision_violation_response=
            db_supervision_violation_response,
        )
        db_incarceration_sentence = generate_incarceration_sentence(
            person=db_person,
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value,
            state_code=_US_MO,
            external_id=_EXTERNAL_ID,
            incarceration_periods=[db_incarceration_period],
        )
        db_sentence_group = generate_sentence_group(
            person=db_person,
            state_code=_US_MO,
            external_id=_EXTERNAL_ID_WITH_SUFFIX,
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value,
            incarceration_sentences=[db_incarceration_sentence],
            supervision_sentences=[],
        )
        db_external_id = generate_external_id(
            person=db_person,
            state_code=_US_MO,
            id_type=_ID_TYPE,
            external_id=_EXTERNAL_ID,
        )
        db_person.external_ids = [db_external_id]
        db_person.sentence_groups = [db_sentence_group]

        self._commit_to_db(db_person)

        # Even though this violation response doesn't have a deciding_body_type set, it will not clear the values in
        # db_supervision_violation_response.
        supervision_violation_response = StateSupervisionViolationResponse.new_with_defaults(
            state_code=_US_MO,
            response_type=StateSupervisionViolationResponseType.
            PERMANENT_DECISION,
            response_type_raw_text=StateSupervisionViolationResponseType.
            PERMANENT_DECISION.value,
        )
        incarceration_period = StateIncarcerationPeriod.new_with_defaults(
            state_code=_US_MO,
            external_id=_EXTERNAL_ID,
            admission_reason=StateIncarcerationPeriodAdmissionReason.
            PAROLE_REVOCATION,
            source_supervision_violation_response=
            supervision_violation_response,
            status=StateIncarcerationPeriodStatus.PRESENT_WITHOUT_INFO,
        )
        incarceration_sentence = StateIncarcerationSentence.new_with_defaults(
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            state_code=_US_MO,
            external_id=_EXTERNAL_ID,
            incarceration_periods=[incarceration_period],
        )
        sentence_group = StateSentenceGroup.new_with_defaults(
            state_code=_US_MO,
            external_id=_EXTERNAL_ID_WITH_SUFFIX,
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            incarceration_sentences=[incarceration_sentence],
            supervision_sentences=[],
        )
        external_id = StatePersonExternalId.new_with_defaults(
            state_code=_US_MO, id_type=_ID_TYPE, external_id=_EXTERNAL_ID)
        person = StatePerson.new_with_defaults(
            sentence_groups=[sentence_group],
            external_ids=[external_id],
            state_code=_US_MO,
        )

        expected_person = self.to_entity(db_person)

        # Act 1 - Match
        session = self._session()
        matched_entities = entity_matching.match(session,
                                                 _US_MO,
                                                 ingested_people=[person])

        self.assert_people_match_pre_and_post_commit([expected_person],
                                                     matched_entities.people,
                                                     session)
        self.assertEqual(0, matched_entities.error_count)
        self.assertEqual(1, matched_entities.total_root_entities)
Ejemplo n.º 16
0
    def test_supervisionViolationsWithDifferentParents_mergesViolations(self):
        db_person = generate_person(person_id=_ID, full_name=_FULL_NAME)
        db_supervision_violation = generate_supervision_violation(
            person=db_person,
            state_code=_US_MO,
            supervision_violation_id=_ID,
            external_id=_EXTERNAL_ID)
        db_placeholder_supervision_period = generate_supervision_period(
            person=db_person,
            state_code=_US_MO,
            supervision_period_id=_ID,
            supervision_violation_entries=[db_supervision_violation])
        db_incarceration_sentence = generate_incarceration_sentence(
            person=db_person,
            state_code=_US_MO,
            incarceration_sentence_id=_ID,
            external_id=_EXTERNAL_ID,
            supervision_periods=[db_placeholder_supervision_period])
        db_sentence_group = generate_sentence_group(
            person=db_person,
            state_code=_US_MO,
            sentence_group_id=_ID,
            external_id=_EXTERNAL_ID,
            incarceration_sentences=[db_incarceration_sentence])
        db_external_id = generate_external_id(person=db_person,
                                              person_external_id_id=_ID,
                                              state_code=_US_MO,
                                              external_id=_EXTERNAL_ID)
        db_person.sentence_groups = [db_sentence_group]
        db_person.external_ids = [db_external_id]

        self._commit_to_db(db_person)

        supervision_violation = attr.evolve(
            self.to_entity(db_supervision_violation),
            supervision_violation_id=None,
            external_id=_EXTERNAL_ID_WITH_SUFFIX)
        placeholder_supervision_period = attr.evolve(
            self.to_entity(db_placeholder_supervision_period),
            supervision_period_id=None,
            supervision_violation_entries=[supervision_violation])
        supervision_sentence = StateSupervisionSentence.new_with_defaults(
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            state_code=_US_MO,
            external_id=_EXTERNAL_ID,
            supervision_periods=[placeholder_supervision_period])
        sentence_group = attr.evolve(
            self.to_entity(db_sentence_group),
            sentence_group_id=None,
            incarceration_sentences=[],
            supervision_sentences=[supervision_sentence])
        external_id = attr.evolve(self.to_entity(db_external_id),
                                  person_external_id_id=None)
        person = attr.evolve(self.to_entity(db_person),
                             person_id=None,
                             sentence_groups=[sentence_group],
                             external_ids=[external_id])

        expected_supervision_violation = attr.evolve(
            self.to_entity(db_supervision_violation))
        expected_placeholder_supervision_period_is = attr.evolve(
            placeholder_supervision_period,
            supervision_violation_entries=[expected_supervision_violation])
        expected_placeholder_supervision_period_ss = attr.evolve(
            self.to_entity(db_placeholder_supervision_period),
            supervision_violation_entries=[expected_supervision_violation])
        expected_supervision_sentence = attr.evolve(
            supervision_sentence,
            supervision_periods=[expected_placeholder_supervision_period_ss])
        expected_incarceration_sentence = attr.evolve(
            self.to_entity(db_incarceration_sentence),
            supervision_periods=[expected_placeholder_supervision_period_is])
        expected_sentence_group = attr.evolve(
            self.to_entity(db_sentence_group),
            incarceration_sentences=[expected_incarceration_sentence],
            supervision_sentences=[expected_supervision_sentence])
        expected_external_id = attr.evolve(self.to_entity(db_external_id))
        expected_person = attr.evolve(
            self.to_entity(db_person),
            external_ids=[expected_external_id],
            sentence_groups=[expected_sentence_group])
        # Act 1 - Match
        session = self._session()
        matched_entities = entity_matching.match(session,
                                                 _US_MO,
                                                 ingested_people=[person])

        self.assert_people_match_pre_and_post_commit([expected_person],
                                                     matched_entities.people,
                                                     session)
        self.assertEqual(0, matched_entities.error_count)
        self.assertEqual(1, matched_entities.total_root_entities)
Ejemplo n.º 17
0
    def test_runMatch_supervisionPeriodDateChangesSoItDoesNotMatchSentenceOrViolations(
            self):
        # Arrange
        db_supervising_officer = generate_agent(
            agent_id=_ID,
            external_id=_EXTERNAL_ID,
            state_code=_US_MO,
            agent_type=StateAgentType.SUPERVISION_OFFICER.value)
        db_person = generate_person(person_id=_ID,
                                    supervising_officer=db_supervising_officer)
        db_external_id = generate_external_id(person_external_id_id=_ID,
                                              external_id=_EXTERNAL_ID,
                                              state_code=_US_MO,
                                              id_type=_ID_TYPE)

        # Violation has been date matched to the open supervision period
        db_supervision_violation = generate_supervision_violation(
            person=db_person,
            state_code=_US_MO,
            supervision_violation_id=_ID,
            external_id=_EXTERNAL_ID,
            violation_date=_DATE_4)

        db_supervision_period_open = generate_supervision_period(
            person=db_person,
            supervision_period_id=_ID_2,
            external_id=_EXTERNAL_ID_2,
            start_date=_DATE_2,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO.value,
            state_code=_US_MO,
            supervising_officer=db_supervising_officer,
            supervision_violation_entries=[db_supervision_violation])
        db_supervision_sentence = generate_supervision_sentence(
            person=db_person,
            external_id=_EXTERNAL_ID,
            supervision_sentence_id=_ID,
            state_code=_US_MO,
            start_date=_DATE_1,
            supervision_periods=[db_supervision_period_open])
        db_sentence_group = generate_sentence_group(
            external_id=_EXTERNAL_ID,
            sentence_group_id=_ID,
            state_code=_US_MO,
            supervision_sentences=[db_supervision_sentence])
        db_person.external_ids = [db_external_id]
        db_person.sentence_groups = [db_sentence_group]
        self._commit_to_db(db_person)

        supervsion_period_updated = StateSupervisionPeriod.new_with_defaults(
            state_code=_US_MO,
            external_id=db_supervision_period_open.external_id,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO,
            start_date=_DATE_2,
            termination_date=_DATE_3,
        )

        placeholder_supervision_sentence = StateSupervisionSentence.new_with_defaults(
            state_code=_US_MO, supervision_periods=[supervsion_period_updated])
        sentence_group = StateSentenceGroup.new_with_defaults(
            external_id=db_sentence_group.external_id,
            state_code=_US_MO,
            supervision_sentences=[placeholder_supervision_sentence])
        external_id = StatePersonExternalId.new_with_defaults(
            external_id=db_external_id.external_id,
            state_code=_US_MO,
            id_type=db_external_id.id_type)
        person = StatePerson.new_with_defaults(
            external_ids=[external_id], sentence_groups=[sentence_group])

        expected_person = attr.evolve(self.to_entity(db_person))
        expected_sentence = expected_person.sentence_groups[
            0].supervision_sentences[0]
        expected_original_supervision_period = expected_sentence.supervision_periods[
            0]

        # Violation is moved off of the supervision period (it no longer matches) and the termination date is updated
        expected_original_supervision_period.supervision_violation_entries = []
        expected_original_supervision_period.termination_date = _DATE_3

        # A placeholder periods is created to hold the existing supervision violation
        expected_new_placeholder_supervision_period = StateSupervisionPeriod.new_with_defaults(
            state_code=_US_MO,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO,
            supervision_violation_entries=[
                self.to_entity(db_supervision_violation)
            ])
        expected_sentence.supervision_periods.append(
            expected_new_placeholder_supervision_period)

        # Act
        session = self._session()
        matched_entities = entity_matching.match(session,
                                                 _US_MO,
                                                 ingested_people=[person])

        # Assert
        self.assert_people_match_pre_and_post_commit([expected_person],
                                                     matched_entities.people,
                                                     session,
                                                     debug=True)
        self.assert_no_errors(matched_entities)
        self.assertEqual(1, matched_entities.total_root_entities)
Ejemplo n.º 18
0
    def test_runMatch_supervisingOfficerMovedFromSupervisionPeriodToPerson(
            self):
        # Arrange
        db_supervising_officer = generate_agent(
            agent_id=_ID,
            external_id=_EXTERNAL_ID,
            state_code=_US_MO,
            agent_type=StateAgentType.SUPERVISION_OFFICER.value)
        db_person = generate_person(person_id=_ID,
                                    supervising_officer=db_supervising_officer)
        db_external_id = generate_external_id(person_external_id_id=_ID,
                                              external_id=_EXTERNAL_ID,
                                              state_code=_US_MO,
                                              id_type=_ID_TYPE)
        db_supervision_period = generate_supervision_period(
            person=db_person,
            supervision_period_id=_ID,
            external_id=_EXTERNAL_ID,
            start_date=_DATE_1,
            termination_date=_DATE_2,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO.value,
            state_code=_US_MO,
            supervising_officer=db_supervising_officer)
        db_supervision_period_open = generate_supervision_period(
            person=db_person,
            supervision_period_id=_ID_2,
            external_id=_EXTERNAL_ID_2,
            start_date=_DATE_2,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO.value,
            state_code=_US_MO,
            supervising_officer=db_supervising_officer)
        db_supervision_sentence = generate_supervision_sentence(
            person=db_person,
            external_id=_EXTERNAL_ID,
            supervision_sentence_id=_ID,
            state_code=_US_MO,
            start_date=_DATE_1,
            supervision_periods=[
                db_supervision_period, db_supervision_period_open
            ])
        db_sentence_group = generate_sentence_group(
            external_id=_EXTERNAL_ID,
            sentence_group_id=_ID,
            state_code=_US_MO,
            supervision_sentences=[db_supervision_sentence])
        db_person.external_ids = [db_external_id]
        db_person.sentence_groups = [db_sentence_group]
        self._commit_to_db(db_person)

        new_supervising_officer = StateAgent.new_with_defaults(
            external_id=_EXTERNAL_ID_2,
            state_code=_US_MO,
            agent_type=StateAgentType.SUPERVISION_OFFICER)

        new_supervision_period = StateSupervisionPeriod.new_with_defaults(
            external_id=_EXTERNAL_ID_3,
            state_code=_US_MO,
            start_date=_DATE_3,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO,
            supervising_officer=new_supervising_officer)
        supervision_period_update = StateSupervisionPeriod.new_with_defaults(
            external_id=db_supervision_period_open.external_id,
            state_code=_US_MO,
            termination_date=_DATE_3)
        supervision_sentence = StateSupervisionSentence.new_with_defaults(
            external_id=db_supervision_sentence.external_id,
            state_code=_US_MO,
            supervision_periods=[
                supervision_period_update, new_supervision_period
            ])
        sentence_group = StateSentenceGroup.new_with_defaults(
            external_id=db_sentence_group.external_id,
            state_code=_US_MO,
            supervision_sentences=[supervision_sentence])

        external_id = attr.evolve(self.to_entity(db_external_id),
                                  person_external_id_id=None)
        person = StatePerson.new_with_defaults(
            external_ids=[external_id], sentence_groups=[sentence_group])

        expected_person = attr.evolve(self.to_entity(db_person))
        expected_person.supervising_officer = new_supervising_officer
        expected_supervision_sentence = \
            expected_person.sentence_groups[0].supervision_sentences[0]

        expected_unchanged_supervision_period = \
            attr.evolve(self.to_entity(db_supervision_period))
        expected_updated_supervision_period = \
            attr.evolve(self.to_entity(db_supervision_period_open),
                        termination_date=
                        supervision_period_update.termination_date,
                        supervising_officer=
                        expected_unchanged_supervision_period.
                        supervising_officer)
        expected_supervision_sentence.supervision_periods = [
            expected_unchanged_supervision_period,
            expected_updated_supervision_period, new_supervision_period
        ]

        # Act
        session = self._session()
        matched_entities = entity_matching.match(session,
                                                 _US_MO,
                                                 ingested_people=[person])

        # Assert
        self.assert_people_match_pre_and_post_commit([expected_person],
                                                     matched_entities.people,
                                                     session)
        self.assert_no_errors(matched_entities)
        self.assertEqual(1, matched_entities.total_root_entities)
Ejemplo n.º 19
0
    def test_runMatch_supervisingOfficerNotMovedFromPersonOntoOpenSupervisionPeriods(
            self):
        db_supervising_officer = generate_agent(agent_id=_ID,
                                                external_id=_EXTERNAL_ID,
                                                state_code=_US_MO)

        db_person = generate_person(person_id=_ID,
                                    supervising_officer=db_supervising_officer)
        db_external_id = generate_external_id(person_external_id_id=_ID,
                                              external_id=_EXTERNAL_ID,
                                              state_code=_US_MO,
                                              id_type=_ID_TYPE)
        db_supervision_period = generate_supervision_period(
            person=db_person,
            supervision_period_id=_ID,
            external_id=_EXTERNAL_ID,
            start_date=_DATE_1,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO.value,
            state_code=_US_MO,
            supervising_officer=db_supervising_officer)
        db_supervision_period_another = generate_supervision_period(
            person=db_person,
            supervision_period_id=_ID_2,
            external_id=_EXTERNAL_ID_2,
            start_date=_DATE_2,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO.value,
            state_code=_US_MO,
            supervising_officer=db_supervising_officer)
        db_closed_supervision_period = generate_supervision_period(
            person=db_person,
            supervision_period_id=_ID_3,
            external_id=_EXTERNAL_ID_3,
            start_date=_DATE_3,
            termination_date=_DATE_4,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO.value,
            state_code=_US_MO,
            supervising_officer=db_supervising_officer)
        db_supervision_sentence = generate_supervision_sentence(
            person=db_person,
            external_id=_EXTERNAL_ID,
            supervision_sentence_id=_ID,
            start_date=_DATE_1,
            supervision_periods=[
                db_supervision_period, db_supervision_period_another,
                db_closed_supervision_period
            ])
        db_sentence_group = generate_sentence_group(
            external_id=_EXTERNAL_ID,
            sentence_group_id=_ID,
            supervision_sentences=[db_supervision_sentence])
        db_person.external_ids = [db_external_id]
        db_person.sentence_groups = [db_sentence_group]
        self._commit_to_db(db_person)

        external_id = attr.evolve(self.to_entity(db_external_id),
                                  person_external_id_id=None)
        new_supervising_officer = StateAgent.new_with_defaults(
            external_id=_EXTERNAL_ID_2,
            state_code=_US_MO,
            agent_type=StateAgentType.SUPERVISION_OFFICER)
        person = StatePerson.new_with_defaults(
            external_ids=[external_id],
            supervising_officer=new_supervising_officer)

        expected_person = attr.evolve(self.to_entity(db_person))

        # Act 1 - Match
        session = self._session()
        matched_entities = entity_matching.match(session,
                                                 _US_MO,
                                                 ingested_people=[person])

        # Assert 1 - Match
        self.assert_people_match_pre_and_post_commit([expected_person],
                                                     matched_entities.people,
                                                     session)
        self.assert_no_errors(matched_entities)
        self.assertEqual(1, matched_entities.total_root_entities)