|
1 | 1 | from unittest import mock |
2 | 2 |
|
| 3 | +import pandas as pd |
3 | 4 | import requests |
4 | 5 |
|
5 | 6 | from dataretrieval.waterdata.utils import ( |
| 7 | + _arrange_cols, |
6 | 8 | _format_api_dates, |
7 | 9 | _get_args, |
8 | 10 | _handle_stats_nesting, |
@@ -114,6 +116,50 @@ def test_handle_stats_nesting_tolerates_missing_drop_columns(): |
114 | 116 | assert df["monitoring_location_id"].iloc[0] == "USGS-12345" |
115 | 117 |
|
116 | 118 |
|
| 119 | +# --- _arrange_cols ---------------------------------------------------------- |
| 120 | + |
| 121 | + |
| 122 | +def test_arrange_cols_does_not_mutate_caller_properties(): |
| 123 | + """`_arrange_cols` must not mutate the caller's `properties` list. |
| 124 | +
|
| 125 | + Regression: previously the function did |
| 126 | + ``properties.append("geometry")`` and |
| 127 | + ``properties[properties.index("id")] = output_id`` in place, so the |
| 128 | + caller's list grew and was rewritten across successive calls. |
| 129 | + """ |
| 130 | + df = pd.DataFrame( |
| 131 | + { |
| 132 | + "id": ["a", "b"], |
| 133 | + "value": [1.0, 2.0], |
| 134 | + "geometry": ["p1", "p2"], |
| 135 | + } |
| 136 | + ) |
| 137 | + properties = ["id", "value"] |
| 138 | + snapshot = list(properties) |
| 139 | + |
| 140 | + _arrange_cols(df, properties, output_id="daily_id") |
| 141 | + _arrange_cols(df, properties, output_id="daily_id") |
| 142 | + |
| 143 | + assert properties == snapshot, ( |
| 144 | + f"caller's properties list was mutated: {properties!r} != {snapshot!r}" |
| 145 | + ) |
| 146 | + |
| 147 | + |
| 148 | +def test_arrange_cols_swaps_id_in_returned_columns(): |
| 149 | + """`'id'` in `properties` should still resolve to the output_id column.""" |
| 150 | + df = pd.DataFrame({"id": ["a"], "value": [1.0]}) |
| 151 | + result = _arrange_cols(df, ["id", "value"], output_id="daily_id") |
| 152 | + assert "daily_id" in result.columns |
| 153 | + assert "id" not in result.columns |
| 154 | + |
| 155 | + |
| 156 | +def test_arrange_cols_keeps_geometry_when_present(): |
| 157 | + """Geometry must come along even if the caller didn't list it.""" |
| 158 | + df = pd.DataFrame({"id": ["a"], "value": [1.0], "geometry": ["p1"]}) |
| 159 | + result = _arrange_cols(df, ["value"], output_id="daily_id") |
| 160 | + assert "geometry" in result.columns |
| 161 | + |
| 162 | + |
117 | 163 | # --- _format_api_dates ------------------------------------------------------- |
118 | 164 |
|
119 | 165 |
|
|
0 commit comments