From bcd8dc0733f4b52936c10a6517480d025dee1227 Mon Sep 17 00:00:00 2001 From: stanislas Date: Tue, 19 Mar 2024 00:00:59 +0100 Subject: [PATCH] Added a lot of unit tests --- tests/integration_tests/test_connectors.py | 25 +- tests/unit_tests/conftest.py | 240 +++++++++++------- tests/unit_tests/test_cancellation.py | 0 .../test_gestion_sports_connector.py | 13 +- tests/utils.py | 25 -- 5 files changed, 182 insertions(+), 121 deletions(-) delete mode 100644 tests/unit_tests/test_cancellation.py delete mode 100644 tests/utils.py diff --git a/tests/integration_tests/test_connectors.py b/tests/integration_tests/test_connectors.py index 6c262bc..b9e8d4c 100644 --- a/tests/integration_tests/test_connectors.py +++ b/tests/integration_tests/test_connectors.py @@ -8,10 +8,29 @@ import pendulum import pytest from aiohttp import ClientSession from connectors import GestionSportsConnector -from models import Booking, BookingFilter, User +from models import Booking, BookingFilter, Club, User +from pendulum import DateTime from yarl import URL -from tests import utils + +def retrieve_booking_datetime( + a_booking_filter: BookingFilter, a_club: Club +) -> DateTime: + """ + Utility to retrieve the booking datetime from the booking filter and the club + + :param a_booking_filter: the booking filter that contains the date to book + :param a_club: the club which has the number of days before the date and the booking time + """ + booking_opening = a_club.booking_platform.booking_opening + opening_time = pendulum.parse(booking_opening.opening_time) + booking_hour = opening_time.hour + booking_minute = opening_time.minute + + date_to_book = a_booking_filter.date + return date_to_book.subtract(days=booking_opening.days_before).at( + booking_hour, booking_minute + ) @patch.dict( @@ -216,7 +235,7 @@ def test_wait_until_booking_time(mock_now): sport_name="Padel", date=pendulum.parse("2024-03-21T13:30:00Z") ) - booking_datetime = utils.retrieve_booking_datetime(booking_filter, club) + booking_datetime = retrieve_booking_datetime(booking_filter, club) seconds = [ booking_datetime.subtract(seconds=3), diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/conftest.py index e301c8a..a02afff 100644 --- a/tests/unit_tests/conftest.py +++ b/tests/unit_tests/conftest.py @@ -20,99 +20,167 @@ TEST_FOLDER = Path(__file__).parent.parent DATA_FOLDER = TEST_FOLDER / "data" RESPONSES_FOLDER = DATA_FOLDER / "responses" -court11 = Court(id="1", name="Court 1", number=1, isIndoor=True) -court12 = Court(id="2", name="Court 2", number=2, isIndoor=False) -court13 = Court(id="3", name="Court 3", number=3, isIndoor=True) -court14 = Court(id="4", name="Court 4", number=4, isIndoor=True) -sport1 = Sport( - name="Sport1", - id=8, - duration=99, - price=54, - players=3, - courts=[court11, court12, court13, court14], -) - -court21 = Court(id="1", name="Court 1", number=1, isIndoor=False) -court22 = Court(id="2", name="Court 2", number=2, isIndoor=True) -court23 = Court(id="3", name="Court 3", number=3, isIndoor=True) -court24 = Court(id="4", name="Court 4", number=4, isIndoor=True) - -sport2 = Sport( - name="Sport 2", - id=10, - duration=44, - price=23, - players=1, - courts=[court21, court22, court23, court24], -) - -landing_url = Url( - name="landing-page", - path="landing.html", -) - -login_url = Url( - name="login", - path="login.html", - payloadTemplate="gestion-sports/login-payload.txt", -) - -booking_url = Url( - name="booking", - path="booking.html", - payloadTemplate="gestion-sports/booking-payload.txt", -) - -user_bookings_url = Url( - name="user-bookings", - path="user_bookings.html", - payloadTemplate="gestion-sports/user-bookings-payload.txt", -) - -cancellation_url = Url( - name="cancellation", - path="cancel.html", - payloadTemplate="gestion-sports/booking-cancellation-payload.txt", -) - -booking_opening = BookingOpening(daysBefore=10, time="03:27") - -total_bookings = TotalBookings(peakHours=3, offPeakHours="unlimited") - -booking_platform = BookingPlatform( - id="gestion-sports", - clubId=21, - url="https://ptf1.com", - hoursBeforeCancellation=7, - bookingOpening=booking_opening, - totalBookings=total_bookings, - sports=[sport1, sport2], - urls={ - "landing-page": landing_url, - "login": login_url, - "booking": booking_url, - "user-bookings": user_bookings_url, - "cancellation": cancellation_url, - }, -) - -club = Club( - id="super_club", - name="Super Club", - url="https://superclub.com", - bookingPlatform=booking_platform, -) +@pytest.fixture +def court11() -> Court: + return Court(id="1", name="Court 1", number=1, isIndoor=True) @pytest.fixture -def a_club() -> Club: - return club +def court12() -> Court: + return Court(id="2", name="Court 2", number=2, isIndoor=False) @pytest.fixture -def connector() -> GestionSportsConnector: +def court13() -> Court: + return Court(id="3", name="Court 3", number=3, isIndoor=True) + + +@pytest.fixture +def court14() -> Court: + return Court(id="4", name="Court 4", number=4, isIndoor=True) + + +@pytest.fixture +def sport1(court11, court12, court13, court14) -> Sport: + return Sport( + name="Sport1", + id=8, + duration=99, + price=54, + players=3, + courts=[court11, court12, court13, court14], + ) + + +@pytest.fixture +def court21() -> Court: + return Court(id="1", name="Court 1", number=1, isIndoor=False) + + +@pytest.fixture +def court22() -> Court: + return Court(id="2", name="Court 2", number=2, isIndoor=True) + + +@pytest.fixture +def court23() -> Court: + return Court(id="3", name="Court 3", number=3, isIndoor=True) + + +@pytest.fixture +def court24() -> Court: + return Court(id="4", name="Court 4", number=4, isIndoor=True) + + +@pytest.fixture +def sport2(court21, court22, court23, court24) -> Sport: + return Sport( + name="Sport 2", + id=10, + duration=44, + price=23, + players=1, + courts=[court21, court22, court23, court24], + ) + + +@pytest.fixture +def landing_url() -> Url: + return Url( + name="landing-page", + path="landing.html", + ) + + +@pytest.fixture +def login_url() -> Url: + return Url( + name="login", + path="login.html", + payloadTemplate="gestion-sports/login-payload.txt", + ) + + +@pytest.fixture +def booking_url() -> Url: + return Url( + name="booking", + path="booking.html", + payloadTemplate="gestion-sports/booking-payload.txt", + ) + + +@pytest.fixture +def user_bookings_url() -> Url: + return Url( + name="user-bookings", + path="user_bookings.html", + payloadTemplate="gestion-sports/user-bookings-payload.txt", + ) + + +@pytest.fixture +def cancellation_url() -> Url: + return Url( + name="cancellation", + path="cancel.html", + payloadTemplate="gestion-sports/booking-cancellation-payload.txt", + ) + + +@pytest.fixture +def booking_opening() -> BookingOpening: + return BookingOpening(daysBefore=10, time="03:27") + + +@pytest.fixture +def total_bookings() -> TotalBookings: + return TotalBookings(peakHours=3, offPeakHours="unlimited") + + +@pytest.fixture +def booking_platform( + booking_opening, + total_bookings, + sport1, + sport2, + landing_url, + login_url, + booking_url, + user_bookings_url, + cancellation_url, +) -> BookingPlatform: + return BookingPlatform( + id="gestion-sports", + clubId=21, + url="https://ptf1.com", + hoursBeforeCancellation=7, + bookingOpening=booking_opening, + totalBookings=total_bookings, + sports=[sport1, sport2], + urls={ + "landing-page": landing_url, + "login": login_url, + "booking": booking_url, + "user-bookings": user_bookings_url, + "cancellation": cancellation_url, + }, + ) + + +@pytest.fixture +def club(booking_platform) -> Club: + return Club( + id="super_club", + name="Super Club", + url="https://superclub.com", + bookingPlatform=booking_platform, + ) + + +@pytest.fixture +def connector(club) -> GestionSportsConnector: return GestionSportsConnector(club) diff --git a/tests/unit_tests/test_cancellation.py b/tests/unit_tests/test_cancellation.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/unit_tests/test_gestion_sports_connector.py b/tests/unit_tests/test_gestion_sports_connector.py index 7ba5b52..e7c3972 100644 --- a/tests/unit_tests/test_gestion_sports_connector.py +++ b/tests/unit_tests/test_gestion_sports_connector.py @@ -91,10 +91,9 @@ def set_full_cancellation_responses(aioresponses, connector, responses): set_cancellation_response(aioresponses, connector, responses[4]) -def test_urls(a_club): - connector = GestionSportsConnector(a_club) - base_url = a_club.booking_platform.url - relative_urls = a_club.booking_platform.urls +def test_urls(connector, club): + base_url = club.booking_platform.url + relative_urls = club.booking_platform.urls relative_landing_url = relative_urls.get("landing-page").path assert connector.landing_url == f"{base_url}/{relative_landing_url}" @@ -113,12 +112,12 @@ def test_urls(a_club): @patch("config.get_resources_folder") -def test_urls_payload_templates(mock_resources, a_club): +def test_urls_payload_templates(mock_resources, club): path_to_resources = Path("some/path/to/resource") mock_resources.return_value = path_to_resources - connector = GestionSportsConnector(a_club) - relative_urls = a_club.booking_platform.urls + connector = GestionSportsConnector(club) + relative_urls = club.booking_platform.urls login_payload = relative_urls.get("login").payload_template assert connector.login_template == path_to_resources / login_payload diff --git a/tests/utils.py b/tests/utils.py deleted file mode 100644 index 7d769c0..0000000 --- a/tests/utils.py +++ /dev/null @@ -1,25 +0,0 @@ -import pendulum -from models import BookingFilter, Club -from pendulum import DateTime - -from tests.fixtures import a_booking_filter - - -def retrieve_booking_datetime( - a_booking_filter: BookingFilter, a_club: Club -) -> DateTime: - """ - Utility to retrieve the booking datetime from the booking filter and the club - - :param a_booking_filter: the booking filter that contains the date to book - :param a_club: the club which has the number of days before the date and the booking time - """ - booking_opening = a_club.booking_platform.booking_opening - opening_time = pendulum.parse(booking_opening.opening_time) - booking_hour = opening_time.hour - booking_minute = opening_time.minute - - date_to_book = a_booking_filter.date - return date_to_book.subtract(days=booking_opening.days_before).at( - booking_hour, booking_minute - )