Add the Duck.booked_by() method

This commit is contained in:
Gergely Polonkai 2015-01-22 16:12:54 +01:00 committed by Gergely Polonkai
parent 7274a32233
commit 15ad940445
2 changed files with 40 additions and 0 deletions

View File

@ -87,6 +87,17 @@ class Duck(models.Model):
return Booking.duck_booking_time(self) / Booking.total_booking_time()
def booked_by(self):
l = self.booking_set.filter(end_ts = None)
if len(l) == 0:
return None
if len(l) > 1:
raise RuntimeError(u"Duck is booked more than once!")
return l[0].user
class DuckName(models.Model):
"""Model to hold name suggestions for Ducks"""

View File

@ -249,3 +249,32 @@ class SimilarCompTest(TestCase):
def test_bad_similar_competence(self):
l = Competence.get_similar_comps('development')
self.assertEqual(len(l), 0)
class BookingTest(TestCase):
def setUp(self):
spec = Species(name = 'test')
spec.save()
loc = Location(name = 'test')
loc.save()
user = User(username = 'test')
user.save()
self.booked_duck = Duck(species = spec, location = loc, donated_by = user)
self.booked_duck.save()
comp = Competence(name = 'test', added_by = user)
comp.save()
booking = Booking(duck = self.booked_duck, user = user, comp_req = comp)
booking.save()
self.unbooked_duck = Duck(species = spec, location = loc, donated_by = user)
self.unbooked_duck.save()
def test_booked_duck(self):
self.assertNotEqual(self.booked_duck.booked_by(), None)
def test_unbooked_duck(self):
self.assertEqual(self.unbooked_duck.booked_by(), None)