Skip to content

Commit cfee83e

Browse files
committed
Create test_reservoir.py
1 parent d383052 commit cfee83e

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import unittest
2+
import pandas as pd
3+
4+
from pownet.data_model import ReservoirParams
5+
6+
7+
class TestReservoirParams(unittest.TestCase):
8+
9+
def setUp(self):
10+
"""Set up common data for tests."""
11+
self.default_inflow_ts = pd.Series([10.0, 12.0, 15.0], index=[1, 2, 3])
12+
self.default_minflow_ts = pd.Series([1.0, 1.0, 1.0], index=[1, 2, 3])
13+
self.valid_params = {
14+
"name": "TestReservoir",
15+
"min_day": 150,
16+
"max_day": 270,
17+
"min_level": 100.0,
18+
"max_level": 150.0,
19+
"max_head": 50.0,
20+
"max_storage": 1000000.0,
21+
"max_release": 500.0,
22+
"max_generation": 100.0,
23+
"turbine_factor": 0.85,
24+
"inflow_ts": self.default_inflow_ts,
25+
"minflow_ts": self.default_minflow_ts,
26+
"upstream_units": [],
27+
"downstream_flow_fracs": {"Downstream1": 0.6, "Downstream2": 0.4},
28+
}
29+
30+
def test_valid_params_creation(self):
31+
"""Test successful creation with valid parameters."""
32+
try:
33+
ReservoirParams(**self.valid_params)
34+
except ValueError:
35+
self.fail("ReservoirParams raised ValueError unexpectedly for valid data.")
36+
37+
def test_downstream_flow_fracs_sum_not_one(self):
38+
"""Test ValueError if downstream flow fractions do not sum to 1."""
39+
params = self.valid_params.copy()
40+
params["downstream_flow_fracs"] = {
41+
"Downstream1": 0.5,
42+
"Downstream2": 0.4,
43+
} # Sums to 0.9
44+
with self.assertRaisesRegex(
45+
ValueError, "Downstream units for TestReservoir do not sum to 1"
46+
):
47+
ReservoirParams(**params)
48+
49+
params["downstream_flow_fracs"] = {
50+
"Downstream1": 0.7,
51+
"Downstream2": 0.4,
52+
} # Sums to 1.1
53+
with self.assertRaisesRegex(
54+
ValueError, "Downstream units for TestReservoir do not sum to 1"
55+
):
56+
ReservoirParams(**params)
57+
58+
def test_downstream_flow_fracs_sum_is_one_edge_cases(self):
59+
"""Test successful creation when downstream flow fractions sum is close to 1."""
60+
params = self.valid_params.copy()
61+
params["downstream_flow_fracs"] = {
62+
"D1": 0.999
63+
} # Test lower bound (assuming only one downstream)
64+
# To make this test pass, let's adjust for multiple units
65+
params["downstream_flow_fracs"] = {"D1": 0.5, "D2": 0.49999}
66+
try:
67+
ReservoirParams(**params)
68+
except ValueError:
69+
self.fail(
70+
"ReservoirParams raised ValueError for sum slightly less than 1 but within tolerance."
71+
)
72+
73+
params["downstream_flow_fracs"] = {"D1": 0.5, "D2": 0.50001}
74+
try:
75+
ReservoirParams(**params)
76+
except ValueError:
77+
self.fail(
78+
"ReservoirParams raised ValueError for sum slightly more than 1 but within tolerance."
79+
)
80+
81+
def test_mismatched_timeseries_indices(self):
82+
"""Test ValueError if inflow_ts and minflow_ts have different indices."""
83+
params = self.valid_params.copy()
84+
params["minflow_ts"] = pd.Series(
85+
[1.0, 1.0, 1.0], index=[1, 2, 4]
86+
) # Mismatched index
87+
with self.assertRaisesRegex(
88+
ValueError,
89+
"Inflows and minflows for TestReservoir are not indexed the same",
90+
):
91+
ReservoirParams(**params)
92+
93+
def test_timeseries_index_not_starting_at_one(self):
94+
"""Test ValueError if timeseries indices do not start at 1."""
95+
params_inflow = self.valid_params.copy()
96+
params_inflow["inflow_ts"] = pd.Series([10.0, 12.0], index=[0, 1])
97+
params_inflow["minflow_ts"] = pd.Series(
98+
[1.0, 1.0], index=[0, 1]
99+
) # Keep minflow consistent for this test focus
100+
with self.assertRaises(ValueError):
101+
ReservoirParams(**params_inflow)
102+
103+
params_minflow = self.valid_params.copy()
104+
# Correct inflow to isolate minflow test
105+
params_minflow["inflow_ts"] = pd.Series([10.0, 12.0], index=[1, 2])
106+
params_minflow["minflow_ts"] = pd.Series([1.0, 1.0], index=[0, 1])
107+
with self.assertRaises(ValueError):
108+
ReservoirParams(**params_minflow)
109+
110+
def test_inflow_less_than_minflow(self):
111+
"""Test ValueError if inflow_ts is less than minflow_ts on any day."""
112+
params = self.valid_params.copy()
113+
params["inflow_ts"] = pd.Series(
114+
[10.0, 0.5, 15.0], index=[1, 2, 3]
115+
) # Day 2 inflow < minflow
116+
params["minflow_ts"] = pd.Series([1.0, 1.0, 1.0], index=[1, 2, 3])
117+
with self.assertRaises(ValueError):
118+
ReservoirParams(**params)
119+
120+
def test_empty_downstream_flow_fracs(self):
121+
"""Test creation with empty downstream_flow_fracs."""
122+
params = self.valid_params.copy()
123+
params["downstream_flow_fracs"] = {}
124+
try:
125+
ReservoirParams(**params)
126+
except ValueError:
127+
self.fail(
128+
"ReservoirParams raised ValueError unexpectedly for empty downstream_flow_fracs."
129+
)
130+
131+
def test_inflow_equal_to_minflow(self):
132+
"""Test successful creation if inflow_ts is equal to minflow_ts."""
133+
params = self.valid_params.copy()
134+
params["inflow_ts"] = pd.Series([1.0, 2.0, 3.0], index=[1, 2, 3])
135+
params["minflow_ts"] = pd.Series([1.0, 2.0, 3.0], index=[1, 2, 3])
136+
try:
137+
ReservoirParams(**params)
138+
except ValueError:
139+
self.fail(
140+
"ReservoirParams raised ValueError when inflow_ts equals minflow_ts."
141+
)
142+
143+
144+
if __name__ == "__main__":
145+
unittest.main(argv=["first-arg-is-ignored"], exit=False)

0 commit comments

Comments
 (0)