def is_fully_incarcerated_for_range(self, range_to_cover: DateRange) -> bool: """Returns True if this person is incarcerated for the full duration of the date range.""" months_range_overlaps = range_to_cover.get_months_range_overlaps_at_all( ) if not months_range_overlaps: return False months_without_complete_incarceration = [] for year, month in months_range_overlaps: was_incarcerated_all_month = ( year, month) in self.months_fully_incarcerated if not was_incarcerated_all_month: months_without_complete_incarceration.append((year, month)) for year, month in months_without_complete_incarceration: overlapping_periods = self.month_to_overlapping_incarceration_periods[ year][month] range_portion_overlapping_month = range_to_cover.portion_overlapping_with_month( year, month) if not range_portion_overlapping_month: raise ValueError( f'Expecting only months that overlap with the range, month ({year}, {month}) does not.' ) remaining_ranges_to_cover = self._get_portions_of_range_not_covered_by_periods_subset( range_portion_overlapping_month, overlapping_periods) if remaining_ranges_to_cover: return False return True
def is_excluded_from_supervision_population_for_range( self, range_to_cover: DateRange) -> bool: """Returns True if this person is incarcerated for the full duration of the date range.""" months_range_overlaps = range_to_cover.get_months_range_overlaps_at_all( ) if not months_range_overlaps: return False months_without_exclusion_from_supervision = [] for year, month in months_range_overlaps: was_incarcerated_all_month = ( year, month, ) in self.months_excluded_from_supervision_population if not was_incarcerated_all_month: months_without_exclusion_from_supervision.append((year, month)) for year, month in months_without_exclusion_from_supervision: overlapping_periods = ( self.month_to_overlapping_ips_not_under_supervision_authority[ year][month]) range_portion_overlapping_month = ( range_to_cover.portion_overlapping_with_month(year, month)) if not range_portion_overlapping_month: raise ValueError( f"Expecting only months that overlap with the range, month ({year}, {month}) does not." ) remaining_ranges_to_cover = ( self._get_portions_of_range_not_covered_by_periods_subset( range_portion_overlapping_month, overlapping_periods)) if remaining_ranges_to_cover: return False return True
class TestDateRange(unittest.TestCase): """Tests for DateRange""" def setUp(self) -> None: self.negative_day_range = DateRange( lower_bound_inclusive_date=datetime.date(2019, 2, 3), upper_bound_exclusive_date=datetime.date(2019, 2, 2)) self.zero_day_range = DateRange( lower_bound_inclusive_date=datetime.date(2019, 2, 3), upper_bound_exclusive_date=datetime.date(2019, 2, 3)) self.one_day_range = DateRange( lower_bound_inclusive_date=datetime.date(2019, 2, 3), upper_bound_exclusive_date=datetime.date(2019, 2, 4)) self.single_month_range = DateRange( lower_bound_inclusive_date=datetime.date(2019, 2, 1), upper_bound_exclusive_date=datetime.date(2019, 3, 1)) self.multi_month_range = DateRange( lower_bound_inclusive_date=datetime.date(2019, 2, 3), upper_bound_exclusive_date=datetime.date(2019, 4, 10)) def test_get_months_range_overlaps_at_all(self): self.assertEqual( [], self.negative_day_range.get_months_range_overlaps_at_all()) self.assertEqual( [], self.zero_day_range.get_months_range_overlaps_at_all()) self.assertEqual([(2019, 2)], self.one_day_range.get_months_range_overlaps_at_all()) self.assertEqual( [(2019, 2)], self.single_month_range.get_months_range_overlaps_at_all()) self.assertEqual( [(2019, 2), (2019, 3), (2019, 4)], self.multi_month_range.get_months_range_overlaps_at_all()) def test_portion_overlapping_with_month(self): self.assertEqual( None, self.negative_day_range.portion_overlapping_with_month(2019, 2)) self.assertEqual( None, self.zero_day_range.portion_overlapping_with_month(2019, 2)) self.assertEqual( self.one_day_range, self.one_day_range.portion_overlapping_with_month(2019, 2)) self.assertEqual( self.single_month_range, self.single_month_range.portion_overlapping_with_month(2019, 2)) self.assertEqual( DateRange(lower_bound_inclusive_date=datetime.date(2019, 2, 3), upper_bound_exclusive_date=datetime.date(2019, 3, 1)), self.multi_month_range.portion_overlapping_with_month(2019, 2)) self.assertEqual( DateRange(lower_bound_inclusive_date=datetime.date(2019, 3, 1), upper_bound_exclusive_date=datetime.date(2019, 4, 1)), self.multi_month_range.portion_overlapping_with_month(2019, 3)) self.assertEqual( DateRange(lower_bound_inclusive_date=datetime.date(2019, 4, 1), upper_bound_exclusive_date=datetime.date(2019, 4, 10)), self.multi_month_range.portion_overlapping_with_month(2019, 4))