|
| 1 | +import unittest |
| 2 | +import os |
| 3 | +import tempfile |
| 4 | +import dataclasses |
| 5 | + |
| 6 | + |
| 7 | +from pownet.data_model.power_trade import PowerTradeParams |
| 8 | + |
| 9 | +# --- Test Data --- |
| 10 | +VALID_INTERTIE_POINTS = [("N1", "N2"), ("N3", "N4")] |
| 11 | +VALID_INTERTIE_CAPACITIES = {("N1", "N2"): 100.0, ("N3", "N4"): 200.0} |
| 12 | +VALID_CONNECTED_BA = [("BA1", "BA2"), ("BA3", "BA4")] |
| 13 | +VALID_TOTAL_TRANSFER_LIMITS = {("BA1", "BA2"): 150.0, ("BA3", "BA4"): 250.0} |
| 14 | + |
| 15 | +CSV_HEADER = "intertie_1_ba,intertie_1_node,intertie_2_ba,intertie_2_node,capacity_mw\n" |
| 16 | +VALID_CSV_DATA_ROW1 = "BA1,N1,BA2,N2,100.0\n" |
| 17 | +VALID_CSV_DATA_ROW2 = "BA1,N3,BA2,N4,50.5\n" |
| 18 | +VALID_CSV_DATA_ROW3 = "BA3,N5,BA4,N6,200.0\n" |
| 19 | + |
| 20 | + |
| 21 | +class TestPowerTradeParams(unittest.TestCase): |
| 22 | + """Test suite for the PowerTradeParams dataclass and its methods.""" |
| 23 | + |
| 24 | + def _create_temp_csv_file(self, content: str) -> str: |
| 25 | + """ |
| 26 | + Helper method to create a temporary CSV file with given content. |
| 27 | + The file is registered for cleanup after the test. |
| 28 | + Returns the path to the temporary file. |
| 29 | + """ |
| 30 | + # Use mkstemp for a unique file name and then open it in text mode |
| 31 | + fd, path = tempfile.mkstemp(suffix=".csv", text=True) |
| 32 | + with os.fdopen(fd, "w") as tmp_file: |
| 33 | + tmp_file.write(content) |
| 34 | + self.addCleanup( |
| 35 | + os.remove, path |
| 36 | + ) # Ensures the file is deleted after the test method |
| 37 | + return path |
| 38 | + |
| 39 | + # --- __init__ and __post_init__ Tests --- |
| 40 | + |
| 41 | + def test_successful_initialization(self): |
| 42 | + """Test successful instantiation with valid parameters.""" |
| 43 | + params = PowerTradeParams( |
| 44 | + intertie_points=VALID_INTERTIE_POINTS, |
| 45 | + intertie_capacities=VALID_INTERTIE_CAPACITIES, |
| 46 | + connected_ba=VALID_CONNECTED_BA, |
| 47 | + total_transfer_limits=VALID_TOTAL_TRANSFER_LIMITS, |
| 48 | + ) |
| 49 | + self.assertEqual(params.intertie_points, VALID_INTERTIE_POINTS) |
| 50 | + self.assertEqual(params.intertie_capacities, VALID_INTERTIE_CAPACITIES) |
| 51 | + self.assertEqual(params.connected_ba, VALID_CONNECTED_BA) |
| 52 | + self.assertEqual(params.total_transfer_limits, VALID_TOTAL_TRANSFER_LIMITS) |
| 53 | + |
| 54 | + def test_default_factory_initialization(self): |
| 55 | + """Test instantiation with default factory values (empty lists/dicts).""" |
| 56 | + params = PowerTradeParams() |
| 57 | + self.assertEqual(params.intertie_points, []) |
| 58 | + self.assertEqual(params.intertie_capacities, {}) |
| 59 | + self.assertEqual(params.connected_ba, []) |
| 60 | + self.assertEqual(params.total_transfer_limits, {}) |
| 61 | + |
| 62 | + def test_post_init_negative_intertie_capacity(self): |
| 63 | + """Test __post_init__ raises ValueError for negative intertie capacity.""" |
| 64 | + with self.assertRaisesRegex(ValueError, "cannot be negative"): |
| 65 | + PowerTradeParams(intertie_capacities={("N1", "N2"): -100.0}) |
| 66 | + |
| 67 | + def test_post_init_self_loop_intertie(self): |
| 68 | + """Test __post_init__ raises ValueError for self-loop in intertie_points.""" |
| 69 | + with self.assertRaisesRegex(ValueError, "Self-loop intertie found"): |
| 70 | + PowerTradeParams(intertie_points=[("N1", "N1")]) |
| 71 | + |
| 72 | + def test_post_init_reverse_intertie(self): |
| 73 | + """Test __post_init__ raises ValueError for reverse intertie in intertie_points.""" |
| 74 | + with self.assertRaisesRegex( |
| 75 | + ValueError, "Reverse intertie .* found in intertie points" |
| 76 | + ): |
| 77 | + PowerTradeParams(intertie_points=[("N1", "N2"), ("N2", "N1")]) |
| 78 | + |
| 79 | + def test_post_init_reverse_ba_pair(self): |
| 80 | + """Test __post_init__ raises ValueError for reverse BA pair in connected_ba.""" |
| 81 | + with self.assertRaisesRegex( |
| 82 | + ValueError, "Reverse BA pair .* found in connected BA" |
| 83 | + ): |
| 84 | + PowerTradeParams(connected_ba=[("BA1", "BA2"), ("BA2", "BA1")]) |
| 85 | + |
| 86 | + # --- from_csv Tests --- |
| 87 | + |
| 88 | + def test_from_csv_successful_creation(self): |
| 89 | + """Test successful creation of PowerTradeParams from a valid CSV file.""" |
| 90 | + csv_content = CSV_HEADER + VALID_CSV_DATA_ROW1 + VALID_CSV_DATA_ROW2 |
| 91 | + # BA1,N1,BA2,N2,100.0 |
| 92 | + # BA1,N3,BA2,N4,50.5 |
| 93 | + temp_csv_path = self._create_temp_csv_file(csv_content) |
| 94 | + |
| 95 | + params = PowerTradeParams.from_csv(temp_csv_path) |
| 96 | + |
| 97 | + expected_intertie_points = [("N1", "N2"), ("N3", "N4")] |
| 98 | + expected_intertie_capacities = {("N1", "N2"): 100.0, ("N3", "N4"): 50.5} |
| 99 | + expected_connected_ba = [("BA1", "BA2")] # Only one BA-BA pair |
| 100 | + expected_total_transfer_limits = {("BA1", "BA2"): 150.5} # Sum of capacities |
| 101 | + |
| 102 | + self.assertCountEqual( |
| 103 | + params.intertie_points, expected_intertie_points |
| 104 | + ) # Order might not be guaranteed from set |
| 105 | + self.assertEqual(params.intertie_capacities, expected_intertie_capacities) |
| 106 | + self.assertCountEqual(params.connected_ba, expected_connected_ba) |
| 107 | + self.assertEqual(params.total_transfer_limits, expected_total_transfer_limits) |
| 108 | + |
| 109 | + def test_from_csv_missing_required_column(self): |
| 110 | + """Test from_csv raises ValueError if a required column is missing.""" |
| 111 | + csv_content = "intertie_1_ba,intertie_1_node,intertie_2_ba,capacity_mw\nBA1,N1,BA2,100\n" # Missing intertie_2_node |
| 112 | + temp_csv_path = self._create_temp_csv_file(csv_content) |
| 113 | + with self.assertRaisesRegex( |
| 114 | + ValueError, "CSV file must contain the following columns" |
| 115 | + ): |
| 116 | + PowerTradeParams.from_csv(temp_csv_path) |
| 117 | + |
| 118 | + def test_from_csv_duplicate_intertie_in_file(self): |
| 119 | + """Test from_csv raises ValueError for duplicate intertie points in the CSV.""" |
| 120 | + csv_content = ( |
| 121 | + CSV_HEADER + VALID_CSV_DATA_ROW1 + "BA_X,N1,BA_Y,N2,200.0\n" |
| 122 | + ) # N1-N2 defined twice |
| 123 | + temp_csv_path = self._create_temp_csv_file(csv_content) |
| 124 | + with self.assertRaisesRegex(ValueError, "Duplicate intertie point found"): |
| 125 | + PowerTradeParams.from_csv(temp_csv_path) |
| 126 | + |
| 127 | + def test_from_csv_invalid_capacity_value(self): |
| 128 | + """Test from_csv raises ValueError if capacity_mw is not a valid float.""" |
| 129 | + csv_content = CSV_HEADER + "BA1,N1,BA2,N2,not_a_number\n" |
| 130 | + temp_csv_path = self._create_temp_csv_file(csv_content) |
| 131 | + with self.assertRaises(ValueError) as cm: # Specific message comes from float() |
| 132 | + PowerTradeParams.from_csv(temp_csv_path) |
| 133 | + self.assertIn("could not convert string to float", str(cm.exception).lower()) |
| 134 | + |
| 135 | + def test_from_csv_headers_only_file(self): |
| 136 | + """Test from_csv with a file containing only headers (no data rows).""" |
| 137 | + temp_csv_path = self._create_temp_csv_file(CSV_HEADER) |
| 138 | + params = PowerTradeParams.from_csv(temp_csv_path) |
| 139 | + self.assertEqual(params.intertie_points, []) |
| 140 | + self.assertEqual(params.intertie_capacities, {}) |
| 141 | + self.assertEqual(params.connected_ba, []) |
| 142 | + self.assertEqual(params.total_transfer_limits, {}) |
| 143 | + |
| 144 | + def test_from_csv_truly_empty_file(self): |
| 145 | + """Test from_csv with a completely empty file (0 bytes).""" |
| 146 | + # This should raise an error from pandas.read_csv |
| 147 | + # The typical error is pandas.errors.EmptyDataError |
| 148 | + temp_csv_path = self._create_temp_csv_file("") # Creates an empty file |
| 149 | + |
| 150 | + # Import pandas errors for a more specific check, if desired |
| 151 | + # from pandas.errors import EmptyDataError |
| 152 | + with self.assertRaises(Exception) as cm: |
| 153 | + PowerTradeParams.from_csv(temp_csv_path) |
| 154 | + |
| 155 | + self.assertIn("no columns to parse", str(cm.exception).lower()) |
| 156 | + |
| 157 | + def test_from_csv_correct_total_transfer_limits_aggregation(self): |
| 158 | + """Test from_csv correctly aggregates capacities for total_transfer_limits.""" |
| 159 | + csv_content = ( |
| 160 | + CSV_HEADER |
| 161 | + + "BA1,N1,BA2,N2,100.0\n" # BA1-BA2 |
| 162 | + + "BA1,N3,BA2,N4,50.0\n" # BA1-BA2 |
| 163 | + + "BA3,N5,BA4,N6,75.0\n" # BA3-BA4 |
| 164 | + ) |
| 165 | + temp_csv_path = self._create_temp_csv_file(csv_content) |
| 166 | + params = PowerTradeParams.from_csv(temp_csv_path) |
| 167 | + expected_limits = {("BA1", "BA2"): 150.0, ("BA3", "BA4"): 75.0} |
| 168 | + self.assertEqual(params.total_transfer_limits, expected_limits) |
| 169 | + |
| 170 | + # --- from_csv triggering __post_init__ validations --- |
| 171 | + |
| 172 | + def test_from_csv_validates_negative_capacity(self): |
| 173 | + """Test from_csv leads to __post_init__ validation for negative capacity.""" |
| 174 | + csv_content = CSV_HEADER + "BA1,N1,BA2,N2,-100.0\n" |
| 175 | + temp_csv_path = self._create_temp_csv_file(csv_content) |
| 176 | + with self.assertRaisesRegex(ValueError, "cannot be negative"): |
| 177 | + PowerTradeParams.from_csv(temp_csv_path) |
| 178 | + |
| 179 | + def test_from_csv_validates_self_loop_intertie(self): |
| 180 | + """Test from_csv leads to __post_init__ validation for self-loop interties.""" |
| 181 | + csv_content = CSV_HEADER + "BA1,N1,BA2,N1,100.0\n" # N1-N1 intertie |
| 182 | + temp_csv_path = self._create_temp_csv_file(csv_content) |
| 183 | + with self.assertRaisesRegex(ValueError, "Self-loop intertie found"): |
| 184 | + PowerTradeParams.from_csv(temp_csv_path) |
| 185 | + |
| 186 | + def test_from_csv_validates_reverse_intertie(self): |
| 187 | + """Test from_csv leads to __post_init__ validation for reverse interties.""" |
| 188 | + csv_content = ( |
| 189 | + CSV_HEADER |
| 190 | + + "BA1,N1,BA2,N2,100.0\n" |
| 191 | + + "BA3,N2,BA4,N1,50.0\n" # N2-N1 intertie, distinct from N1-N2 |
| 192 | + ) |
| 193 | + temp_csv_path = self._create_temp_csv_file(csv_content) |
| 194 | + # This should create intertie_points [('N1','N2'), ('N2','N1')] which __post_init__ catches |
| 195 | + with self.assertRaisesRegex( |
| 196 | + ValueError, "Reverse intertie .* found in intertie points" |
| 197 | + ): |
| 198 | + PowerTradeParams.from_csv(temp_csv_path) |
| 199 | + |
| 200 | + def test_from_csv_validates_reverse_ba_pair(self): |
| 201 | + """Test from_csv leads to __post_init__ validation for reverse BA pairs.""" |
| 202 | + csv_content = ( |
| 203 | + CSV_HEADER |
| 204 | + + "BA1,N1,BA2,N2,100.0\n" # Corridor BA1-BA2 |
| 205 | + + "BA2,N3,BA1,N4,50.0\n" # Corridor BA2-BA1 |
| 206 | + ) |
| 207 | + temp_csv_path = self._create_temp_csv_file(csv_content) |
| 208 | + # This creates connected_ba [('BA1','BA2'), ('BA2','BA1')] which __post_init__ catches |
| 209 | + with self.assertRaisesRegex( |
| 210 | + ValueError, "Reverse BA pair .* found in connected BA" |
| 211 | + ): |
| 212 | + PowerTradeParams.from_csv(temp_csv_path) |
| 213 | + |
| 214 | + def test_from_csv_allows_self_connected_ba_if_not_reversed(self): |
| 215 | + """Test that a BA connected to itself is allowed if not explicitly reversed.""" |
| 216 | + csv_content = CSV_HEADER + "BA1,N1,BA1,N2,100.0\n" # Intertie within BA1 |
| 217 | + temp_csv_path = self._create_temp_csv_file(csv_content) |
| 218 | + params = PowerTradeParams.from_csv(temp_csv_path) |
| 219 | + |
| 220 | + self.assertCountEqual(params.connected_ba, [("BA1", "BA1")]) |
| 221 | + self.assertEqual(params.total_transfer_limits, {("BA1", "BA1"): 100.0}) |
| 222 | + # No error should be raised by __post_init__ for connected_ba |
| 223 | + |
| 224 | + |
| 225 | +if __name__ == "__main__": |
| 226 | + unittest.main(argv=["first-arg-is-ignored"], exit=False) |
0 commit comments