Skip to content

Commit

Permalink
Fix crash when an empty time series is cached (fixes #489)
Browse files Browse the repository at this point in the history
  • Loading branch information
aptiko committed Mar 19, 2023
1 parent e57e815 commit 7367e91
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion enhydris/models/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def get_data(self, start_date=None, end_date=None, timezone=None):

def _get_data_from_cache(self, start_date, end_date):
data = cache.get(f"timeseries_data_{self.id}")
if data is None:
if data is None or data.empty:
raise DataNotInCache()
if self.start_date is None:
return data # Data should be empty in that case; just return it
Expand Down
14 changes: 14 additions & 0 deletions enhydris/tests/test_models/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,20 @@ def _get_data_and_check_num_queries(self, num_queries, *, start_date, end_date):
data = self.timeseries.get_data(start_date=start_date, end_date=end_date)
pd.testing.assert_frame_equal(data.data, self.expected_result)

def test_cached_empty_dataframe(self):
"""Test the case where an empty dataframe was previously cached"""

cache.clear()

# Get (and thus cache) the time series, starting from a timestamp more recent
# than the end date (that is, cache an empty dataframe).
start_date = self.timeseries.end_date + dt.timedelta(minutes=1)
self.timeseries.get_data(start_date=start_date)

# Then ensure that getting the time series works
data = self.timeseries.get_data(start_date=self.timeseries.end_date)
self.assertEqual(len(data.data), 1)


class TimeseriesSetDataTestCase(TestTimeseriesMixin, TestCase):
def setUp(self):
Expand Down

0 comments on commit 7367e91

Please sign in to comment.