Skip to content

Commit

Permalink
test: Correct the test_lightning.py::test_empty_array_error
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementLaplace committed Jan 14, 2025
1 parent a008f9f commit 7676013
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions satpy/composites/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def _normalize_time(self, data:xr.DataArray, attrs:dict) -> xr.DataArray:
# exit if data is empty afer filtering
if data.size == 0 :
LOG.error(f"All the flash_age events happened before {begin_time}")
raise ValueError(f"Invalid data: data size is zero. All flash_age \
events occurred before the specified start time ({begin_time})."
raise ValueError(f"Invalid data: data size is zero. All flash_age "
f"events occurred before the specified start time ({begin_time})."
)
# Normalize the time values
normalized_data = (data - begin_time) / (end_time - begin_time)
Expand Down
22 changes: 13 additions & 9 deletions satpy/tests/compositor_tests/test_lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

import datetime
import logging
from unittest import mock

import numpy as np
import pytest
import xarray as xr

from satpy.composites.lightning import LightningTimeCompositor
Expand Down Expand Up @@ -66,23 +66,27 @@ def test_empty_array_error(caplog):
time_range=60,
reference_time="end_time")
attrs_flash_age = {"variable_name": "flash_time","name": "flash_time",
"start_time": datetime.datetime(2024, 8, 1, 10, 50, 0),
"end_time": datetime.datetime(2024, 8, 1, 11, 0, 0),"reader": "li_l2_nc"}
"start_time": np.datetime64(datetime.datetime(2024, 8, 1, 10, 0, 0)),
"end_time": datetime.datetime(2024, 8, 1, 11, 0, 0),
"reader": "li_l2_nc"}
flash_age_value = np.array(["2024-08-01T09:00:00"], dtype="datetime64[ns]")
flash_age = xr.DataArray(
flash_age_value,
dims=["y"],
coords={
"crs": "8B +proj=longlat +ellps=WGS84 +type=crs"
},attrs = attrs_flash_age,name="flash_time")
with mock.patch("sys.exit") as mock_exit:
# Capture logging output
with caplog.at_level(logging.ERROR):
with caplog.at_level(logging.ERROR):
# Simulate the operation that raises the exception
with pytest.raises(ValueError, match="data size is zero") as excinfo:
_ = comp([flash_age])

mock_exit.assert_called_once_with(1)

assert "All the flash_age events happened before 2024-08-01T10:00:00" in caplog.text
# Assert the exception message
assert str(excinfo.value) == (
f"Invalid data: data size is zero. All flash_age events occurred before "
f"the specified start time ({attrs_flash_age['start_time']})."
)
assert "All the flash_age events happened before 2024-08-01T10:00:00" in caplog.text

def test_update_missing_metadata():
"""Test the _update_missing_metadata method."""
Expand Down

0 comments on commit 7676013

Please sign in to comment.