Ejemplo n.º 1
0
    def test_map_program_combinations_calculation_month_count_36(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='US_ND',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='US_ND',
            ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        included_event = ProgramReferralEvent(
            state_code='US_ND',
            event_date=date(2012, 12, 10),
            program_id='XXX'
        )

        not_included_event = ProgramReferralEvent(
            state_code='US_ND',
            event_date=date(2009, 12, 10),
            program_id='ZZZ'
        )

        program_events = [included_event, not_included_event]

        program_combinations = calculator.map_program_combinations(
            person, program_events, ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=36,
            person_metadata=_DEFAULT_PERSON_METADATA
        )

        expected_combinations_count = expected_metric_combos_count(
            [included_event], len(calculator_utils.METRIC_PERIOD_MONTHS))

        self.assertEqual(expected_combinations_count, len(program_combinations))
        assert all(value == 1 for _combination, value in program_combinations)
Ejemplo n.º 2
0
    def test_map_program_combinations_full_info_probation(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='US_ND',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='US_ND', ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        event_date = date(2009, 10, 31)

        program_events = [
            ProgramReferralEvent(
                state_code='US_ND',
                event_date=event_date,
                program_id='XXX',
                supervision_type=StateSupervisionType.PROBATION,
                assessment_score=22,
                assessment_type=StateAssessmentType.LSIR,
                supervising_officer_external_id='OFFICERZ',
                supervising_district_external_id='135')
        ]

        program_combinations = calculator.map_program_combinations(
            person,
            program_events,
            ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month='2009-10',
            calculation_month_count=-1)

        expected_combinations_count = expected_metric_combos_count(
            program_events)

        self.assertEqual(expected_combinations_count,
                         len(program_combinations))
        assert all(value == 1 for _combination, value in program_combinations)
Ejemplo n.º 3
0
    def test_map_program_combinations(self):
        person = StatePerson.new_with_defaults(
            state_code="US_ND",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race = StatePersonRace.new_with_defaults(state_code="US_ND",
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="US_ND", ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        program_events = [
            ProgramReferralEvent(state_code="US_ND",
                                 event_date=date(2019, 10, 10),
                                 program_id="XXX"),
            ProgramParticipationEvent(state_code="US_ND",
                                      event_date=date(2019, 2, 2),
                                      program_id="ZZZ"),
        ]

        program_combinations = calculator.map_program_combinations(
            person,
            program_events,
            ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=-1,
            person_metadata=_DEFAULT_PERSON_METADATA,
        )

        expected_combinations_count = expected_metric_combos_count(
            program_events)

        self.assertEqual(expected_combinations_count,
                         len(program_combinations))
        assert all(value == 1 for _combination, value in program_combinations)
Ejemplo n.º 4
0
    def setUp(self) -> None:
        self.state_code = "US_ND"
        self.person = StatePerson.new_with_defaults(
            state_code=self.state_code,
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        self.race = StatePersonRace.new_with_defaults(
            state_code=self.state_code, race=Race.WHITE)

        self.person.races = [self.race]

        self.ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code=self.state_code, ethnicity=Ethnicity.NOT_HISPANIC)

        self.person.ethnicities = [self.ethnicity]
        self.metric_producer = metric_producer.ViolationMetricProducer()
        self.pipeline_config = pipeline.ViolationPipeline().pipeline_config
Ejemplo n.º 5
0
    def test_produce_program_metrics(self) -> None:
        person = StatePerson.new_with_defaults(
            state_code="US_ND",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race = StatePersonRace.new_with_defaults(state_code="US_ND", race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="US_ND", ethnicity=Ethnicity.NOT_HISPANIC
        )

        person.ethnicities = [ethnicity]

        program_events = [
            ProgramReferralEvent(
                state_code="US_ND", event_date=date(2019, 10, 10), program_id="XXX"
            ),
            ProgramParticipationEvent(
                state_code="US_ND", event_date=date(2019, 2, 2), program_id="ZZZ"
            ),
        ]

        metrics = self.metric_producer.produce_metrics(
            person,
            program_events,
            ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=-1,
            person_metadata=_DEFAULT_PERSON_METADATA,
            pipeline_job_id=PIPELINE_JOB_ID,
            pipeline_type=self.pipeline_config.pipeline_type,
        )

        expected_count = expected_metrics_count(program_events)

        self.assertEqual(expected_count, len(metrics))
Ejemplo n.º 6
0
    def test_map_program_combinations_relevant_periods(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='US_ND',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='US_ND',
            ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        program_events = [
            ProgramReferralEvent(
                state_code='US_ND',
                event_date=date(2007, 12, 7),
                program_id='XXX',
                supervision_type=StateSupervisionType.PAROLE,
                assessment_score=22,
                assessment_type=StateAssessmentType.LSIR,
                supervising_officer_external_id='OFFICERZ',
                supervising_district_external_id='135'
            )
        ]

        program_combinations = calculator.map_program_combinations(
            person, program_events, ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=-1,
            person_metadata=_DEFAULT_PERSON_METADATA
        )

        expected_combinations_count = expected_metric_combos_count(
            program_events, len(calculator_utils.METRIC_PERIOD_MONTHS))

        self.assertEqual(expected_combinations_count, len(program_combinations))
        assert all(value == 1 for _combination, value in program_combinations)
Ejemplo n.º 7
0
    def test_map_incarceration_combinations_release_relevant_periods(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='CA',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='CA', ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_event = IncarcerationReleaseEvent(
            state_code='CA',
            event_date=date(2010, 10, 2),
            facility='SAN QUENTIN',
            county_of_residence=_COUNTY_OF_RESIDENCE,
        )

        incarceration_events = [incarceration_event]

        incarceration_combinations = calculator.map_incarceration_combinations(
            person=person,
            incarceration_events=incarceration_events,
            inclusions=ALL_INCLUSIONS_DICT,
            calculation_month_limit=-1)

        expected_combinations_count = expected_metric_combos_count(
            person, incarceration_events, ALL_INCLUSIONS_DICT, 0,
            len(calculator_utils.METRIC_PERIOD_MONTHS))

        self.assertEqual(expected_combinations_count,
                         len(incarceration_combinations))
        assert all(value == 1
                   for _combination, value in incarceration_combinations)

        for combo, _ in incarceration_combinations:
            assert combo.get('year') == 2010
    def test_add_demographic_characteristics(self):
        characteristics = {}

        person = StatePerson.new_with_defaults(
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
            races=[StatePersonRace.new_with_defaults(race=Race.ASIAN)])

        event_date = date(2010, 9, 1)

        updated_characteristics = add_demographic_characteristics(
            characteristics, person, event_date)

        expected_output = {
            'age_bucket': '25-29',
            'race': [Race.ASIAN],
            'gender': Gender.FEMALE
        }

        self.assertEqual(updated_characteristics, expected_output)
Ejemplo n.º 9
0
    def test_add_person_characteristics_IncludeSecondaryExternalId(self):
        person = StatePerson.new_with_defaults(
            state_code="US_XX",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
            races=[StatePersonRace.new_with_defaults(state_code="US_XX", )],
            ethnicities=[
                StatePersonEthnicity.new_with_defaults(state_code="US_XX", )
            ],
            external_ids=[
                StatePersonExternalId.new_with_defaults(external_id="DOC1341",
                                                        id_type="US_XX_DOC",
                                                        state_code="US_XX"),
                StatePersonExternalId.new_with_defaults(external_id="SID9889",
                                                        id_type="US_XX_SID",
                                                        state_code="US_XX"),
            ],
        )

        event_date = date(2010, 9, 1)

        person_metadata = PersonMetadata()

        updated_characteristics = person_characteristics(
            person,
            event_date,
            person_metadata,
            "test_pipeline",
        )

        expected_output = {
            "age_bucket": "25-29",
            "gender": Gender.FEMALE,
            "person_id": person.person_id,
            "person_external_id": "DOC1341",
            "secondary_person_external_id": "SID9889",
        }

        self.assertEqual(updated_characteristics, expected_output)
Ejemplo n.º 10
0
    def test_produce_incarceration_metrics_calculation_month_count_exclude(
            self):
        person = StatePerson.new_with_defaults(
            state_code="US_XX",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race = StatePersonRace.new_with_defaults(state_code="US_XX",
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="US_XX", ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_event = IncarcerationStandardAdmissionEvent(
            state_code="US_XX",
            event_date=date(1990, 3, 12),
            facility="FACILITY X",
            county_of_residence=_COUNTY_OF_RESIDENCE,
        )

        incarceration_events = [incarceration_event]

        metrics = self.metric_producer.produce_metrics(
            person=person,
            identifier_events=incarceration_events,
            metric_inclusions=ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=1,
            person_metadata=_DEFAULT_PERSON_METADATA,
            pipeline_job_id=PIPELINE_JOB_ID,
            pipeline_type=self.pipeline_config.pipeline_type,
        )

        self.assertEqual(0, len(metrics))
Ejemplo n.º 11
0
    def test_add_person_characteristics_EmptyRaceEthnicity(self):
        person = StatePerson.new_with_defaults(
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
            races=[StatePersonRace.new_with_defaults()],
            ethnicities=[StatePersonEthnicity.new_with_defaults()])

        event_date = date(2010, 9, 1)

        person_metadata = PersonMetadata()

        updated_characteristics = person_characteristics(
            person, event_date, person_metadata, 'pipeline')

        expected_output = {
            'age_bucket': '25-29',
            'gender': Gender.FEMALE,
            'person_id': person.person_id
        }

        self.assertEqual(updated_characteristics, expected_output)
Ejemplo n.º 12
0
    def test_map_incarceration_combinations_calculation_month_count_exclude(
            self):
        person = StatePerson.new_with_defaults(
            state_code="CA",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race = StatePersonRace.new_with_defaults(state_code="CA",
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="CA", ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_event = IncarcerationAdmissionEvent(
            state_code="CA",
            event_date=date(1990, 3, 12),
            facility="SAN QUENTIN",
            county_of_residence=_COUNTY_OF_RESIDENCE,
        )

        incarceration_events = [incarceration_event]

        incarceration_combinations = calculator.map_incarceration_combinations(
            person=person,
            incarceration_events=incarceration_events,
            metric_inclusions=ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=1,
            person_metadata=_DEFAULT_PERSON_METADATA,
        )

        self.assertEqual(0, len(incarceration_combinations))
Ejemplo n.º 13
0
    def test_build_person_metadata(self):
        person = StatePerson.new_with_defaults(state_code='US_XX',
                                               person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='US_XX',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='US_XX', ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        person_metadata = build_person_metadata(
            person, self.state_race_ethnicity_population_counts)

        expected_person_metadata = PersonMetadata(
            prioritized_race_or_ethnicity=Race.WHITE.value)

        self.assertEqual(expected_person_metadata, person_metadata)
Ejemplo n.º 14
0
    def test_add_person_characteristics(self):
        person = StatePerson.new_with_defaults(
            state_code='US_XX',
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
            races=[StatePersonRace.new_with_defaults(race=Race.ASIAN)])

        event_date = date(2010, 9, 1)

        person_metadata = PersonMetadata(prioritized_race_or_ethnicity='ASIAN')

        updated_characteristics = person_characteristics(
            person, event_date, person_metadata, 'pipeline')

        expected_output = {
            'person_id': person.person_id,
            'age_bucket': '25-29',
            'race': [Race.ASIAN],
            'prioritized_race_or_ethnicity': 'ASIAN',
            'gender': Gender.FEMALE
        }

        self.assertEqual(updated_characteristics, expected_output)
def test_for_characteristics_races_ethnicities_one_race_one_ethnicity():
    race_white = StatePersonRace.new_with_defaults(state_code='CA',
                                                   race=Race.WHITE)

    ethnicity_hispanic = StatePersonEthnicity.new_with_defaults(
        state_code='CA', ethnicity=Ethnicity.HISPANIC)

    characteristics = {'gender': 'female', 'age': '<25'}

    combinations = calculator_utils.for_characteristics_races_ethnicities(
        [race_white], [ethnicity_hispanic], characteristics)

    assert combinations == [
        {},
        {
            'gender': 'female'
        },
        {
            'age': '<25'
        },
        {
            'gender': 'female',
            'age': '<25'
        },
        {
            'race': Race.WHITE
        },
        {
            'race': Race.WHITE,
            'gender': 'female'
        },
        {
            'age': '<25',
            'race': Race.WHITE
        },
        {
            'age': '<25',
            'race': Race.WHITE,
            'gender': 'female'
        },
        {
            'ethnicity': Ethnicity.HISPANIC
        },
        {
            'ethnicity': Ethnicity.HISPANIC,
            'gender': 'female'
        },
        {
            'age': '<25',
            'ethnicity': Ethnicity.HISPANIC
        },
        {
            'age': '<25',
            'ethnicity': Ethnicity.HISPANIC,
            'gender': 'female'
        },
        {
            'race': Race.WHITE,
            'ethnicity': Ethnicity.HISPANIC
        },
        {
            'race': Race.WHITE,
            'ethnicity': Ethnicity.HISPANIC,
            'gender': 'female'
        },
        {
            'age': '<25',
            'race': Race.WHITE,
            'ethnicity': Ethnicity.HISPANIC
        },
        {
            'race': Race.WHITE,
            'ethnicity': Ethnicity.HISPANIC,
            'age': '<25',
            'gender': 'female'
        },
    ]
Ejemplo n.º 16
0
    def test_map_program_combinations_relevant_periods_multiple_supervisions(self):
        person = StatePerson.new_with_defaults(person_id=12345,
                                               birthdate=date(1984, 8, 31),
                                               gender=Gender.FEMALE)

        race = StatePersonRace.new_with_defaults(state_code='US_ND',
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code='US_ND',
            ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        program_events = [
            ProgramReferralEvent(
                state_code='US_ND',
                event_date=date(2007, 1, 7),
                program_id='XXX',
                supervision_type=StateSupervisionType.PAROLE,
                assessment_score=22,
                assessment_type=StateAssessmentType.LSIR,
                supervising_officer_external_id='OFFICERZ',
                supervising_district_external_id='135'
            ),
            ProgramReferralEvent(
                state_code='US_ND',
                event_date=date(2007, 1, 11),
                program_id='XXX',
                supervision_type=StateSupervisionType.PROBATION,
                assessment_score=22,
                assessment_type=StateAssessmentType.LSIR,
                supervising_officer_external_id='OFFICERZ',
                supervising_district_external_id='135'
            )
        ]

        program_combinations = calculator.map_program_combinations(
            person, program_events, ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=-1,
            person_metadata=_DEFAULT_PERSON_METADATA
        )

        relevant_periods = [36, 12]

        expected_combinations_count = expected_metric_combos_count(
            program_events, len(relevant_periods),
            duplicated_months_different_supervision_types=True)

        self.assertEqual(expected_combinations_count, len(program_combinations))
        assert all(value == 1 for _combination, value in program_combinations)
        parole_combos = 0
        probation_combos = 0
        for combo, _ in program_combinations:
            if combo.get('supervision_type') \
                and combo_has_enum_value_for_key(
                        combo, 'methodology', MetricMethodologyType.PERSON):
                # Ensure that all person-based metrics are of type parole
                if combo.get('supervision_type') == \
                        StateSupervisionType.PAROLE:
                    parole_combos += 1
                elif combo.get('supervision_type') == \
                        StateSupervisionType.PROBATION:
                    probation_combos += 1

        # Assert that there are the same number of parole and probation
        # person-based combinations
        assert parole_combos == probation_combos
Ejemplo n.º 17
0
    def test_map_incarceration_combinations_all_types(self):
        person = StatePerson.new_with_defaults(
            state_code="CA",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race = StatePersonRace.new_with_defaults(state_code="CA",
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="CA", ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_events = [
            IncarcerationStayEvent(
                admission_reason=StateIncarcerationPeriodAdmissionReason.
                PAROLE_REVOCATION,
                admission_reason_raw_text="NEW_ADMISSION",
                supervision_type_at_admission=
                StateSupervisionPeriodSupervisionType.PAROLE,
                state_code="CA",
                event_date=date(2000, 3, 31),
                facility="SAN QUENTIN",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                most_serious_offense_ncic_code=_NCIC_CODE,
                most_serious_offense_statute=_STATUTE,
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
            IncarcerationAdmissionEvent(
                state_code="CA",
                event_date=date(2000, 3, 12),
                facility="SAN QUENTIN",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                admission_reason=AdmissionReason.PAROLE_REVOCATION,
                supervision_type_at_admission=
                StateSupervisionPeriodSupervisionType.PAROLE,
                admission_reason_raw_text="PAROLE_REVOCATION",
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
            IncarcerationReleaseEvent(
                state_code="CA",
                event_date=date(2003, 4, 12),
                facility="SAN QUENTIN",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                supervision_type_at_release=
                StateSupervisionPeriodSupervisionType.PAROLE,
            ),
        ]

        incarceration_combinations = calculator.map_incarceration_combinations(
            person=person,
            incarceration_events=incarceration_events,
            metric_inclusions=ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=-1,
            person_metadata=_DEFAULT_PERSON_METADATA,
        )

        expected_combinations_count = expected_metric_combos_count(
            incarceration_events)

        self.assertEqual(expected_combinations_count,
                         len(incarceration_combinations))
        assert all(value == 1
                   for _combination, value in incarceration_combinations)
Ejemplo n.º 18
0
    def test_map_incarceration_combinations_multiple_overlapping_stays(self):
        person = StatePerson.new_with_defaults(
            state_code="US_ND",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race = StatePersonRace.new_with_defaults(state_code="US_ND",
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="US_ND", ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_events = [
            IncarcerationStayEvent(
                state_code="US_ND",
                event_date=date(2019, 11, 30),
                facility="JRCC",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
            IncarcerationStayEvent(
                state_code="US_ND",
                event_date=date(2019, 11, 30),
                facility="JRCC",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
            IncarcerationStayEvent(
                state_code="US_ND",
                event_date=date(2019, 11, 30),
                facility="JRCC",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
        ]

        incarceration_combinations = calculator.map_incarceration_combinations(
            person=person,
            incarceration_events=incarceration_events,
            metric_inclusions=ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=-1,
            person_metadata=_DEFAULT_PERSON_METADATA,
        )

        expected_combinations_count = expected_metric_combos_count(
            incarceration_events)

        self.assertEqual(expected_combinations_count,
                         len(incarceration_combinations))
        assert all(value == 1
                   for _combination, value in incarceration_combinations)
Ejemplo n.º 19
0
    def testConvert_FullIngestInfo(self):
        # Arrange
        metadata = FakeIngestMetadata.for_state(region="us_nd")

        ingest_info = IngestInfo()
        ingest_info.state_agents.add(state_agent_id="AGENT_ID1",
                                     full_name="AGENT WILLIAMS")
        ingest_info.state_agents.add(state_agent_id="AGENT_ID2",
                                     full_name="AGENT HERNANDEZ")
        ingest_info.state_agents.add(state_agent_id="AGENT_ID3",
                                     full_name="AGENT SMITH")
        ingest_info.state_agents.add(state_agent_id="AGENT_ID4",
                                     full_name="AGENT PO")
        ingest_info.state_agents.add(state_agent_id="JUDGE_AGENT_ID_1",
                                     full_name="JUDGE JUDY")
        ingest_info.state_agents.add(state_agent_id="AGENT_ID_PO",
                                     full_name="AGENT PAROLEY")
        ingest_info.state_agents.add(
            state_agent_id="AGENT_ID_TERM",
            full_name="AGENT TERMY",
            agent_type="SUPERVISION_OFFICER",
        )
        ingest_info.state_agents.add(
            state_agent_id="AGENT_ID_SUPERVISING",
            full_name="SUPERVISING AGENT",
        )

        # We expect the external_ids coming in to have the format
        # [type]:[external_id]
        ii_person_external_id_1 = US_ND_ELITE + ":" + "EXTERNAL_ID1"
        ii_person_external_id_2 = US_ND_SID + ":" + "EXTERNAL_ID2"

        ingest_info.state_people.add(
            state_person_id="PERSON_ID",
            state_person_race_ids=["RACE_ID1", "RACE_ID2"],
            state_person_ethnicity_ids=["ETHNICITY_ID"],
            state_alias_ids=["ALIAS_ID1", "ALIAS_ID2"],
            state_person_external_ids_ids=[
                ii_person_external_id_1,
                ii_person_external_id_2,
            ],
            state_assessment_ids=["ASSESSMENT_ID"],
            state_program_assignment_ids=["PROGRAM_ASSIGNMENT_ID"],
            state_sentence_group_ids=["GROUP_ID1", "GROUP_ID2"],
            supervising_officer_id="AGENT_ID_SUPERVISING",
        )
        ingest_info.state_person_races.add(
            state_person_race_id="RACE_ID1",
            race="WHITE",
        )
        ingest_info.state_person_races.add(state_person_race_id="RACE_ID2",
                                           race="OTHER")
        ingest_info.state_person_ethnicities.add(
            state_person_ethnicity_id="ETHNICITY_ID", ethnicity="HISPANIC")
        ingest_info.state_aliases.add(state_alias_id="ALIAS_ID1",
                                      full_name="LONNY BREAUX")
        ingest_info.state_aliases.add(state_alias_id="ALIAS_ID2",
                                      full_name="FRANK OCEAN")
        ingest_info.state_person_external_ids.add(
            state_person_external_id_id=ii_person_external_id_1,
            id_type=US_ND_ELITE)
        ingest_info.state_person_external_ids.add(
            state_person_external_id_id=ii_person_external_id_2,
            id_type=US_ND_SID)
        ingest_info.state_assessments.add(
            state_assessment_id="ASSESSMENT_ID",
            assessment_class="MENTAL_HEALTH",
            conducting_agent_id="AGENT_ID1",
        )
        ingest_info.state_program_assignments.add(
            state_program_assignment_id="PROGRAM_ASSIGNMENT_ID",
            participation_status="DISCHARGED",
            referral_date="2019/02/10",
            start_date="2019/02/11",
            discharge_date="2019/02/12",
            program_id="PROGRAM_ID",
            program_location_id="PROGRAM_LOCATION_ID",
            discharge_reason="COMPLETED",
            referring_agent_id="AGENT_ID4",
        )
        ingest_info.state_sentence_groups.add(
            state_sentence_group_id="GROUP_ID1",
            state_supervision_sentence_ids=["SUPERVISION_SENTENCE_ID1"],
            state_incarceration_sentence_ids=[
                "INCARCERATION_SENTENCE_ID1",
                "INCARCERATION_SENTENCE_ID2",
            ],
        )
        ingest_info.state_sentence_groups.add(
            state_sentence_group_id="GROUP_ID2",
            state_supervision_sentence_ids=["SUPERVISION_SENTENCE_ID2"],
            state_fine_ids=["FINE_ID"],
        )
        ingest_info.state_fines.add(state_fine_id="FINE_ID", status="PAID")
        ingest_info.state_supervision_sentences.add(
            state_supervision_sentence_id="SUPERVISION_SENTENCE_ID1",
            state_charge_ids=["CHARGE_ID1", "CHARGE_ID2"],
            state_supervision_period_ids=["S_PERIOD_ID1"],
        )
        ingest_info.state_supervision_sentences.add(
            state_supervision_sentence_id="SUPERVISION_SENTENCE_ID2",
            state_charge_ids=["CHARGE_ID2"],
            state_supervision_period_ids=["S_PERIOD_ID2"],
        )
        ingest_info.state_incarceration_sentences.add(
            state_incarceration_sentence_id="INCARCERATION_SENTENCE_ID1",
            state_charge_ids=["CHARGE_ID1"],
            state_incarceration_period_ids=["I_PERIOD_ID"],
        )
        ingest_info.state_incarceration_sentences.add(
            state_incarceration_sentence_id="INCARCERATION_SENTENCE_ID2",
            state_charge_ids=["CHARGE_ID2", "CHARGE_ID3"],
            state_supervision_period_ids=["S_PERIOD_ID3"],
        )
        ingest_info.state_charges.add(
            state_charge_id="CHARGE_ID1",
            state_court_case_id="CASE_ID",
            state_bond_id="BOND_ID",
            classification_type="M",
            classification_subtype="1",
            ncic_code="5006",
        )
        ingest_info.state_charges.add(
            state_charge_id="CHARGE_ID2",
            state_court_case_id="CASE_ID",
            classification_type="M",
            classification_subtype="2",
        )
        ingest_info.state_charges.add(
            state_charge_id="CHARGE_ID3",
            state_court_case_id="CASE_ID",
            classification_type="F",
            classification_subtype="3",
            ncic_code="5006",
            description="Obstruction of investigation",
        )
        ingest_info.state_court_cases.add(
            state_court_case_id="CASE_ID",
            judge_id="JUDGE_AGENT_ID_1",
        )
        ingest_info.state_bonds.add(state_bond_id="BOND_ID", status="POSTED")
        ingest_info.state_supervision_periods.add(
            state_supervision_period_id="S_PERIOD_ID1",
            state_supervision_violation_entry_ids=["VIOLATION_ID"],
            supervision_type="PAROLE",
            supervision_level="MED",
            state_supervision_contact_ids=["SUPERVISION_CONTACT_ID"],
            state_program_assignment_ids=["PROGRAM_ASSIGNMENT_ID"],
        )
        ingest_info.state_supervision_periods.add(
            state_supervision_period_id="S_PERIOD_ID2",
            supervision_type="PAROLE")
        ingest_info.state_supervision_periods.add(
            state_supervision_period_id="S_PERIOD_ID3",
            state_assessment_ids=["ASSESSMENT_ID"],
            supervising_officer_id="AGENT_ID_PO",
            supervision_type="PROBATION",
            state_supervision_case_type_entry_ids=["CASE_TYPE_ID"],
        )
        ingest_info.state_supervision_case_type_entries.add(
            state_supervision_case_type_entry_id="CASE_TYPE_ID",
            case_type="DOMESTIC_VIOLENCE",
        )

        ingest_info.state_incarceration_periods.add(
            state_incarceration_period_id="I_PERIOD_ID",
            state_incarceration_incident_ids=["INCIDENT_ID"],
            state_parole_decision_ids=["DECISION_ID"],
            state_assessment_ids=["ASSESSMENT_ID"],
            state_program_assignment_ids=["PROGRAM_ASSIGNMENT_ID"],
            source_supervision_violation_response_id="RESPONSE_ID",
        )

        ingest_info.state_supervision_violation_type_entries.add(
            state_supervision_violation_type_entry_id="VIOLATION_TYPE_ENTRY_ID",
            violation_type="FELONY",
            state_code="US_ND",
        )

        ingest_info.state_supervision_violated_condition_entries.add(
            state_supervision_violated_condition_entry_id=
            "VIOLATED_CONDITION_ENTRY_ID",
            condition="CURFEW",
            state_code="US_ND",
        )

        ingest_info.state_supervision_violations.add(
            state_supervision_violation_id="VIOLATION_ID",
            state_supervision_violation_response_ids=["RESPONSE_ID"],
            state_supervision_violated_condition_entry_ids=[
                "VIOLATED_CONDITION_ENTRY_ID"
            ],
            state_supervision_violation_type_entry_ids=[
                "VIOLATION_TYPE_ENTRY_ID"
            ],
        )

        ingest_info.state_supervision_violated_condition_entries.add(
            state_supervision_violated_condition_entry_id=
            "VIOLATED_CONDITION_ENTRY_ID",
            condition="CURFEW",
            state_code="US_ND",
        )

        ingest_info.state_supervision_violation_response_decision_entries.add(
            state_supervision_violation_response_decision_entry_id=
            "VIOLATION_RESPONSE_DECISION_ENTRY_ID",
            decision="REVOCATION",
            revocation_type="REINCARCERATION",
            state_code="US_ND",
        )

        ingest_info.state_supervision_violation_responses.add(
            state_supervision_violation_response_id="RESPONSE_ID",
            decision_agent_ids=["AGENT_ID_TERM"],
            state_supervision_violation_response_decision_entry_ids=[
                "VIOLATION_RESPONSE_DECISION_ENTRY_ID"
            ],
            response_type="CITATION",
        )
        ingest_info.state_incarceration_incidents.add(
            state_incarceration_incident_id="INCIDENT_ID",
            incident_type="CONTRABAND",
            responding_officer_id="AGENT_ID2",
            state_incarceration_incident_outcome_ids=["INCIDENT_OUTCOME_ID"],
        )

        ingest_info.state_incarceration_incident_outcomes.add(
            state_incarceration_incident_outcome_id="INCIDENT_OUTCOME_ID",
            outcome_type="GOOD_TIME_LOSS",
            date_effective="2/10/2018",
            hearing_date="2/6/2018",
            report_date="2/8/2018",
            state_code="US_ND",
            outcome_description="Good time",
            punishment_length_days="7",
        )
        ingest_info.state_parole_decisions.add(
            state_parole_decision_id="DECISION_ID",
            decision_agent_ids=["AGENT_ID2", "AGENT_ID3"],
        )
        ingest_info.state_supervision_contacts.add(
            state_supervision_contact_id="SUPERVISION_CONTACT_ID",
            contacted_agent_id="AGENT_ID_PO",
        )

        # Act
        result = self._convert_and_throw_on_errors(ingest_info, metadata)

        # Assert
        supervision_contact = StateSupervisionContact.new_with_defaults(
            external_id="SUPERVISION_CONTACT_ID",
            state_code="US_ND",
            contacted_agent=StateAgent.new_with_defaults(
                external_id="AGENT_ID_PO",
                state_code="US_ND",
                agent_type=StateAgentType.PRESENT_WITHOUT_INFO,
                full_name='{"full_name": "AGENT PAROLEY"}',
            ),
        )

        incident_outcome = StateIncarcerationIncidentOutcome.new_with_defaults(
            external_id="INCIDENT_OUTCOME_ID",
            outcome_type=StateIncarcerationIncidentOutcomeType.GOOD_TIME_LOSS,
            outcome_type_raw_text="GOOD_TIME_LOSS",
            date_effective=datetime.date(year=2018, month=2, day=10),
            hearing_date=datetime.date(year=2018, month=2, day=6),
            report_date=datetime.date(year=2018, month=2, day=8),
            state_code="US_ND",
            outcome_description="GOOD TIME",
            punishment_length_days=7,
        )

        incident = StateIncarcerationIncident.new_with_defaults(
            external_id="INCIDENT_ID",
            state_code="US_ND",
            incident_type=StateIncarcerationIncidentType.CONTRABAND,
            incident_type_raw_text="CONTRABAND",
            responding_officer=StateAgent.new_with_defaults(
                external_id="AGENT_ID2",
                state_code="US_ND",
                agent_type=StateAgentType.PRESENT_WITHOUT_INFO,
                full_name='{"full_name": "AGENT HERNANDEZ"}',
            ),
            incarceration_incident_outcomes=[incident_outcome],
        )

        assessment = StateAssessment.new_with_defaults(
            external_id="ASSESSMENT_ID",
            state_code="US_ND",
            assessment_class=StateAssessmentClass.MENTAL_HEALTH,
            assessment_class_raw_text="MENTAL_HEALTH",
            conducting_agent=StateAgent.new_with_defaults(
                external_id="AGENT_ID1",
                agent_type=StateAgentType.PRESENT_WITHOUT_INFO,
                state_code="US_ND",
                full_name='{"full_name": "AGENT WILLIAMS"}',
            ),
        )

        program_assignment = StateProgramAssignment.new_with_defaults(
            external_id="PROGRAM_ASSIGNMENT_ID",
            state_code="US_ND",
            participation_status=StateProgramAssignmentParticipationStatus.
            DISCHARGED,
            participation_status_raw_text="DISCHARGED",
            referral_date=datetime.date(year=2019, month=2, day=10),
            start_date=datetime.date(year=2019, month=2, day=11),
            discharge_date=datetime.date(year=2019, month=2, day=12),
            program_id="PROGRAM_ID",
            program_location_id="PROGRAM_LOCATION_ID",
            discharge_reason=StateProgramAssignmentDischargeReason.COMPLETED,
            discharge_reason_raw_text="COMPLETED",
            referring_agent=StateAgent.new_with_defaults(
                external_id="AGENT_ID4",
                agent_type=StateAgentType.PRESENT_WITHOUT_INFO,
                state_code="US_ND",
                full_name='{"full_name": "AGENT PO"}',
            ),
        )

        response = StateSupervisionViolationResponse.new_with_defaults(
            external_id="RESPONSE_ID",
            state_code="US_ND",
            response_type=StateSupervisionViolationResponseType.CITATION,
            response_type_raw_text="CITATION",
            decision_agents=[
                StateAgent.new_with_defaults(
                    external_id="AGENT_ID_TERM",
                    state_code="US_ND",
                    full_name='{"full_name": "AGENT TERMY"}',
                    agent_type=StateAgentType.SUPERVISION_OFFICER,
                    agent_type_raw_text="SUPERVISION_OFFICER",
                )
            ],
            supervision_violation_response_decisions=[
                StateSupervisionViolationResponseDecisionEntry.
                new_with_defaults(
                    state_code="US_ND",
                    decision=StateSupervisionViolationResponseDecision.
                    REVOCATION,
                    decision_raw_text="REVOCATION",
                    revocation_type=
                    StateSupervisionViolationResponseRevocationType.
                    REINCARCERATION,
                    revocation_type_raw_text="REINCARCERATION",
                )
            ],
        )

        violation = StateSupervisionViolation.new_with_defaults(
            external_id="VIOLATION_ID",
            state_code="US_ND",
            supervision_violation_responses=[response],
            supervision_violation_types=[
                StateSupervisionViolationTypeEntry.new_with_defaults(
                    state_code="US_ND",
                    violation_type=StateSupervisionViolationType.FELONY,
                    violation_type_raw_text="FELONY",
                )
            ],
            supervision_violated_conditions=[
                StateSupervisionViolatedConditionEntry.new_with_defaults(
                    state_code="US_ND",
                    condition="CURFEW",
                )
            ],
        )

        court_case = StateCourtCase.new_with_defaults(
            external_id="CASE_ID",
            state_code="US_ND",
            status=StateCourtCaseStatus.PRESENT_WITHOUT_INFO,
            court_type=StateCourtType.PRESENT_WITHOUT_INFO,
            judge=StateAgent.new_with_defaults(
                external_id="JUDGE_AGENT_ID_1",
                state_code="US_ND",
                agent_type=StateAgentType.PRESENT_WITHOUT_INFO,
                full_name='{"full_name": "JUDGE JUDY"}',
            ),
        )

        charge_1 = StateCharge.new_with_defaults(
            external_id="CHARGE_ID1",
            classification_type=StateChargeClassificationType.MISDEMEANOR,
            classification_type_raw_text="M",
            classification_subtype="1",
            ncic_code="5006",
            description="FALSE STATEMENT",
            state_code="US_ND",
            status=ChargeStatus.PRESENT_WITHOUT_INFO,
            court_case=court_case,
            bond=StateBond.new_with_defaults(
                external_id="BOND_ID",
                state_code="US_ND",
                status=BondStatus.POSTED,
                status_raw_text="POSTED",
            ),
        )

        charge_2 = StateCharge.new_with_defaults(
            external_id="CHARGE_ID2",
            classification_type=StateChargeClassificationType.MISDEMEANOR,
            classification_type_raw_text="M",
            classification_subtype="2",
            state_code="US_ND",
            status=ChargeStatus.PRESENT_WITHOUT_INFO,
            court_case=court_case,
        )

        charge_3 = StateCharge.new_with_defaults(
            external_id="CHARGE_ID3",
            state_code="US_ND",
            classification_type=StateChargeClassificationType.FELONY,
            classification_type_raw_text="F",
            classification_subtype="3",
            ncic_code="5006",
            description="OBSTRUCTION OF INVESTIGATION",
            status=ChargeStatus.PRESENT_WITHOUT_INFO,
            court_case=court_case,
        )

        incarceration_sentence_1 = StateIncarcerationSentence.new_with_defaults(
            external_id="INCARCERATION_SENTENCE_ID1",
            state_code="US_ND",
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            incarceration_type=StateIncarcerationType.STATE_PRISON,
            charges=[charge_1],
            incarceration_periods=[
                StateIncarcerationPeriod.new_with_defaults(
                    external_id="I_PERIOD_ID",
                    status=StateIncarcerationPeriodStatus.PRESENT_WITHOUT_INFO,
                    incarceration_type=StateIncarcerationType.STATE_PRISON,
                    state_code="US_ND",
                    incarceration_incidents=[incident],
                    program_assignments=[program_assignment],
                    parole_decisions=[
                        StateParoleDecision.new_with_defaults(
                            external_id="DECISION_ID",
                            state_code="US_ND",
                            decision_agents=[
                                StateAgent.new_with_defaults(
                                    external_id="AGENT_ID2",
                                    agent_type=StateAgentType.
                                    PRESENT_WITHOUT_INFO,
                                    state_code="US_ND",
                                    full_name=
                                    '{"full_name": "AGENT HERNANDEZ"}',
                                ),
                                StateAgent.new_with_defaults(
                                    external_id="AGENT_ID3",
                                    state_code="US_ND",
                                    agent_type=StateAgentType.
                                    PRESENT_WITHOUT_INFO,
                                    full_name='{"full_name": "AGENT SMITH"}',
                                ),
                            ],
                        )
                    ],
                    assessments=[assessment],
                    source_supervision_violation_response=response,
                )
            ],
        )

        incarceration_sentence_2 = StateIncarcerationSentence.new_with_defaults(
            external_id="INCARCERATION_SENTENCE_ID2",
            state_code="US_ND",
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            incarceration_type=StateIncarcerationType.STATE_PRISON,
            charges=[charge_2, charge_3],
            supervision_periods=[
                StateSupervisionPeriod.new_with_defaults(
                    external_id="S_PERIOD_ID3",
                    status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO,
                    state_code="US_ND",
                    supervision_type=StateSupervisionType.PROBATION,
                    supervision_type_raw_text="PROBATION",
                    assessments=[assessment],
                    supervising_officer=StateAgent.new_with_defaults(
                        external_id="AGENT_ID_PO",
                        state_code="US_ND",
                        agent_type=StateAgentType.PRESENT_WITHOUT_INFO,
                        full_name='{"full_name": "AGENT PAROLEY"}',
                    ),
                    case_type_entries=[
                        StateSupervisionCaseTypeEntry.new_with_defaults(
                            case_type=StateSupervisionCaseType.
                            DOMESTIC_VIOLENCE,
                            case_type_raw_text="DOMESTIC_VIOLENCE",
                            state_code="US_ND",
                            external_id="CASE_TYPE_ID",
                        )
                    ],
                )
            ],
        )

        expected_result = [
            StatePerson.new_with_defaults(
                state_code="US_ND",
                external_ids=[
                    StatePersonExternalId.new_with_defaults(
                        external_id="EXTERNAL_ID1",
                        state_code="US_ND",
                        id_type=US_ND_ELITE,
                    ),
                    StatePersonExternalId.new_with_defaults(
                        external_id="EXTERNAL_ID2",
                        state_code="US_ND",
                        id_type=US_ND_SID,
                    ),
                ],
                races=[
                    StatePersonRace(race=Race.WHITE,
                                    race_raw_text="WHITE",
                                    state_code="US_ND"),
                    StatePersonRace(race=Race.OTHER,
                                    race_raw_text="OTHER",
                                    state_code="US_ND"),
                ],
                ethnicities=[
                    StatePersonEthnicity(
                        ethnicity=Ethnicity.HISPANIC,
                        ethnicity_raw_text="HISPANIC",
                        state_code="US_ND",
                    )
                ],
                aliases=[
                    StatePersonAlias.new_with_defaults(
                        full_name='{"full_name": "LONNY BREAUX"}',
                        state_code="US_ND"),
                    StatePersonAlias.new_with_defaults(
                        full_name='{"full_name": "FRANK OCEAN"}',
                        state_code="US_ND"),
                ],
                supervising_officer=StateAgent.new_with_defaults(
                    external_id="AGENT_ID_SUPERVISING",
                    state_code="US_ND",
                    agent_type=StateAgentType.PRESENT_WITHOUT_INFO,
                    full_name='{"full_name": "SUPERVISING AGENT"}',
                ),
                assessments=[assessment],
                program_assignments=[program_assignment],
                sentence_groups=[
                    StateSentenceGroup.new_with_defaults(
                        external_id="GROUP_ID1",
                        status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
                        state_code="US_ND",
                        supervision_sentences=[
                            StateSupervisionSentence.new_with_defaults(
                                external_id="SUPERVISION_SENTENCE_ID1",
                                state_code="US_ND",
                                status=StateSentenceStatus.
                                PRESENT_WITHOUT_INFO,
                                charges=[charge_1, charge_2],
                                supervision_periods=[
                                    StateSupervisionPeriod.new_with_defaults(
                                        external_id="S_PERIOD_ID1",
                                        status=StateSupervisionPeriodStatus.
                                        PRESENT_WITHOUT_INFO,
                                        supervision_level=StateSupervisionLevel
                                        .MEDIUM,
                                        supervision_level_raw_text="MED",
                                        state_code="US_ND",
                                        supervision_type=StateSupervisionType.
                                        PAROLE,
                                        supervision_type_raw_text="PAROLE",
                                        supervision_violation_entries=[
                                            violation
                                        ],
                                        program_assignments=[
                                            program_assignment
                                        ],
                                        supervision_contacts=[
                                            supervision_contact
                                        ],
                                    )
                                ],
                            )
                        ],
                        incarceration_sentences=[
                            incarceration_sentence_1,
                            incarceration_sentence_2,
                        ],
                    ),
                    StateSentenceGroup.new_with_defaults(
                        external_id="GROUP_ID2",
                        status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
                        state_code="US_ND",
                        supervision_sentences=[
                            StateSupervisionSentence.new_with_defaults(
                                external_id="SUPERVISION_SENTENCE_ID2",
                                state_code="US_ND",
                                status=StateSentenceStatus.
                                PRESENT_WITHOUT_INFO,
                                charges=[charge_2],
                                supervision_periods=[
                                    StateSupervisionPeriod.new_with_defaults(
                                        external_id="S_PERIOD_ID2",
                                        status=StateSupervisionPeriodStatus.
                                        PRESENT_WITHOUT_INFO,
                                        state_code="US_ND",
                                        supervision_type=StateSupervisionType.
                                        PAROLE,
                                        supervision_type_raw_text="PAROLE",
                                    )
                                ],
                            )
                        ],
                        fines=[
                            StateFine.new_with_defaults(
                                external_id="FINE_ID",
                                state_code="US_ND",
                                status=StateFineStatus.PAID,
                                status_raw_text="PAID",
                            )
                        ],
                    ),
                ],
            )
        ]

        print("\n", expected_result, "\n\n\n", result)

        self.assertCountEqual(expected_result, result)
Ejemplo n.º 20
0
    def testConvert_FullIngestInfo(self):
        # Arrange
        metadata = IngestMetadata('us_nd',
                                  _JURISDICTION_ID,
                                  _INGEST_TIME,
                                  system_level=SystemLevel.STATE)

        ingest_info = IngestInfo()
        ingest_info.state_agents.add(state_agent_id='AGENT_ID1',
                                     full_name='AGENT WILLIAMS')
        ingest_info.state_agents.add(state_agent_id='AGENT_ID2',
                                     full_name='AGENT HERNANDEZ')
        ingest_info.state_agents.add(state_agent_id='AGENT_ID3',
                                     full_name='AGENT SMITH')
        ingest_info.state_agents.add(state_agent_id='AGENT_ID4',
                                     full_name='AGENT PO')
        ingest_info.state_agents.add(state_agent_id='JUDGE_AGENT_ID_1',
                                     full_name='JUDGE JUDY')
        ingest_info.state_agents.add(state_agent_id='AGENT_ID_PO',
                                     full_name='AGENT PAROLEY')
        ingest_info.state_agents.add(state_agent_id='AGENT_ID_TERM',
                                     full_name='AGENT TERMY',
                                     agent_type='SUPERVISION_OFFICER')
        ingest_info.state_agents.add(
            state_agent_id='AGENT_ID_SUPERVISING',
            full_name='SUPERVISING AGENT',
        )

        # We expect the external_ids coming in to have the format
        # [type]:[external_id]
        ii_person_external_id_1 = US_ND_ELITE + ':' + 'EXTERNAL_ID1'
        ii_person_external_id_2 = US_ND_SID + ':' + 'EXTERNAL_ID2'

        ingest_info.state_people.add(
            state_person_id='PERSON_ID',
            state_person_race_ids=['RACE_ID1', 'RACE_ID2'],
            state_person_ethnicity_ids=['ETHNICITY_ID'],
            state_alias_ids=['ALIAS_ID1', 'ALIAS_ID2'],
            state_person_external_ids_ids=[
                ii_person_external_id_1, ii_person_external_id_2
            ],
            state_assessment_ids=['ASSESSMENT_ID'],
            state_program_assignment_ids=['PROGRAM_ASSIGNMENT_ID'],
            state_sentence_group_ids=['GROUP_ID1', 'GROUP_ID2'],
            supervising_officer_id='AGENT_ID_SUPERVISING',
        )
        ingest_info.state_person_races.add(
            state_person_race_id='RACE_ID1',
            race='WHITE',
        )
        ingest_info.state_person_races.add(state_person_race_id='RACE_ID2',
                                           race='OTHER')
        ingest_info.state_person_ethnicities.add(
            state_person_ethnicity_id='ETHNICITY_ID', ethnicity='HISPANIC')
        ingest_info.state_aliases.add(state_alias_id='ALIAS_ID1',
                                      full_name='LONNY BREAUX')
        ingest_info.state_aliases.add(state_alias_id='ALIAS_ID2',
                                      full_name='FRANK OCEAN')
        ingest_info.state_person_external_ids.add(
            state_person_external_id_id=ii_person_external_id_1,
            id_type=US_ND_ELITE)
        ingest_info.state_person_external_ids.add(
            state_person_external_id_id=ii_person_external_id_2,
            id_type=US_ND_SID)
        ingest_info.state_assessments.add(state_assessment_id='ASSESSMENT_ID',
                                          assessment_class='MENTAL_HEALTH',
                                          conducting_agent_id='AGENT_ID1')
        ingest_info.state_program_assignments.add(
            state_program_assignment_id='PROGRAM_ASSIGNMENT_ID',
            participation_status='DISCHARGED',
            referral_date='2019/02/10',
            start_date='2019/02/11',
            discharge_date='2019/02/12',
            program_id='PROGRAM_ID',
            program_location_id='PROGRAM_LOCATION_ID',
            discharge_reason='COMPLETED',
            referring_agent_id='AGENT_ID4')
        ingest_info.state_sentence_groups.add(
            state_sentence_group_id='GROUP_ID1',
            state_supervision_sentence_ids=['SUPERVISION_SENTENCE_ID1'],
            state_incarceration_sentence_ids=[
                'INCARCERATION_SENTENCE_ID1', 'INCARCERATION_SENTENCE_ID2'
            ])
        ingest_info.state_sentence_groups.add(
            state_sentence_group_id='GROUP_ID2',
            state_supervision_sentence_ids=['SUPERVISION_SENTENCE_ID2'],
            state_fine_ids=['FINE_ID'])
        ingest_info.state_fines.add(state_fine_id='FINE_ID', status='PAID')
        ingest_info.state_supervision_sentences.add(
            state_supervision_sentence_id='SUPERVISION_SENTENCE_ID1',
            state_charge_ids=['CHARGE_ID1', 'CHARGE_ID2'],
            state_supervision_period_ids=['S_PERIOD_ID1'])
        ingest_info.state_supervision_sentences.add(
            state_supervision_sentence_id='SUPERVISION_SENTENCE_ID2',
            state_charge_ids=['CHARGE_ID2'],
            state_supervision_period_ids=['S_PERIOD_ID2'])
        ingest_info.state_incarceration_sentences.add(
            state_incarceration_sentence_id='INCARCERATION_SENTENCE_ID1',
            state_charge_ids=['CHARGE_ID1'],
            state_incarceration_period_ids=['I_PERIOD_ID'])
        ingest_info.state_incarceration_sentences.add(
            state_incarceration_sentence_id='INCARCERATION_SENTENCE_ID2',
            state_charge_ids=['CHARGE_ID2', 'CHARGE_ID3'],
            state_supervision_period_ids=['S_PERIOD_ID3'])
        ingest_info.state_charges.add(
            state_charge_id='CHARGE_ID1',
            state_court_case_id='CASE_ID',
            state_bond_id='BOND_ID',
            classification_type='M',
            classification_subtype='1',
            ncic_code='5006',
        )
        ingest_info.state_charges.add(
            state_charge_id='CHARGE_ID2',
            state_court_case_id='CASE_ID',
            classification_type='M',
            classification_subtype='2',
        )
        ingest_info.state_charges.add(
            state_charge_id='CHARGE_ID3',
            state_court_case_id='CASE_ID',
            classification_type='F',
            classification_subtype='3',
            ncic_code='5006',
            description='Obstruction of investigation',
        )
        ingest_info.state_court_cases.add(
            state_court_case_id='CASE_ID',
            judge_id='JUDGE_AGENT_ID_1',
        )
        ingest_info.state_bonds.add(state_bond_id='BOND_ID', status='POSTED')
        ingest_info.state_supervision_periods.add(
            state_supervision_period_id='S_PERIOD_ID1',
            state_supervision_violation_entry_ids=['VIOLATION_ID'],
            supervision_type='PAROLE',
            supervision_level='MED',
            state_supervision_contact_ids=['SUPERVISION_CONTACT_ID'],
            state_program_assignment_ids=['PROGRAM_ASSIGNMENT_ID'])
        ingest_info.state_supervision_periods.add(
            state_supervision_period_id='S_PERIOD_ID2',
            supervision_type='PAROLE')
        ingest_info.state_supervision_periods.add(
            state_supervision_period_id='S_PERIOD_ID3',
            state_assessment_ids=['ASSESSMENT_ID'],
            supervising_officer_id='AGENT_ID_PO',
            supervision_type='PROBATION',
            state_supervision_case_type_entry_ids=['CASE_TYPE_ID'],
        )
        ingest_info.state_supervision_case_type_entries.add(
            state_supervision_case_type_entry_id='CASE_TYPE_ID',
            case_type='DOMESTIC_VIOLENCE')

        ingest_info.state_incarceration_periods.add(
            state_incarceration_period_id='I_PERIOD_ID',
            state_incarceration_incident_ids=['INCIDENT_ID'],
            state_parole_decision_ids=['DECISION_ID'],
            state_assessment_ids=['ASSESSMENT_ID'],
            state_program_assignment_ids=['PROGRAM_ASSIGNMENT_ID'],
            source_supervision_violation_response_id='RESPONSE_ID')

        ingest_info.state_supervision_violation_type_entries.add(
            state_supervision_violation_type_entry_id='VIOLATION_TYPE_ENTRY_ID',
            violation_type='FELONY',
            state_code='US_ND')

        ingest_info.state_supervision_violated_condition_entries.add(
            state_supervision_violated_condition_entry_id=
            'VIOLATED_CONDITION_ENTRY_ID',
            condition='CURFEW',
            state_code='US_ND')

        ingest_info.state_supervision_violations.add(
            state_supervision_violation_id='VIOLATION_ID',
            state_supervision_violation_response_ids=['RESPONSE_ID'],
            state_supervision_violated_condition_entry_ids=[
                'VIOLATED_CONDITION_ENTRY_ID'
            ],
            state_supervision_violation_type_entry_ids=[
                'VIOLATION_TYPE_ENTRY_ID'
            ],
        )

        ingest_info.state_supervision_violated_condition_entries.add(
            state_supervision_violated_condition_entry_id=
            'VIOLATED_CONDITION_ENTRY_ID',
            condition='CURFEW',
            state_code='US_ND')

        ingest_info.state_supervision_violation_response_decision_entries.add(
            state_supervision_violation_response_decision_entry_id=
            'VIOLATION_RESPONSE_DECISION_ENTRY_ID',
            decision='REVOCATION',
            revocation_type='REINCARCERATION',
            state_code='US_ND')

        ingest_info.state_supervision_violation_responses.add(
            state_supervision_violation_response_id='RESPONSE_ID',
            decision_agent_ids=['AGENT_ID_TERM'],
            state_supervision_violation_response_decision_entry_ids=[
                'VIOLATION_RESPONSE_DECISION_ENTRY_ID'
            ],
            response_type='CITATION')
        ingest_info.state_incarceration_incidents.add(
            state_incarceration_incident_id='INCIDENT_ID',
            incident_type='CONTRABAND',
            responding_officer_id='AGENT_ID2',
            state_incarceration_incident_outcome_ids=['INCIDENT_OUTCOME_ID'],
        )

        ingest_info.state_incarceration_incident_outcomes.add(
            state_incarceration_incident_outcome_id='INCIDENT_OUTCOME_ID',
            outcome_type='GOOD_TIME_LOSS',
            date_effective='2/10/2018',
            hearing_date='2/6/2018',
            report_date='2/8/2018',
            state_code='US_ND',
            outcome_description='Good time',
            punishment_length_days='7',
        )
        ingest_info.state_parole_decisions.add(
            state_parole_decision_id='DECISION_ID',
            decision_agent_ids=['AGENT_ID2', 'AGENT_ID3'])
        ingest_info.state_supervision_contacts.add(
            state_supervision_contact_id='SUPERVISION_CONTACT_ID',
            contacted_agent_id='AGENT_ID_PO')

        # Act
        result = self._convert_and_throw_on_errors(ingest_info, metadata)

        # Assert
        supervision_contact = StateSupervisionContact.new_with_defaults(
            external_id='SUPERVISION_CONTACT_ID',
            state_code='US_ND',
            contacted_agent=StateAgent.new_with_defaults(
                external_id='AGENT_ID_PO',
                state_code='US_ND',
                agent_type=StateAgentType.PRESENT_WITHOUT_INFO,
                full_name='{"full_name": "AGENT PAROLEY"}'),
        )

        incident_outcome = StateIncarcerationIncidentOutcome.new_with_defaults(
            external_id='INCIDENT_OUTCOME_ID',
            outcome_type=StateIncarcerationIncidentOutcomeType.GOOD_TIME_LOSS,
            outcome_type_raw_text='GOOD_TIME_LOSS',
            date_effective=datetime.date(year=2018, month=2, day=10),
            hearing_date=datetime.date(year=2018, month=2, day=6),
            report_date=datetime.date(year=2018, month=2, day=8),
            state_code='US_ND',
            outcome_description='GOOD TIME',
            punishment_length_days=7,
        )

        incident = StateIncarcerationIncident.new_with_defaults(
            external_id='INCIDENT_ID',
            state_code='US_ND',
            incident_type=StateIncarcerationIncidentType.CONTRABAND,
            incident_type_raw_text='CONTRABAND',
            responding_officer=StateAgent.new_with_defaults(
                external_id='AGENT_ID2',
                state_code='US_ND',
                agent_type=StateAgentType.PRESENT_WITHOUT_INFO,
                full_name='{"full_name": "AGENT HERNANDEZ"}',
            ),
            incarceration_incident_outcomes=[incident_outcome])

        assessment = StateAssessment.new_with_defaults(
            external_id='ASSESSMENT_ID',
            state_code='US_ND',
            assessment_class=StateAssessmentClass.MENTAL_HEALTH,
            assessment_class_raw_text='MENTAL_HEALTH',
            conducting_agent=StateAgent.new_with_defaults(
                external_id='AGENT_ID1',
                agent_type=StateAgentType.PRESENT_WITHOUT_INFO,
                state_code='US_ND',
                full_name='{"full_name": "AGENT WILLIAMS"}',
            ))

        program_assignment = StateProgramAssignment.new_with_defaults(
            external_id='PROGRAM_ASSIGNMENT_ID',
            state_code='US_ND',
            participation_status=StateProgramAssignmentParticipationStatus.
            DISCHARGED,
            participation_status_raw_text='DISCHARGED',
            referral_date=datetime.date(year=2019, month=2, day=10),
            start_date=datetime.date(year=2019, month=2, day=11),
            discharge_date=datetime.date(year=2019, month=2, day=12),
            program_id='PROGRAM_ID',
            program_location_id='PROGRAM_LOCATION_ID',
            discharge_reason=StateProgramAssignmentDischargeReason.COMPLETED,
            discharge_reason_raw_text='COMPLETED',
            referring_agent=StateAgent.new_with_defaults(
                external_id='AGENT_ID4',
                agent_type=StateAgentType.PRESENT_WITHOUT_INFO,
                state_code='US_ND',
                full_name='{"full_name": "AGENT PO"}'))

        response = StateSupervisionViolationResponse.new_with_defaults(
            external_id='RESPONSE_ID',
            state_code='US_ND',
            response_type=StateSupervisionViolationResponseType.CITATION,
            response_type_raw_text='CITATION',
            decision_agents=[
                StateAgent.new_with_defaults(
                    external_id='AGENT_ID_TERM',
                    state_code='US_ND',
                    full_name='{"full_name": "AGENT TERMY"}',
                    agent_type=StateAgentType.SUPERVISION_OFFICER,
                    agent_type_raw_text='SUPERVISION_OFFICER',
                )
            ],
            supervision_violation_response_decisions=[
                StateSupervisionViolationResponseDecisionEntry.
                new_with_defaults(
                    state_code='US_ND',
                    decision=StateSupervisionViolationResponseDecision.
                    REVOCATION,
                    decision_raw_text='REVOCATION',
                    revocation_type=
                    StateSupervisionViolationResponseRevocationType.
                    REINCARCERATION,
                    revocation_type_raw_text='REINCARCERATION')
            ])

        violation = StateSupervisionViolation.new_with_defaults(
            external_id='VIOLATION_ID',
            state_code='US_ND',
            supervision_violation_responses=[response],
            supervision_violation_types=[
                StateSupervisionViolationTypeEntry.new_with_defaults(
                    state_code='US_ND',
                    violation_type=StateSupervisionViolationType.FELONY,
                    violation_type_raw_text='FELONY',
                )
            ],
            supervision_violated_conditions=[
                StateSupervisionViolatedConditionEntry.new_with_defaults(
                    state_code='US_ND',
                    condition='CURFEW',
                )
            ])

        court_case = StateCourtCase.new_with_defaults(
            external_id='CASE_ID',
            state_code='US_ND',
            status=StateCourtCaseStatus.PRESENT_WITHOUT_INFO,
            court_type=StateCourtType.PRESENT_WITHOUT_INFO,
            judge=StateAgent.new_with_defaults(
                external_id='JUDGE_AGENT_ID_1',
                state_code='US_ND',
                agent_type=StateAgentType.PRESENT_WITHOUT_INFO,
                full_name='{"full_name": "JUDGE JUDY"}',
            ))

        charge_1 = StateCharge.new_with_defaults(
            external_id='CHARGE_ID1',
            classification_type=StateChargeClassificationType.MISDEMEANOR,
            classification_type_raw_text='M',
            classification_subtype='1',
            ncic_code='5006',
            description='FALSE STATEMENT',
            state_code='US_ND',
            status=ChargeStatus.PRESENT_WITHOUT_INFO,
            court_case=court_case,
            bond=StateBond.new_with_defaults(external_id='BOND_ID',
                                             state_code='US_ND',
                                             status=BondStatus.POSTED,
                                             status_raw_text='POSTED'))

        charge_2 = StateCharge.new_with_defaults(
            external_id='CHARGE_ID2',
            classification_type=StateChargeClassificationType.MISDEMEANOR,
            classification_type_raw_text='M',
            classification_subtype='2',
            state_code='US_ND',
            status=ChargeStatus.PRESENT_WITHOUT_INFO,
            court_case=court_case)

        charge_3 = StateCharge.new_with_defaults(
            external_id='CHARGE_ID3',
            state_code='US_ND',
            classification_type=StateChargeClassificationType.FELONY,
            classification_type_raw_text='F',
            classification_subtype='3',
            ncic_code='5006',
            description='OBSTRUCTION OF INVESTIGATION',
            status=ChargeStatus.PRESENT_WITHOUT_INFO,
            court_case=court_case)

        incarceration_sentence_1 = StateIncarcerationSentence.new_with_defaults(
            external_id='INCARCERATION_SENTENCE_ID1',
            state_code='US_ND',
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            incarceration_type=StateIncarcerationType.STATE_PRISON,
            charges=[charge_1],
            incarceration_periods=[
                StateIncarcerationPeriod.new_with_defaults(
                    external_id='I_PERIOD_ID',
                    status=StateIncarcerationPeriodStatus.PRESENT_WITHOUT_INFO,
                    incarceration_type=StateIncarcerationType.STATE_PRISON,
                    state_code='US_ND',
                    incarceration_incidents=[incident],
                    program_assignments=[program_assignment],
                    parole_decisions=[
                        StateParoleDecision.new_with_defaults(
                            external_id='DECISION_ID',
                            state_code='US_ND',
                            decision_agents=[
                                StateAgent.new_with_defaults(
                                    external_id='AGENT_ID2',
                                    agent_type=StateAgentType.
                                    PRESENT_WITHOUT_INFO,
                                    state_code='US_ND',
                                    full_name='{"full_name": "AGENT HERNANDEZ"}'
                                ),
                                StateAgent.new_with_defaults(
                                    external_id='AGENT_ID3',
                                    state_code='US_ND',
                                    agent_type=StateAgentType.
                                    PRESENT_WITHOUT_INFO,
                                    full_name='{"full_name": "AGENT SMITH"}')
                            ])
                    ],
                    assessments=[assessment],
                    source_supervision_violation_response=response,
                )
            ])

        incarceration_sentence_2 = StateIncarcerationSentence.new_with_defaults(
            external_id='INCARCERATION_SENTENCE_ID2',
            state_code='US_ND',
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            incarceration_type=StateIncarcerationType.STATE_PRISON,
            charges=[charge_2, charge_3],
            supervision_periods=[
                StateSupervisionPeriod.new_with_defaults(
                    external_id='S_PERIOD_ID3',
                    status=StateSupervisionPeriodStatus.PRESENT_WITHOUT_INFO,
                    state_code='US_ND',
                    supervision_type=StateSupervisionType.PROBATION,
                    supervision_type_raw_text='PROBATION',
                    assessments=[assessment],
                    supervising_officer=StateAgent.new_with_defaults(
                        external_id='AGENT_ID_PO',
                        state_code='US_ND',
                        agent_type=StateAgentType.PRESENT_WITHOUT_INFO,
                        full_name='{"full_name": "AGENT PAROLEY"}',
                    ),
                    case_type_entries=[
                        StateSupervisionCaseTypeEntry.new_with_defaults(
                            case_type=StateSupervisionCaseType.
                            DOMESTIC_VIOLENCE,
                            case_type_raw_text='DOMESTIC_VIOLENCE',
                            state_code='US_ND',
                            external_id='CASE_TYPE_ID')
                    ])
            ])

        expected_result = [
            StatePerson.new_with_defaults(
                state_code='US_ND',
                external_ids=[
                    StatePersonExternalId.new_with_defaults(
                        external_id='EXTERNAL_ID1',
                        state_code='US_ND',
                        id_type=US_ND_ELITE),
                    StatePersonExternalId.new_with_defaults(
                        external_id='EXTERNAL_ID2',
                        state_code='US_ND',
                        id_type=US_ND_SID)
                ],
                races=[
                    StatePersonRace(race=Race.WHITE,
                                    race_raw_text='WHITE',
                                    state_code='US_ND'),
                    StatePersonRace(race=Race.OTHER,
                                    race_raw_text='OTHER',
                                    state_code='US_ND'),
                ],
                ethnicities=[
                    StatePersonEthnicity(ethnicity=Ethnicity.HISPANIC,
                                         ethnicity_raw_text='HISPANIC',
                                         state_code='US_ND')
                ],
                aliases=[
                    StatePersonAlias.new_with_defaults(
                        full_name='{"full_name": "LONNY BREAUX"}',
                        state_code='US_ND'),
                    StatePersonAlias.new_with_defaults(
                        full_name='{"full_name": "FRANK OCEAN"}',
                        state_code='US_ND'),
                ],
                supervising_officer=StateAgent.new_with_defaults(
                    external_id='AGENT_ID_SUPERVISING',
                    state_code='US_ND',
                    agent_type=StateAgentType.PRESENT_WITHOUT_INFO,
                    full_name='{"full_name": "SUPERVISING AGENT"}'),
                assessments=[assessment],
                program_assignments=[program_assignment],
                sentence_groups=[
                    StateSentenceGroup.new_with_defaults(
                        external_id='GROUP_ID1',
                        status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
                        state_code='US_ND',
                        supervision_sentences=[
                            StateSupervisionSentence.new_with_defaults(
                                external_id='SUPERVISION_SENTENCE_ID1',
                                state_code='US_ND',
                                status=StateSentenceStatus.
                                PRESENT_WITHOUT_INFO,
                                charges=[charge_1, charge_2],
                                supervision_periods=[
                                    StateSupervisionPeriod.new_with_defaults(
                                        external_id='S_PERIOD_ID1',
                                        status=StateSupervisionPeriodStatus.
                                        PRESENT_WITHOUT_INFO,
                                        supervision_level=StateSupervisionLevel
                                        .MEDIUM,
                                        supervision_level_raw_text='MED',
                                        state_code='US_ND',
                                        supervision_type=StateSupervisionType.
                                        PAROLE,
                                        supervision_type_raw_text='PAROLE',
                                        supervision_violation_entries=[
                                            violation
                                        ],
                                        program_assignments=[
                                            program_assignment
                                        ],
                                        supervision_contacts=[
                                            supervision_contact
                                        ],
                                    )
                                ])
                        ],
                        incarceration_sentences=[
                            incarceration_sentence_1, incarceration_sentence_2
                        ]),
                    StateSentenceGroup.new_with_defaults(
                        external_id='GROUP_ID2',
                        status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
                        state_code='US_ND',
                        supervision_sentences=[
                            StateSupervisionSentence.new_with_defaults(
                                external_id='SUPERVISION_SENTENCE_ID2',
                                state_code='US_ND',
                                status=StateSentenceStatus.
                                PRESENT_WITHOUT_INFO,
                                charges=[charge_2],
                                supervision_periods=[
                                    StateSupervisionPeriod.new_with_defaults(
                                        external_id='S_PERIOD_ID2',
                                        status=StateSupervisionPeriodStatus.
                                        PRESENT_WITHOUT_INFO,
                                        state_code='US_ND',
                                        supervision_type=StateSupervisionType.
                                        PAROLE,
                                        supervision_type_raw_text='PAROLE',
                                    )
                                ])
                        ],
                        fines=[
                            StateFine.new_with_defaults(
                                external_id='FINE_ID',
                                state_code='US_ND',
                                status=StateFineStatus.PAID,
                                status_raw_text='PAID')
                        ])
                ])
        ]

        print("\n", expected_result, "\n\n\n", result)

        self.assertCountEqual(expected_result, result)
Ejemplo n.º 21
0
    def test_produce_program_metrics_multiple_supervision_types(self) -> None:
        person = StatePerson.new_with_defaults(
            state_code="US_ND",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race = StatePersonRace.new_with_defaults(state_code="US_ND", race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="US_ND", ethnicity=Ethnicity.NOT_HISPANIC
        )

        person.ethnicities = [ethnicity]

        event_date = date(2009, 10, 7)

        program_events = [
            ProgramReferralEvent(
                state_code="US_ND",
                event_date=event_date,
                program_id="XXX",
                supervision_type=StateSupervisionType.PAROLE,
                assessment_score=22,
                assessment_type=StateAssessmentType.LSIR,
                supervising_officer_external_id="OFFICERZ",
                supervising_district_external_id="135",
            ),
            ProgramReferralEvent(
                state_code="US_ND",
                event_date=date(2009, 10, 7),
                program_id="XXX",
                supervision_type=StateSupervisionType.PROBATION,
                assessment_score=22,
                assessment_type=StateAssessmentType.LSIR,
                supervising_officer_external_id="OFFICERZ",
                supervising_district_external_id="135",
            ),
            ProgramParticipationEvent(
                state_code="US_ND",
                event_date=event_date,
                program_id="XXX",
                program_location_id="YYY",
                supervision_type=StateSupervisionType.PAROLE,
            ),
            ProgramParticipationEvent(
                state_code="US_ND",
                event_date=event_date,
                program_id="XXX",
                program_location_id="YYY",
                supervision_type=StateSupervisionType.PROBATION,
            ),
        ]

        metrics = self.metric_producer.produce_metrics(
            person,
            program_events,
            ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month="2009-10",
            calculation_month_count=-1,
            person_metadata=_DEFAULT_PERSON_METADATA,
            pipeline_job_id=PIPELINE_JOB_ID,
            pipeline_type=self.pipeline_config.pipeline_type,
        )

        expected_count = expected_metrics_count(program_events)

        self.assertEqual(expected_count, len(metrics))
Ejemplo n.º 22
0
    def testConvert_FullIngestInfo(self):
        # Arrange
        metadata = IngestMetadata('us_nd',
                                  _JURISDICTION_ID,
                                  _INGEST_TIME,
                                  system_level=SystemLevel.STATE)

        ingest_info = IngestInfo()
        ingest_info.state_agents.add(state_agent_id='AGENT_ID1',
                                     full_name='AGENT WILLIAMS')
        ingest_info.state_agents.add(state_agent_id='AGENT_ID2',
                                     full_name='AGENT HERNANDEZ')
        ingest_info.state_agents.add(state_agent_id='AGENT_ID3',
                                     full_name='AGENT SMITH')
        ingest_info.state_agents.add(state_agent_id='JUDGE_AGENT_ID_1',
                                     full_name='JUDGE JUDY')

        # We expect the external_ids coming in to have the format
        # [type]:[external_id]
        ii_person_external_id_1 = US_ND_ELITE + ':' + 'EXTERNAL_ID1'
        ii_person_external_id_2 = US_ND_SID + ':' + 'EXTERNAL_ID2'

        ingest_info.state_people.add(
            state_person_id='PERSON_ID',
            state_person_race_ids=['RACE_ID1', 'RACE_ID2'],
            state_person_ethnicity_ids=['ETHNICITY_ID'],
            state_alias_ids=['ALIAS_ID1', 'ALIAS_ID2'],
            state_person_external_ids_ids=[
                ii_person_external_id_1, ii_person_external_id_2
            ],
            state_assessment_ids=['ASSESSMENT_ID'],
            state_sentence_group_ids=['GROUP_ID1', 'GROUP_ID2'])
        ingest_info.state_person_races.add(
            state_person_race_id='RACE_ID1',
            race='WHITE',
        )
        ingest_info.state_person_races.add(state_person_race_id='RACE_ID2',
                                           race='OTHER')
        ingest_info.state_person_ethnicities.add(
            state_person_ethnicity_id='ETHNICITY_ID', ethnicity='HISPANIC')
        ingest_info.state_aliases.add(state_alias_id='ALIAS_ID1',
                                      full_name='LONNY BREAUX')
        ingest_info.state_aliases.add(state_alias_id='ALIAS_ID2',
                                      full_name='FRANK OCEAN')
        ingest_info.state_person_external_ids.add(
            state_person_external_id_id=ii_person_external_id_1,
            id_type=US_ND_ELITE)
        ingest_info.state_person_external_ids.add(
            state_person_external_id_id=ii_person_external_id_2,
            id_type=US_ND_SID)
        ingest_info.state_assessments.add(state_assessment_id='ASSESSMENT_ID',
                                          assessment_class='MENTAL_HEALTH',
                                          conducting_agent_id='AGENT_ID1')
        ingest_info.state_sentence_groups.add(
            state_sentence_group_id='GROUP_ID1',
            state_supervision_sentence_ids=['SUPERVISION_SENTENCE_ID1'],
            state_incarceration_sentence_ids=[
                'INCARCERATION_SENTENCE_ID1', 'INCARCERATION_SENTENCE_ID2'
            ])
        ingest_info.state_sentence_groups.add(
            state_sentence_group_id='GROUP_ID2',
            state_supervision_sentence_ids=['SUPERVISION_SENTENCE_ID2'],
            state_fine_ids=['FINE_ID'])
        ingest_info.state_fines.add(state_fine_id='FINE_ID', status='PAID')
        ingest_info.state_supervision_sentences.add(
            state_supervision_sentence_id='SUPERVISION_SENTENCE_ID1',
            state_charge_ids=['CHARGE_ID1', 'CHARGE_ID2'],
            state_supervision_period_ids=['S_PERIOD_ID1'])
        ingest_info.state_supervision_sentences.add(
            state_supervision_sentence_id='SUPERVISION_SENTENCE_ID2',
            state_charge_ids=['CHARGE_ID2'],
            state_supervision_period_ids=['S_PERIOD_ID2'])
        ingest_info.state_incarceration_sentences.add(
            state_incarceration_sentence_id='INCARCERATION_SENTENCE_ID1',
            state_charge_ids=['CHARGE_ID1'],
            state_incarceration_period_ids=['I_PERIOD_ID'])
        ingest_info.state_incarceration_sentences.add(
            state_incarceration_sentence_id='INCARCERATION_SENTENCE_ID2',
            state_charge_ids=['CHARGE_ID2', 'CHARGE_ID3'],
            state_supervision_period_ids=['S_PERIOD_ID3'])
        ingest_info.state_charges.add(
            state_charge_id='CHARGE_ID1',
            state_court_case_id='CASE_ID',
            state_bond_id='BOND_ID',
            classification_type='M',
            classification_subtype='1',
        )
        ingest_info.state_charges.add(
            state_charge_id='CHARGE_ID2',
            state_court_case_id='CASE_ID',
            classification_type='M',
            classification_subtype='2',
        )
        ingest_info.state_charges.add(state_charge_id='CHARGE_ID3',
                                      state_court_case_id='CASE_ID',
                                      classification_type='F',
                                      classification_subtype='3')
        ingest_info.state_court_cases.add(
            state_court_case_id='CASE_ID',
            judge_id='JUDGE_AGENT_ID_1',
        )
        ingest_info.state_bonds.add(state_bond_id='BOND_ID', status='POSTED')
        ingest_info.state_supervision_periods.add(
            state_supervision_period_id='S_PERIOD_ID1',
            state_supervision_violation_ids=['VIOLATION_ID'],
            supervision_type='PAROLE',
            supervision_level='MED')
        ingest_info.state_supervision_periods.add(
            state_supervision_period_id='S_PERIOD_ID2',
            supervision_type='PAROLE')
        ingest_info.state_supervision_periods.add(
            state_supervision_period_id='S_PERIOD_ID3',
            state_assessment_ids=['ASSESSMENT_ID'],
            supervision_type='PROBATION')
        ingest_info.state_incarceration_periods.add(
            state_incarceration_period_id='I_PERIOD_ID',
            state_incarceration_incident_ids=['INCIDENT_ID'],
            state_parole_decision_ids=['DECISION_ID'],
            state_assessment_ids=['ASSESSMENT_ID'],
        )
        ingest_info.state_supervision_violations.add(
            state_supervision_violation_id='VIOLATION_ID',
            state_supervision_violation_response_ids=['RESPONSE_ID'])
        ingest_info.state_supervision_violation_responses.add(
            state_supervision_violation_response_id='RESPONSE_ID',
            response_type='CITATION')
        ingest_info.state_incarceration_incidents.add(
            state_incarceration_incident_id='INCIDENT_ID',
            incident_type='CONTRABAND',
            responding_officer_id='AGENT_ID2',
            state_incarceration_incident_outcome_ids=['INCIDENT_OUTCOME_ID'],
        )

        ingest_info.state_incarceration_incident_outcomes.add(
            state_incarceration_incident_outcome_id='INCIDENT_OUTCOME_ID',
            outcome_type='GOOD_TIME_LOSS',
            date_effective='2/10/2018',
            state_code='US_ND',
            outcome_description='Good time',
            punishment_length_days='7',
        )
        ingest_info.state_parole_decisions.add(
            state_parole_decision_id='DECISION_ID',
            decision_agent_ids=['AGENT_ID2', 'AGENT_ID3'])

        # Act
        result = self._convert_and_throw_on_errors(ingest_info, metadata)

        # Assert
        incident_outcome = StateIncarcerationIncidentOutcome.new_with_defaults(
            external_id='INCIDENT_OUTCOME_ID',
            outcome_type=StateIncarcerationIncidentOutcomeType.GOOD_TIME_LOSS,
            outcome_type_raw_text='GOOD_TIME_LOSS',
            date_effective=datetime.date(year=2018, month=2, day=10),
            state_code='US_ND',
            outcome_description='GOOD TIME',
            punishment_length_days=7,
        )

        incident = StateIncarcerationIncident.new_with_defaults(
            external_id='INCIDENT_ID',
            state_code='US_ND',
            incident_type=StateIncarcerationIncidentType.CONTRABAND,
            incident_type_raw_text='CONTRABAND',
            responding_officer=StateAgent.new_with_defaults(
                external_id='AGENT_ID2',
                state_code='US_ND',
                full_name='{"full_name": "AGENT HERNANDEZ"}',
            ),
            incarceration_incident_outcomes=[incident_outcome])

        assessment = StateAssessment.new_with_defaults(
            external_id='ASSESSMENT_ID',
            state_code='US_ND',
            assessment_class=StateAssessmentClass.MENTAL_HEALTH,
            assessment_class_raw_text='MENTAL_HEALTH',
            conducting_agent=StateAgent.new_with_defaults(
                external_id='AGENT_ID1',
                state_code='US_ND',
                full_name='{"full_name": "AGENT WILLIAMS"}',
            ))

        violation = StateSupervisionViolation.new_with_defaults(
            external_id='VIOLATION_ID',
            state_code='US_ND',
            supervision_violation_responses=[
                StateSupervisionViolationResponse.new_with_defaults(
                    external_id='RESPONSE_ID',
                    state_code='US_ND',
                    response_type=StateSupervisionViolationResponseType.
                    CITATION,
                    response_type_raw_text='CITATION')
            ])

        court_case = StateCourtCase.new_with_defaults(
            external_id='CASE_ID',
            state_code='US_ND',
            status=StateCourtCaseStatus.PRESENT_WITHOUT_INFO,
            court_type=StateCourtType.PRESENT_WITHOUT_INFO,
            judge=StateAgent.new_with_defaults(
                external_id='JUDGE_AGENT_ID_1',
                state_code='US_ND',
                full_name='{"full_name": "JUDGE JUDY"}',
            ))

        charge_1 = StateCharge.new_with_defaults(
            external_id='CHARGE_ID1',
            classification_type=StateChargeClassificationType.MISDEMEANOR,
            classification_type_raw_text='M',
            classification_subtype='1',
            state_code='US_ND',
            status=ChargeStatus.PRESENT_WITHOUT_INFO,
            court_case=court_case,
            bond=StateBond.new_with_defaults(external_id='BOND_ID',
                                             state_code='US_ND',
                                             status=BondStatus.POSTED,
                                             status_raw_text='POSTED'))

        charge_2 = StateCharge.new_with_defaults(
            external_id='CHARGE_ID2',
            classification_type=StateChargeClassificationType.MISDEMEANOR,
            classification_type_raw_text='M',
            classification_subtype='2',
            state_code='US_ND',
            status=ChargeStatus.PRESENT_WITHOUT_INFO,
            court_case=court_case)

        charge_3 = StateCharge.new_with_defaults(
            external_id='CHARGE_ID3',
            state_code='US_ND',
            classification_type=StateChargeClassificationType.FELONY,
            classification_type_raw_text='F',
            classification_subtype='3',
            status=ChargeStatus.PRESENT_WITHOUT_INFO,
            court_case=court_case)

        expected_result = [
            StatePerson.new_with_defaults(
                external_ids=[
                    StatePersonExternalId.new_with_defaults(
                        external_id='EXTERNAL_ID1',
                        state_code='US_ND',
                        id_type=US_ND_ELITE),
                    StatePersonExternalId.new_with_defaults(
                        external_id='EXTERNAL_ID2',
                        state_code='US_ND',
                        id_type=US_ND_SID)
                ],
                races=[
                    StatePersonRace(race=Race.WHITE,
                                    race_raw_text='WHITE',
                                    state_code='US_ND'),
                    StatePersonRace(race=Race.OTHER,
                                    race_raw_text='OTHER',
                                    state_code='US_ND'),
                ],
                ethnicities=[
                    StatePersonEthnicity(ethnicity=Ethnicity.HISPANIC,
                                         ethnicity_raw_text='HISPANIC',
                                         state_code='US_ND')
                ],
                aliases=[
                    StatePersonAlias.new_with_defaults(
                        full_name='{"full_name": "LONNY BREAUX"}',
                        state_code='US_ND'),
                    StatePersonAlias.new_with_defaults(
                        full_name='{"full_name": "FRANK OCEAN"}',
                        state_code='US_ND'),
                ],
                assessments=[assessment],
                sentence_groups=[
                    StateSentenceGroup.new_with_defaults(
                        external_id='GROUP_ID1',
                        status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
                        state_code='US_ND',
                        supervision_sentences=[
                            StateSupervisionSentence.new_with_defaults(
                                external_id='SUPERVISION_SENTENCE_ID1',
                                state_code='US_ND',
                                status=StateSentenceStatus.
                                PRESENT_WITHOUT_INFO,
                                charges=[charge_1, charge_2],
                                supervision_periods=[
                                    StateSupervisionPeriod.new_with_defaults(
                                        external_id='S_PERIOD_ID1',
                                        status=StateSupervisionPeriodStatus.
                                        PRESENT_WITHOUT_INFO,
                                        supervision_level=StateSupervisionLevel
                                        .MEDIUM,
                                        supervision_level_raw_text='MED',
                                        state_code='US_ND',
                                        supervision_type=StateSupervisionType.
                                        PAROLE,
                                        supervision_type_raw_text='PAROLE',
                                        supervision_violations=[violation])
                                ])
                        ],
                        incarceration_sentences=[
                            StateIncarcerationSentence.new_with_defaults(
                                external_id='INCARCERATION_SENTENCE_ID1',
                                state_code='US_ND',
                                status=StateSentenceStatus.
                                PRESENT_WITHOUT_INFO,
                                incarceration_type=StateIncarcerationType.
                                STATE_PRISON,
                                charges=[charge_1],
                                incarceration_periods=[
                                    StateIncarcerationPeriod.new_with_defaults(
                                        external_id='I_PERIOD_ID',
                                        status=StateIncarcerationPeriodStatus.
                                        PRESENT_WITHOUT_INFO,
                                        incarceration_type=
                                        StateIncarcerationType.STATE_PRISON,
                                        state_code='US_ND',
                                        incarceration_incidents=[incident],
                                        parole_decisions=[
                                            StateParoleDecision.
                                            new_with_defaults(
                                                external_id='DECISION_ID',
                                                state_code='US_ND',
                                                decision_agents=[
                                                    StateAgent.
                                                    new_with_defaults(
                                                        external_id='AGENT_ID2',
                                                        state_code='US_ND',
                                                        full_name=
                                                        '{"full_name": '
                                                        '"AGENT HERNANDEZ"}'),
                                                    StateAgent.
                                                    new_with_defaults(
                                                        external_id='AGENT_ID3',
                                                        state_code='US_ND',
                                                        full_name=
                                                        '{"full_name": '
                                                        '"AGENT SMITH"}')
                                                ])
                                        ],
                                        assessments=[assessment],
                                    )
                                ]),
                            StateIncarcerationSentence.new_with_defaults(
                                external_id='INCARCERATION_SENTENCE_ID2',
                                state_code='US_ND',
                                status=StateSentenceStatus.
                                PRESENT_WITHOUT_INFO,
                                incarceration_type=StateIncarcerationType.
                                STATE_PRISON,
                                charges=[charge_2, charge_3],
                                supervision_periods=[
                                    StateSupervisionPeriod.new_with_defaults(
                                        external_id='S_PERIOD_ID3',
                                        status=StateSupervisionPeriodStatus.
                                        PRESENT_WITHOUT_INFO,
                                        state_code='US_ND',
                                        supervision_type=StateSupervisionType.
                                        PROBATION,
                                        supervision_type_raw_text='PROBATION',
                                        assessments=[assessment])
                                ])
                        ]),
                    StateSentenceGroup.new_with_defaults(
                        external_id='GROUP_ID2',
                        status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
                        state_code='US_ND',
                        supervision_sentences=[
                            StateSupervisionSentence.new_with_defaults(
                                external_id='SUPERVISION_SENTENCE_ID2',
                                state_code='US_ND',
                                status=StateSentenceStatus.
                                PRESENT_WITHOUT_INFO,
                                charges=[charge_2],
                                supervision_periods=[
                                    StateSupervisionPeriod.new_with_defaults(
                                        external_id='S_PERIOD_ID2',
                                        status=StateSupervisionPeriodStatus.
                                        PRESENT_WITHOUT_INFO,
                                        state_code='US_ND',
                                        supervision_type=StateSupervisionType.
                                        PAROLE,
                                        supervision_type_raw_text='PAROLE',
                                    )
                                ])
                        ],
                        fines=[
                            StateFine.new_with_defaults(
                                external_id='FINE_ID',
                                state_code='US_ND',
                                status=StateFineStatus.PAID,
                                status_raw_text='PAID')
                        ])
                ])
        ]

        print(expected_result, "\n\n\n", result)

        self.assertEqual(result, expected_result)
Ejemplo n.º 23
0
    def test_map_incarceration_combinations_includes_statute_output(self):
        person = StatePerson.new_with_defaults(
            state_code="CA",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race = StatePersonRace.new_with_defaults(state_code="CA",
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="CA", ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_events = [
            IncarcerationStayEvent(
                state_code="CA",
                event_date=date(2010, 3, 31),
                facility="FACILITY 33",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                most_serious_offense_ncic_code=_NCIC_CODE,
                most_serious_offense_statute=_STATUTE,
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
            IncarcerationStayEvent(
                state_code="CA",
                event_date=date(2010, 4, 30),
                facility="FACILITY 33",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                most_serious_offense_ncic_code=_NCIC_CODE,
                most_serious_offense_statute=_STATUTE,
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
            IncarcerationStayEvent(
                state_code="CA",
                event_date=date(2010, 5, 31),
                facility="FACILITY 33",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                most_serious_offense_ncic_code=_NCIC_CODE,
                most_serious_offense_statute=_STATUTE,
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
        ]

        incarceration_combinations = calculator.map_incarceration_combinations(
            person=person,
            incarceration_events=incarceration_events,
            metric_inclusions=ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=-1,
            person_metadata=_DEFAULT_PERSON_METADATA,
        )

        expected_combinations_count = expected_metric_combos_count(
            incarceration_events)

        self.assertEqual(expected_combinations_count,
                         len(incarceration_combinations))
        assert all(value == 1
                   for _combination, value in incarceration_combinations)
        assert all(
            combo.get("most_serious_offense_statute") is not None
            for combo, value in incarceration_combinations
            if combo.get("person_id") is not None)
Ejemplo n.º 24
0
    def test_produce_incarceration_metrics_all_types(self):
        person = StatePerson.new_with_defaults(
            state_code="US_XX",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race = StatePersonRace.new_with_defaults(state_code="US_XX",
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="US_XX", ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_events = [
            IncarcerationStayEvent(
                admission_reason=StateIncarcerationPeriodAdmissionReason.
                PAROLE_REVOCATION,
                admission_reason_raw_text="NEW_ADMISSION",
                state_code="US_XX",
                event_date=date(2000, 3, 31),
                facility="FACILITY X",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                most_serious_offense_ncic_code=_NCIC_CODE,
                most_serious_offense_statute=_STATUTE,
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
            IncarcerationStandardAdmissionEvent(
                state_code="US_XX",
                event_date=date(2000, 3, 12),
                facility="FACILITY X",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                admission_reason=AdmissionReason.PAROLE_REVOCATION,
                admission_reason_raw_text="PAROLE_REVOCATION",
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
            IncarcerationReleaseEvent(
                state_code="US_XX",
                event_date=date(2003, 4, 12),
                facility="FACILITY X",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                supervision_type_at_release=
                StateSupervisionPeriodSupervisionType.PAROLE,
            ),
            IncarcerationCommitmentFromSupervisionAdmissionEvent(
                state_code="US_XX",
                event_date=date(2000, 3, 12),
                facility="FACILITY X",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                admission_reason=AdmissionReason.PAROLE_REVOCATION,
                admission_reason_raw_text="PAROLE_REVOCATION",
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
                supervision_type=StateSupervisionPeriodSupervisionType.PAROLE,
            ),
        ]

        metrics = self.metric_producer.produce_metrics(
            person=person,
            identifier_events=incarceration_events,
            metric_inclusions=ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=-1,
            person_metadata=_DEFAULT_PERSON_METADATA,
            pipeline_job_id=PIPELINE_JOB_ID,
            pipeline_type=self.pipeline_config.pipeline_type,
        )

        expected_count = self.expected_metrics_count(incarceration_events)

        self.assertEqual(expected_count, len(metrics))
Ejemplo n.º 25
0
    def test_produce_incarceration_metrics_includes_statute_output(self):
        person = StatePerson.new_with_defaults(
            state_code="US_XX",
            person_id=12345,
            birthdate=date(1984, 8, 31),
            gender=Gender.FEMALE,
        )

        race = StatePersonRace.new_with_defaults(state_code="US_XX",
                                                 race=Race.WHITE)

        person.races = [race]

        ethnicity = StatePersonEthnicity.new_with_defaults(
            state_code="US_XX", ethnicity=Ethnicity.NOT_HISPANIC)

        person.ethnicities = [ethnicity]

        incarceration_events = [
            IncarcerationStayEvent(
                state_code="US_XX",
                event_date=date(2010, 3, 31),
                facility="FACILITY 33",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                most_serious_offense_ncic_code=_NCIC_CODE,
                most_serious_offense_statute=_STATUTE,
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
            IncarcerationStayEvent(
                state_code="US_XX",
                event_date=date(2010, 4, 30),
                facility="FACILITY 33",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                most_serious_offense_ncic_code=_NCIC_CODE,
                most_serious_offense_statute=_STATUTE,
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
            IncarcerationStayEvent(
                state_code="US_XX",
                event_date=date(2010, 5, 31),
                facility="FACILITY 33",
                county_of_residence=_COUNTY_OF_RESIDENCE,
                most_serious_offense_ncic_code=_NCIC_CODE,
                most_serious_offense_statute=_STATUTE,
                specialized_purpose_for_incarceration=
                StateSpecializedPurposeForIncarceration.TREATMENT_IN_PRISON,
            ),
        ]

        metrics = self.metric_producer.produce_metrics(
            person=person,
            identifier_events=incarceration_events,
            metric_inclusions=ALL_METRICS_INCLUSIONS_DICT,
            calculation_end_month=None,
            calculation_month_count=-1,
            person_metadata=_DEFAULT_PERSON_METADATA,
            pipeline_job_id=PIPELINE_JOB_ID,
            pipeline_type=self.pipeline_config.pipeline_type,
        )

        expected_count = self.expected_metrics_count(incarceration_events)

        self.assertEqual(expected_count, len(metrics))

        assert all(
            isinstance(metric, IncarcerationPopulationMetric)
            and metric.most_serious_offense_statute is not None
            for metric in metrics if metric.person_id is not None)
def test_for_characteristics_races_ethnicities_multiple_races_and_ethnicities(
):
    race_white = StatePersonRace.new_with_defaults(state_code='CA',
                                                   race=Race.WHITE)

    race_black = StatePersonRace.new_with_defaults(state_code='CA',
                                                   race=Race.BLACK)

    ethnicity_hispanic = StatePersonEthnicity.new_with_defaults(
        state_code='CA', ethnicity=Ethnicity.HISPANIC)

    ethnicity_not_hispanic = StatePersonEthnicity.new_with_defaults(
        state_code='CA', ethnicity=Ethnicity.NOT_HISPANIC)

    characteristics = {'age': '<25'}

    combinations = calculator_utils.for_characteristics_races_ethnicities(
        [race_white, race_black], [ethnicity_hispanic, ethnicity_not_hispanic],
        characteristics)

    assert combinations == [{}, {
        'age': '<25'
    }, {
        'race': Race.WHITE
    }, {
        'age': '<25',
        'race': Race.WHITE
    }, {
        'race': Race.BLACK
    }, {
        'age': '<25',
        'race': Race.BLACK
    }, {
        'ethnicity': Ethnicity.HISPANIC
    }, {
        'age': '<25',
        'ethnicity': Ethnicity.HISPANIC
    }, {
        'ethnicity': Ethnicity.NOT_HISPANIC
    }, {
        'age': '<25',
        'ethnicity': Ethnicity.NOT_HISPANIC
    }, {
        'race': Race.WHITE,
        'ethnicity': Ethnicity.HISPANIC
    }, {
        'age': '<25',
        'race': Race.WHITE,
        'ethnicity': Ethnicity.HISPANIC
    }, {
        'race': Race.WHITE,
        'ethnicity': Ethnicity.NOT_HISPANIC
    }, {
        'age': '<25',
        'race': Race.WHITE,
        'ethnicity': Ethnicity.NOT_HISPANIC
    }, {
        'race': Race.BLACK,
        'ethnicity': Ethnicity.HISPANIC
    }, {
        'age': '<25',
        'race': Race.BLACK,
        'ethnicity': Ethnicity.HISPANIC
    }, {
        'race': Race.BLACK,
        'ethnicity': Ethnicity.NOT_HISPANIC
    }, {
        'age': '<25',
        'race': Race.BLACK,
        'ethnicity': Ethnicity.NOT_HISPANIC
    }]