From 57c1878dc9d5bf665ab0634653f50822c00f3251 Mon Sep 17 00:00:00 2001 From: Ondrej Kohout Date: Wed, 26 Dec 2018 19:43:05 +0100 Subject: [PATCH 1/9] Add tests to existing occupancy calculation And make it work. --- app.py | 8 +++--- test.py | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 test.py diff --git a/app.py b/app.py index f3bf1ec..aef176d 100644 --- a/app.py +++ b/app.py @@ -33,7 +33,7 @@ def get(self, hotelroom_id, start_date, end_date): hotelroom = session.query(HotelRooms).get(hotelroom_id) # get number of bookings and cancellations - num_of_bookings = session.query(func.count(Bookings)).filter( + num_of_bookings = session.query(func.count(Bookings.id)).filter( and_( Bookings.hotelroom_id == hotelroom_id, Bookings.reserved_night_date.between( @@ -41,8 +41,8 @@ def get(self, hotelroom_id, start_date, end_date): ), Bookings.row_type == 'booking' ) - ).all() - num_of_cancellations = session.query(func.Count(Bookings)).filter( + ).scalar() + num_of_cancellations = session.query(func.Count(Bookings.id)).filter( and_( Bookings.hotelroom_id == hotelroom_id, Bookings.reserved_night_date.between( @@ -50,7 +50,7 @@ def get(self, hotelroom_id, start_date, end_date): ), Bookings.row_type == 'cancellations' ) - ).all() + ).scalar() # calculate numerator and denominator for occupancy net_bookings = num_of_bookings - num_of_cancellations diff --git a/test.py b/test.py new file mode 100644 index 0000000..31f214e --- /dev/null +++ b/test.py @@ -0,0 +1,88 @@ +from datetime import date +from decimal import Decimal + +import pytest +from sqlalchemy import create_engine +from sqlalchemy.orm import Session, sessionmaker + +import app, models + + +@pytest.fixture +def db_connection(): + + engine = create_engine( + # TODO make this configurable (for both the fixture and the app) + "postgresql://prix:prix@localhost:5432/interview" + ) + + models.Base.metadata.create_all(bind=engine) + + yield engine.connect() + + # TODO properly close the scoped session usedd by the app + from utils import _SESSION + if _SESSION: + _SESSION.close() + + models.Base.metadata.reflect(bind=engine) + models.Base.metadata.drop_all(bind=engine) + + +@pytest.fixture +def db_session(db_connection): + Session = sessionmaker(bind=db_connection) + session = Session() + yield session + session.close() + + +@pytest.fixture +def hotel(db_session): + hotel = models.Hotels(id=1, name="Intercontinental") + db_session.add(hotel) + db_session.commit() + return hotel + + +@pytest.fixture +def hotelroom(db_session, hotel): + hotelroom = models.HotelRooms( + id=1, + hotel_id=hotel.id, + name="Queen Suite", + capacity=10, + ) + db_session.add(hotelroom) + db_session.commit() + return hotelroom + + +@pytest.fixture +def bookings(db_session, hotelroom): + bookings = [ + models.Bookings( + id=i, + hotelroom_id=hotelroom.id, + reserved_night_date=date(2018, 12, 26), + booking_datetime=date(2018, 12, 26), + row_type="booking", + price=Decimal("100.00"), + ) for i in range(1, 7) + ] + db_session.add_all(bookings) + db_session.commit() + return bookings + + +def test_occupancy(bookings): + + hotelroom_id = 1, + start_date = "2018-12-26" + end_date = "2018-12-26" + + response = app.OccupancyEndpoint().get( + hotelroom_id, start_date, end_date + ) + + assert response["occupancy"] == "60.0" From 00bfaaff9845bff1ba7154ca3aa4c1c9bf70e3ef Mon Sep 17 00:00:00 2001 From: Ondrej Kohout Date: Wed, 26 Dec 2018 19:52:56 +0100 Subject: [PATCH 2/9] Add blocked rooms as booking object type I was tempted to try this, did it as first approach as it was really easy to do. Will come back to this task re-implementing it as a whole new model representing the blocks with number for room blocked on it. --- app.py | 11 ++++++++++- test.py | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index aef176d..63201be 100644 --- a/app.py +++ b/app.py @@ -51,9 +51,18 @@ def get(self, hotelroom_id, start_date, end_date): Bookings.row_type == 'cancellations' ) ).scalar() + num_of_blocked_slots = session.query(func.Count(Bookings.id)).filter( + and_( + Bookings.hotelroom_id == hotelroom_id, + Bookings.reserved_night_date.between( + start_date, end_date + ), + Bookings.row_type == 'block' + ) + ).scalar() # calculate numerator and denominator for occupancy - net_bookings = num_of_bookings - num_of_cancellations + net_bookings = num_of_blocked_slots + num_of_bookings - num_of_cancellations total_available_rooms = hotelroom.capacity * ((end_date - start_date).days + 1) # check to make sure total_available_rooms is not 0 (division by zero error) diff --git a/test.py b/test.py index 31f214e..3d1eeb0 100644 --- a/test.py +++ b/test.py @@ -59,17 +59,24 @@ def hotelroom(db_session, hotel): @pytest.fixture -def bookings(db_session, hotelroom): - bookings = [ - models.Bookings( - id=i, +def make_booking(db_session, hotelroom): + def make(**overrides): + booking_data = dict( + id=1, hotelroom_id=hotelroom.id, reserved_night_date=date(2018, 12, 26), booking_datetime=date(2018, 12, 26), row_type="booking", price=Decimal("100.00"), - ) for i in range(1, 7) - ] + ) + booking_data.update(overrides) + return models.Bookings(**booking_data) + return make + + +@pytest.fixture +def bookings(db_session, make_booking): + bookings = [make_booking(id=i) for i in range(1, 7)] db_session.add_all(bookings) db_session.commit() return bookings @@ -86,3 +93,26 @@ def test_occupancy(bookings): ) assert response["occupancy"] == "60.0" + + +def test_occupancy_with_blocked_rooms(db_session, bookings, make_booking): + + hotelroom_id = 1, + start_date = "2018-12-26" + end_date = "2018-12-26" + + blocked_rooms = [ + make_booking( + id=i, + reserved_night_date=date(2018, 12, 26), + row_type="block" + ) for i in range(7, 11) + ] + db_session.add_all(blocked_rooms) + db_session.commit() + + response = app.OccupancyEndpoint().get( + hotelroom_id, start_date, end_date + ) + + assert response["occupancy"] == "100.0" From 39ca341480782891b3e9d9832817e0702804d23e Mon Sep 17 00:00:00 2001 From: Ondrej Kohout Date: Wed, 26 Dec 2018 20:46:52 +0100 Subject: [PATCH 3/9] Return occupancy curve (non-cumulative) To start with something.) --- app.py | 28 ++++++++++++++++++++++++--- test.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index 63201be..603ad73 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,7 @@ -from datetime import datetime +from datetime import date, datetime, timedelta +from collections import Counter -from flask import Flask +from flask import Flask, request from flask_restplus import Api, Resource from sqlalchemy import and_, func @@ -95,7 +96,28 @@ def get(self, hotelroom_id, reserved_night_date): # get a database session session = get_session() - occupancy = [] + days = request.args.get("days", 90) + today = date.today() + + bookings = session.query(Bookings.booking_datetime).filter( + and_( + Bookings.hotelroom_id == hotelroom_id, + Bookings.reserved_night_date == reserved_night_date, + Bookings.booking_datetime >= today - timedelta(days=days), + ) + ).all() + + occupancy_per_day = Counter( + [booking.booking_datetime.date() for booking in bookings] + ) + # fill up occupancy per each day of the range + # (including days with no booking) + occupancy_per_day = [ + occupancy_per_day.get(today - timedelta(days=day), 0) + for day in reversed(range(days)) + ] + + occupancy = occupancy_per_day revenue_booking_curve = [] # write code here for Question 2 diff --git a/test.py b/test.py index 3d1eeb0..4d5188a 100644 --- a/test.py +++ b/test.py @@ -1,5 +1,7 @@ from datetime import date from decimal import Decimal +import itertools +from unittest.mock import patch import pytest from sqlalchemy import create_engine @@ -82,22 +84,22 @@ def bookings(db_session, make_booking): return bookings -def test_occupancy(bookings): +def test_occupancy(bookings, hotelroom): - hotelroom_id = 1, start_date = "2018-12-26" end_date = "2018-12-26" response = app.OccupancyEndpoint().get( - hotelroom_id, start_date, end_date + hotelroom.id, start_date, end_date ) assert response["occupancy"] == "60.0" -def test_occupancy_with_blocked_rooms(db_session, bookings, make_booking): +def test_occupancy_with_blocked_rooms( + db_session, bookings, hotelroom, make_booking +): - hotelroom_id = 1, start_date = "2018-12-26" end_date = "2018-12-26" @@ -112,7 +114,53 @@ def test_occupancy_with_blocked_rooms(db_session, bookings, make_booking): db_session.commit() response = app.OccupancyEndpoint().get( - hotelroom_id, start_date, end_date + hotelroom.id, start_date, end_date ) assert response["occupancy"] == "100.0" + + +def test_occupancy_with_date_range(): + pass + + +@pytest.fixture +def request(): + with patch("app.request") as request: + yield request + + +def test_booking_curve_occupancy( + db_session, request, hotelroom, make_booking +): + + # for testing shorten the 90 days default + request.args = {"days": 5} + + reserved_night_date = date(2018, 12, 26) + + ids = itertools.count() + bookings = [] + + def make_bookings(n, **overrides): + bookings = [ + make_booking( + id=next(ids), + reserved_night_date=reserved_night_date, + **overrides + ) for i in range(n) + ] + db_session.add_all(bookings) + db_session.commit() + + make_bookings(4, booking_datetime=date(2018, 12, 23)) + make_bookings(1, booking_datetime=date(2018, 12, 24)) + make_bookings(3, booking_datetime=date(2018, 12, 26)) + + response = app.BookingCurveEndpoint().get( + hotelroom.id, reserved_night_date + ) + + expected_occupancy_curve = [0, 4, 1, 0, 3] + + assert response["booking_curve"]["occupancy"] == expected_occupancy_curve From b7564798a79de98df5c67c794ce606bb5bf00580 Mon Sep 17 00:00:00 2001 From: Ondrej Kohout Date: Wed, 26 Dec 2018 21:17:33 +0100 Subject: [PATCH 4/9] Cumulative occupancy curve --- app.py | 10 +++++++--- test.py | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 603ad73..491f3eb 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,7 @@ from datetime import date, datetime, timedelta from collections import Counter +import itertools +import operator from flask import Flask, request from flask_restplus import Api, Resource @@ -99,6 +101,7 @@ def get(self, hotelroom_id, reserved_night_date): days = request.args.get("days", 90) today = date.today() + # bookings for the given room and the last 90 days bookings = session.query(Bookings.booking_datetime).filter( and_( Bookings.hotelroom_id == hotelroom_id, @@ -116,12 +119,13 @@ def get(self, hotelroom_id, reserved_night_date): occupancy_per_day.get(today - timedelta(days=day), 0) for day in reversed(range(days)) ] + # accumulate occupancy curve + occupancy = list( + itertools.accumulate(occupancy_per_day, func=operator.add) + ) - occupancy = occupancy_per_day revenue_booking_curve = [] - # write code here for Question 2 - return { 'booking_curve': { "occupancy": occupancy, diff --git a/test.py b/test.py index 4d5188a..a3d14d0 100644 --- a/test.py +++ b/test.py @@ -161,6 +161,6 @@ def make_bookings(n, **overrides): hotelroom.id, reserved_night_date ) - expected_occupancy_curve = [0, 4, 1, 0, 3] + expected_occupancy_curve = [0, 4, 5, 5, 8] assert response["booking_curve"]["occupancy"] == expected_occupancy_curve From 5207cc966fbff9d909407256b0cd6a587eaad2c7 Mon Sep 17 00:00:00 2001 From: Ondrej Kohout Date: Wed, 26 Dec 2018 21:20:08 +0100 Subject: [PATCH 5/9] Account for prior occupancy --- app.py | 16 +++++++++++++++- test.py | 6 +++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 491f3eb..ef1be01 100644 --- a/app.py +++ b/app.py @@ -100,13 +100,25 @@ def get(self, hotelroom_id, reserved_night_date): days = request.args.get("days", 90) today = date.today() + start_date = today - timedelta(days=days-1) + + # bookings made prior the curve start date + prior_occupancy = session.query( + func.count(Bookings.id) + ).filter( + and_( + Bookings.hotelroom_id == hotelroom_id, + Bookings.reserved_night_date == reserved_night_date, + Bookings.booking_datetime < start_date, + ) + ).scalar() # bookings for the given room and the last 90 days bookings = session.query(Bookings.booking_datetime).filter( and_( Bookings.hotelroom_id == hotelroom_id, Bookings.reserved_night_date == reserved_night_date, - Bookings.booking_datetime >= today - timedelta(days=days), + Bookings.booking_datetime >= start_date ) ).all() @@ -119,6 +131,8 @@ def get(self, hotelroom_id, reserved_night_date): occupancy_per_day.get(today - timedelta(days=day), 0) for day in reversed(range(days)) ] + # add prior occupancy + occupancy_per_day[0] += prior_occupancy # accumulate occupancy curve occupancy = list( itertools.accumulate(occupancy_per_day, func=operator.add) diff --git a/test.py b/test.py index a3d14d0..497c184 100644 --- a/test.py +++ b/test.py @@ -153,6 +153,10 @@ def make_bookings(n, **overrides): db_session.add_all(bookings) db_session.commit() + # bookings prior the curve date range + make_bookings(4, booking_datetime=date(2018, 12, 21)) + + # bookings within the curve date range make_bookings(4, booking_datetime=date(2018, 12, 23)) make_bookings(1, booking_datetime=date(2018, 12, 24)) make_bookings(3, booking_datetime=date(2018, 12, 26)) @@ -161,6 +165,6 @@ def make_bookings(n, **overrides): hotelroom.id, reserved_night_date ) - expected_occupancy_curve = [0, 4, 5, 5, 8] + expected_occupancy_curve = [4, 8, 9, 9, 12] assert response["booking_curve"]["occupancy"] == expected_occupancy_curve From 2d37d717adb20287fe37ac56ce639a205205d207 Mon Sep 17 00:00:00 2001 From: Ondrej Kohout Date: Wed, 26 Dec 2018 21:28:21 +0100 Subject: [PATCH 6/9] Occupancy curve as percentage not just sum --- app.py | 15 ++++++++++----- test.py | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index ef1be01..017626d 100644 --- a/app.py +++ b/app.py @@ -98,6 +98,9 @@ def get(self, hotelroom_id, reserved_night_date): # get a database session session = get_session() + # get the hotelroom object to calculate capacity + hotelroom = session.query(HotelRooms).get(hotelroom_id) + days = request.args.get("days", 90) today = date.today() start_date = today - timedelta(days=days-1) @@ -133,16 +136,18 @@ def get(self, hotelroom_id, reserved_night_date): ] # add prior occupancy occupancy_per_day[0] += prior_occupancy - # accumulate occupancy curve - occupancy = list( - itertools.accumulate(occupancy_per_day, func=operator.add) - ) + # accumulate occupancy and calculate percentage + occupancy_percentage = [ + str(round(occupancy * 100 / hotelroom.capacity, 2)) + for occupancy + in itertools.accumulate(occupancy_per_day, func=operator.add) + ] revenue_booking_curve = [] return { 'booking_curve': { - "occupancy": occupancy, + "occupancy": occupancy_percentage, "revenue": revenue_booking_curve } } diff --git a/test.py b/test.py index 497c184..e4880cd 100644 --- a/test.py +++ b/test.py @@ -154,7 +154,7 @@ def make_bookings(n, **overrides): db_session.commit() # bookings prior the curve date range - make_bookings(4, booking_datetime=date(2018, 12, 21)) + make_bookings(2, booking_datetime=date(2018, 12, 21)) # bookings within the curve date range make_bookings(4, booking_datetime=date(2018, 12, 23)) @@ -165,6 +165,6 @@ def make_bookings(n, **overrides): hotelroom.id, reserved_night_date ) - expected_occupancy_curve = [4, 8, 9, 9, 12] + expected_occupancy_curve = ["20.0", "60.0", "70.0", "70.0", "100.0"] assert response["booking_curve"]["occupancy"] == expected_occupancy_curve From 538010915800997c041f19e6f012f7d2c80bb57c Mon Sep 17 00:00:00 2001 From: Ondrej Kohout Date: Wed, 26 Dec 2018 21:55:21 +0100 Subject: [PATCH 7/9] Add revenue curve --- app.py | 37 +++++++++++++++++++++++++--------- test.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index 017626d..b089d58 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,6 @@ from datetime import date, datetime, timedelta -from collections import Counter +from decimal import Decimal +from collections import Counter, defaultdict import itertools import operator @@ -106,18 +107,22 @@ def get(self, hotelroom_id, reserved_night_date): start_date = today - timedelta(days=days-1) # bookings made prior the curve start date - prior_occupancy = session.query( - func.count(Bookings.id) + prior_occupancy, prior_revenue = session.query( + func.count(Bookings.id), + func.sum(Bookings.price), ).filter( and_( Bookings.hotelroom_id == hotelroom_id, Bookings.reserved_night_date == reserved_night_date, Bookings.booking_datetime < start_date, ) - ).scalar() + ).one() # bookings for the given room and the last 90 days - bookings = session.query(Bookings.booking_datetime).filter( + bookings = session.query( + Bookings.booking_datetime, + Bookings.price, + ).filter( and_( Bookings.hotelroom_id == hotelroom_id, Bookings.reserved_night_date == reserved_night_date, @@ -125,25 +130,39 @@ def get(self, hotelroom_id, reserved_night_date): ) ).all() + # get occupancy and revenue per day out of existing bookings occupancy_per_day = Counter( [booking.booking_datetime.date() for booking in bookings] ) - # fill up occupancy per each day of the range + revenue_per_day = defaultdict(Decimal) + for booking in bookings: + revenue_per_day[booking.booking_datetime.date()] += booking.price + + # occupancy and revenue per each day of the range # (including days with no booking) occupancy_per_day = [ occupancy_per_day.get(today - timedelta(days=day), 0) for day in reversed(range(days)) ] - # add prior occupancy + revenue_per_day = [ + revenue_per_day.get(today - timedelta(days=day), 0) + for day in reversed(range(days)) + ] + + # account for prior occupancy and revenue occupancy_per_day[0] += prior_occupancy + revenue_per_day[0] += prior_revenue + # accumulate occupancy and calculate percentage occupancy_percentage = [ str(round(occupancy * 100 / hotelroom.capacity, 2)) for occupancy in itertools.accumulate(occupancy_per_day, func=operator.add) ] - - revenue_booking_curve = [] + # accumulate revenue + revenue_booking_curve = list( + itertools.accumulate(revenue_per_day, func=operator.add) + ) return { 'booking_curve': { diff --git a/test.py b/test.py index e4880cd..3246347 100644 --- a/test.py +++ b/test.py @@ -133,6 +133,7 @@ def request(): def test_booking_curve_occupancy( db_session, request, hotelroom, make_booking ): + # TODO add also bookings for different room - should be filtered out # for testing shorten the 90 days default request.args = {"days": 5} @@ -168,3 +169,64 @@ def make_bookings(n, **overrides): expected_occupancy_curve = ["20.0", "60.0", "70.0", "70.0", "100.0"] assert response["booking_curve"]["occupancy"] == expected_occupancy_curve + + +def test_booking_curve_revenue( + db_session, request, hotelroom, make_booking +): + # TODO add also bookings for different room - should be filtered out + + # for testing shorten the 90 days default + request.args = {"days": 5} + + reserved_night_date = date(2018, 12, 26) + + ids = itertools.count() + bookings = [] + + def make_bookings(prices, **overrides): + bookings = [ + make_booking( + id=next(ids), + reserved_night_date=reserved_night_date, + price=price, + **overrides + ) for price in prices + ] + db_session.add_all(bookings) + db_session.commit() + + # bookings prior the curve date range + make_bookings( + ("100.0", "200.0"), + booking_datetime=date(2018, 12, 21) + ) + + # bookings within the curve date range + make_bookings( + ("100.0", "100.0", "100.0", "100.0"), + booking_datetime=date(2018, 12, 23) + ) + make_bookings( + ("100.0",), + booking_datetime=date(2018, 12, 24) + ) + make_bookings( + ("100.0", "100.0"), + booking_datetime=date(2018, 12, 26) + ) + + response = app.BookingCurveEndpoint().get( + hotelroom.id, reserved_night_date + ) + + expected_revenue_curve = [ + Decimal("300.00"), + Decimal("700.00"), + Decimal("800.00"), + Decimal("800.00"), + Decimal("1000.00") + ] + + assert response["booking_curve"]["revenue"] == expected_revenue_curve + From 735590b4594c68264965b4f9f8bb99e1ca99e63a Mon Sep 17 00:00:00 2001 From: Ondrej Kohout Date: Wed, 26 Dec 2018 22:30:49 +0100 Subject: [PATCH 8/9] Change blocked rooms to be a separate model --- app.py | 15 +++++++++------ models.py | 12 ++++++++++++ test.py | 15 +++++++-------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app.py b/app.py index b089d58..18967f4 100644 --- a/app.py +++ b/app.py @@ -8,7 +8,7 @@ from flask_restplus import Api, Resource from sqlalchemy import and_, func -from models import Bookings, HotelRooms, Hotels +from models import BlockedRooms, Bookings, HotelRooms, Hotels from utils import get_session app = Flask(__name__) @@ -55,18 +55,21 @@ def get(self, hotelroom_id, start_date, end_date): Bookings.row_type == 'cancellations' ) ).scalar() - num_of_blocked_slots = session.query(func.Count(Bookings.id)).filter( + num_of_blocked_rooms = session.query(func.Sum(BlockedRooms.rooms)).filter( and_( - Bookings.hotelroom_id == hotelroom_id, - Bookings.reserved_night_date.between( + BlockedRooms.hotelroom_id == hotelroom_id, + BlockedRooms.reserved_night_date.between( start_date, end_date ), - Bookings.row_type == 'block' ) ).scalar() + num_of_bookings = num_of_bookings or 0 + num_of_cancellations = num_of_cancellations or 0 + num_of_blocked_rooms = num_of_blocked_rooms or 0 + # calculate numerator and denominator for occupancy - net_bookings = num_of_blocked_slots + num_of_bookings - num_of_cancellations + net_bookings = num_of_blocked_rooms + num_of_bookings - num_of_cancellations total_available_rooms = hotelroom.capacity * ((end_date - start_date).days + 1) # check to make sure total_available_rooms is not 0 (division by zero error) diff --git a/models.py b/models.py index 17eb837..7c68929 100644 --- a/models.py +++ b/models.py @@ -40,3 +40,15 @@ class Bookings(Base): row_type = Column(Text, nullable=False) price = Column(Numeric(10, 2), nullable=False) + + +class BlockedRooms(Base): + __tablename__ = 'blockedrooms' + id = Column(Integer, primary_key=True) + + hotelroom_id = Column(Integer, ForeignKey('hotelrooms.id'), nullable=False, index=True) + hotelroom = relationship("HotelRooms", primaryjoin=hotelroom_id == HotelRooms.id) + + reserved_night_date = Column(Date, nullable=False) + + rooms = Column(Integer, nullable=False) diff --git a/test.py b/test.py index 3246347..59d8cb4 100644 --- a/test.py +++ b/test.py @@ -103,14 +103,13 @@ def test_occupancy_with_blocked_rooms( start_date = "2018-12-26" end_date = "2018-12-26" - blocked_rooms = [ - make_booking( - id=i, - reserved_night_date=date(2018, 12, 26), - row_type="block" - ) for i in range(7, 11) - ] - db_session.add_all(blocked_rooms) + blocked_rooms = models.BlockedRooms( + id=1, + hotelroom_id=hotelroom.id, + reserved_night_date=date(2018, 12, 26), + rooms=4 + ) + db_session.add(blocked_rooms) db_session.commit() response = app.OccupancyEndpoint().get( From f4e8c30a91ff2e21388f6a22d3aea5f72d3f8930 Mon Sep 17 00:00:00 2001 From: Ondrej Kohout Date: Wed, 26 Dec 2018 22:43:27 +0100 Subject: [PATCH 9/9] Minor docstring changes --- app.py | 6 +++--- test.py | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 18967f4..898f24d 100644 --- a/app.py +++ b/app.py @@ -109,7 +109,7 @@ def get(self, hotelroom_id, reserved_night_date): today = date.today() start_date = today - timedelta(days=days-1) - # bookings made prior the curve start date + # bookings for the given room made prior the curve start date prior_occupancy, prior_revenue = session.query( func.count(Bookings.id), func.sum(Bookings.price), @@ -121,7 +121,7 @@ def get(self, hotelroom_id, reserved_night_date): ) ).one() - # bookings for the given room and the last 90 days + # bookings for the given room made within the curve date range bookings = session.query( Bookings.booking_datetime, Bookings.price, @@ -142,7 +142,7 @@ def get(self, hotelroom_id, reserved_night_date): revenue_per_day[booking.booking_datetime.date()] += booking.price # occupancy and revenue per each day of the range - # (including days with no booking) + # (including days with no bookings) occupancy_per_day = [ occupancy_per_day.get(today - timedelta(days=day), 0) for day in reversed(range(days)) diff --git a/test.py b/test.py index 59d8cb4..9eff1c8 100644 --- a/test.py +++ b/test.py @@ -228,4 +228,3 @@ def make_bookings(prices, **overrides): ] assert response["booking_curve"]["revenue"] == expected_revenue_curve -