示例#1
0
    def test_get_violation_and_response_history_no_violations(self):
        supervision_violation_response = (
            StateSupervisionViolationResponse.new_with_defaults(
                state_code="US_XX",
                supervision_violation_response_id=_DEFAULT_SSVR_ID,
                response_type=StateSupervisionViolationResponseType.VIOLATION_REPORT,
                response_date=datetime.date(2009, 1, 7),
                supervision_violation_response_decisions=[
                    StateSupervisionViolationResponseDecisionEntry.new_with_defaults(
                        state_code="US_XX",
                        decision=StateSupervisionViolationResponseDecision.CONTINUANCE,
                    )
                ],
            )
        )

        revocation_date = datetime.date(2009, 2, 13)

        violation_history = violation_utils.get_violation_and_response_history(
            revocation_date, [supervision_violation_response], UsXxViolationDelegate()
        )

        expected_output = violation_utils.ViolationHistory(
            most_severe_violation_type=None,
            most_severe_violation_type_subtype=None,
            most_severe_response_decision=StateSupervisionViolationResponseDecision.CONTINUANCE,
            response_count=1,
            violation_history_description=None,
            violation_type_frequency_counter=None,
        )

        self.assertEqual(expected_output, violation_history)
示例#2
0
    def test_identify_most_severe_violation_type(self) -> None:
        violation = StateSupervisionViolation.new_with_defaults(
            state_code="US_XX",
            supervision_violation_types=[
                StateSupervisionViolationTypeEntry.new_with_defaults(
                    state_code="US_XX",
                    violation_type=StateSupervisionViolationType.TECHNICAL,
                ),
                StateSupervisionViolationTypeEntry.new_with_defaults(
                    state_code="US_XX",
                    violation_type=StateSupervisionViolationType.FELONY,
                ),
            ],
        )

        (
            most_severe_violation_type,
            most_severe_violation_type_subtype,
        ) = violation_utils.identify_most_severe_violation_type_and_subtype(
            [violation], UsXxViolationDelegate()
        )

        self.assertEqual(
            most_severe_violation_type, StateSupervisionViolationType.FELONY
        )
        self.assertEqual(
            most_severe_violation_type_subtype,
            StateSupervisionViolationType.FELONY.value,
        )
示例#3
0
 def setUp(self) -> None:
     self.fake_bq_source_factory = FakeReadFromBigQueryFactory()
     self.fake_bq_sink_factory = FakeWriteToBigQueryFactory(
         FakeWriteToBigQuery)
     self.violation_delegate_patcher = mock.patch(
         "recidiviz.calculator.pipeline.violation.identifier.get_state_specific_violation_delegate"
     )
     self.mock_violation_delegate = self.violation_delegate_patcher.start()
     self.mock_violation_delegate.return_value = UsXxViolationDelegate()
示例#4
0
    def test_get_violation_and_response_history(self):
        supervision_violation = StateSupervisionViolation.new_with_defaults(
            supervision_violation_id=123455,
            state_code="US_XX",
            violation_date=datetime.date(2009, 1, 3),
            supervision_violation_types=[
                StateSupervisionViolationTypeEntry.new_with_defaults(
                    state_code="US_XX",
                    violation_type=StateSupervisionViolationType.TECHNICAL,
                ),
                StateSupervisionViolationTypeEntry.new_with_defaults(
                    state_code="US_XX",
                    violation_type=StateSupervisionViolationType.FELONY,
                ),
                StateSupervisionViolationTypeEntry.new_with_defaults(
                    state_code="US_XX",
                    violation_type=StateSupervisionViolationType.ABSCONDED,
                ),
            ],
        )

        supervision_violation_response = StateSupervisionViolationResponse.new_with_defaults(
            supervision_violation_response_id=_DEFAULT_SSVR_ID,
            response_type=StateSupervisionViolationResponseType.VIOLATION_REPORT,
            state_code="US_XX",
            response_date=datetime.date(2009, 1, 7),
            supervision_violation_response_decisions=[
                StateSupervisionViolationResponseDecisionEntry.new_with_defaults(
                    state_code="US_XX",
                    decision=StateSupervisionViolationResponseDecision.REVOCATION,
                    revocation_type=StateSupervisionViolationResponseRevocationType.REINCARCERATION,
                ),
                StateSupervisionViolationResponseDecisionEntry.new_with_defaults(
                    state_code="US_XX",
                    decision=StateSupervisionViolationResponseDecision.CONTINUANCE,
                ),
            ],
            supervision_violation=supervision_violation,
        )

        revocation_date = datetime.date(2009, 2, 13)

        violation_history = violation_utils.get_violation_and_response_history(
            revocation_date, [supervision_violation_response], UsXxViolationDelegate()
        )

        expected_output = violation_utils.ViolationHistory(
            most_severe_violation_type=StateSupervisionViolationType.FELONY,
            most_severe_violation_type_subtype=StateSupervisionViolationType.FELONY.value,
            most_severe_response_decision=StateSupervisionViolationResponseDecision.REVOCATION,
            response_count=1,
            violation_history_description="1felony",
            violation_type_frequency_counter=[["TECHNICAL", "FELONY", "ABSCONDED"]],
        )

        self.assertEqual(expected_output, violation_history)
示例#5
0
    def test_get_violation_type_frequency_counter_no_types(self) -> None:
        violations = [StateSupervisionViolation.new_with_defaults(state_code="US_XX")]

        violation_type_frequency_counter = (
            violation_utils.get_violation_type_frequency_counter(
                violations, UsXxViolationDelegate()
            )
        )

        self.assertIsNone(violation_type_frequency_counter)
示例#6
0
    def test_get_violation_and_response_history_citation_date(self):
        supervision_violation = StateSupervisionViolation.new_with_defaults(
            supervision_violation_id=123455,
            state_code="US_XX",
            supervision_violation_types=[
                StateSupervisionViolationTypeEntry.new_with_defaults(
                    state_code="US_XX",
                    violation_type=StateSupervisionViolationType.ABSCONDED,
                ),
                StateSupervisionViolationTypeEntry.new_with_defaults(
                    state_code="US_XX",
                    violation_type=StateSupervisionViolationType.MISDEMEANOR,
                ),
            ],
        )

        supervision_violation_response = StateSupervisionViolationResponse.new_with_defaults(
            state_code="US_XX",
            supervision_violation_response_id=_DEFAULT_SSVR_ID,
            response_type=StateSupervisionViolationResponseType.CITATION,
            response_date=datetime.date(2009, 1, 7),
            supervision_violation_response_decisions=[
                StateSupervisionViolationResponseDecisionEntry.new_with_defaults(
                    state_code="US_XX",
                    decision=StateSupervisionViolationResponseDecision.REVOCATION,
                    revocation_type=StateSupervisionViolationResponseRevocationType.REINCARCERATION,
                ),
                StateSupervisionViolationResponseDecisionEntry.new_with_defaults(
                    state_code="US_XX",
                    decision=StateSupervisionViolationResponseDecision.CONTINUANCE,
                    revocation_type=StateSupervisionViolationResponseRevocationType.SHOCK_INCARCERATION,
                ),
            ],
            supervision_violation=supervision_violation,
        )

        revocation_date = datetime.date(2009, 2, 13)

        violation_history = violation_utils.get_violation_and_response_history(
            revocation_date, [supervision_violation_response], UsXxViolationDelegate()
        )

        expected_output = violation_utils.ViolationHistory(
            most_severe_violation_type=StateSupervisionViolationType.MISDEMEANOR,
            most_severe_violation_type_subtype=StateSupervisionViolationType.MISDEMEANOR.value,
            most_severe_response_decision=StateSupervisionViolationResponseDecision.REVOCATION,
            response_count=1,
            violation_history_description="1misdemeanor",
            violation_type_frequency_counter=[["ABSCONDED", "MISDEMEANOR"]],
        )

        self.assertEqual(expected_output, violation_history)
示例#7
0
    def setUp(self) -> None:
        self.fake_person_id = 12345
        self.fake_supervision_violation_id = 23456

        self.fake_person = entities.StatePerson.new_with_defaults(
            state_code="US_XX",
            person_id=self.fake_person_id,
            gender=Gender.FEMALE,
            birthdate=date(1985, 2, 1),
        )
        self.identifier = ViolationIdentifier()
        self.violation_delegate_patcher = mock.patch(
            "recidiviz.calculator.pipeline.violation.identifier.get_state_specific_violation_delegate"
        )
        self.mock_violation_delegate = self.violation_delegate_patcher.start()
        self.mock_violation_delegate.return_value = UsXxViolationDelegate()
示例#8
0
    def test_get_violation_and_response_history_no_responses(self):
        revocation_date = datetime.date(2009, 2, 13)

        violation_history = violation_utils.get_violation_and_response_history(
            revocation_date, [], UsXxViolationDelegate()
        )

        expected_output = violation_utils.ViolationHistory(
            most_severe_violation_type=None,
            most_severe_violation_type_subtype=None,
            most_severe_response_decision=None,
            response_count=0,
            violation_history_description=None,
            violation_type_frequency_counter=None,
        )

        self.assertEqual(expected_output, violation_history)
示例#9
0
    def test_identify_most_severe_violation_type_test_all_types(self) -> None:
        for violation_type in StateSupervisionViolationType:
            violation = StateSupervisionViolation.new_with_defaults(
                state_code="US_XX",
                supervision_violation_types=[
                    StateSupervisionViolationTypeEntry.new_with_defaults(
                        state_code="US_XX", violation_type=violation_type
                    )
                ],
            )
            (
                most_severe_violation_type,
                most_severe_violation_type_subtype,
            ) = violation_utils.identify_most_severe_violation_type_and_subtype(
                [violation], UsXxViolationDelegate()
            )

            self.assertEqual(most_severe_violation_type, violation_type)
            self.assertEqual(violation_type.value, most_severe_violation_type_subtype)
示例#10
0
    def test_get_violation_type_frequency_counter(self) -> None:
        violations = [
            StateSupervisionViolation.new_with_defaults(
                state_code="US_XX",
                supervision_violation_types=[
                    StateSupervisionViolationTypeEntry.new_with_defaults(
                        state_code="US_XX",
                        violation_type=StateSupervisionViolationType.ABSCONDED,
                    ),
                    StateSupervisionViolationTypeEntry.new_with_defaults(
                        state_code="US_XX",
                        violation_type=StateSupervisionViolationType.FELONY,
                    ),
                ],
            )
        ]

        violation_type_frequency_counter = (
            violation_utils.get_violation_type_frequency_counter(
                violations, UsXxViolationDelegate()
            )
        )

        self.assertEqual([["ABSCONDED", "FELONY"]], violation_type_frequency_counter)
示例#11
0
    def test_get_violation_and_response_history_outside_lookback(self):
        supervision_violation = StateSupervisionViolation.new_with_defaults(
            supervision_violation_id=123455,
            state_code="US_XX",
            violation_date=datetime.date(2009, 1, 3),
            supervision_violation_types=[
                StateSupervisionViolationTypeEntry.new_with_defaults(
                    state_code="US_XX",
                    violation_type=StateSupervisionViolationType.TECHNICAL,
                ),
                StateSupervisionViolationTypeEntry.new_with_defaults(
                    state_code="US_XX",
                    violation_type=StateSupervisionViolationType.FELONY,
                ),
                StateSupervisionViolationTypeEntry.new_with_defaults(
                    state_code="US_XX",
                    violation_type=StateSupervisionViolationType.ABSCONDED,
                ),
            ],
        )

        # This is outside of the lookback window
        supervision_violation_response_before_look_back = (
            StateSupervisionViolationResponse.new_with_defaults(
                supervision_violation_response_id=_DEFAULT_SSVR_ID,
                response_type=StateSupervisionViolationResponseType.VIOLATION_REPORT,
                state_code="US_XX",
                response_date=datetime.date(2018, 7, 25),
            )
        )

        supervision_violation_response = StateSupervisionViolationResponse.new_with_defaults(
            supervision_violation_response_id=_DEFAULT_SSVR_ID,
            response_type=StateSupervisionViolationResponseType.VIOLATION_REPORT,
            state_code="US_XX",
            response_date=datetime.date(2019, 1, 20),
            supervision_violation_response_decisions=[
                StateSupervisionViolationResponseDecisionEntry.new_with_defaults(
                    state_code="US_XX",
                    decision=StateSupervisionViolationResponseDecision.REVOCATION,
                    revocation_type=StateSupervisionViolationResponseRevocationType.REINCARCERATION,
                ),
                StateSupervisionViolationResponseDecisionEntry.new_with_defaults(
                    state_code="US_XX",
                    decision=StateSupervisionViolationResponseDecision.CONTINUANCE,
                    revocation_type=StateSupervisionViolationResponseRevocationType.SHOCK_INCARCERATION,
                ),
            ],
            supervision_violation=supervision_violation,
        )

        violation_responses = [
            supervision_violation_response_before_look_back,
            supervision_violation_response,
        ]

        end_date = datetime.date(2019, 9, 5)

        violation_history = violation_utils.get_violation_and_response_history(
            end_date, violation_responses, UsXxViolationDelegate()
        )

        expected_output = violation_utils.ViolationHistory(
            most_severe_violation_type=StateSupervisionViolationType.FELONY,
            most_severe_violation_type_subtype=StateSupervisionViolationType.FELONY.value,
            most_severe_response_decision=StateSupervisionViolationResponseDecision.REVOCATION,
            response_count=1,
            violation_history_description="1felony",
            violation_type_frequency_counter=[["TECHNICAL", "FELONY", "ABSCONDED"]],
        )

        self.assertEqual(expected_output, violation_history)