Example #1
0
    def test_associatedSupervisionPeriodsWithSentences(self):
        # Arrange
        sp_no_match = StateSupervisionPeriod.new_with_defaults(
            external_id=_EXTERNAL_ID,
            start_date=_DATE,
            termination_date=_DATE_2)

        sp_1 = StateSupervisionPeriod.new_with_defaults(
            external_id=_EXTERNAL_ID,
            start_date=_DATE_4,
            termination_date=_DATE_6)

        sp_2 = StateSupervisionPeriod.new_with_defaults(
            external_id=_EXTERNAL_ID_2,
            start_date=_DATE_6,
            termination_date=None)

        placeholder_ss = StateSupervisionSentence.new_with_defaults(
            supervision_periods=[sp_no_match, sp_1, sp_2])

        ss = StateSupervisionSentence.new_with_defaults(
            external_id=_EXTERNAL_ID, start_date=_DATE_4, completion_date=None)
        inc_s = StateIncarcerationSentence.new_with_defaults(
            external_id=_EXTERNAL_ID,
            start_date=_DATE_3,
            completion_date=_DATE_5)

        sg = StateSentenceGroup.new_with_defaults(
            incarceration_sentences=[inc_s],
            supervision_sentences=[ss, placeholder_ss])

        state_person = StatePerson.new_with_defaults(sentence_groups=[sg])

        expected_sp_1 = attr.evolve(sp_1)
        expected_sp_2 = attr.evolve(sp_2)

        expected_placeholder_ss = attr.evolve(
            placeholder_ss, supervision_periods=[sp_no_match])
        expected_inc_s = attr.evolve(inc_s,
                                     supervision_periods=[expected_sp_1])
        expected_ss = attr.evolve(
            ss, supervision_periods=[expected_sp_1, expected_sp_2])

        expected_sg = attr.evolve(
            sg,
            supervision_sentences=[expected_ss, expected_placeholder_ss],
            incarceration_sentences=[expected_inc_s])
        expected_person = attr.evolve(state_person,
                                      sentence_groups=[expected_sg])

        # Act
        input_people = \
            converter.convert_entity_people_to_schema_people(
                [state_person])
        move_supervision_periods_onto_sentences_by_date(input_people)

        # Assert
        self.assert_people_match([expected_person], input_people)
    def test_associatedPeriodsWithSentences_doNotAssociateToClosedButUnterminatedSentences(
            self):
        # Arrange
        sp_1 = StateSupervisionPeriod.new_with_defaults(
            external_id=_EXTERNAL_ID,
            start_date=_DATE_6,
            termination_date=None)

        ip_1 = StateIncarcerationPeriod.new_with_defaults(
            external_id=_EXTERNAL_ID,
            admission_date=_DATE_6,
            release_date=None)

        placeholder_ss = StateSupervisionSentence.new_with_defaults(
            supervision_periods=[sp_1], incarceration_periods=[ip_1])
        ss = StateSupervisionSentence.new_with_defaults(
            external_id=_EXTERNAL_ID,
            status=StateSentenceStatus.COMPLETED,
            start_date=_DATE_4,
            completion_date=None)
        inc_s = StateIncarcerationSentence.new_with_defaults(
            external_id=_EXTERNAL_ID,
            status=StateSentenceStatus.COMPLETED,
            start_date=_DATE_4,
            completion_date=None)

        sg = StateSentenceGroup.new_with_defaults(
            incarceration_sentences=[inc_s],
            supervision_sentences=[ss, placeholder_ss])

        state_person = StatePerson.new_with_defaults(sentence_groups=[sg])

        expected_sp_1 = attr.evolve(sp_1)
        expected_ip_1 = attr.evolve(ip_1)

        expected_placeholder_ss = attr.evolve(
            placeholder_ss,
            supervision_periods=[expected_sp_1],
            incarceration_periods=[expected_ip_1])
        expected_inc_s = attr.evolve(inc_s)
        expected_ss = attr.evolve(ss)
        expected_sg = attr.evolve(
            sg,
            supervision_sentences=[expected_ss, expected_placeholder_ss],
            incarceration_sentences=[expected_inc_s])
        expected_person = attr.evolve(state_person,
                                      sentence_groups=[expected_sg])

        # Act
        input_people = converter.convert_entity_people_to_schema_people(
            [state_person])
        move_periods_onto_sentences_by_date(input_people)

        # Assert
        self.assert_people_match([expected_person], input_people)
Example #3
0
    def test_getExternalIdsOfCls(self):
        supervision_sentence = StateSupervisionSentence.new_with_defaults(
            external_id=_EXTERNAL_ID)
        supervision_sentence_2 = attr.evolve(supervision_sentence,
                                             external_id=_EXTERNAL_ID_2)
        supervision_sentence_3 = attr.evolve(supervision_sentence,
                                             external_id=_EXTERNAL_ID_3)
        sentence_group = StateSentenceGroup.new_with_defaults(
            external_id=_EXTERNAL_ID,
            supervision_sentences=[
                supervision_sentence, supervision_sentence_2
            ])
        sentence_group_2 = StateSentenceGroup.new_with_defaults(
            external_id=_EXTERNAL_ID_2,
            supervision_sentences=[
                supervision_sentence_2, supervision_sentence_3
            ])
        external_id = StatePersonExternalId.new_with_defaults(
            external_id=_EXTERNAL_ID)
        person = StatePerson.new_with_defaults(
            external_ids=[external_id],
            sentence_groups=[sentence_group, sentence_group_2])

        self.assertCountEqual([_EXTERNAL_ID, _EXTERNAL_ID_2, _EXTERNAL_ID_3],
                              get_external_ids_of_cls(
                                  [person], StateSupervisionSentence))
        self.assertCountEqual([_EXTERNAL_ID, _EXTERNAL_ID_2],
                              get_external_ids_of_cls([person],
                                                      StateSentenceGroup))
        self.assertCountEqual([_EXTERNAL_ID],
                              get_external_ids_of_cls([person], StatePerson))
Example #4
0
    def test_usId_getPostIncarcerationSupervisionType_ignoreOutOfDatePeriods_After(
            self):
        incarceration_period = StateIncarcerationPeriod.new_with_defaults(
            incarceration_period_id=1,
            admission_reason=StateIncarcerationPeriodAdmissionReason.
            RETURN_FROM_SUPERVISION,
            external_id='ip1',
            state_code='US_ID',
            admission_date=date(2019, 9, 13),
            release_date=date(2020, 1, 4),
            release_reason=StateIncarcerationPeriodReleaseReason.
            CONDITIONAL_RELEASE)

        succeeding_supervision_period = StateSupervisionPeriod.new_with_defaults(
            supervision_period_id=1,
            start_date=incarceration_period.release_date + relativedelta(
                days=(INCARCERATION_SUPERVISION_TYPE_DAYS_LIMIT + 100)),
            supervision_period_supervision_type=
            StateSupervisionPeriodSupervisionType.PAROLE)

        supervision_sentence = StateSupervisionSentence.new_with_defaults(
            supervision_sentence_id=1,
            external_id='XXX',
            supervision_periods=[succeeding_supervision_period])

        self.assertIsNone(
            us_id_get_post_incarceration_supervision_type(
                incarceration_sentences=[],
                supervision_sentences=[supervision_sentence],
                incarceration_period=incarceration_period))
    def test_usMo_getPreIncarcerationSupervisionType(self):
        incarceration_period = StateIncarcerationPeriod.new_with_defaults(
            incarceration_period_id=1,
            admission_reason=StateIncarcerationPeriodAdmissionReason.
            PAROLE_REVOCATION,
            external_id='ip1',
            state_code='US_MO',
            admission_date=self.REVOCATION_DATE)

        supervision_sentence_parole = FakeUsMoSupervisionSentence.fake_sentence_from_sentence(
            StateSupervisionSentence.new_with_defaults(
                supervision_sentence_id=1,
                external_id=self.PAROLE_SENTENCE_EXTERNAL_ID,
                start_date=datetime.date(2017, 2, 1),
                supervision_type=StateSupervisionType.PROBATION),
            supervision_type=StateSupervisionType.PAROLE)

        # Even though the supervision type of the sentence is PROBATION, we find that it's actually a PAROLE
        # sentence from the statuses.
        self.assertEqual(
            StateSupervisionPeriodSupervisionType.PAROLE,
            us_mo_get_pre_incarceration_supervision_type(
                incarceration_sentences=[],
                supervision_sentences=[supervision_sentence_parole],
                incarceration_period=incarceration_period))
Example #6
0
    def test_usMo_getPreIncarcerationSupervisionType_noSupervisionPeriod(self):
        incarceration_period = StateIncarcerationPeriod.new_with_defaults(
            incarceration_period_id=1,
            admission_reason=StateIncarcerationPeriodAdmissionReason.
            PAROLE_REVOCATION,
            external_id='ip1',
            state_code='US_MO',
            admission_date=date(2018, 3, 5))

        supervision_sentence_prob = StateSupervisionSentence.new_with_defaults(
            supervision_sentence_id=1,
            external_id='US_MO',
            start_date=date(2018, 2, 1),
            completion_date=date(2018, 3, 6),
            status=StateSentenceStatus.COMPLETED,
            projected_completion_date=date(2018, 5, 19),
            supervision_type=StateSupervisionType.PROBATION)
        invalid_incarceration_sentence = StateIncarcerationSentence.new_with_defaults(
            incarceration_sentence_id=1,
            external_id='US_MO',
            start_date=date(2018, 2, 1),
            completion_date=date(2018, 3, 4),
            status=StateSentenceStatus.COMPLETED)

        self.assertEqual(
            StateSupervisionPeriodSupervisionType.PAROLE,
            get_pre_incarceration_supervision_type(
                [invalid_incarceration_sentence], [supervision_sentence_prob],
                incarceration_period, []))
Example #7
0
    def test_month_supervision_type_no_supervision_all_month(self):
        # Arrange
        supervision_period = StateSupervisionPeriod.new_with_defaults(
            supervision_period_id=1234,
            external_id='sp1',
            state_code='US_MO',
            start_date=(self.end_of_month_date - datetime.timedelta(days=60)))

        supervision_sentence = FakeUsMoSupervisionSentence.fake_sentence_from_sentence(
            StateSupervisionSentence.new_with_defaults(
                external_id='ss1',
                state_code='US_MO',
                start_date=supervision_period.start_date),
            supervision_type_spans=[
                SupervisionTypeSpan(
                    start_date=supervision_period.start_date,
                    end_date=self.start_of_month_date,
                    supervision_type=StateSupervisionType.PROBATION),
                # Incarcerated / suspended since start of month
                SupervisionTypeSpan(start_date=self.start_of_month_date,
                                    end_date=None,
                                    supervision_type=None)
            ])

        # Act
        supervision_period_supervision_type = \
            us_mo_get_month_supervision_type(self.end_of_month_date,
                                             supervision_sentences=[supervision_sentence],
                                             incarceration_sentences=[],
                                             supervision_period=supervision_period)

        # Assert
        self.assertEqual(
            supervision_period_supervision_type,
            StateSupervisionPeriodSupervisionType.INTERNAL_UNKNOWN)
Example #8
0
    def test_usMo_getPreIncarcerationSupervisionType(self):
        incarceration_period = StateIncarcerationPeriod.new_with_defaults(
            incarceration_period_id=1,
            admission_reason=StateIncarcerationPeriodAdmissionReason.
            PAROLE_REVOCATION,
            external_id='ip1',
            state_code='US_MO',
            admission_date=datetime.date(year=2019, month=9, day=13))

        supervision_sentence_parole = FakeUsMoSupervisionSentence.fake_sentence_from_sentence(
            StateSupervisionSentence.new_with_defaults(
                supervision_sentence_id=1,
                external_id='1167633-20171012-2',
                start_date=datetime.date(2017, 2, 1),
                supervision_type=StateSupervisionType.PROBATION),
            supervision_type_spans=[
                SupervisionTypeSpan(
                    start_date=datetime.date(2017, 2, 1),
                    end_date=None,
                    supervision_type=StateSupervisionType.PAROLE)
            ])

        # Even though the supervision type of the sentence is PROBATION, we find that it's actually a PAROLE
        # sentence from the statuses.
        self.assertEqual(
            StateSupervisionPeriodSupervisionType.PAROLE,
            us_mo_get_pre_incarceration_supervision_type(
                incarceration_sentences=[],
                supervision_sentences=[supervision_sentence_parole],
                incarceration_period=incarceration_period))
Example #9
0
    def test_usId_getPreIncarcerationSupervisionType_ignoreOutOfDatePeriods_Before(
            self):
        incarceration_period = StateIncarcerationPeriod.new_with_defaults(
            incarceration_period_id=1,
            admission_reason=StateIncarcerationPeriodAdmissionReason.
            RETURN_FROM_SUPERVISION,
            external_id='ip1',
            state_code='US_ID',
            admission_date=date(2019, 9, 13))

        preceding_supervision_period = StateSupervisionPeriod.new_with_defaults(
            supervision_period_id=1,
            start_date=incarceration_period.admission_date - relativedelta(
                days=INCARCERATION_SUPERVISION_TYPE_DAYS_LIMIT + 100),
            termination_date=incarceration_period.admission_date -
            relativedelta(days=INCARCERATION_SUPERVISION_TYPE_DAYS_LIMIT + 10),
            supervision_period_supervision_type=
            StateSupervisionPeriodSupervisionType.PAROLE)

        supervision_sentence = StateSupervisionSentence.new_with_defaults(
            supervision_sentence_id=1,
            external_id='XXX',
            supervision_periods=[preceding_supervision_period])

        self.assertIsNone(
            us_id_get_pre_incarceration_supervision_type(
                incarceration_sentences=[],
                supervision_sentences=[supervision_sentence],
                incarceration_period=incarceration_period))
Example #10
0
    def test_create_mo_supervision_sentence(self):
        # Arrange
        raw_sentence_statuses = [{
            'sentence_external_id': '1345495-20190808-1',
            'sentence_status_external_id': '1345495-20190808-1-1',
            'status_code': '15I1000',
            'status_date': '20190808',
            'status_description': 'New Court Probation'
        }]

        sentence = StateSupervisionSentence.new_with_defaults(
            external_id='1345495-20190808-1')

        # Act
        us_mo_sentence = UsMoSupervisionSentence.from_supervision_sentence(
            sentence, raw_sentence_statuses)

        # Assert
        self.assertEqual(us_mo_sentence.base_sentence, sentence)
        self.assertEqual(us_mo_sentence.sentence_statuses, [
            UsMoSentenceStatus(
                sentence_status_external_id='1345495-20190808-1-1',
                sentence_external_id='1345495-20190808-1',
                status_code='15I1000',
                status_date=datetime.date(year=2019, month=8, day=8),
                status_description='New Court Probation')
        ])

        self.assertTrue(isinstance(us_mo_sentence, StateSupervisionSentence))
        self.assertEqual(us_mo_sentence.external_id, sentence.external_id)
Example #11
0
    def test_resentenced_probation_revisit(self):
        raw_statuses = [{
            'sentence_external_id': '1254438-20130418-2',
            'sentence_status_external_id': '1254438-20130418-2-2',
            'status_code': '25I1000',
            'status_date': '20140610',
            'status_description': 'Court Probation-Addl Charge'
        }, {
            'sentence_external_id': '1254438-20130418-2',
            'sentence_status_external_id': '1254438-20130418-2-8',
            'status_code': '95O1040',
            'status_date': '20170717',
            'status_description': 'Resentenced'
        }, {
            'sentence_external_id': '1254438-20130418-2',
            'sentence_status_external_id': '1254438-20130418-2-9',
            'status_code': '35I1000',
            'status_date': '20170717',
            'status_description': 'Court Probation-Revisit'
        }]

        base_sentence = StateSupervisionSentence.new_with_defaults(
            external_id='1254438-20130418-2',
            start_date=datetime.date(year=2014, month=6, day=10))
        us_mo_sentence = UsMoSupervisionSentence.from_supervision_sentence(
            base_sentence, raw_statuses)

        self.assertEqual(
            us_mo_sentence.get_sentence_supervision_type_on_day(
                self.validation_date), StateSupervisionType.PROBATION)
Example #12
0
    def test_get_month_supervision_type_no_dates_on_sentence(self):
        any_date_in_month = date(2018, 4, 13)

        supervision_period = \
            StateSupervisionPeriod.new_with_defaults(
                supervision_period_id=111,
                external_id='sp1',
                status=StateSupervisionPeriodStatus.TERMINATED,
                state_code='US_ND',
                start_date=date(2018, 3, 5),
                termination_date=date(2018, 5, 19),
                termination_reason=StateSupervisionPeriodTerminationReason.DISCHARGE,
                supervision_type=None
            )

        supervision_sentence = \
            StateSupervisionSentence.new_with_defaults(
                supervision_sentence_id=111,
                external_id='ss1',
                status=StateSentenceStatus.COMPLETED,
                supervision_periods=[supervision_period]
            )

        supervision_sentences = [supervision_sentence]
        incarceration_sentences = []

        supervision_type = get_month_supervision_type(any_date_in_month,
                                                      supervision_sentences,
                                                      incarceration_sentences,
                                                      supervision_period)

        self.assertEqual(
            StateSupervisionPeriodSupervisionType.INTERNAL_UNKNOWN,
            supervision_type)
Example #13
0
    def test_prob_rev_codes_not_applicable(self):
        raw_statuses = [{
            'sentence_external_id': '1163420-20180116-1',
            'sentence_status_external_id': '1163420-20180116-1-1',
            'status_code': '15I1000',
            'status_date': '20180116',
            'status_description': 'New Court Probation'
        }, {
            'sentence_external_id': '1163420-20180116-1',
            'sentence_status_external_id': '1163420-20180116-1-3',
            'status_code': '95O2120',
            'status_date': '20180925',
            'status_description': 'Prob Rev-Codes Not Applicable'
        }, {
            'sentence_external_id': '1163420-20180116-1',
            'sentence_status_external_id': '1163420-20180116-1-4',
            'status_code': '35I1000',
            'status_date': '20180925',
            'status_description': 'Court Probation-Revisit'
        }]

        base_sentence = StateSupervisionSentence.new_with_defaults(
            external_id='1163420-20180116-1',
            start_date=datetime.date(year=2014, month=6, day=10))
        us_mo_sentence = UsMoSupervisionSentence.from_supervision_sentence(
            base_sentence, raw_statuses)

        self.assertEqual(
            us_mo_sentence.get_sentence_supervision_type_on_day(
                self.validation_date), StateSupervisionType.PROBATION)
Example #14
0
    def test_diversion_probation_after_community_court_ref_investigation(self):
        raw_statuses = [{
            'sentence_external_id': '1324786-20180214-1',
            'sentence_status_external_id': '1324786-20180214-1-1',
            'status_code': '05I5100',
            'status_date': '20180214',
            'status_description': 'New Community Corr Court Ref'
        }, {
            'sentence_external_id': '1324786-20180214-1',
            'sentence_status_external_id': '1324786-20180214-1-2',
            'status_code': '95O5100',
            'status_date': '20180323',
            'status_description': 'Comm Corr Court Ref Closed'
        }, {
            'sentence_external_id': '1324786-20180214-1',
            'sentence_status_external_id': '1324786-20180214-1-3',
            'status_code': '35I2000',
            'status_date': '20180323',
            'status_description': 'Diversion Supv-Revisit'
        }]

        base_sentence = StateSupervisionSentence.new_with_defaults(
            external_id='1324786-20180214-1',
            start_date=datetime.date(year=2018, month=3, day=23))
        us_mo_sentence = UsMoSupervisionSentence.from_supervision_sentence(
            base_sentence, raw_statuses)

        self.assertEqual(
            us_mo_sentence.get_sentence_supervision_type_on_day(
                self.validation_date), StateSupervisionType.PROBATION)
Example #15
0
    def test_interstate_compact_parole_classified_as_probation(self):
        raw_statuses = [
            {
                'sentence_external_id': '165467-20171227-1',
                'sentence_status_external_id': '165467-20171227-1-1',
                'status_code': '05I5200',
                'status_date': '20171227',
                'status_description': 'New Interstate Compact-Invest'
            },
            {
                'sentence_external_id': '165467-20171227-1',
                'sentence_status_external_id': '165467-20171227-1-2',
                'status_code': '95O5200',
                'status_date': '20180123',
                'status_description': 'Interstate Invest Closed'
            },
            {
                'sentence_external_id': '165467-20171227-1',
                'sentence_status_external_id': '165467-20171227-1-3',
                'status_code': '35I4100',
                'status_date': '20180129',
                'status_description': 'IS Compact-Parole-Revisit'
            },
        ]

        base_sentence = StateSupervisionSentence.new_with_defaults(
            external_id='165467-20171227-1',
            start_date=datetime.date(year=2018, month=1, day=29))
        us_mo_sentence = UsMoSupervisionSentence.from_supervision_sentence(
            base_sentence, raw_statuses)

        self.assertEqual(
            us_mo_sentence.get_sentence_supervision_type_on_day(
                self.validation_date), StateSupervisionType.PROBATION)
Example #16
0
    def test_usId_getPostIncarcerationSupervisionType(self):
        incarceration_period = StateIncarcerationPeriod.new_with_defaults(
            incarceration_period_id=1,
            admission_reason=StateIncarcerationPeriodAdmissionReason.
            RETURN_FROM_SUPERVISION,
            external_id='ip1',
            state_code='US_ID',
            admission_date=date(2019, 9, 13),
            release_date=date(2020, 3, 1),
            release_reason=StateIncarcerationPeriodReleaseReason.
            CONDITIONAL_RELEASE)

        succeeding_supervision_period = StateSupervisionPeriod.new_with_defaults(
            supervision_period_id=1,
            start_date=incarceration_period.release_date,
            supervision_period_supervision_type=
            StateSupervisionPeriodSupervisionType.PAROLE)

        supervision_sentence = StateSupervisionSentence.new_with_defaults(
            supervision_sentence_id=1,
            external_id='XXX',
            supervision_periods=[succeeding_supervision_period])

        self.assertEqual(
            StateSupervisionPeriodSupervisionType.PAROLE,
            us_id_get_post_incarceration_supervision_type(
                incarceration_sentences=[],
                supervision_sentences=[supervision_sentence],
                incarceration_period=incarceration_period))
Example #17
0
    def test_probation_after_investigation_status_list_unsorted(self):
        raw_statuses = [{
            'sentence_external_id': '282443-20180427-1',
            'sentence_status_external_id': '282443-20180427-1-3',
            'status_code': '35I1000',
            'status_date': '20180525',
            'status_description': 'Court Probation-Revisit'
        }, {
            'sentence_external_id': '282443-20180427-1',
            'sentence_status_external_id': '282443-20180427-1-2',
            'status_code': '95O5630',
            'status_date': '20180525',
            'status_description': 'SAR Cancelled by Court'
        }, {
            'sentence_external_id': '282443-20180427-1',
            'sentence_status_external_id': '282443-20180427-1-1',
            'status_code': '05I5600',
            'status_date': '20180427',
            'status_description': 'New Sentencing Assessment'
        }]

        base_sentence = StateSupervisionSentence.new_with_defaults(
            external_id='282443-20180427-1',
            start_date=datetime.date(year=2018, month=5, day=25))
        us_mo_sentence = UsMoSupervisionSentence.from_supervision_sentence(
            base_sentence, raw_statuses)

        self.assertEqual(
            us_mo_sentence.get_sentence_supervision_type_on_day(
                self.validation_date), StateSupervisionType.PROBATION)
    def test_usMo_getPreIncarcerationSupervisionType_ignoreOutOfDateSentences(
            self):
        incarceration_period = StateIncarcerationPeriod.new_with_defaults(
            incarceration_period_id=1,
            admission_reason=StateIncarcerationPeriodAdmissionReason.
            PAROLE_REVOCATION,
            external_id='ip1',
            state_code='US_MO',
            admission_date=self.REVOCATION_DATE)

        supervision_sentence_parole = FakeUsMoSupervisionSentence.fake_sentence_from_sentence(
            StateSupervisionSentence.new_with_defaults(
                supervision_sentence_id=1,
                external_id=self.PAROLE_SENTENCE_EXTERNAL_ID,
                start_date=datetime.date(2017, 2, 1),
                supervision_type=StateSupervisionType.PROBATION),
            supervision_type=StateSupervisionType.PAROLE)

        old_incarceration_sentence = FakeUsMoIncarcerationSentence.fake_sentence_from_sentence(
            StateIncarcerationSentence.new_with_defaults(
                incarceration_sentence_id=1,
                external_id='US_MO',
                start_date=datetime.date(2017, 2, 1),
                completion_date=datetime.date(2017, 3, 4),
                status=StateSentenceStatus.COMPLETED),
            supervision_type=None  # Terminated already
        )

        self.assertEqual(
            StateSupervisionPeriodSupervisionType.PAROLE,
            us_mo_get_pre_incarceration_supervision_type(
                [old_incarceration_sentence], [supervision_sentence_parole],
                incarceration_period))
    def test_ConvertSentenceToStateSpecificType_supervision_sentence_mo(self):
        """Tests that for MO, supervision sentences get converted to UsMoSupervisionSentence."""
        supervision_sentence_id = 123

        supervision_sentence = StateSupervisionSentence.new_with_defaults(
            supervision_sentence_id=supervision_sentence_id,
            state_code="US_MO",
            external_id="123-external-id",
            start_date=date(2000, 1, 1),
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
        )

        expected_sentence = UsMoSupervisionSentence.new_with_defaults(
            supervision_sentence_id=supervision_sentence_id,
            state_code="US_MO",
            external_id="123-external-id",
            start_date=date(2000, 1, 1),
            base_sentence=supervision_sentence,
            sentence_statuses=[self.TEST_CONVERTED_MO_STATUS],
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
        )

        self.run_test_pipeline(
            self.TEST_PERSON_ID,
            supervision_sentence,
            self.TEST_MO_SENTENCE_STATUS_ROWS,
            expected_sentence,
        )
    def test_associateSvrsWithIps_dontAssociateTheSameSvr(self):
        # Arrange
        svr_1 = StateSupervisionViolationResponse.new_with_defaults(
            response_date=_DATE_1,
            revocation_type=StateSupervisionViolationResponseRevocationType.
            REINCARCERATION)
        placeholder_sv = StateSupervisionViolation.new_with_defaults(
            supervision_violation_responses=[svr_1])
        placeholder_sp = StateSupervisionPeriod.new_with_defaults(
            supervision_violation_entries=[placeholder_sv])
        placeholder_ss = StateSupervisionSentence.new_with_defaults(
            supervision_periods=[placeholder_sp])

        ip_1 = StateIncarcerationPeriod.new_with_defaults(
            admission_date=_DATE_2,
            admission_reason=StateIncarcerationPeriodAdmissionReason.
            PROBATION_REVOCATION)
        ip_2 = StateIncarcerationPeriod.new_with_defaults(
            admission_date=_DATE_4,
            admission_reason=StateIncarcerationPeriodAdmissionReason.
            PROBATION_REVOCATION)
        placeholder_is = StateIncarcerationSentence.new_with_defaults(
            incarceration_periods=[ip_1, ip_2])
        placeholder_sg = StateSentenceGroup.new_with_defaults(
            incarceration_sentences=[placeholder_is],
            supervision_sentences=[placeholder_ss])
        placeholder_person = StatePerson.new_with_defaults(
            sentence_groups=[placeholder_sg])

        expected_svr_1 = attr.evolve(svr_1)
        expected_placeholder_sv = attr.evolve(
            placeholder_sv, supervision_violation_responses=[expected_svr_1])
        expected_placeholder_sp = attr.evolve(
            placeholder_sp,
            supervision_violation_entries=[expected_placeholder_sv])
        expected_placeholder_ss = attr.evolve(
            placeholder_ss, supervision_periods=[expected_placeholder_sp])

        expected_ip_1 = attr.evolve(
            ip_1, source_supervision_violation_response=expected_svr_1)
        expected_ip_2 = attr.evolve(ip_2)

        expected_placeholder_is = attr.evolve(
            placeholder_is,
            incarceration_periods=[expected_ip_1, expected_ip_2])
        expected_placeholder_sg = attr.evolve(
            placeholder_sg,
            supervision_sentences=[expected_placeholder_ss],
            incarceration_sentences=[expected_placeholder_is])
        expected_placeholder_person = attr.evolve(
            placeholder_person, sentence_groups=[expected_placeholder_sg])

        # Act
        input_people = \
            converter.convert_entity_people_to_schema_people(
                [placeholder_person])
        associate_revocation_svrs_with_ips(input_people)

        # Assert
        self.assert_people_match([expected_placeholder_person], input_people)
    def test_associatePeriodsWithSentence_periodNoLongerMatches(self):
        # Arrange
        sp_which_no_longer_overlaps = StateSupervisionPeriod.new_with_defaults(
            supervision_period_id=_ID,
            external_id=_EXTERNAL_ID,
            state_code=_STATE_CODE,
            start_date=_DATE_6)
        ip_which_no_longer_overlaps = StateIncarcerationPeriod.new_with_defaults(
            incarceration_period_id=_ID,
            external_id=_EXTERNAL_ID,
            state_code=_STATE_CODE,
            admission_date=_DATE_6)

        # This sentence, which has already been written to the DB, has presumably been updated so that the date range no
        # longer overlaps with the attached periods.
        inc_s_updated_dates = StateIncarcerationSentence.new_with_defaults(
            incarceration_sentence_id=_ID_2,
            external_id=_EXTERNAL_ID,
            state_code=_STATE_CODE,
            start_date=_DATE_3,
            completion_date=_DATE_5,
            incarceration_periods=[ip_which_no_longer_overlaps],
            supervision_periods=[sp_which_no_longer_overlaps])

        sg = StateSentenceGroup.new_with_defaults(
            sentence_group_id=_ID_3,
            state_code=_STATE_CODE,
            incarceration_sentences=[inc_s_updated_dates])

        state_person = StatePerson.new_with_defaults(person_id=_ID,
                                                     sentence_groups=[sg])

        expected_sp = attr.evolve(sp_which_no_longer_overlaps)
        expected_ip = attr.evolve(ip_which_no_longer_overlaps)
        expected_is = attr.evolve(inc_s_updated_dates,
                                  incarceration_periods=[],
                                  supervision_periods=[])

        # We expect that a new placeholder supervision sentence has been created to hold on to the orphaned periods
        expected_placeholder_ss = StateSupervisionSentence.new_with_defaults(
            state_code=_STATE_CODE,
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            incarceration_periods=[expected_ip],
            supervision_periods=[expected_sp])
        expected_sg = attr.evolve(
            sg,
            incarceration_sentences=[expected_is],
            supervision_sentences=[expected_placeholder_ss])
        expected_person = attr.evolve(state_person,
                                      sentence_groups=[expected_sg])

        # Act
        input_people = converter.convert_entity_people_to_schema_people(
            [state_person])
        move_periods_onto_sentences_by_date(input_people)

        # Assert
        self.assert_people_match([expected_person], input_people)
Example #22
0
    def test_get_supervision_period_supervision_type_from_sentence(self):
        supervision_sentence = StateSupervisionSentence.new_with_defaults(
            supervision_sentence_id=111)

        for supervision_type in StateSupervisionType:
            supervision_sentence.supervision_type = supervision_type

            # Assert this doesn't fail for all possible supervision types
            _ = get_supervision_period_supervision_type_from_sentence(
                supervision_sentence)
Example #23
0
    def test_get_month_supervision_type_sentence_supervision_ends_different_days(
            self):
        # Arrange
        supervision_period = StateSupervisionPeriod.new_with_defaults(
            supervision_period_id=1234,
            external_id='sp1',
            state_code='US_MO',
            start_date=(self.end_of_month_date - datetime.timedelta(days=60)))

        mid_month_date = self.end_of_month_date - datetime.timedelta(days=10)
        second_to_last_month_date = self.end_of_month_date - datetime.timedelta(
            days=1)
        incarceration_sentence = FakeUsMoIncarcerationSentence.fake_sentence_from_sentence(
            StateIncarcerationSentence.new_with_defaults(
                external_id='ss1',
                state_code='US_MO',
                start_date=supervision_period.start_date),
            supervision_type_spans=[
                SupervisionTypeSpan(
                    start_date=supervision_period.start_date,
                    end_date=second_to_last_month_date,
                    supervision_type=StateSupervisionType.PAROLE),
                SupervisionTypeSpan(start_date=second_to_last_month_date,
                                    end_date=None,
                                    supervision_type=None)
            ])

        supervision_sentence = FakeUsMoSupervisionSentence.fake_sentence_from_sentence(
            StateSupervisionSentence.new_with_defaults(
                external_id='ss1',
                state_code='US_MO',
                start_date=supervision_period.start_date),
            supervision_type_spans=[
                # Probation sentence starts after parole sentence ends
                SupervisionTypeSpan(
                    start_date=supervision_period.start_date,
                    end_date=mid_month_date,
                    supervision_type=StateSupervisionType.PROBATION),
                SupervisionTypeSpan(start_date=mid_month_date,
                                    end_date=None,
                                    supervision_type=None)
            ])

        # Act
        supervision_period_supervision_type = \
            us_mo_get_month_supervision_type(self.end_of_month_date,
                                             supervision_sentences=[supervision_sentence],
                                             incarceration_sentences=[incarceration_sentence],
                                             supervision_period=supervision_period)

        # Assert

        # Since the probation sentence ends before the parole sentence, the last valid supervision type is PAROLE
        self.assertEqual(supervision_period_supervision_type,
                         StateSupervisionPeriodSupervisionType.PAROLE)
Example #24
0
    def test_readPersons_ndSpecific_unexpectedRoot_raises(self):
        ingested_supervision_sentence = \
            StateSupervisionSentence.new_with_defaults(
                external_id=_EXTERNAL_ID)
        ingested_sentence_group = StateSentenceGroup.new_with_defaults(
            supervision_sentences=[ingested_supervision_sentence])
        ingested_person = StatePerson.new_with_defaults(
            sentence_groups=[ingested_sentence_group])

        with pytest.raises(EntityMatchingError):
            session = SessionFactory.for_schema_base(StateBase)
            read_persons(session, 'us_nd', [ingested_person])
Example #25
0
    def test_get_month_supervision_type_dual_ends_mid_month(self):
        # Arrange
        supervision_period = StateSupervisionPeriod.new_with_defaults(
            supervision_period_id=1234,
            external_id='sp1',
            state_code='US_MO',
            start_date=(self.end_of_month_date - datetime.timedelta(days=60)))

        second_to_last_month_date = self.end_of_month_date - datetime.timedelta(
            days=1)
        incarceration_sentence = FakeUsMoIncarcerationSentence.fake_sentence_from_sentence(
            StateIncarcerationSentence.new_with_defaults(
                external_id='ss1',
                state_code='US_MO',
                start_date=supervision_period.start_date),
            supervision_type_spans=[
                SupervisionTypeSpan(
                    start_date=supervision_period.start_date,
                    end_date=second_to_last_month_date,
                    supervision_type=StateSupervisionType.PAROLE),
                SupervisionTypeSpan(start_date=second_to_last_month_date,
                                    end_date=None,
                                    supervision_type=None)
            ])

        supervision_sentence = FakeUsMoSupervisionSentence.fake_sentence_from_sentence(
            StateSupervisionSentence.new_with_defaults(
                external_id='ss1',
                state_code='US_MO',
                start_date=supervision_period.start_date),
            supervision_type_spans=[
                # Probation sentence starts after parole sentence ends
                SupervisionTypeSpan(
                    start_date=supervision_period.start_date,
                    end_date=second_to_last_month_date,
                    supervision_type=StateSupervisionType.PROBATION),
                SupervisionTypeSpan(start_date=second_to_last_month_date,
                                    end_date=None,
                                    supervision_type=None)
            ])

        # Act
        supervision_period_supervision_type = \
            us_mo_get_month_supervision_type(self.end_of_month_date,
                                             supervision_sentences=[supervision_sentence],
                                             incarceration_sentences=[incarceration_sentence],
                                             supervision_period=supervision_period)

        # Assert

        # Even though both sentences are terminated before the last day, we still return DUAL
        self.assertEqual(supervision_period_supervision_type,
                         StateSupervisionPeriodSupervisionType.DUAL)
    def _sup_sentence_with_type(
        self, supervision_type: Optional[StateSupervisionType]
    ) -> UsMoSupervisionSentence:
        base_sentence = StateSupervisionSentence.new_with_defaults(
            external_id=f'164735-20120627-{self.sentence_id_counter}',
            state_code='US_MO',
            start_date=datetime.date(year=2012, month=6, day=27))
        mo_sentence = FakeUsMoSupervisionSentence.fake_sentence_from_sentence(
            base_sentence, supervision_type=supervision_type)

        self.sentence_id_counter += 1

        return mo_sentence
    def test_get_supervision_period_supervision_type_from_sentence(self):
        supervision_sentence = StateSupervisionSentence.new_with_defaults(
            state_code="US_XX",
            supervision_sentence_id=111,
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
        )

        for supervision_type in StateSupervisionType:
            supervision_sentence.supervision_type = supervision_type

            # Assert this doesn't fail for all possible supervision types
            _ = _get_sentence_supervision_type_from_sentence(
                supervision_sentence)
    def test_set_supervision_periods_on_sentences_no_sps(self):
        state_code = "US_XX"

        supervision_sentence_1 = StateSupervisionSentence.new_with_defaults(
            state_code=state_code,
            supervision_sentence_id=123,
            start_date=date(2000, 1, 1),
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
        )

        supervision_sentence_2 = StateSupervisionSentence.new_with_defaults(
            state_code=state_code,
            supervision_sentence_id=567,
            start_date=date(2000, 1, 1),
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
        )

        person_and_entities = {
            "sentences": [supervision_sentence_1, supervision_sentence_2],
            "supervision_periods": [],
        }

        test_pipeline = TestPipeline()

        output = (
            test_pipeline
            | beam.Create([(12345, person_and_entities)])
            | "Set SPs on sentences" >> beam.ParDo(
                entity_hydration_utils.SetSupervisionPeriodsOnSentences()))

        updated_ss_1 = attr.evolve(supervision_sentence_1)
        updated_ss_2 = attr.evolve(supervision_sentence_2)

        assert_that(
            output,
            equal_to([(12345, updated_ss_1), (12345, updated_ss_2)]),
        )

        test_pipeline.run()
Example #29
0
    def test_associateViolationsWithSupervisionPeriodsWhereViolationNoLongerOverlaps(
            self):
        # Act
        sv_ss = StateSupervisionViolation.new_with_defaults(
            supervision_violation_id=_ID,
            state_code=_STATE_CODE,
            violation_date=_DATE_4)

        # This supervision period, which has already been written to the DB, has presumably been updated so that the
        # date range no longer overlaps with the attached violation (or the violation has been updated).
        sp_ss = StateSupervisionPeriod.new_with_defaults(
            supervision_period_id=_ID,
            external_id=_EXTERNAL_ID,
            state_code=_STATE_CODE,
            start_date=_DATE,
            termination_date=_DATE_3,
            supervision_violation_entries=[sv_ss])
        ss = StateSupervisionSentence.new_with_defaults(
            supervision_sentence_id=_ID,
            external_id=_EXTERNAL_ID,
            state_code=_STATE_CODE,
            supervision_periods=[sp_ss])

        sg = StateSentenceGroup.new_with_defaults(sentence_group_id=_ID,
                                                  state_code=_STATE_CODE,
                                                  supervision_sentences=[ss])

        state_person = StatePerson.new_with_defaults(person_id=_ID,
                                                     sentence_groups=[sg])

        expected_sv_ss = attr.evolve(sv_ss)
        expected_sp_ss = attr.evolve(sp_ss, supervision_violation_entries=[])
        expected_placeholder_sp_ss = StateSupervisionPeriod.new_with_defaults(
            state_code=_STATE_CODE,
            status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO,
            supervision_violation_entries=[expected_sv_ss])
        expected_ss = attr.evolve(
            ss,
            supervision_periods=[expected_placeholder_sp_ss, expected_sp_ss])
        expected_sg = attr.evolve(sg, supervision_sentences=[expected_ss])
        expected_person = attr.evolve(state_person,
                                      sentence_groups=[expected_sg])

        # Act
        input_people = \
            converter.convert_entity_people_to_schema_people(
                [state_person])
        move_violations_onto_supervision_periods_by_date(input_people)

        # Assert
        self.assert_people_match([expected_person], input_people)
    def test_associatedPeriodsWithSentences_oneDayPeriodOverlapsWithStartOfSentence(
            self):
        sp = StateSupervisionPeriod.new_with_defaults(external_id=_EXTERNAL_ID,
                                                      start_date=_DATE_2,
                                                      termination_date=_DATE_2)
        placeholder_ss = StateSupervisionSentence.new_with_defaults(
            supervision_periods=[sp])

        ss = StateSupervisionSentence.new_with_defaults(
            external_id=_EXTERNAL_ID,
            start_date=_DATE_2,
            completion_date=_DATE_3,
            status=StateSentenceStatus.SERVING)

        sg = StateSentenceGroup.new_with_defaults(
            supervision_sentences=[ss, placeholder_ss])

        state_person = StatePerson.new_with_defaults(sentence_groups=[sg])

        expected_sp = attr.evolve(sp)

        expected_placeholder_ss = attr.evolve(placeholder_ss,
                                              supervision_periods=[],
                                              incarceration_periods=[])
        expected_ss = attr.evolve(ss, supervision_periods=[expected_sp])
        expected_sg = attr.evolve(
            sg, supervision_sentences=[expected_ss, expected_placeholder_ss])
        expected_person = attr.evolve(state_person,
                                      sentence_groups=[expected_sg])

        # Act
        input_people = converter.convert_entity_people_to_schema_people(
            [state_person])
        move_periods_onto_sentences_by_date(input_people)

        # Assert
        self.assert_people_match([expected_person], input_people)