def test_getMostRelevantSupervisionType_chooseDualIfExists(self) -> None:
     for supervision_type in StateSupervisionPeriodSupervisionType:
         types = {
             StateSupervisionPeriodSupervisionType.DUAL, supervision_type
         }
         self.assertEqual(
             StateSupervisionPeriodSupervisionType.DUAL,
             get_most_relevant_supervision_type(types),
         )
 def test_getMostRelevantSupervisionType_dualIfProbationAndParole(
         self) -> None:
     types = {
         StateSupervisionPeriodSupervisionType.PROBATION,
         StateSupervisionPeriodSupervisionType.PAROLE,
     }
     self.assertEqual(
         StateSupervisionPeriodSupervisionType.DUAL,
         get_most_relevant_supervision_type(types),
     )
def us_id_get_most_recent_supervision_period_supervision_type_before_upper_bound_day(
    upper_bound_exclusive_date: date,
    lower_bound_inclusive_date: Optional[date],
    supervision_periods: List[StateSupervisionPeriod],
) -> Optional[StateSupervisionPeriodSupervisionType]:
    """Finds the most recent nonnull supervision period supervision type on the supervision periods, preceding or
    overlapping the provided date. An optional lower bound may be provided to limit the lookback window.

    Returns the most recent StateSupervisionPeriodSupervisionType. If there is no valid supervision
    type found (e.g. the person has only been incarcerated for the time window), returns None. In the case where
    multiple SupervisionPeriodSupervisionTypes end on the same day, this returns only the most relevant
    SupervisionPeriodSupervisionType based on our own ranking.
    """
    supervision_types_by_end_date: Dict[
        date, Set[StateSupervisionPeriodSupervisionType]] = defaultdict(set)

    lower_bound_exclusive_date = (lower_bound_inclusive_date -
                                  relativedelta(days=1)
                                  if lower_bound_inclusive_date else date.min)

    for supervision_period in supervision_periods:
        start_date = supervision_period.start_date

        if not start_date:
            continue

        termination_date = (supervision_period.termination_date
                            if supervision_period.termination_date else
                            date.today())

        supervision_period_supervision_type = (
            supervision_period.supervision_period_supervision_type)

        if not supervision_period_supervision_type:
            continue

        if not date_spans_overlap_exclusive(
                start_1=lower_bound_exclusive_date,
                end_1=upper_bound_exclusive_date,
                start_2=start_date,
                end_2=termination_date,
        ):
            continue

        supervision_types_by_end_date[termination_date].add(
            supervision_period_supervision_type)

    if not supervision_types_by_end_date:
        return None

    max_end_date = max(supervision_types_by_end_date.keys())

    return get_most_relevant_supervision_type(
        supervision_types_by_end_date[max_end_date])
Exemplo n.º 4
0
def supervision_period_supervision_type_mapper(
    supervision_types_str: Optional[str],
) -> Optional[StateSupervisionPeriodSupervisionType]:
    """Maps a list of supervision type codes to a supervision type. If both probation and parole types are present, this
    will return StateSupervisionPeriodSupervisionType.DUAL."""
    if not supervision_types_str:
        return None

    supervision_type_strs = supervision_types_str.split(" ")

    supervision_types = set()
    for supervision_type_str in supervision_type_strs:
        supervision_type = STR_TO_SUPERVISION_PERIOD_SUPERVISION_TYPE_MAPPINGS.get(
            supervision_type_str, None)
        if not supervision_type:
            raise ValueError(
                f"No mapping for supervision period supervision type {supervision_type}"
            )

        supervision_types.add(supervision_type)

    return get_most_relevant_supervision_type(supervision_types)
 def test_getMostRelevantSupervisionType_allEnums(self) -> None:
     for supervision_type in StateSupervisionPeriodSupervisionType:
         types = {supervision_type}
         self.assertEqual(supervision_type,
                          get_most_relevant_supervision_type(types))