From 9a9bc57a346102d4845fc573864ca9240d97320b Mon Sep 17 00:00:00 2001 From: Ruidy Nemausat Date: Tue, 7 Apr 2020 11:26:37 +0200 Subject: [PATCH] booking price test case --- rental/models/booking.py | 14 +++++++------- rental/tests.py | 13 +++---------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/rental/models/booking.py b/rental/models/booking.py index b0b72cc..1d497b4 100644 --- a/rental/models/booking.py +++ b/rental/models/booking.py @@ -18,13 +18,13 @@ class Booking(models.Model): end = models.DateField() price = models.DecimalField(max_digits=6, decimal_places=2, null=True) - def get_reservation_price(place, start, end): + def price(self): """ Compute booking price as a function of place and dates """ - if type(start) == str: - start = datetime.strptime(start, '%Y-%m-%d') - if type(end) == str: - end = datetime.strptime(end, '%Y-%m-%d') - nights = (end - start).days + if type(self.start) == str: + self.start = datetime.strptime(self.start, '%Y-%m-%d') + if type(self.end) == str: + self.end = datetime.strptime(self.end, '%Y-%m-%d') + nights = (self.end - self.start).days - return place.price * nights + return self.place.price * nights diff --git a/rental/tests.py b/rental/tests.py index 7a9c6eb..0b078af 100644 --- a/rental/tests.py +++ b/rental/tests.py @@ -5,15 +5,8 @@ from rental.models.place import Place from rental.models.booking import Booking from rental.models.guest import Guest -place_name = 'T2' -place = get_object_or_404(Place, name=place_name) - -x = get_reservation_price(place_name, start, end) -print(x) - - -class TestBooking(TestCase): +class BookingTestCase(TestCase): def setUp(self): self.place = Place.objects.create( name='TX', @@ -28,7 +21,7 @@ class TestBooking(TestCase): self.start = datetime(2019, 11, 14) self.end = datetime(2019, 11, 20) - def test_bookingPrice(self): + def test_BookingPrice(self): # place = Place.objects.get(name='TX') booking = Booking.objects.create( place=self.place, @@ -37,4 +30,4 @@ class TestBooking(TestCase): guest=self.guest ) - self.assertEqual(booking.get_reservation_price()) + self.assertEqual(booking.price(), 600)