Skip to content

armgit / fli

Public Imported from GitHub
0

Google Flights MCP, CLI and Python Library

fix: normalize single-digit month/day in CLI date input (#71)

Michal Tajchert07b5aac09e99
diff --git a/fli/cli/utils.py b/fli/cli/utils.py
index 6eb79ad..d322583 100644
--- a/fli/cli/utils.py
+++ b/fli/cli/utils.py
@@ -1,7 +1,5 @@
"""CLI utility functions for display and validation."""
-from datetime import datetime
-
import plotext as plt
import typer
from click import Context, Parameter
@@ -13,6 +11,7 @@ from rich.text import Text
from fli.cli.console import console
from fli.cli.enums import DayOfWeek
+from fli.core.builders import normalize_date
from fli.core.parsers import ParseError
from fli.core.parsers import parse_airlines as core_parse_airlines
from fli.core.parsers import parse_max_stops as core_parse_max_stops
@@ -25,8 +24,7 @@ def validate_date(ctx: Context, param: Parameter, value: str) -> str | None:
return None
try:
- datetime.strptime(value, "%Y-%m-%d")
- return value
+ return normalize_date(value)
except ValueError as e:
raise typer.BadParameter("Date must be in YYYY-MM-DD format") from e
diff --git a/fli/core/builders.py b/fli/core/builders.py
index 53529b5..d9aad65 100644
--- a/fli/core/builders.py
+++ b/fli/core/builders.py
@@ -9,6 +9,22 @@ from datetime import datetime, timedelta
from fli.models import Airport, FlightSegment, TimeRestrictions, TripType
+def normalize_date(date_str: str) -> str:
+ """Normalize a date string to zero-padded YYYY-MM-DD format.
+
+ Args:
+ date_str: Date string in YYYY-MM-DD format (e.g., '2026-4-2' or '2026-04-02')
+
+ Returns:
+ Zero-padded date string (e.g., '2026-04-02')
+
+ Raises:
+ ValueError: If the date string is not a valid date
+
+ """
+ return datetime.strptime(date_str, "%Y-%m-%d").strftime("%Y-%m-%d")
+
+
def build_time_restrictions(
departure_window: str | None = None,
arrival_window: str | None = None,
@@ -69,6 +85,8 @@ def build_flight_segments(
Tuple of (list of FlightSegment objects, TripType)
"""
+ departure_date = normalize_date(departure_date)
+
segments = [
FlightSegment(
departure_airport=[[origin, 0]],
@@ -81,6 +99,7 @@ def build_flight_segments(
trip_type = TripType.ONE_WAY
if return_date:
+ return_date = normalize_date(return_date)
trip_type = TripType.ROUND_TRIP
segments.append(
FlightSegment(
@@ -116,6 +135,8 @@ def build_date_search_segments(
Tuple of (list of FlightSegment objects, TripType)
"""
+ start_date = normalize_date(start_date)
+
segments = [
FlightSegment(
departure_airport=[[origin, 0]],
diff --git a/tests/cli/test_utils.py b/tests/cli/test_utils.py
index 0f09c55..a2d97e8 100644
--- a/tests/cli/test_utils.py
+++ b/tests/cli/test_utils.py
@@ -38,6 +38,13 @@ def test_validate_date_valid(mock_context, mock_param):
assert result == date
+def test_validate_date_single_digit(mock_context, mock_param):
+ """Test date validation normalizes single-digit month and day."""
+ assert validate_date(mock_context, mock_param, "2026-4-2") == "2026-04-02"
+ assert validate_date(mock_context, mock_param, "2026-12-5") == "2026-12-05"
+ assert validate_date(mock_context, mock_param, "2026-1-15") == "2026-01-15"
+
+
def test_validate_date_invalid(mock_context, mock_param):
"""Test date validation with invalid date."""
with pytest.raises(BadParameter):
diff --git a/tests/core/__init__.py b/tests/core/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/core/test_builders.py b/tests/core/test_builders.py
new file mode 100644
index 0000000..01a430d
--- /dev/null
+++ b/tests/core/test_builders.py
@@ -0,0 +1,75 @@
+import pytest
+
+from fli.core.builders import build_date_search_segments, build_flight_segments, normalize_date
+from fli.models import Airport, TripType
+
+
+class TestNormalizeDate:
+ """Tests for normalize_date."""
+
+ def test_already_padded(self):
+ assert normalize_date("2026-04-02") == "2026-04-02"
+
+ def test_single_digit_month_and_day(self):
+ assert normalize_date("2026-4-2") == "2026-04-02"
+
+ def test_single_digit_day(self):
+ assert normalize_date("2026-12-5") == "2026-12-05"
+
+ def test_single_digit_month(self):
+ assert normalize_date("2026-1-15") == "2026-01-15"
+
+ def test_invalid_date_raises(self):
+ with pytest.raises(ValueError):
+ normalize_date("not-a-date")
+
+ def test_invalid_month_raises(self):
+ with pytest.raises(ValueError):
+ normalize_date("2026-13-01")
+
+
+class TestBuildFlightSegments:
+ """Tests for date normalization in build_flight_segments."""
+
+ def test_normalizes_departure_date(self):
+ segments, _ = build_flight_segments(
+ origin=Airport.JFK,
+ destination=Airport.LAX,
+ departure_date="2026-4-2",
+ )
+ assert segments[0].travel_date == "2026-04-02"
+
+ def test_normalizes_return_date(self):
+ segments, trip_type = build_flight_segments(
+ origin=Airport.JFK,
+ destination=Airport.LAX,
+ departure_date="2026-4-2",
+ return_date="2026-4-9",
+ )
+ assert trip_type == TripType.ROUND_TRIP
+ assert segments[0].travel_date == "2026-04-02"
+ assert segments[1].travel_date == "2026-04-09"
+
+
+class TestBuildDateSearchSegments:
+ """Tests for date normalization in build_date_search_segments."""
+
+ def test_normalizes_start_date(self):
+ segments, _ = build_date_search_segments(
+ origin=Airport.JFK,
+ destination=Airport.LAX,
+ start_date="2026-4-2",
+ )
+ assert segments[0].travel_date == "2026-04-02"
+
+ def test_normalizes_start_date_round_trip(self):
+ segments, trip_type = build_date_search_segments(
+ origin=Airport.JFK,
+ destination=Airport.LAX,
+ start_date="2026-4-2",
+ is_round_trip=True,
+ trip_duration=7,
+ )
+ assert trip_type == TripType.ROUND_TRIP
+ assert segments[0].travel_date == "2026-04-02"
+ assert segments[1].travel_date == "2026-04-09"