Skip to content

Commit

Permalink
Migrate availability test
Browse files Browse the repository at this point in the history
  • Loading branch information
href committed Nov 25, 2014
1 parent 3d55a54 commit 636ed8a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
6 changes: 4 additions & 2 deletions libres/db/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,10 @@ def reordered_keylist(self, allocations, new_quota):
def availability(self, start=None, end=None):
"""Goes through all allocations and sums up the availability."""

start = start if start else datetime.min
end = end if end else datetime.max
start = start if start else calendar.mindatetime
end = end if end else calendar.maxdatetime

start, end = self.prepare_range(start, end)

return self.queries.availability_by_range(start, end, [self.resource])

Expand Down
4 changes: 4 additions & 0 deletions libres/modules/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from libres.modules import errors, utils


mindatetime = arrow.get(datetime.min).datetime
maxdatetime = arrow.get(datetime.max).datetime


def normalize_dates(dates, timezone):
dates = list(utils.pairs(dates))

Expand Down
85 changes: 85 additions & 0 deletions libres/tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1241,3 +1241,88 @@ def test_quota_changes(scheduler):

assert a7_ == a4
assert a7_ != a7


def test_availability(scheduler):
start = datetime(2011, 1, 1, 15, 0)
end = datetime(2011, 1, 1, 16, 0)

a = scheduler.allocate(
(start, end), raster_value=15, partly_available=True)[0]

scheduler.approve_reservations(
scheduler.reserve(
u'test@example.org',
(datetime(2011, 1, 1, 15, 0), datetime(2011, 1, 1, 15, 15))
)
)
scheduler.commit()

assert a.availability == 75.0
assert a.availability == scheduler.availability()

scheduler.approve_reservations(
scheduler.reserve(
u'test@example.org',
(datetime(2011, 1, 1, 15, 45), datetime(2011, 1, 1, 16, 0))
)
)
scheduler.commit()

assert a.availability == 50.0
assert a.availability == scheduler.availability()

scheduler.approve_reservations(
scheduler.reserve(
u'test@example.org',
(datetime(2011, 1, 1, 15, 15), datetime(2011, 1, 1, 15, 30))
)
)
scheduler.commit()

assert a.availability == 25.0
assert a.availability == scheduler.availability()

scheduler.approve_reservations(
scheduler.reserve(
u'test@example.org',
(datetime(2011, 1, 1, 15, 30), datetime(2011, 1, 1, 15, 45))
)
)
scheduler.commit()

assert a.availability == 0.0
assert a.availability == scheduler.availability()

sc2 = scheduler.clone()
sc2.name = 'clone'

a = sc2.allocate((start, end), quota=4)[0]
assert a.availability == 100.0 # master only!

sc2.approve_reservations(sc2.reserve(u'test@example.org', (start, end)))
sc2.commit()

assert sc2.availability() == 75.0
assert a.availability == 0.0 # master only!

sc2.approve_reservations(
sc2.reserve(u'test@example.org', (start, end))
)
sc2.commit()
assert sc2.availability() == 50.0

sc2.approve_reservations(
sc2.reserve(u'test@example.org', (start, end))
)
sc2.commit()
assert sc2.availability() == 25.0

sc2.approve_reservations(
sc2.reserve(u'test@example.org', (start, end))
)
sc2.commit()
assert sc2.availability() == 0.0

sc2.extinguish_managed_records()
sc2.commit()

0 comments on commit 636ed8a

Please sign in to comment.