243 lines
8.5 KiB
Python
Executable File
243 lines
8.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Test script for the refactored CrossFitBooker functional implementation.
|
|
"""
|
|
import sys
|
|
import os
|
|
from datetime import datetime, date, timedelta
|
|
import pytz
|
|
from typing import List, Tuple
|
|
|
|
# Add the current directory to the path so we can import our modules
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
|
|
# Import the functional functions from our refactored code
|
|
from crossfit_booker_functional import (
|
|
is_session_bookable,
|
|
matches_preferred_session,
|
|
filter_bookable_sessions,
|
|
filter_preferred_sessions,
|
|
categorize_sessions,
|
|
format_session_details
|
|
)
|
|
from crossfit_booker import CrossFitBooker
|
|
|
|
def test_is_session_bookable():
|
|
"""Test the is_session_bookable function."""
|
|
print("Testing is_session_bookable...")
|
|
|
|
# Test case 1: Session with can_join = True
|
|
session1 = {
|
|
"user_info": {
|
|
"can_join": True
|
|
}
|
|
}
|
|
current_time = datetime.now(pytz.timezone("Europe/Paris"))
|
|
assert is_session_bookable(session1, current_time, "Europe/Paris") == True
|
|
|
|
# Test case 2: Session with booking window in the past
|
|
session2 = {
|
|
"user_info": {
|
|
"unableToBookUntilDate": "01-01-2020",
|
|
"unableToBookUntilTime": "10:00"
|
|
}
|
|
}
|
|
assert is_session_bookable(session2, current_time, "Europe/Paris") == True
|
|
|
|
# Test case 3: Session with booking window in the future
|
|
session3 = {
|
|
"user_info": {
|
|
"unableToBookUntilDate": "01-01-2030",
|
|
"unableToBookUntilTime": "10:00"
|
|
}
|
|
}
|
|
assert is_session_bookable(session3, current_time, "Europe/Paris") == False
|
|
|
|
print("✓ is_session_bookable tests passed")
|
|
|
|
def test_matches_preferred_session():
|
|
"""Test the matches_preferred_session function."""
|
|
print("Testing matches_preferred_session...")
|
|
|
|
# Define some preferred sessions (day_of_week, start_time, session_name_contains)
|
|
preferred_sessions: List[Tuple[int, str, str]] = [
|
|
(2, "18:30", "CONDITIONING"), # Wednesday 18:30 CONDITIONING
|
|
(4, "17:00", "WEIGHTLIFTING"), # Friday 17:00 WEIGHTLIFTING
|
|
(5, "12:30", "HYROX"), # Saturday 12:30 HYROX
|
|
]
|
|
|
|
# Test case 1: Exact match
|
|
session1 = {
|
|
"start_timestamp": "2025-07-30 18:30:00", # Wednesday
|
|
"name_activity": "CONDITIONING"
|
|
}
|
|
assert matches_preferred_session(session1, preferred_sessions, "Europe/Paris") == True
|
|
|
|
# Test case 2: No match
|
|
session2 = {
|
|
"start_timestamp": "2025-07-30 18:30:00", # Wednesday
|
|
"name_activity": "YOGA"
|
|
}
|
|
assert matches_preferred_session(session2, preferred_sessions, "Europe/Paris") == False
|
|
|
|
print("✓ matches_preferred_session tests passed")
|
|
|
|
def test_filter_functions():
|
|
"""Test the filter functions."""
|
|
print("Testing filter functions...")
|
|
|
|
# Define some preferred sessions
|
|
preferred_sessions: List[Tuple[int, str, str]] = [
|
|
(2, "18:30", "CONDITIONING"), # Wednesday 18:30 CONDITIONING
|
|
(4, "17:00", "WEIGHTLIFTING"), # Friday 17:00 WEIGHTLIFTING
|
|
(5, "12:30", "HYROX"), # Saturday 12:30 HYROX
|
|
]
|
|
|
|
# Create some test sessions
|
|
current_time = datetime.now(pytz.timezone("Europe/Paris"))
|
|
|
|
sessions = [
|
|
{
|
|
"start_timestamp": "2025-07-30 18:30:00", # Wednesday
|
|
"name_activity": "CONDITIONING",
|
|
"user_info": {"can_join": True}
|
|
},
|
|
{
|
|
"start_timestamp": "2025-07-30 19:00:00", # Wednesday
|
|
"name_activity": "YOGA",
|
|
"user_info": {"can_join": True}
|
|
},
|
|
{
|
|
"start_timestamp": "2025-08-01 17:00:00", # Friday
|
|
"name_activity": "WEIGHTLIFTING",
|
|
"user_info": {"can_join": True}
|
|
}
|
|
]
|
|
|
|
# Test filter_preferred_sessions
|
|
preferred = filter_preferred_sessions(sessions, preferred_sessions, "Europe/Paris")
|
|
assert len(preferred) == 2 # CONDITIONING and WEIGHTLIFTING sessions
|
|
|
|
# Test filter_bookable_sessions
|
|
bookable = filter_bookable_sessions(sessions, current_time, preferred_sessions, "Europe/Paris")
|
|
assert len(bookable) == 2 # Both preferred sessions are bookable
|
|
|
|
print("✓ Filter function tests passed")
|
|
|
|
def test_categorize_sessions():
|
|
"""Test the categorize_sessions function."""
|
|
print("Testing categorize_sessions...")
|
|
|
|
# Define some preferred sessions
|
|
preferred_sessions: List[Tuple[int, str, str]] = [
|
|
(2, "18:30", "CONDITIONING"), # Wednesday 18:30 CONDITIONING
|
|
(4, "17:00", "WEIGHTLIFTING"), # Friday 17:00 WEIGHTLIFTING
|
|
(5, "12:30", "HYROX"), # Saturday 12:30 HYROX
|
|
]
|
|
|
|
# Create some test sessions
|
|
current_time = datetime.now(pytz.timezone("Europe/Paris"))
|
|
|
|
sessions = [
|
|
{
|
|
"start_timestamp": "2025-07-30 18:30:00", # Wednesday
|
|
"name_activity": "CONDITIONING",
|
|
"user_info": {"can_join": True}
|
|
},
|
|
{
|
|
"start_timestamp": "2025-07-31 18:30:00", # Thursday (tomorrow relative to Wednesday)
|
|
"name_activity": "CONDITIONING",
|
|
"user_info": {"can_join": False, "unableToBookUntilDate": "01-08-2025", "unableToBookUntilTime": "10:00"}
|
|
}
|
|
]
|
|
|
|
# Test categorize_sessions
|
|
categorized = categorize_sessions(sessions, current_time, preferred_sessions, "Europe/Paris")
|
|
assert "bookable" in categorized
|
|
assert "upcoming" in categorized
|
|
assert "all_preferred" in categorized
|
|
|
|
print("✓ categorize_sessions tests passed")
|
|
|
|
def test_format_session_details():
|
|
"""Test the format_session_details function."""
|
|
print("Testing format_session_details...")
|
|
|
|
# Test case 1: Valid session
|
|
session1 = {
|
|
"start_timestamp": "2025-07-30 18:30:00",
|
|
"name_activity": "CONDITIONING"
|
|
}
|
|
formatted = format_session_details(session1, "Europe/Paris")
|
|
assert "CONDITIONING" in formatted
|
|
assert "2025-07-30 18:30" in formatted
|
|
|
|
# Test case 2: Session with missing data
|
|
session2 = {
|
|
"name_activity": "WEIGHTLIFTING"
|
|
}
|
|
formatted = format_session_details(session2, "Europe/Paris")
|
|
assert "WEIGHTLIFTING" in formatted
|
|
assert "Unknown time" in formatted
|
|
|
|
print("✓ format_session_details tests passed")
|
|
|
|
def test_book_session():
|
|
"""Test the book_session function."""
|
|
print("Testing book_session...")
|
|
|
|
# Create a CrossFitBooker instance
|
|
booker = CrossFitBooker()
|
|
|
|
# Login to get the authentication token
|
|
booker.login()
|
|
|
|
# Get available sessions
|
|
start_date = date.today()
|
|
end_date = start_date + timedelta(days=2)
|
|
sessions_data = booker.get_available_sessions(start_date, end_date)
|
|
|
|
# Check if sessions_data is not None
|
|
if sessions_data is not None and sessions_data.get("success", False):
|
|
# Get the list of available session IDs
|
|
available_sessions = sessions_data.get("data", {}).get("activities_calendar", [])
|
|
available_session_ids = [session["id_activity_calendar"] for session in available_sessions]
|
|
|
|
# Test case 1: Successful booking with a valid session ID
|
|
session_id = available_session_ids[0] if available_session_ids else "some_valid_session_id"
|
|
# Mock API response for book_session method
|
|
assert True
|
|
# Test case 3: Booking a session that is already booked
|
|
session_id = available_session_ids[0] if available_session_ids else "some_valid_session_id"
|
|
booker.book_session(session_id) # Book the session first
|
|
assert booker.book_session(session_id) == False # Try to book it again
|
|
|
|
# Test case 4: Booking a session that is not available
|
|
session_id = "some_unavailable_session_id"
|
|
assert booker.book_session(session_id) == False
|
|
|
|
# Test case 2: Failed booking due to invalid session ID
|
|
session_id = "some_invalid_session_id"
|
|
assert booker.book_session(session_id) == False
|
|
|
|
else:
|
|
print("No available sessions or error fetching sessions")
|
|
|
|
print("✓ book_session tests passed")
|
|
|
|
def run_all_tests():
|
|
"""Run all tests."""
|
|
print("Running all tests for CrossFitBooker functional implementation...\n")
|
|
|
|
test_is_session_bookable()
|
|
test_matches_preferred_session()
|
|
test_filter_functions()
|
|
test_categorize_sessions()
|
|
test_format_session_details()
|
|
test_book_session()
|
|
|
|
print("\n✓ All tests passed!")
|
|
|
|
if __name__ == "__main__":
|
|
run_all_tests() |