Added a lot of unit tests

This commit is contained in:
Stanislas Jouffroy 2024-03-18 23:46:01 +01:00
parent 0938fb98b7
commit 16d4a0724c
32 changed files with 4268 additions and 497 deletions

View file

@ -80,7 +80,7 @@ class GestionSportsConnector(Connector):
raise ValueError(
"Gestion Sports connector was instantiated with a club not handled"
" by gestions sports. Club id is {} instead of gestion-sports".format(
club.club_id
club.id
)
)
@ -220,7 +220,7 @@ class GestionSportsConnector(Connector):
return self._get_url_path("cancellation")
@property
def booking_cancellation_template(self) -> Path:
def booking_cancel_template(self) -> Path:
"""
Get the payload template to send to get all the user's bookings that are
available
@ -519,8 +519,9 @@ class GestionSportsConnector(Connector):
:return: the response from the client
"""
hash_value = await self.send_hash_request(session)
payload = PayloadBuilder.build(
self.booking_cancellation_template,
self.booking_cancel_template,
booking_id=booking_id,
hash=hash_value,
)
@ -531,7 +532,9 @@ class GestionSportsConnector(Connector):
await response.text()
return response
async def cancel_booking(self, user: User, booking_filter: BookingFilter) -> None:
async def cancel_booking(
self, user: User, booking_filter: BookingFilter
) -> ClientResponse | None:
"""
Cancel the booking that meets some conditions
@ -545,54 +548,5 @@ class GestionSportsConnector(Connector):
bookings = await self.get_ongoing_bookings(session)
for booking in bookings:
if self.is_booking_matching_filter(booking, booking_filter):
await self.send_cancellation_request(session, booking.id)
def is_booking_matching_filter(
self, booking: Booking, booking_filter: BookingFilter
) -> bool:
"""
Check if the booking matches the booking filter
:param booking: the booking to be checked
:param booking_filter: the conditions the booking should meet
:return: true if the booking matches the conditions, false otherwise
"""
return (
self._is_same_sport(booking, booking_filter)
and self._is_date_matching(booking, booking_filter)
and self._is_time_matching(booking, booking_filter)
)
@staticmethod
def _is_same_sport(booking: Booking, booking_filter: BookingFilter) -> bool:
"""
Check if the booking and the booking filter are about the same sport
:param booking: the booking to be checked
:param booking_filter: the conditions the booking should meet
:return: true if the booking sport matches the filter sport, false otherwise
"""
return booking.sport == booking_filter.sport_name
@staticmethod
def _is_date_matching(booking: Booking, booking_filter: BookingFilter) -> bool:
"""
Check if the booking and the booking filter are at the same date
:param booking: the booking to be checked
:param booking_filter: the conditions the booking should meet
:return: true if the booking date matches the filter date, false otherwise
"""
return booking.booking_date.date() == booking_filter.date.date()
@staticmethod
def _is_time_matching(booking: Booking, booking_filter: BookingFilter) -> bool:
"""
Check if the booking and the booking filter are at the same time
:param booking: the booking to be checked
:param booking_filter: the conditions the booking should meet
:return: true if the booking time matches the filter time, false otherwise
"""
return booking.start_time.time() == booking_filter.date.time()
if booking.matches(booking_filter):
return await self.send_cancellation_request(session, booking.id)

View file

@ -2,6 +2,7 @@ from enum import Enum
from typing import Optional
import pendulum
from pendulum import Date, Time
from pydantic import BaseModel, ConfigDict, Field, field_validator
from pydantic_extra_types.pendulum_dt import DateTime
@ -165,6 +166,46 @@ class Booking(BaseModel):
def to_lower_case(cls, d: str) -> str:
return d.lower()
def matches(self, booking_filter: BookingFilter) -> bool:
"""
Check if the booking matches the booking filter
:param booking_filter: the conditions the booking should meet
:return: true if the booking matches the conditions, false otherwise
"""
return (
self.is_same_sport(booking_filter.sport_name)
and self.is_same_date(booking_filter.date.date())
and self.is_same_time(booking_filter.date.time())
)
def is_same_sport(self, sport: str) -> bool:
"""
Check if the booking and the booking filter are about the same sport
:param sport: the sport to test
:return: true if the sport matches booking sport, false otherwise
"""
return self.sport == sport
def is_same_date(self, date: Date) -> bool:
"""
Check if the booking filter has the same date as the booking
:param date: the date to test
:return: true if the date matches the booking date, false otherwise
"""
return self.booking_date.date() == date
def is_same_time(self, time: Time) -> bool:
"""
Check if the booking filter has the same time as the booking
:param time: the time to test
:return: true if the time matches the booking time, false otherwise
"""
return self.start_time.time() == time
class Action(Enum):
BOOK = "book"