def get_free_periods(self): total_time = Period(self.opening_time, self.closing_time) all_blocked_periods = list( map(lambda a: a.get_period(), self.appointments)) return total_time.subtract(all_blocked_periods)
def test_period_is_overlapping(): start = datetime.fromisoformat('2020-10-01T12:00:00') end = datetime.fromisoformat('2020-10-01T13:00:00') period1 = Period(start, end) period2 = Period(start, end) is_overlapping = period1.is_overlapped(period2) assert is_overlapping == True
def test_period_is_overlapping_5(): start1 = datetime.fromisoformat('2020-10-01T12:00:00') end1 = datetime.fromisoformat('2020-10-01T15:00:00') period1 = Period(start1, end1) start2 = datetime.fromisoformat('2020-10-01T15:00:00') end2 = datetime.fromisoformat('2020-10-01T18:00:00') period2 = Period(start2, end2) is_overlapping = period1.is_overlapped(period2) assert is_overlapping == True
def test_period_contains_3(): start1 = datetime.fromisoformat('2020-10-01T10:00:00') end1 = datetime.fromisoformat('2020-10-01T18:00:00') period1 = Period(start1, end1) start2 = datetime.fromisoformat('2020-10-01T17:00:00') end2 = datetime.fromisoformat('2020-10-01T22:00:00') period2 = Period(start2, end2) is_contained = period1.contains(period2) assert is_contained == False
def test_period_subtract_contained_2(): start1 = datetime.fromisoformat('2020-10-01T14:00:00') end1 = datetime.fromisoformat('2020-10-01T15:00:00') period1 = Period(start1, end1) start2 = datetime.fromisoformat('2020-10-01T10:00:00') end2 = datetime.fromisoformat('2020-10-01T18:00:00') period2 = Period(start2, end2) subtracted = period1 - period2 assert len(subtracted) == 0
def test_period_subtract_not_overlapping(): start1 = datetime.fromisoformat('2000-10-01T12:00:00') end1 = datetime.fromisoformat('2000-10-01T16:00:00') period1 = Period(start1, end1) start2 = datetime.fromisoformat('2020-10-01T12:00:00') end2 = datetime.fromisoformat('2020-10-01T18:00:00') period2 = Period(start2, end2) subtracted = period1 - period2 assert len(subtracted) == 1 assert period1 in subtracted
def test_calendar_get_free_periods_2(): appointment = Appointment('2020-10-01T10:00:00', '2020-10-01T12:00:00') calendar = Calendar() calendar.add_appointment(appointment) free_periods = calendar.get_free_periods() assert len(free_periods) == 2 assert Period.fromIsoFormat( '2020-10-01T08:00:00', '2020-10-01T10:00:00') in free_periods assert Period.fromIsoFormat( '2020-10-01T12:00:00', '2020-10-01T18:00:00') in free_periods
def test_period_get_overlapping(): start1 = datetime.fromisoformat('2020-10-01T12:00:00') end1 = datetime.fromisoformat('2020-10-01T18:00:00') period1 = Period(start1, end1) start2 = datetime.fromisoformat('2020-10-01T09:00:00') end2 = datetime.fromisoformat('2020-10-01T12:30:00') period2 = Period(start2, end2) overlapping = period1.get_overlapped_period(period2) expected_start = datetime.fromisoformat('2020-10-01T12:00:00') expected_end = datetime.fromisoformat('2020-10-01T12:30:00') assert overlapping == Period(expected_start, expected_end)
def test_period_add_not_overlapping(): start1 = datetime.fromisoformat('2000-10-01T12:00:00') end1 = datetime.fromisoformat('2000-10-01T18:00:00') period1 = Period(start1, end1) start2 = datetime.fromisoformat('2020-10-01T09:00:00') end2 = datetime.fromisoformat('2020-10-01T12:30:00') period2 = Period(start2, end2) added = period1 + period2 assert len(added) == 2 assert period1 in added assert period2 in added
def test_sort_2(): start1 = datetime.fromisoformat('2000-10-01T10:00:00') end1 = datetime.fromisoformat('2000-10-01T14:00:00') period1 = Period(start1, end1) start2 = datetime.fromisoformat('2020-10-01T10:00:00') end2 = datetime.fromisoformat('2020-10-01T14:00:00') period2 = Period(start2, end2) sorted_periods = sort_by_start([period2, period1]) assert len(sorted_periods) == 2 assert sorted_periods[0] == period1 assert sorted_periods[1] == period2
def test_merge_3(): period1 = Period.fromIsoFormat( '2020-10-01T10:00:00', '2020-10-01T14:00:00') period2 = Period.fromIsoFormat( '2020-10-01T13:00:00', '2020-10-01T18:00:00') merged = merge([period2, period1]) expected_period = Period.fromIsoFormat( '2020-10-01T10:00:00', '2020-10-01T18:00:00') assert len(merged) == 1 assert expected_period in merged
def test_subtract_2(): period = Period.fromIsoFormat('2000-10-01T10:00:00', '2000-10-01T18:00:00') subtract = Period.fromIsoFormat( '2000-10-01T14:00:00', '2000-10-01T15:00:00') subtracted = period.subtract([subtract]) expected_1 = Period.fromIsoFormat( '2000-10-01T10:00:00', '2000-10-01T14:00:00') expected_2 = Period.fromIsoFormat( '2000-10-01T15:00:00', '2000-10-01T18:00:00') assert len(subtracted) == 2 assert expected_1 in subtracted assert expected_2 in subtracted
def test_subtract(): period = Period.fromIsoFormat('2000-10-01T10:00:00', '2000-10-01T14:00:00') subtracted = period.subtract([]) assert len(subtracted) == 1 assert period in subtracted
def test_sort_1(): start1 = datetime.fromisoformat('2020-10-01T10:00:00') end1 = datetime.fromisoformat('2020-10-01T14:00:00') period1 = Period(start1, end1) sorted_periods = sort_by_start([period1]) assert len(sorted_periods) == 1
def test_merge_4(): period1 = Period.fromIsoFormat( '2020-10-01T08:00:00', '2020-10-01T12:00:00') period2 = Period.fromIsoFormat( '2020-10-01T14:00:00', '2020-10-01T15:00:00') period3 = Period.fromIsoFormat( '2020-10-01T12:00:00', '2020-10-01T18:00:00') merged = merge([period2, period3, period1]) expected_1 = Period.fromIsoFormat( '2020-10-01T08:00:00', '2020-10-01T18:00:00') assert len(merged) == 1 assert expected_1 in merged
def test_appointment(): appointment = Appointment('2020-10-01T08:00:00', '2020-10-01T12:00:00') period = appointment.get_period() assert period is not None assert period == Period.fromIsoFormat('2020-10-01T08:00:00', '2020-10-01T12:00:00')
def test_period_add_overlapping_2(): start1 = datetime.fromisoformat('2020-10-01T12:00:00') end1 = datetime.fromisoformat('2020-10-01T18:00:00') period1 = Period(start1, end1) start2 = datetime.fromisoformat('2020-10-01T10:00:00') end2 = datetime.fromisoformat('2020-10-01T16:00:00') period2 = Period(start2, end2) added = period1 + period2 expected_start = datetime.fromisoformat('2020-10-01T10:00:00') expected_end = datetime.fromisoformat('2020-10-01T18:00:00') expected_period = Period(expected_start, expected_end) assert len(added) == 1 assert expected_period in added
def test_calendar_get_free_periods_for_date_2(): calendar = Calendar() calendar.set_opening_times('THURSDAY', '08:00:00', '18:00:00') appointment1 = Appointment( start='2020-10-01T10:00:00', end='2020-10-01T12:00:00') calendar.add_appointment(appointment1) target_date = date.fromisoformat('2020-10-01') free_periods = calendar.get_free_periods_for_date(target_date) assert len(free_periods) == 2 assert Period.fromIsoFormat( '2020-10-01T08:00:00', '2020-10-01T10:00:00') in free_periods assert Period.fromIsoFormat( '2020-10-01T12:00:00', '2020-10-01T18:00:00') in free_periods
def test_merge_2(): start1 = datetime.fromisoformat('2020-10-01T10:00:00') end1 = datetime.fromisoformat('2020-10-01T14:00:00') period1 = Period(start1, end1) merged = merge([period1]) assert len(merged) == 1 assert period1 in merged
def test_period_subtract_overlapping_3(): start1 = datetime.fromisoformat('2020-10-01T10:00:00') end1 = datetime.fromisoformat('2020-10-01T18:00:00') period1 = Period(start1, end1) start2 = datetime.fromisoformat('2020-10-01T16:00:00') end2 = datetime.fromisoformat('2020-10-01T22:00:00') period2 = Period(start2, end2) subtracted = period1 - period2 expected_start = datetime.fromisoformat('2020-10-01T10:00:00') expected_end = datetime.fromisoformat('2020-10-01T16:00:00') expected_period = Period(expected_start, expected_end) assert len(subtracted) == 1 assert expected_period in subtracted
def test_calendar_get_free_periods_3(): appointment1 = Appointment( start='2020-10-01T10:00:00', end='2020-10-01T12:00:00') appointment2 = Appointment( start='2020-10-01T14:00:00', end='2020-10-01T15:00:00') appointment3 = Appointment( start='2020-10-01T14:00:00', end='2020-10-01T18:00:00') calendar = Calendar() calendar.add_appointment(appointment1) calendar.add_appointment(appointment2) calendar.add_appointment(appointment3) free_periods = calendar.get_free_periods() assert len(free_periods) == 2 assert Period.fromIsoFormat( '2020-10-01T08:00:00', '2020-10-01T10:00:00') in free_periods assert Period.fromIsoFormat( '2020-10-01T12:00:00', '2020-10-01T14:00:00') in free_periods
def test_merge_4(): period1 = Period.fromIsoFormat( '2020-10-01T10:00:00', '2020-10-01T14:00:00') period2 = Period.fromIsoFormat( '2020-10-01T13:00:00', '2020-10-01T18:00:00') period3 = Period.fromIsoFormat( '2020-10-01T20:00:00', '2020-10-01T21:00:00') merged = merge([period2, period3, period1]) expected_1 = Period.fromIsoFormat( '2020-10-01T10:00:00', '2020-10-01T18:00:00') expected_2 = Period.fromIsoFormat( '2020-10-01T20:00:00', '2020-10-01T21:00:00') assert len(merged) == 2 assert expected_1 in merged assert expected_2 in merged
def get_free_periods_for_date(self, date): weekday = date.weekday() opening_times = self.open_periods[weekday] # todo extend for multiple opening periods opening_time = opening_times[0] open = datetime.combine(date, opening_time.start) close = datetime.combine(date, opening_time.end) total_time = Period(open, close) print(total_time) all_blocked_periods = list( map(lambda a: a.get_period(), self.appointments)) return total_time.subtract(all_blocked_periods)
def test_calendar_get_free_periods_for_date(): calendar = Calendar() calendar.set_opening_times('THURSDAY', '08:00:00', '18:00:00') target_date = date.fromisoformat('2020-10-01') free_periods = calendar.get_free_periods_for_date(target_date) assert len(free_periods) == 1 assert Period.fromIsoFormat( '2020-10-01T08:00:00', '2020-10-01T18:00:00') in free_periods
def test_period_subtract_contained(): start1 = datetime.fromisoformat('2020-10-01T10:00:00') end1 = datetime.fromisoformat('2020-10-01T18:00:00') period1 = Period(start1, end1) start2 = datetime.fromisoformat('2020-10-01T13:00:00') end2 = datetime.fromisoformat('2020-10-01T14:00:00') period2 = Period(start2, end2) subtracted = period1 - period2 expected_start_1 = datetime.fromisoformat('2020-10-01T10:00:00') expected_end_1 = datetime.fromisoformat('2020-10-01T13:00:00') expected_first = Period(expected_start_1, expected_end_1) expected_start_2 = datetime.fromisoformat('2020-10-01T14:00:00') expected_end_2 = datetime.fromisoformat('2020-10-01T18:00:00') expected_second = Period(expected_start_2, expected_end_2) assert len(subtracted) == 2 assert expected_first in subtracted assert expected_second in subtracted
def test_subtract_3(): period = Period.fromIsoFormat('2000-10-01T10:00:00', '2000-10-01T22:00:00') subtract_1 = Period.fromIsoFormat( '2000-10-01T14:00:00', '2000-10-01T15:00:00') subtract_2 = Period.fromIsoFormat( '2000-10-01T17:00:00', '2000-10-01T18:00:00') subtracted = period.subtract([subtract_1, subtract_2]) expected_1 = Period.fromIsoFormat( '2000-10-01T10:00:00', '2000-10-01T14:00:00') expected_2 = Period.fromIsoFormat( '2000-10-01T15:00:00', '2000-10-01T17:00:00') expected_3 = Period.fromIsoFormat( '2000-10-01T18:00:00', '2000-10-01T22:00:00') assert len(subtracted) == 3 assert expected_1 in subtracted assert expected_2 in subtracted assert expected_3 in subtracted
def __init__(self, start, end): self.period = Period.fromIsoFormat(start, end)
def test_period(): start = datetime.fromisoformat('2020-10-01T12:00:00') end = datetime.fromisoformat('2020-10-01T13:00:00') period = Period(start, end) assert period.start == datetime(2020, 10, 1, 12, 0) and period.end == datetime(2020, 10, 1, 13, 0)
def test_period_get_duration(): start = datetime.fromisoformat('2020-10-01T12:00:00') end = datetime.fromisoformat('2020-10-01T13:00:00') period = Period(start, end) duration = period.get_duration() assert duration == timedelta(hours=1)