Permalink
Browse files

Allow to specify list of conflicting rental states

  • Loading branch information...
Benestar committed Aug 20, 2018
1 parent 1877713 commit 33b99dc0c6297a8dacce34b6bdcf3ac9732226c2
Showing with 41 additions and 5 deletions.
  1. +9 −2 rental/availability.py
  2. +32 −3 rental/tests/test_availability.py
@@ -21,6 +21,12 @@ def __eq__(self, other):
self.end == other.end and
self.value == other.value)

def __repr__(self):
return '<Interval begin:"%s" end:"%s" value:%d>' % (self.begin, self.end, self.value)

def __str__(self):
return '[%s, %s] -> %d' % (self.begin, self.end, self.value)


class Availability:
"""
@@ -33,7 +39,8 @@ class Availability:
:author: Leo Tappe
"""

def __init__(self, start_date, return_date, depot_id):
def __init__(self, start_date, return_date, depot_id,
conflicting_states=[Rental.STATE_APPROVED]):
self.start_date = start_date
self.return_date = return_date
self.depot_id = depot_id
@@ -42,7 +49,7 @@ def __init__(self, start_date, return_date, depot_id):
start_date__lt=self.return_date,
return_date__gt=self.start_date,
depot_id=self.depot_id,
state=Rental.STATE_APPROVED
state__in=conflicting_states
)

def get_availability_intervals(self, item):
@@ -23,12 +23,12 @@ def setUp(self):
self.start = datetime.now() + timedelta(days=3)
self.end = datetime.now() + timedelta(days=7)

def create_conflicting_rental(self, start, end, quantity):
def create_conflicting_rental(self, start, end, quantity, state=Rental.STATE_APPROVED):
rental = Rental.objects.create(
depot=self.depot,
start_date=start,
return_date=end,
state=Rental.STATE_APPROVED
state=state
)
ItemRental.objects.create(
rental=rental,
@@ -69,7 +69,7 @@ def test_no_overlap(self):
expected = [Interval(self.start, self.end, 10)]
self.assertEqual(intervals, expected)

def test_not_relevant(self):
def test_not_relevant_item(self):
start = self.start + timedelta(days=1)
end = self.end + timedelta(days=-1)
rental = self.create_non_conflicting_rental(start, end)
@@ -81,6 +81,35 @@ def test_not_relevant(self):
expected = [Interval(self.start, self.end, 10)]
self.assertEqual(intervals, expected)

def test_not_relevant_state(self):
start = self.start + timedelta(days=1)
end = self.end + timedelta(days=-1)
self.create_conflicting_rental(start, end, 3, Rental.STATE_PENDING)

availability = Availability(self.start, self.end, self.depot.id)
self.assertEqual([], list(availability.rentals))

intervals = availability.get_availability_intervals(self.item)
expected = [Interval(self.start, self.end, 10)]
self.assertEqual(intervals, expected)

def test_relevant_state(self):
start = self.start + timedelta(days=1)
end = self.end + timedelta(days=-1)
rental = self.create_conflicting_rental(start, end, 3, Rental.STATE_PENDING)

availability = Availability(self.start, self.end, self.depot.id,
conflicting_states=[Rental.STATE_PENDING])
self.assertEqual([rental], list(availability.rentals))

intervals = availability.get_availability_intervals(self.item)
expected = [
Interval(self.start, start, 10),
Interval(start, end, 7),
Interval(end, self.end, 10)
]
self.assertEqual(intervals, expected)

def test_completely_enclosing(self):
start = self.start + timedelta(days=-1)
end = self.end + timedelta(days=1)

0 comments on commit 33b99dc

Please sign in to comment.