Skip to content

Commit

Permalink
Adds more tests, fixing bugs found in the process
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krienbühl committed Feb 6, 2015
1 parent 3b256e7 commit ba1c158
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
14 changes: 9 additions & 5 deletions libres/db/models/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def is_available(self, start=None, end=None):

return True

def limit_timespan(self, start, end):
def limit_timespan(self, start, end, timezone=None):
""" Takes the given timespan and moves the start/end date to
the closest reservable slot. So if 10:00 - 11:00 is requested it will
Expand All @@ -289,23 +289,27 @@ def limit_timespan(self, start, end):
to form a datetime. (time in, datetime out -> maybe not the best idea)
"""
timezone = timezone or self.timezone

if self.partly_available:
assert isinstance(start, time)
assert isinstance(end, time)

s, e = calendar.get_date_range(self.start, start, end)
s, e = calendar.get_date_range(
self.display_start(timezone), start, end
)

if self.display_end() < e:
if self.display_end(timezone) < e:
e = self.display_end()

if self.display_start() > s:
if self.display_start(timezone) > s:
s = self.display_start()

s, e = rasterize_span(s, e, self.raster)

return s, e + timedelta(microseconds=1)
else:
return self.display_start(), self.display_end()
return self.display_start(timezone), self.display_end(timezone)

@property
def pending_reservations(self):
Expand Down
46 changes: 45 additions & 1 deletion libres/tests/test_scheduler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from copy import copy
from datetime import datetime, timedelta
from datetime import datetime, timedelta, time
from libres.db.models import Reservation, Allocation, ReservedSlot
from libres.modules import calendar, errors, events
from libres.modules import utils
Expand Down Expand Up @@ -936,6 +936,50 @@ def test_partly(scheduler):
))


def test_allocation_by_ids(scheduler):
dates = [
(datetime(2015, 1, 1, 15, 0), datetime(2015, 1, 1, 16, 0)),
(datetime(2015, 1, 2, 15, 0), datetime(2015, 1, 2, 16, 0)),
]

ids = [a.id for a in scheduler.allocate(dates)]
scheduler.commit()

assert scheduler.allocations_by_ids(ids).count() == 2


def test_allocation_dates_by_ids(scheduler):
dates = [
(datetime(2015, 1, 1, 15, 0), datetime(2015, 1, 1, 16, 0)),
(datetime(2015, 1, 2, 15, 0), datetime(2015, 1, 2, 16, 0)),
]

ids = [a.id for a in scheduler.allocate(dates, partly_available=True)]
scheduler.commit()

d = list(scheduler.allocation_dates_by_ids(ids))

standardize = lambda dt: calendar.standardize_date(dt, 'Europe/Zurich')
as_utc = lambda dt: calendar.to_timezone(dt, 'UTC')

assert as_utc(d[0][0]) == standardize(dates[0][0])
assert as_utc(d[0][1]) == standardize(dates[0][1]) - timedelta(
microseconds=1)
assert as_utc(d[1][0]) == standardize(dates[1][0])
assert as_utc(d[1][1]) == standardize(dates[1][1]) - timedelta(
microseconds=1)

d = list(scheduler.allocation_dates_by_ids(
ids, start_time=time(15, 0), end_time=time(15, 30)))

assert d[0][0] == standardize(dates[0][0])
assert d[0][1] == standardize(dates[0][1]) - timedelta(
microseconds=1, seconds=30 * 60)
assert d[1][0] == standardize(dates[1][0])
assert d[1][1] == standardize(dates[1][1]) - timedelta(
microseconds=1, seconds=30 * 60)


def test_quotas(scheduler):
start = datetime(2011, 1, 1, 15, 0)
end = datetime(2011, 1, 1, 16, 0)
Expand Down

0 comments on commit ba1c158

Please sign in to comment.