def test_mergeIncarcerationPeriods_doNotMergeNonConsecutiveSequences( self) -> None: incarceration_period_1 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_1, admission_reason=StateIncarcerationPeriodAdmissionReason. NEW_ADMISSION, ) incarceration_period_2 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID_3, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER, ) ingested_incarceration_periods = [ incarceration_period_1, incarceration_period_2, ] expected_incarceration_periods = [ incarceration_period_1, incarceration_period_2, ] merged_periods = _merge_incarceration_periods_helper( ingested_incarceration_periods) self.assert_schema_object_lists_equal(expected_incarceration_periods, merged_periods) incarceration_period_2.external_id = _EXTERNAL_ID_2 expected_merged_incarceration_period_1 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID + "|" + _EXTERNAL_ID_2, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_1, admission_reason=StateIncarcerationPeriodAdmissionReason. NEW_ADMISSION, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER, ) expected_incarceration_periods = [ expected_merged_incarceration_period_1 ] merged_periods = _merge_incarceration_periods_helper( ingested_incarceration_periods) self.assert_schema_object_lists_equal(expected_incarceration_periods, merged_periods)
def test_mergeIncarcerationPeriods_doNotMergeWithPlaceholder(self) -> None: incarceration_period = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_1, admission_reason=StateIncarcerationPeriodAdmissionReason. NEW_ADMISSION, ) placeholder_incarceration_period = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, status=StateIncarcerationPeriodStatus.PRESENT_WITHOUT_INFO, ) ingested_incarceration_periods = [ incarceration_period, placeholder_incarceration_period, ] expected_periods = [ incarceration_period, placeholder_incarceration_period ] merged_periods = _merge_incarceration_periods_helper( ingested_incarceration_periods) self.assert_schema_object_lists_equal(expected_periods, merged_periods)
def test_mergeIncarcerationPeriods_doNotMergeNonConsecutiveSequences(self): incarceration_period_1 = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_1, admission_reason=StateIncarcerationPeriodAdmissionReason. NEW_ADMISSION) incarceration_period_2 = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID_3, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER) ingested_incarceration_periods = [ incarceration_period_1, incarceration_period_2 ] expected_incarceration_periods = [ attr.evolve(incarceration_period_1), attr.evolve(incarceration_period_2) ] merged_periods = _merge_incarceration_periods_helper( ingested_incarceration_periods) self.assertCountEqual(expected_incarceration_periods, merged_periods) incarceration_period_2.external_id = _EXTERNAL_ID_2 expected_merged_incarceration_period_1 = \ StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID + '|' + _EXTERNAL_ID_2, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_1, admission_reason= StateIncarcerationPeriodAdmissionReason.NEW_ADMISSION, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER) expected_incarceration_periods = [ expected_merged_incarceration_period_1 ] merged_periods = _merge_incarceration_periods_helper( ingested_incarceration_periods) self.assertCountEqual(expected_incarceration_periods, merged_periods)
def test_mergeIncarcerationPeriods_doNotMergeWithPlaceholder(self): incarceration_period = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_1, admission_reason= StateIncarcerationPeriodAdmissionReason.NEW_ADMISSION) placeholder_incarceration_period = \ StateIncarcerationPeriod.new_with_defaults() ingested_incarceration_periods = [ incarceration_period, placeholder_incarceration_period] expected_periods = [ attr.evolve(incarceration_period), attr.evolve(placeholder_incarceration_period)] merged_periods = _merge_incarceration_periods_helper( ingested_incarceration_periods) self.assertCountEqual(expected_periods, merged_periods)
def test_mergeIncarcerationPeriods_multipleTransfersSameDate(self): incarceration_period_1 = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_1, admission_reason= StateIncarcerationPeriodAdmissionReason.NEW_ADMISSION) incarceration_period_2 = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID_2, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER) incarceration_period_3 = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID_3, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY_2, admission_date=_DATE_2, admission_reason= StateIncarcerationPeriodAdmissionReason.TRANSFER) incarceration_period_4 = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID_4, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY_2, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER) incarceration_period_5 = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID_5, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY_3, admission_date=_DATE_2, admission_reason= StateIncarcerationPeriodAdmissionReason.TRANSFER) incarceration_period_6 = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID_6, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY_3, release_date=_DATE_2, release_reason= StateIncarcerationPeriodReleaseReason.SENTENCE_SERVED) expected_merged_incarceration_period_1 = \ StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID + '|' + _EXTERNAL_ID_2, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_1, admission_reason= StateIncarcerationPeriodAdmissionReason.NEW_ADMISSION, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER) expected_merged_incarceration_period_2 = \ StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID_3 + '|' + _EXTERNAL_ID_4, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY_2, admission_date=_DATE_2, admission_reason= StateIncarcerationPeriodAdmissionReason.TRANSFER, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER) expected_merged_incarceration_period_3 = \ StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID_5 + '|' + _EXTERNAL_ID_6, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY_3, admission_date=_DATE_2, admission_reason= StateIncarcerationPeriodAdmissionReason.TRANSFER, release_date=_DATE_2, release_reason= StateIncarcerationPeriodReleaseReason.SENTENCE_SERVED) expected_incarceration_periods = [ expected_merged_incarceration_period_1, expected_merged_incarceration_period_2, expected_merged_incarceration_period_3] ingested_incarceration_periods = [ incarceration_period_1, incarceration_period_5, incarceration_period_2, incarceration_period_4, incarceration_period_3, incarceration_period_6 ] merged_periods = _merge_incarceration_periods_helper( ingested_incarceration_periods) self.assertCountEqual(expected_incarceration_periods, merged_periods)
def test_mergeIncarcerationPeriods(self): incarceration_period_1 = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_1, admission_reason= StateIncarcerationPeriodAdmissionReason.NEW_ADMISSION) incarceration_period_2 = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID_2, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER) incarceration_period_3 = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID_3, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY_2, admission_date=_DATE_2, admission_reason= StateIncarcerationPeriodAdmissionReason.TRANSFER) incarceration_period_4 = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID_4, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY_2, release_date=_DATE_3, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER) incarceration_period_5 = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID_5, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_4, admission_reason= StateIncarcerationPeriodAdmissionReason.TRANSFER) incarceration_period_6 = StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID_6, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY_3, release_date=_DATE_5, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER) placeholder_period = StateIncarcerationPeriod.new_with_defaults() expected_merged_incarceration_period_1 = \ StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID + '|' + _EXTERNAL_ID_2, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_1, admission_reason= StateIncarcerationPeriodAdmissionReason.NEW_ADMISSION, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER) expected_merged_incarceration_period_2 = \ StateIncarcerationPeriod.new_with_defaults( external_id=_EXTERNAL_ID_3 + '|' + _EXTERNAL_ID_4, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY_2, admission_date=_DATE_2, admission_reason= StateIncarcerationPeriodAdmissionReason.TRANSFER, release_date=_DATE_3, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER) expected_unmerged_incarceration_period = attr.evolve( incarceration_period_5) expected_unmerged_incarceration_period_another = attr.evolve( incarceration_period_6) expected_placeholder_period = attr.evolve(placeholder_period) expected_incarceration_periods = [ expected_placeholder_period, expected_merged_incarceration_period_1, expected_merged_incarceration_period_2, expected_unmerged_incarceration_period, expected_unmerged_incarceration_period_another] ingested_incarceration_periods = [ placeholder_period, incarceration_period_1, incarceration_period_5, incarceration_period_2, incarceration_period_4, incarceration_period_3, incarceration_period_6 ] merged_periods = _merge_incarceration_periods_helper( ingested_incarceration_periods) self.assertCountEqual(expected_incarceration_periods, merged_periods)
def test_mergeIncarcerationPeriods_multipleTransfersSameDate(self) -> None: incarceration_period_1 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_1, admission_reason=StateIncarcerationPeriodAdmissionReason. NEW_ADMISSION, ) incarceration_period_2 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID_2, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER, ) incarceration_period_3 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID_3, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY_2, admission_date=_DATE_2, admission_reason=StateIncarcerationPeriodAdmissionReason.TRANSFER, ) incarceration_period_4 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID_4, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY_2, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER, ) incarceration_period_5 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID_5, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY_3, admission_date=_DATE_2, admission_reason=StateIncarcerationPeriodAdmissionReason.TRANSFER, ) incarceration_period_6 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID_6, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY_3, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason. SENTENCE_SERVED, ) expected_merged_incarceration_period_1 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID + "|" + _EXTERNAL_ID_2, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_1, admission_reason=StateIncarcerationPeriodAdmissionReason. NEW_ADMISSION, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER, ) expected_merged_incarceration_period_2 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID_3 + "|" + _EXTERNAL_ID_4, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY_2, admission_date=_DATE_2, admission_reason=StateIncarcerationPeriodAdmissionReason.TRANSFER, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER, ) expected_merged_incarceration_period_3 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID_5 + "|" + _EXTERNAL_ID_6, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY_3, admission_date=_DATE_2, admission_reason=StateIncarcerationPeriodAdmissionReason.TRANSFER, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason. SENTENCE_SERVED, ) expected_incarceration_periods = [ expected_merged_incarceration_period_1, expected_merged_incarceration_period_2, expected_merged_incarceration_period_3, ] ingested_incarceration_periods = [ incarceration_period_1, incarceration_period_5, incarceration_period_2, incarceration_period_4, incarceration_period_3, incarceration_period_6, ] merged_periods = _merge_incarceration_periods_helper( ingested_incarceration_periods) self.assert_schema_object_lists_equal(expected_incarceration_periods, merged_periods)
def test_mergeIncarcerationPeriods(self) -> None: incarceration_period_1 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_1, admission_reason=StateIncarcerationPeriodAdmissionReason. NEW_ADMISSION, ) incarceration_period_2 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID_2, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER, ) incarceration_period_3 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID_3, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY_2, admission_date=_DATE_2, admission_reason=StateIncarcerationPeriodAdmissionReason.TRANSFER, ) incarceration_period_4 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID_4, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY_2, release_date=_DATE_3, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER, ) incarceration_period_5 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID_5, status=StateIncarcerationPeriodStatus.IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_4, admission_reason=StateIncarcerationPeriodAdmissionReason.TRANSFER, ) incarceration_period_6 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID_6, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY_3, release_date=_DATE_5, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER, ) placeholder_period = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, status=StateIncarcerationPeriodStatus.PRESENT_WITHOUT_INFO, ) expected_merged_incarceration_period_1 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID + "|" + _EXTERNAL_ID_2, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY, admission_date=_DATE_1, admission_reason=StateIncarcerationPeriodAdmissionReason. NEW_ADMISSION, release_date=_DATE_2, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER, ) expected_merged_incarceration_period_2 = schema.StateIncarcerationPeriod( state_code=_STATE_CODE, external_id=_EXTERNAL_ID_3 + "|" + _EXTERNAL_ID_4, status=StateIncarcerationPeriodStatus.NOT_IN_CUSTODY, facility=_FACILITY_2, admission_date=_DATE_2, admission_reason=StateIncarcerationPeriodAdmissionReason.TRANSFER, release_date=_DATE_3, release_reason=StateIncarcerationPeriodReleaseReason.TRANSFER, ) expected_incarceration_periods = [ placeholder_period, expected_merged_incarceration_period_1, expected_merged_incarceration_period_2, incarceration_period_5, incarceration_period_6, ] ingested_incarceration_periods = [ placeholder_period, incarceration_period_1, incarceration_period_5, incarceration_period_2, incarceration_period_4, incarceration_period_3, incarceration_period_6, ] merged_periods = _merge_incarceration_periods_helper( ingested_incarceration_periods) self.assert_schema_object_lists_equal(expected_incarceration_periods, merged_periods)