def merge_incomplete_periods( new_entity: schema.StateIncarcerationPeriod, old_entity: schema.StateIncarcerationPeriod, ) -> schema.StateIncarcerationPeriod: """Merges two incarceration periods with information about admission and release into one period. Assumes the status of the release event is the most relevant, up-to-date status. Args: new_entity: The out-of-session period (i.e. new to this ingest run). old_entity: The in-session period (i.e. pulled out of the DB), if there is one. """ # Complete match, perform normal merge. if new_entity.external_id == old_entity.external_id: default_merge_flat_fields(new_entity=new_entity, old_entity=old_entity) return old_entity # Determine updated external_id new_complete = is_incarceration_period_complete(new_entity) old_complete = is_incarceration_period_complete(old_entity) if new_complete != old_complete: updated_external_id = ( new_entity.external_id if new_complete else old_entity.external_id ) else: admission_period, release_period = ( (new_entity, old_entity) if new_entity.admission_date else (old_entity, new_entity) ) updated_external_id = ( admission_period.external_id + _INCARCERATION_PERIOD_ID_DELIMITER + release_period.external_id ) # Keep the new status if the new period is a release period updated_status = new_entity.status if new_entity.release_date else old_entity.status updated_status_raw_text = ( new_entity.status_raw_text if new_entity.release_date else old_entity.status_raw_text ) # Copy all fields from new onto old new_fields = get_set_entity_field_names(new_entity, EntityFieldType.FLAT_FIELD) for child_field_name in new_fields: old_entity.set_field(child_field_name, new_entity.get_field(child_field_name)) # Always update the external id and status old_entity.external_id = updated_external_id old_entity.status = updated_status old_entity.status_raw_text = updated_status_raw_text return old_entity
def test_mergeFlatFields_twoDbEntities(self) -> None: to_entity = schema.StateSentenceGroup( state_code=_STATE_CODE, sentence_group_id=_ID, county_code="county_code", status=StateSentenceStatus.SERVING, ) from_entity = schema.StateSentenceGroup( state_code=_STATE_CODE, sentence_group_id=_ID_2, county_code="county_code-updated", max_length_days=10, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, ) expected_entity = schema.StateSentenceGroup( state_code=_STATE_CODE, sentence_group_id=_ID, county_code="county_code-updated", max_length_days=10, status=StateSentenceStatus.SERVING.value, ) merged_entity = default_merge_flat_fields(new_entity=from_entity, old_entity=to_entity) self.assert_schema_objects_equal(expected_entity, merged_entity)
def test_mergeFlatFields(self): ing_entity = schema.StateSentenceGroup( county_code='county_code-updated', max_length_days=10, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value) db_entity = schema.StateSentenceGroup( sentence_group_id=_ID, county_code='county_code', status=StateSentenceStatus.SERVING) expected_entity = schema.StateSentenceGroup( sentence_group_id=_ID, county_code='county_code-updated', max_length_days=10, status=StateSentenceStatus.SERVING.value) merged_entity = default_merge_flat_fields( new_entity=ing_entity, old_entity=db_entity) self.assert_schema_objects_equal(expected_entity, merged_entity)