def test_add_reserved_slot(scheduler): allocation = Allocation(raster=15, resource=scheduler.resource) allocation.start = datetime(2011, 1, 1, 15, tzinfo=utc) allocation.end = datetime(2011, 1, 1, 15, 59, tzinfo=utc) allocation.group = new_uuid().hex allocation.mirror_of = scheduler.resource reservation = new_uuid() slot = ReservedSlot(resource=allocation.resource) slot.start = allocation.start slot.end = allocation.end slot.allocation = allocation slot.reservation = reservation # Ensure that the same slot cannot be doubly used another = ReservedSlot(resource=allocation.resource) another.start = allocation.start another.end = allocation.end another.allocation = allocation another.reservation = reservation scheduler.session.add(allocation) scheduler.session.add(slot) scheduler.session.add(another) with pytest.raises(IntegrityError): scheduler.session.flush()
def add_something(resource=None): resource = resource or uuid() allocation = Allocation(raster=15, resource=resource, mirror_of=resource) allocation.start = datetime(2011, 1, 1, 15) allocation.end = datetime(2011, 1, 1, 15, 59) allocation.group = uuid() Session.add(allocation)
def add_something(resource=None): resource = resource or uuid() allocation = Allocation( raster=15, resource=resource, mirror_of=resource) allocation.start = datetime(2011, 1, 1, 15) allocation.end = datetime(2011, 1, 1, 15, 59) allocation.group = uuid() Session.add(allocation)
def test_whole_day(): allocation = Allocation( raster=15, resource=new_uuid(), timezone='Europe/Zurich' ) # the whole-day is relative to the allocation's timezone allocation.start = datetime(2013, 1, 1, 23, 0, tzinfo=utc) allocation.end = datetime(2013, 1, 2, 23, 0, tzinfo=utc) assert allocation.whole_day allocation.start = datetime(2013, 1, 1, 23, 0, tzinfo=utc) allocation.end = datetime(2013, 1, 2, 22, 59, 59, 999999, tzinfo=utc) assert allocation.whole_day allocation.start = datetime(2013, 1, 1, 23, 0, tzinfo=utc) allocation.end = datetime(2013, 1, 2, 22, 59, 59, 999999, tzinfo=utc) assert allocation.whole_day allocation.start = datetime(2013, 1, 1, 15, 0, tzinfo=utc) allocation.end = datetime(2013, 1, 1, 0, 0, tzinfo=utc) with pytest.raises(AssertionError): allocation.whole_day
def test_add_allocation(scheduler): allocation = Allocation(raster=15, resource=scheduler.resource) allocation.start = datetime(2011, 1, 1, 15, tzinfo=utc) allocation.end = datetime(2011, 1, 1, 15, 59, tzinfo=utc) allocation.group = new_uuid().hex allocation.mirror_of = scheduler.resource scheduler.session.add(allocation) scheduler.commit() assert scheduler.session.query(Allocation).count() == 1
def test_reserved_slot_date_display(scheduler): start = datetime(2015, 2, 5, 10, 0, tzinfo=utc) end = datetime(2015, 2, 5, 12, 0, tzinfo=utc) allocation = Allocation(raster=5) allocation.start = start allocation.end = end slot = ReservedSlot() slot.allocation = allocation slot.start = start slot.end = end tz = timezone('Europe/Zurich') assert slot.display_start(timezone='Europe/Zurich') == tz.localize( datetime(2015, 2, 5, 11, 0) ) assert slot.display_end(timezone='Europe/Zurich') == tz.localize( datetime(2015, 2, 5, 13, 0) )
def test_date_functions(scheduler): allocation = Allocation(raster=60, resource=scheduler.resource) allocation.timezone = 'UTC' allocation.start = datetime(2011, 1, 1, 12, 30, tzinfo=utc) allocation.end = datetime(2011, 1, 1, 14, 00, tzinfo=utc) assert allocation.start.hour == 12 assert allocation.start.minute == 0 assert allocation.end.hour == 13 assert allocation.end.minute == 59 start = datetime(2011, 1, 1, 11, 00) end = datetime(2011, 1, 1, 12, 5) assert allocation.overlaps(start, end) assert not allocation.contains(start, end) start = datetime(2011, 1, 1, 13, 00) end = datetime(2011, 1, 1, 15, 00) assert allocation.overlaps(start, end) assert not allocation.contains(start, end)
def test_reservation_form_partly_available(): allocation = Allocation() allocation.timezone = 'Europe/Zurich' allocation.start = datetime(2015, 1, 1, 10) allocation.end = datetime(2015, 1, 1, 16) allocation.partly_available = True form = ReservationForm.for_allocation(allocation)() assert hasattr(form, 'start') assert hasattr(form, 'end') assert form.start.default == time(10, 00) assert form.end.default == time(16, 00) allocation.partly_available = False form = ReservationForm.for_allocation(allocation)() assert not hasattr(form, 'start') assert not hasattr(form, 'end')
def test_reservation_form_quota(): allocation = Allocation() allocation.quota = 1 allocation.quota_limit = 1 form = ReservationForm.for_allocation(allocation)() assert not hasattr(form, 'quota') allocation.quota = 2 allocation.quota_limit = 1 form = ReservationForm.for_allocation(allocation)() assert not hasattr(form, 'quota') allocation.quota = 1 allocation.quota_limit = 2 form = ReservationForm.for_allocation(allocation)() assert not hasattr(form, 'quota') allocation.quota = 2 allocation.quota_limit = 2 form = ReservationForm.for_allocation(allocation)() assert hasattr(form, 'quota') allocation.quota = 2 allocation.quota_limit = 0 form = ReservationForm.for_allocation(allocation)() assert hasattr(form, 'quota')
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) )