Fixed up the damm tests

This commit is contained in:
Jonas Linter
2025-12-02 15:24:30 +01:00
parent 56d67984cf
commit c0e601e308
4 changed files with 179 additions and 84 deletions

View File

@@ -9,6 +9,43 @@ from typing import Optional
from xml.etree import ElementTree as ET
def validate_and_convert_id(field_name: str, value: str | int) -> str:
"""Validate that an ID field is convertible to integer and return as string.
This helper ensures ID fields (like reservation_id, guest_id) are valid integers,
which is important since the Pydantic models will convert them from strings to ints.
Args:
field_name: Name of the field for error messages
value: The ID value (can be string or int)
Returns:
String representation of the validated integer ID
Raises:
ValueError: If value cannot be converted to a valid positive integer
"""
def _raise_invalid_type_error():
"""Raise error for invalid ID type."""
msg = (
f"{field_name} must be convertible to a positive integer, "
f"got: {value!r} (type: {type(value).__name__})"
)
raise ValueError(msg)
try:
# Convert to int first to validate it's a valid integer
int_value = int(value)
if int_value <= 0:
msg = f"{field_name} must be a positive integer, got: {value}"
raise ValueError(msg)
# Return as string for XML attributes
return str(int_value)
except (ValueError, TypeError):
_raise_invalid_type_error()
class RoomReservationBuilder:
"""Builder for creating roomReservation XML elements with daily sales."""
@@ -133,7 +170,7 @@ class ReservationXMLBuilder:
def __init__(
self,
hotel_id: str,
reservation_id: str,
reservation_id: str | int,
reservation_number: str,
reservation_date: str,
creation_time: Optional[str] = None,
@@ -146,7 +183,7 @@ class ReservationXMLBuilder:
Args:
hotel_id: Hotel ID
reservation_id: Reservation ID
reservation_id: Reservation ID (must be convertible to positive integer)
reservation_number: Reservation number
reservation_date: Reservation date in YYYY-MM-DD format
creation_time: Creation timestamp (defaults to reservation_date + T00:00:00)
@@ -156,7 +193,7 @@ class ReservationXMLBuilder:
advertising_campagne: Advertising campaign
"""
self.hotel_id = hotel_id
self.reservation_id = reservation_id
self.reservation_id = validate_and_convert_id("reservation_id", reservation_id)
self.reservation_number = reservation_number
self.reservation_date = reservation_date
self.creation_time = creation_time or f"{reservation_date}T00:00:00"
@@ -170,7 +207,7 @@ class ReservationXMLBuilder:
def set_guest(
self,
guest_id: str,
guest_id: str | int,
first_name: str,
last_name: str,
email: str,
@@ -182,7 +219,7 @@ class ReservationXMLBuilder:
"""Set guest information for the reservation.
Args:
guest_id: Guest ID
guest_id: Guest ID (must be convertible to positive integer)
first_name: Guest first name
last_name: Guest last name
email: Guest email
@@ -194,8 +231,9 @@ class ReservationXMLBuilder:
Returns:
Self for method chaining
"""
validated_guest_id = validate_and_convert_id("guest_id", guest_id)
self.guest_data = {
"id": guest_id,
"id": validated_guest_id,
"firstName": first_name,
"lastName": last_name,
"email": email,