Skip to content

Commit

Permalink
Improved error handling (#3)
Browse files Browse the repository at this point in the history
* handle case where aggregator does not retrieve data

* improved test coverage
  • Loading branch information
exxamalte committed Nov 12, 2018
1 parent c61430b commit 6433948
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
68 changes: 36 additions & 32 deletions flightradar24_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,42 @@ async def update(self):
if status == UPDATE_OK:
self._stack.pop()
self._stack.appendleft(data)
# Fill in some gaps in data received.
for key in data:
# Keep record of callsigns.
if key not in self._callsigns and data[key].callsign:
self._callsigns[key] = data[key].callsign
# Fill in callsign from previous update if currently missing.
if not data[key].callsign and key in self._callsigns:
data[key].override(ATTR_CALLSIGN, self._callsigns[key])
# Keep record of latest coordinates.
# Here we are considering (lat=0, lon=0) as unwanted coordinates,
# despite the fact that they are valid. Typically, coordinates
# (0, 0) indicate that the correct coordinates have not been
# received.
if data[key].coordinates \
and data[key].coordinates != INVALID_COORDINATES \
and data[key].coordinates != NONE_COORDINATES:
self._coordinates[key] = data[key].coordinates
# Fill in missing coordinates.
if (not data[key].coordinates
or data[key].coordinates == INVALID_COORDINATES
or data[key].coordinates == NONE_COORDINATES) \
and key in self._coordinates:
data[key].override(ATTR_LATITUDE, self._coordinates[key][0])
data[key].override(ATTR_LONGITUDE, self._coordinates[key][1])
_LOGGER.debug("Callsigns = %s", self._callsigns)
_LOGGER.debug("Coordinates = %s", self._coordinates)
# Filter entries.
filtered_entries = self._filter_entries(data.values())
# Rebuild the entries and use external id as key.
result_entries = {entry.external_id: entry
for entry in filtered_entries}
return status, result_entries
if data:
# Fill in some gaps in data received.
for key in data:
# Keep record of callsigns.
if key not in self._callsigns and data[key].callsign:
self._callsigns[key] = data[key].callsign
# Fill in callsign from previous update if currently missing.
if not data[key].callsign and key in self._callsigns:
data[key].override(ATTR_CALLSIGN, self._callsigns[key])
# Keep record of latest coordinates.
# Here we are considering (lat=0, lon=0) as unwanted
# coordinates, despite the fact that they are valid.
# Typically, coordinates (0, 0) indicate that the correct
# coordinates have not been received.
if data[key].coordinates \
and data[key].coordinates != INVALID_COORDINATES \
and data[key].coordinates != NONE_COORDINATES:
self._coordinates[key] = data[key].coordinates
# Fill in missing coordinates.
if (not data[key].coordinates
or data[key].coordinates == INVALID_COORDINATES
or data[key].coordinates == NONE_COORDINATES) \
and key in self._coordinates:
data[key].override(ATTR_LATITUDE,
self._coordinates[key][0])
data[key].override(ATTR_LONGITUDE,
self._coordinates[key][1])
_LOGGER.debug("Callsigns = %s", self._callsigns)
_LOGGER.debug("Coordinates = %s", self._coordinates)
# Filter entries.
filtered_entries = self._filter_entries(data.values())
# Rebuild the entries and use external id as key.
result_entries = {entry.external_id: entry
for entry in filtered_entries}
return status, result_entries
return status, None

def _filter_entries(self, entries):
"""Filter the provided entries."""
Expand Down
13 changes: 13 additions & 0 deletions tests/test_fr24_flights.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ def test_feed_aggregator(self, mock_session):
assert feed_entry.altitude == 22175
assert feed_entry.callsign == "JST423"

@aioresponses()
def test_feed_aggregator_update_error(self, mock_session):
"""Test updating feed aggregator results in error."""
loop = asyncio.get_event_loop()
home_coordinates = (-31.0, 151.0)
mock_session.get('http://localhost:8754/flights.json', status=500,
body='ERROR')

feed_aggregator = Flightradar24FlightsFeedAggregator(home_coordinates)
status, entries = loop.run_until_complete(feed_aggregator.update())
assert status == UPDATE_ERROR
self.assertIsNone(entries)

def test_entry_without_data(self):
"""Test simple entry without data."""
entry = FeedEntry(None, None)
Expand Down

0 comments on commit 6433948

Please sign in to comment.