Skip to content

Commit 4be3c7a

Browse files
add tests for error handling in analysis_chain
1 parent 4e6a4b6 commit 4be3c7a

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

rdtools/test/analysis_chains_test.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,3 +1018,90 @@ def test_invalid_filter_params_aggregated(
10181018
KeyError, match=f"Key '{filter_param_aggregated}' is not a valid filter parameter."
10191019
):
10201020
sensor_analysis.filter_params_aggregated[filter_param_aggregated] = {}
1021+
1022+
1023+
def test_filter_params_setter_non_dict(sensor_parameters):
1024+
"""Test that filter_params setter raises error for non-dict input."""
1025+
rd_analysis = TrendAnalysis(**sensor_parameters)
1026+
with pytest.raises(ValueError, match="must be a dictionary"):
1027+
rd_analysis.filter_params = "not a dict"
1028+
1029+
1030+
def test_filter_params_aggregated_setter_non_dict(sensor_parameters):
1031+
"""Test that filter_params_aggregated setter raises error for non-dict."""
1032+
rd_analysis = TrendAnalysis(**sensor_parameters)
1033+
with pytest.raises(ValueError, match="must be a dictionary"):
1034+
rd_analysis.filter_params_aggregated = "not a dict"
1035+
1036+
1037+
def test_clearsky_rescale_index_mismatch(sensor_parameters, cs_input):
1038+
"""Test that rescale=True raises error when indices don't match."""
1039+
rd_analysis = TrendAnalysis(**sensor_parameters)
1040+
rd_analysis.set_clearsky(**cs_input)
1041+
1042+
# Create explicit times that don't match the poa_global index
1043+
mismatched_times = pd.date_range(
1044+
"2020-01-01", periods=100, freq="h", tz=cs_input["pvlib_location"].tz
1045+
)
1046+
1047+
with pytest.raises(ValueError, match="rescale=True can only be used"):
1048+
rd_analysis._calc_clearsky_poa(times=mismatched_times, rescale=True)
1049+
1050+
1051+
def test_poa_filter_without_poa(sensor_parameters):
1052+
"""Test that poa_filter raises error when poa is not available."""
1053+
params = sensor_parameters.copy()
1054+
rd_analysis = TrendAnalysis(**params)
1055+
rd_analysis.filter_params = {"poa_filter": {}}
1056+
# Set poa_global to None after initialization
1057+
rd_analysis.poa_global = None
1058+
# Need power_expected to get past other checks
1059+
rd_analysis.power_expected = sensor_parameters["pv"]
1060+
1061+
with pytest.raises(ValueError, match="poa_global must be available"):
1062+
rd_analysis.sensor_analysis()
1063+
1064+
1065+
def test_tcell_filter_without_temperature(sensor_parameters):
1066+
"""Test that tcell_filter raises error when cell temp not available."""
1067+
params = sensor_parameters.copy()
1068+
params["temperature_ambient"] = None
1069+
params["temperature_cell"] = None
1070+
rd_analysis = TrendAnalysis(**params)
1071+
rd_analysis.poa_global = sensor_parameters["poa_global"]
1072+
rd_analysis.filter_params = {"tcell_filter": {}}
1073+
1074+
# Need power_expected to skip thermal calculation
1075+
rd_analysis.power_expected = sensor_parameters["pv"]
1076+
1077+
with pytest.raises(ValueError, match="Cell temperature must be available"):
1078+
rd_analysis.sensor_analysis()
1079+
1080+
1081+
def test_hour_angle_filter_without_location(sensor_parameters):
1082+
"""Test hour_angle_filter raises error without pvlib_location."""
1083+
rd_analysis = TrendAnalysis(**sensor_parameters)
1084+
rd_analysis.filter_params = {"hour_angle_filter": {}}
1085+
1086+
with pytest.raises(ValueError, match="pvlib location must be provided"):
1087+
rd_analysis.sensor_analysis()
1088+
1089+
1090+
def test_clearsky_filter_without_poa(sensor_parameters, cs_input):
1091+
"""Test clearsky_filter raises error without required poa data."""
1092+
rd_analysis = TrendAnalysis(**sensor_parameters)
1093+
rd_analysis.set_clearsky(**cs_input)
1094+
1095+
# Store the pv_energy for filtering, then set poa_global to None
1096+
energy_normalized = rd_analysis.pv_energy.copy()
1097+
rd_analysis.poa_global = None
1098+
rd_analysis.filter_params = {"clearsky_filter": {}}
1099+
1100+
with pytest.raises(ValueError, match="poa_global and poa_global_clearsky"):
1101+
rd_analysis._filter(energy_normalized, "clearsky")
1102+
1103+
1104+
def test_degradation_timeseries_plot_invalid_case(sensor_analysis):
1105+
"""Test plot_degradation_timeseries raises error for invalid case."""
1106+
with pytest.raises(ValueError, match="case must be either"):
1107+
sensor_analysis.plot_degradation_timeseries(case="invalid")

0 commit comments

Comments
 (0)