Skip to content

Commit

Permalink
Handle flight departure time changes correctly
Browse files Browse the repository at this point in the history
Fixes #199.

This is actually a regression that was implemented in v6.1 when I
changed to differentiate flights by flight number. The issue is that
a flight can still have the same flight number but not the same
departure time (if it was rescheduled). Now, both the flight number and
departure time are checked to see whether the flight is equal to one
already scheduled for check in.
  • Loading branch information
jdholtz committed Jan 8, 2024
1 parent 160ada3 commit f285437
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
6 changes: 5 additions & 1 deletion lib/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def __init__(self, flight_info: Dict[str, Any], confirmation_number: str) -> Non

def __eq__(self, other: object) -> bool:
# Define how two flights are equal to each other
return isinstance(other, Flight) and self.flight_number == other.flight_number
return (
isinstance(other, Flight)
and self.flight_number == other.flight_number
and self.departure_time == other.departure_time
)

def _get_flight_time(self, flight: JSON) -> datetime:
flight_date = f"{flight['departureDate']} {flight['departureTime']}"
Expand Down
53 changes: 28 additions & 25 deletions tests/unit/test_flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ def _set_up_flight(self) -> None:
"flights": [{"number": "100"}],
}

departure_time = datetime(1999, 1, 1, 8, 59)
# Needs to be mocked so it is only run when Flight is instantiated
with mock.patch.object(Flight, "_get_flight_time", return_value=None):
with mock.patch.object(Flight, "_get_flight_time", return_value=departure_time):
# pylint: disable=attribute-defined-outside-init
self.flight = Flight(flight_info, "test_num")

Expand All @@ -43,8 +44,10 @@ def test_flight_is_international_when_country_is_specified(

assert flight.is_international == is_international

def test_flights_with_the_same_flight_numbers_are_equal(self, mocker: MockerFixture) -> None:
mocker.patch.object(Flight, "_get_flight_time")
def test_flights_with_the_same_flight_numbers_and_departure_times_are_equal(
self, mocker: MockerFixture
) -> None:
mocker.patch.object(Flight, "_get_flight_time", return_value=datetime(1999, 1, 1, 8, 59))
flight_info = {
"departureAirport": {"name": None},
"arrivalAirport": {"name": None, "country": None},
Expand All @@ -57,32 +60,32 @@ def test_flights_with_the_same_flight_numbers_are_equal(self, mocker: MockerFixt
assert flight1 == flight2

@pytest.mark.parametrize(
"flight_info",
["flight_info", "departure_time"],
[
{
"departureAirport": {"name": None},
"arrivalAirport": {"name": None, "country": None},
"departureTime": None,
"flights": [{"number": "101"}],
},
{
"departureAirport": {"name": None},
"arrivalAirport": {"name": None, "country": None},
"departureTime": None,
"flights": [{"number": "102"}],
},
{
"departureAirport": {"name": None},
"arrivalAirport": {"name": None, "country": None},
"departureTime": None,
"flights": [{"number": "103"}],
},
(
{ # Test different flight numbers
"departureAirport": {"name": None},
"arrivalAirport": {"name": None, "country": None},
"departureTime": None,
"flights": [{"number": "101"}],
},
datetime(1999, 1, 1, 8, 59),
),
(
{ # Test different departure times
"departureAirport": {"name": None},
"arrivalAirport": {"name": None, "country": None},
"departureTime": None,
"flights": [{"number": "100"}],
},
datetime(1999, 1, 1, 9, 59),
),
],
)
def test_flights_with_different_flight_numbers_are_not_equal(
self, mocker: MockerFixture, flight_info: Dict[str, Any]
def test_flights_with_different_flight_numbers_or_departure_times_are_not_equal(
self, mocker: MockerFixture, flight_info: Dict[str, Any], departure_time: datetime
) -> None:
mocker.patch.object(Flight, "_get_flight_time")
mocker.patch.object(Flight, "_get_flight_time", return_value=departure_time)
new_flight = Flight(flight_info, "")

assert self.flight != new_flight
Expand Down

0 comments on commit f285437

Please sign in to comment.