diff --git a/rocketpy/environment/environment_analysis.py b/rocketpy/environment/environment_analysis.py index e4dcbd021..7aeae1562 100644 --- a/rocketpy/environment/environment_analysis.py +++ b/rocketpy/environment/environment_analysis.py @@ -450,7 +450,7 @@ def __find_preferred_timezone(self): tf.timezone_at(lng=self.longitude, lat=self.latitude) ) except ImportError: - warnings.warning( # pragma: no cover + warnings.warn( "'timezonefinder' not installed, defaulting to UTC." + " Install timezonefinder to get local time zone." + " To do so, run 'pip install timezonefinder'" diff --git a/tests/unit/environment/test_environment_analysis.py b/tests/unit/environment/test_environment_analysis.py index 11205ebea..a9f9a945e 100644 --- a/tests/unit/environment/test_environment_analysis.py +++ b/tests/unit/environment/test_environment_analysis.py @@ -1,14 +1,53 @@ import os +from datetime import datetime from unittest.mock import patch import matplotlib as plt import pytest +from rocketpy import EnvironmentAnalysis from rocketpy.tools import import_optional_dependency plt.rcParams.update({"figure.max_open_warning": 0}) +@patch("rocketpy.environment.environment_analysis._EnvironmentAnalysisPlots") +@patch("rocketpy.environment.environment_analysis._EnvironmentAnalysisPrints") +@patch.object( + EnvironmentAnalysis, + "_EnvironmentAnalysis__check_requirements", +) +@patch( + "rocketpy.environment.environment_analysis.import_optional_dependency", + side_effect=ImportError("timezonefinder is not installed"), +) +def test_missing_timezonefinder_defaults_to_utc( + _mock_import_optional_dependency, + _mock_check_requirements, + _mock_prints, + _mock_plots, +): + """Use UTC when automatic timezone detection is unavailable.""" + # Arrange + start_date = datetime(2026, 1, 1) + end_date = datetime(2026, 1, 2) + + # Act + with pytest.warns(UserWarning, match="defaulting to UTC"): + analysis = EnvironmentAnalysis( + start_date=start_date, + end_date=end_date, + latitude=0, + longitude=0, + timezone=None, + ) + + # Assert + assert analysis.preferred_timezone.zone == "UTC" + assert analysis.start_date.tzinfo is not None + assert analysis.end_date.tzinfo is not None + + @pytest.mark.slow @patch("matplotlib.pyplot.show") def test_distribution_plots(mock_show, env_analysis): # pylint: disable=unused-argument