|
1 | 1 | from datetime import datetime, timezone |
2 | 2 |
|
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
3 | 5 | import pytest |
4 | 6 |
|
5 | 7 | from sift_client.sift_types.test_report import ( |
| 8 | + NumericBounds, |
6 | 9 | TestMeasurementCreate, |
7 | 10 | TestMeasurementType, |
8 | 11 | TestMeasurementUpdate, |
@@ -141,16 +144,220 @@ def test_measurement_update(self, report_context): |
141 | 144 | new_step.measure(name="Test Measurement 2", value="string value", bounds="string value") |
142 | 145 | new_step.measure(name="Test Measurement 3", value=True, bounds="true") |
143 | 146 |
|
144 | | - assert len(test_step.measurements) == 3 |
145 | | - assert test_step.measurements[0].name == "Test Measurement" |
146 | | - assert test_step.measurements[0].numeric_value == 10 |
147 | | - assert test_step.measurements[0].measurement_type == TestMeasurementType.DOUBLE |
148 | | - assert test_step.measurements[1].name == "Test Measurement 2" |
149 | | - assert test_step.measurements[1].string_value == "string value" |
150 | | - assert test_step.measurements[1].measurement_type == TestMeasurementType.STRING |
151 | | - assert test_step.measurements[2].name == "Test Measurement 3" |
152 | | - assert test_step.measurements[2].boolean_value == True |
153 | | - assert test_step.measurements[2].measurement_type == TestMeasurementType.BOOLEAN |
| 147 | + measurements = test_step.measurements |
| 148 | + assert len(measurements) == 3 |
| 149 | + assert measurements[0].name == "Test Measurement" |
| 150 | + assert measurements[0].numeric_value == 10 |
| 151 | + assert measurements[0].measurement_type == TestMeasurementType.DOUBLE |
| 152 | + assert measurements[1].name == "Test Measurement 2" |
| 153 | + assert measurements[1].string_value == "string value" |
| 154 | + assert measurements[1].measurement_type == TestMeasurementType.STRING |
| 155 | + assert measurements[2].name == "Test Measurement 3" |
| 156 | + assert measurements[2].boolean_value == True |
| 157 | + assert measurements[2].measurement_type == TestMeasurementType.BOOLEAN |
| 158 | + |
| 159 | + def test_measure_avg_list_within_bounds(self, step): |
| 160 | + """Test measure_avg with a list of values where average is within bounds.""" |
| 161 | + result = step.measure_avg( |
| 162 | + name="Avg Temperature", |
| 163 | + values=[10.0, 20.0, 30.0], # avg = 20.0 |
| 164 | + bounds={"min": 15.0, "max": 25.0}, |
| 165 | + ) |
| 166 | + assert result == True |
| 167 | + assert step.current_step.measurements[0].name == "Avg Temperature" |
| 168 | + assert step.current_step.measurements[0].numeric_value == 20.0 |
| 169 | + assert step.current_step.measurements[0].passed == True |
| 170 | + |
| 171 | + def test_measure_avg_list_outside_bounds(self, report_context, step): |
| 172 | + """Test measure_avg with a list where average is outside bounds.""" |
| 173 | + # Capture initial state to restore after test |
| 174 | + current_step_path = step.current_step.step_path |
| 175 | + initial_open_step_result = report_context.open_step_results.get(current_step_path, True) |
| 176 | + initial_any_failures = report_context.any_failures |
| 177 | + |
| 178 | + result = step.measure_avg( |
| 179 | + name="Avg Temperature Fail", |
| 180 | + values=[50.0, 60.0, 70.0], # avg = 60.0 |
| 181 | + bounds={"min": 15.0, "max": 25.0}, |
| 182 | + ) |
| 183 | + assert result == False |
| 184 | + assert step.current_step.measurements[0].numeric_value == 60.0 |
| 185 | + assert step.current_step.measurements[0].passed == False |
| 186 | + |
| 187 | + # Restore state |
| 188 | + if initial_open_step_result: |
| 189 | + report_context.open_step_results[current_step_path] = True |
| 190 | + if not initial_any_failures: |
| 191 | + report_context.any_failures = False |
| 192 | + |
| 193 | + def test_measure_avg_numpy_array(self, step): |
| 194 | + """Test measure_avg with a numpy array.""" |
| 195 | + result = step.measure_avg( |
| 196 | + name="Avg Pressure", |
| 197 | + values=np.array([100.0, 200.0, 300.0]), # avg = 200.0 |
| 198 | + bounds={"min": 150.0, "max": 250.0}, |
| 199 | + ) |
| 200 | + assert result == True |
| 201 | + assert step.current_step.measurements[0].numeric_value == 200.0 |
| 202 | + assert step.current_step.measurements[0].passed == True |
| 203 | + |
| 204 | + def test_measure_avg_pandas_series(self, step): |
| 205 | + """Test measure_avg with a pandas Series.""" |
| 206 | + series = pd.Series([5.0, 10.0, 15.0]) # avg = 10.0 |
| 207 | + result = step.measure_avg( |
| 208 | + name="Avg Voltage", |
| 209 | + values=series, |
| 210 | + bounds={"min": 5.0, "max": 15.0}, |
| 211 | + ) |
| 212 | + assert result == True |
| 213 | + assert step.current_step.measurements[0].numeric_value == 10.0 |
| 214 | + assert step.current_step.measurements[0].passed == True |
| 215 | + |
| 216 | + def test_measure_avg_with_numeric_bounds_object(self, step): |
| 217 | + """Test measure_avg with NumericBounds object instead of dict.""" |
| 218 | + result = step.measure_avg( |
| 219 | + name="Avg Current", |
| 220 | + values=[1.0, 2.0, 3.0], # avg = 2.0 |
| 221 | + bounds=NumericBounds(min=1.0, max=3.0), |
| 222 | + ) |
| 223 | + assert result == True |
| 224 | + assert step.current_step.measurements[0].numeric_value == 2.0 |
| 225 | + assert step.current_step.measurements[0].passed == True |
| 226 | + |
| 227 | + def test_measure_avg_invalid_type(self, step): |
| 228 | + """Test measure_avg raises ValueError for invalid value type.""" |
| 229 | + with pytest.raises(ValueError, match="Invalid value type"): |
| 230 | + step.measure_avg( |
| 231 | + name="Invalid", |
| 232 | + values="not a list", # type: ignore |
| 233 | + bounds={"min": 0.0, "max": 10.0}, |
| 234 | + ) |
| 235 | + |
| 236 | + def test_measure_avg_with_integers(self, step): |
| 237 | + """Test measure_avg with integer values in list.""" |
| 238 | + result = step.measure_avg( |
| 239 | + name="Avg Count", |
| 240 | + values=[1, 2, 3, 4, 5], # avg = 3.0 |
| 241 | + bounds={"min": 2.0, "max": 4.0}, |
| 242 | + ) |
| 243 | + assert result == True |
| 244 | + assert step.current_step.measurements[0].numeric_value == 3.0 |
| 245 | + assert step.current_step.measurements[0].passed == True |
| 246 | + |
| 247 | + def test_measure_all_list_within_bounds(self, step): |
| 248 | + """Test measure_all with a list of values all within bounds.""" |
| 249 | + result = step.measure_all( |
| 250 | + name="All Temperatures", |
| 251 | + values=[10.0, 15.0, 20.0], |
| 252 | + bounds={"min": 5.0, "max": 25.0}, |
| 253 | + ) |
| 254 | + assert result == True |
| 255 | + |
| 256 | + def test_measure_all_list_some_outside_bounds(self, report_context, step): |
| 257 | + """Test measure_all with a list where some values are outside bounds.""" |
| 258 | + # Capture initial state to restore after test |
| 259 | + current_step_path = step.current_step.step_path |
| 260 | + initial_open_step_result = report_context.open_step_results.get(current_step_path, True) |
| 261 | + initial_any_failures = report_context.any_failures |
| 262 | + |
| 263 | + result = step.measure_all( |
| 264 | + name="temp", |
| 265 | + values=[10.0, 50.0, 20.0, -1.0], # 50.0 and -1.0 are outside |
| 266 | + bounds={"min": 5.0, "max": 25.0}, |
| 267 | + unit="C", |
| 268 | + ) |
| 269 | + assert result == False |
| 270 | + test_step = step.current_step |
| 271 | + measurements = test_step.measurements |
| 272 | + measurements.sort(key=lambda x: x.numeric_value) |
| 273 | + assert len(measurements) == 2 |
| 274 | + assert measurements[0].numeric_value == -1.0 |
| 275 | + assert measurements[0].passed == False |
| 276 | + assert measurements[1].numeric_value == 50.0 |
| 277 | + assert measurements[1].passed == False |
| 278 | + |
| 279 | + # Restore state |
| 280 | + if initial_open_step_result: |
| 281 | + report_context.open_step_results[current_step_path] = True |
| 282 | + if not initial_any_failures: |
| 283 | + report_context.any_failures = False |
| 284 | + |
| 285 | + def test_measure_all_numpy_array(self, step): |
| 286 | + """Test measure_all with a numpy array.""" |
| 287 | + result = step.measure_all( |
| 288 | + name="All Pressures", |
| 289 | + values=np.array([100.0, 150.0, 200.0]), |
| 290 | + bounds={"min": 50.0, "max": 250.0}, |
| 291 | + ) |
| 292 | + assert result == True |
| 293 | + |
| 294 | + def test_measure_all_pandas_series(self, step): |
| 295 | + """Test measure_all with a pandas Series.""" |
| 296 | + series = pd.Series([5.0, 10.0, 15.0]) |
| 297 | + result = step.measure_all( |
| 298 | + name="All Voltages", |
| 299 | + values=series, |
| 300 | + bounds={"min": 0.0, "max": 20.0}, |
| 301 | + ) |
| 302 | + assert result == True |
| 303 | + |
| 304 | + def test_measure_all_with_numeric_bounds_object(self, step): |
| 305 | + """Test measure_all with NumericBounds object instead of dict.""" |
| 306 | + result = step.measure_all( |
| 307 | + name="All Currents", |
| 308 | + values=[1.0, 2.0, 3.0], |
| 309 | + bounds=NumericBounds(min=0.0, max=5.0), |
| 310 | + ) |
| 311 | + assert result == True |
| 312 | + |
| 313 | + def test_measure_all_invalid_type(self, step): |
| 314 | + """Test measure_all raises ValueError for invalid value type.""" |
| 315 | + with pytest.raises(ValueError, match="Invalid value type"): |
| 316 | + step.measure_all( |
| 317 | + name="Invalid", |
| 318 | + values="not a list", # type: ignore |
| 319 | + bounds={"min": 0.0, "max": 10.0}, |
| 320 | + ) |
| 321 | + |
| 322 | + def test_measure_all_no_bounds(self, step): |
| 323 | + """Test measure_all raises ValueError when no bounds provided.""" |
| 324 | + with pytest.raises(ValueError, match="No bounds provided"): |
| 325 | + step.measure_all( |
| 326 | + name="No Bounds", |
| 327 | + values=[1.0, 2.0, 3.0], |
| 328 | + bounds={}, # Empty bounds dict |
| 329 | + ) |
| 330 | + |
| 331 | + def test_measure_all_min_only(self, step): |
| 332 | + """Test measure_all with only minimum bound.""" |
| 333 | + result = step.measure_all( |
| 334 | + name="Min Only", |
| 335 | + values=[10.0, 20.0, 30.0], |
| 336 | + bounds={"min": 5.0}, |
| 337 | + ) |
| 338 | + assert result == True |
| 339 | + |
| 340 | + def test_measure_all_max_only(self, step): |
| 341 | + """Test measure_all with only maximum bound.""" |
| 342 | + result = step.measure_all( |
| 343 | + name="Max Only", |
| 344 | + values=[10.0, 20.0, 30.0], |
| 345 | + bounds={"max": 50.0}, |
| 346 | + ) |
| 347 | + assert result == True |
| 348 | + |
| 349 | + def test_report_outcome(self, report_context, step): |
| 350 | + # Capture current state of report context's failures so we can keep things passed at a high level if the test's induced failures happen as expected. |
| 351 | + current_step_path = step.current_step.step_path |
| 352 | + initial_open_step_result = report_context.open_step_results.get(current_step_path, True) |
| 353 | + initial_any_failures = report_context.any_failures |
| 354 | + assert step.report_outcome("Test Pass Outcome", True, "Test Pass Description") == True |
| 355 | + assert step.report_outcome("Test Fail Outcome", False, "Test Failure Description") == False |
| 356 | + # If this test was successful, mark that at a high level. |
| 357 | + if initial_open_step_result: |
| 358 | + report_context.open_step_results[current_step_path] = True |
| 359 | + if not initial_any_failures: |
| 360 | + report_context.any_failures = False |
154 | 361 |
|
155 | 362 | def test_bad_assert(self, report_context, step): |
156 | 363 | # Capture current state of report context's failures so we can keep things passed at a high level if the test's induced failures happen as expected. |
|
0 commit comments