refactored code to place the booking specific actions inside a gestion-sports element

This commit is contained in:
Stanislas Jouffroy 2024-02-21 23:49:33 +01:00
parent 1ec148e866
commit 552db2aa8a
3 changed files with 11 additions and 12 deletions

View file

@ -2,7 +2,7 @@ import asyncio
import logging
import config
from gestion_sports.gestion_sports_operations import GestionSportsOperations
from gestion_sports.gestion_sports_platform import GestionSportsPlatform
from models import BookingFilter, Club, User
LOGGER = logging.getLogger(__name__)
@ -17,7 +17,7 @@ async def book(club: Club, user: User, booking_filter: BookingFilter) -> int | N
:param booking_filter: the information related to the booking
:return: the id of the booked court, or None if no court was booked
"""
async with GestionSportsOperations(club) as platform:
async with GestionSportsPlatform(club) as platform:
return await platform.book(user, booking_filter)

View file

@ -10,28 +10,28 @@ from pendulum import DateTime
LOGGER = logging.getLogger(__name__)
class GestionSportsOperations:
class GestionSportsPlatform:
def __init__(self, club: Club):
self.platform: GestionSportsConnector | None = None
self.connector: GestionSportsConnector | None = None
self.club: Club = club
self.session: ClientSession | None = None
async def __aenter__(self):
self.session = ClientSession()
self.platform = GestionSportsConnector(self.session, self.club.url)
self.connector = GestionSportsConnector(self.session, self.club.url)
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.session.close()
async def book(self, user: User, booking_filter: BookingFilter) -> int | None:
if self.platform is None or user is None or booking_filter is None:
if self.connector is None or user is None or booking_filter is None:
return None
await self.platform.land()
await self.platform.login(user, self.club)
await self.connector.land()
await self.connector.login(user, self.club)
wait_until_booking_time(self.club, booking_filter)
return await self.platform.book(booking_filter, self.club)
return await self.connector.book(booking_filter, self.club)
def wait_until_booking_time(club: Club, booking_filter: BookingFilter):

View file

@ -3,8 +3,7 @@ from unittest.mock import patch
import pendulum
import pytest
from aioresponses import aioresponses
from gestion_sports import gestion_sports_operations
from gestion_sports.gestion_sports_operations import GestionSportsOperations
from gestion_sports.gestion_sports_platform import GestionSportsPlatform
from models import BookingFilter, Club, User
from tests import fixtures, utils
@ -49,7 +48,7 @@ async def test_booking(
a_booking_success_response,
)
async with GestionSportsOperations(a_club) as gs_operations:
async with GestionSportsPlatform(a_club) as gs_operations:
court_booked = await gs_operations.book(a_user, a_booking_filter)
assert court_booked == a_club.courts_ids[1]