def test_limit_timespan(): # if not partly availabe the limit is always the same allocation = Allocation( raster=15, resource=new_uuid(), partly_available=False, timezone='UTC' ) allocation.start = datetime(2014, 1, 1, 8, 0, tzinfo=utc) allocation.end = datetime(2014, 1, 1, 9, 0, tzinfo=utc) assert allocation.limit_timespan(time(8, 0), time(9, 0)) == ( allocation.display_start(), allocation.display_end() ) assert allocation.limit_timespan(time(7, 0), time(10, 0)) == ( allocation.display_start(), allocation.display_end() ) # if partly available, more complex things happen allocation = Allocation( raster=15, resource=new_uuid(), partly_available=True, timezone='UTC' ) allocation.start = datetime(2014, 1, 1, 8, 0, tzinfo=utc) allocation.end = datetime(2014, 1, 1, 9, 0, tzinfo=utc) assert allocation.limit_timespan(time(8, 0), time(9, 0)) == ( allocation.display_start(), allocation.display_end() ) assert allocation.limit_timespan(time(7, 0), time(10, 0)) == ( allocation.display_start(), allocation.display_end() ) assert allocation.limit_timespan(time(8, 30), time(10, 0)) == ( datetime(2014, 1, 1, 8, 30, tzinfo=utc), datetime(2014, 1, 1, 9, 0, tzinfo=utc) ) assert allocation.limit_timespan(time(8, 30), time(8, 40)) == ( datetime(2014, 1, 1, 8, 30, tzinfo=utc), datetime(2014, 1, 1, 8, 45, tzinfo=utc) ) assert allocation.limit_timespan(time(8, 30), time(0, 0)) == ( datetime(2014, 1, 1, 8, 30, tzinfo=utc), datetime(2014, 1, 1, 9, 0, tzinfo=utc) ) # no problems should arise if whole-day allocations are used allocation.start = datetime(2014, 1, 1, 0, 0, tzinfo=utc) allocation.end = datetime(2014, 1, 2, 0, 0, tzinfo=utc) assert allocation.whole_day assert allocation.limit_timespan(time(0, 0), time(23, 59)) == ( allocation.display_start(), allocation.display_end() ) assert allocation.limit_timespan(time(0, 0), time(0, 0)) == ( datetime(2014, 1, 1, 0, 0, tzinfo=utc), datetime(2014, 1, 1, 0, 0, tzinfo=utc) ) assert allocation.limit_timespan(time(8, 30), time(10, 0)) == ( datetime(2014, 1, 1, 8, 30, tzinfo=utc), datetime(2014, 1, 1, 10, 0, tzinfo=utc) ) assert allocation.limit_timespan(time(8, 30), time(8, 40)) == ( datetime(2014, 1, 1, 8, 30, tzinfo=utc), datetime(2014, 1, 1, 8, 45, tzinfo=utc) ) assert allocation.limit_timespan(time(8, 30), time(0, 0)) == ( datetime(2014, 1, 1, 8, 30, tzinfo=utc), datetime(2014, 1, 2, 0, 0, tzinfo=utc) )