Skip to content

Commit

Permalink
Adds more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krienbühl committed Feb 9, 2015
1 parent 6c7ebc1 commit 2a93208
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libres/db/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ def reserve(
raise errors.ReservationTooLong

if start > end or (end - start).seconds < 5 * 60:
raise errors.ReservationParametersInvalid
raise errors.ReservationTooShort

# can all allocations be reserved?
for allocation in self.allocations_in_range(start, end):
Expand Down
4 changes: 4 additions & 0 deletions libres/modules/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class ReservationTooLong(LibresError):
pass


class ReservationTooShort(LibresError):
pass


class ReservationParametersInvalid(LibresError):
pass

Expand Down
2 changes: 1 addition & 1 deletion libres/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def scheduler(request, dsn):
scheduler.rollback()
scheduler.extinguish_managed_records()
scheduler.commit()

scheduler.close()
scheduler.session_provider.stop_service()


@pytest.yield_fixture(scope="session")
Expand Down
73 changes: 73 additions & 0 deletions libres/tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,79 @@ def test_move_allocation_with_existing_reservation(scheduler):
)


def test_move_partly_available_allocation_with_existing_reservation(scheduler):
dates = [(datetime(2015, 2, 9, 10), datetime(2015, 2, 9, 12))]

allocations = scheduler.allocate(dates, partly_available=True)
scheduler.reserve('test@example.org', dates)
scheduler.commit()

# partly available allocations cannot be shrunk smaller if a
# reservation covers that spot...
with pytest.raises(errors.AffectedPendingReservationError):
scheduler.move_allocation(
allocations[0].id,
new_start=datetime(2015, 2, 9, 10),
new_end=datetime(2015, 2, 9, 11)
)

# .. but the allocations may be grown
scheduler.move_allocation(
allocations[0].id,
new_start=datetime(2015, 2, 9, 10),
new_end=datetime(2015, 2, 9, 13)
)
scheduler.commit()


def test_change_allocation_data(scheduler):
dates = [(datetime(2015, 2, 9, 10), datetime(2015, 2, 9, 12))]

allocation = scheduler.allocate(dates, data={'foo': 'bar'})[0]
scheduler.commit()
assert allocation.data == {'foo': 'bar'}

# yes, this a very awkward way to change an allocation's data...
scheduler.move_allocation(
allocation.id, new_start=dates[0][0], new_end=dates[0][1], data={
'bar': 'foo'
},
)
scheduler.commit()
assert allocation.data == {'bar': 'foo'}


def test_remove_grouped_allocation(scheduler):
dates = [
(datetime(2015, 2, 9, 10), datetime(2015, 2, 9, 12)),
(datetime(2015, 2, 10, 10), datetime(2015, 2, 10, 12)),
]

group = scheduler.allocate(dates, grouped=True)[0].group
scheduler.commit()

assert scheduler.managed_allocations().count() == 2

scheduler.remove_allocation(groups=[group])
scheduler.commit()

assert scheduler.managed_allocations().count() == 0


def test_reserve_invalid_email(scheduler):

with pytest.raises(errors.InvalidEmailAddress):
scheduler.reserve(group='foo', email='invalid-email')


def test_reservation_too_short(scheduler):

with pytest.raises(errors.ReservationTooShort):
scheduler.reserve(email='test@example.org', dates=[
(datetime(2015, 2, 9, 10, 0), datetime(2015, 2, 9, 10, 1)),
])


def test_managed_allocations(scheduler):

start = datetime(2014, 4, 4, 14, 0)
Expand Down

0 comments on commit 2a93208

Please sign in to comment.