|
| 1 | +"""Unit tests for the engine-agnostic schema helpers (no license / no mikeio).""" |
| 2 | +import numpy as np |
| 3 | +import pandas as pd |
| 4 | + |
| 5 | +from mikeplus_mcp.contracts import schema |
| 6 | + |
| 7 | + |
| 8 | +def test_parse_column_node(): |
| 9 | + assert schema.parse_column("WaterLevel:C14150801") == { |
| 10 | + "quantity": "WaterLevel", |
| 11 | + "element_id": "C14150801", |
| 12 | + "chainage": None, |
| 13 | + } |
| 14 | + |
| 15 | + |
| 16 | +def test_parse_column_reach_with_chainage(): |
| 17 | + out = schema.parse_column("Discharge:C14150801.2:30.71") |
| 18 | + assert out["quantity"] == "Discharge" |
| 19 | + assert out["element_id"] == "C14150801.2" # the dot belongs to the reach id |
| 20 | + assert out["chainage"] == 30.71 |
| 21 | + |
| 22 | + |
| 23 | +def test_parse_column_non_numeric_chainage_falls_back_to_string(): |
| 24 | + assert schema.parse_column("X:Y:notnum")["chainage"] == "notnum" |
| 25 | + |
| 26 | + |
| 27 | +# real-world column shapes taken from the Sirius_RTC HD result |
| 28 | +COLS = [ |
| 29 | + "WaterLevel:C14150801", # node |
| 30 | + "WaterLevel:C14150801.2:30.71", # reach (synthetic) sharing the id stem |
| 31 | + "Discharge:C14150801.2:30.71", # reach |
| 32 | + "Discharge:Link_29:33.5333", # reach with a named link |
| 33 | +] |
| 34 | + |
| 35 | + |
| 36 | +def test_match_columns_node_is_not_confused_with_reach(): |
| 37 | + # node 'C14150801' must NOT match reach 'C14150801.2' |
| 38 | + assert schema.match_columns(COLS, "WaterLevel", "C14150801") == ["WaterLevel:C14150801"] |
| 39 | + |
| 40 | + |
| 41 | +def test_match_columns_reach_prefix(): |
| 42 | + assert schema.match_columns(COLS, "Discharge", "C14150801.2") == ["Discharge:C14150801.2:30.71"] |
| 43 | + assert schema.match_columns(COLS, "Discharge", "Link_29") == ["Discharge:Link_29:33.5333"] |
| 44 | + |
| 45 | + |
| 46 | +def test_match_columns_no_match(): |
| 47 | + assert schema.match_columns(COLS, "Discharge", "Nope") == [] |
| 48 | + |
| 49 | + |
| 50 | +def _frame(): |
| 51 | + idx = pd.date_range("2020-01-01", periods=5, freq="h") |
| 52 | + return pd.DataFrame( |
| 53 | + { |
| 54 | + "Discharge:Link_1:10": [0.0, 1.0, 2.0, 1.0, 0.0], |
| 55 | + "Discharge:Link_2:5": [0.0, 0.0, 5.0, 0.0, 0.0], # global Discharge peak |
| 56 | + "WaterLevel:Node_1": [1.0, 1.0, 1.0, 1.0, 1.0], |
| 57 | + "WaterLevel:Node_2": [np.nan] * 5, # all-NaN column |
| 58 | + }, |
| 59 | + index=idx, |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def test_summarize_picks_global_peak_and_time(): |
| 64 | + rows = {r["quantity"]: r for r in schema.summarize(_frame())} |
| 65 | + assert set(rows) == {"Discharge", "WaterLevel"} |
| 66 | + |
| 67 | + dis = rows["Discharge"] |
| 68 | + assert dis["peak_value"] == 5.0 |
| 69 | + assert dis["peak_element"] == "Link_2" |
| 70 | + assert dis["peak_time"].startswith("2020-01-01 02:00") |
| 71 | + assert dis["unit"] == "m3/s" |
| 72 | + assert dis["n_series"] == 2 |
| 73 | + |
| 74 | + |
| 75 | +def test_summarize_skips_all_nan_within_quantity(): |
| 76 | + rows = {r["quantity"]: r for r in schema.summarize(_frame())} |
| 77 | + # WaterLevel:Node_2 is all-NaN; the peak must come from Node_1, not crash/NaN |
| 78 | + assert rows["WaterLevel"]["peak_value"] == 1.0 |
| 79 | + |
| 80 | + |
| 81 | +def test_summarize_quantity_filter(): |
| 82 | + rows = schema.summarize(_frame(), quantities=["Discharge"]) |
| 83 | + assert [r["quantity"] for r in rows] == ["Discharge"] |
0 commit comments