|
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | | -from unittest.mock import Mock |
| 5 | +from unittest.mock import Mock, MagicMock |
6 | 6 |
|
7 | 7 | import geopandas as gpd |
8 | 8 | import numpy as np |
| 9 | +import pandas as pd |
9 | 10 |
|
10 | 11 | import pytest |
| 12 | +import shapely |
11 | 13 |
|
12 | 14 | from settings import Config |
13 | 15 | from utility_route_planner.models.mcda.exceptions import InvalidSuitabilityValue, UnassignedValueFoundDuringReclassify |
@@ -111,3 +113,100 @@ def test_missing_values(input_which_should_raise): |
111 | 113 | wrong_input = input_which_should_raise |
112 | 114 | with pytest.raises(UnassignedValueFoundDuringReclassify): |
113 | 115 | validate_values_to_reclassify(values_to_reclassify, wrong_input) |
| 116 | + |
| 117 | + |
| 118 | +class DummyPreprocessor(VectorPreprocessorBase): |
| 119 | + criterion = "dummy_criterion" |
| 120 | + |
| 121 | + def specific_preprocess(self, prepared_data, criterion): |
| 122 | + pass |
| 123 | + |
| 124 | + |
| 125 | +class TestGetMetrics: |
| 126 | + @pytest.fixture |
| 127 | + def get_dummy_criterion(self): |
| 128 | + return MagicMock(spec=RasterPresetCriteria) |
| 129 | + |
| 130 | + def test_get_statistics_invalid_column(self, get_dummy_criterion): |
| 131 | + dummy_criterion = get_dummy_criterion |
| 132 | + dummy_criterion.columns_to_reclassify = ["missing_col"] |
| 133 | + dummy_criterion.layer_names = ["layer1"] |
| 134 | + |
| 135 | + gdf = gpd.GeoDataFrame({"geometry": [None], "col1": ["A"], "suitability_value": [1]}) |
| 136 | + preprocessor = DummyPreprocessor() |
| 137 | + with pytest.raises(ValueError): |
| 138 | + preprocessor.get_statistics(get_dummy_criterion, gdf) |
| 139 | + |
| 140 | + def test_get_statistics_no_reclassify(self, get_dummy_criterion): |
| 141 | + mock_criterion = get_dummy_criterion |
| 142 | + mock_criterion.columns_to_reclassify = [] |
| 143 | + mock_criterion.layer_names = ["layer1"] |
| 144 | + gdf = gpd.GeoDataFrame({"geometry": [], "suitability_value": []}) |
| 145 | + |
| 146 | + preprocessor = DummyPreprocessor() |
| 147 | + result = preprocessor.get_statistics(mock_criterion, gdf) |
| 148 | + assert "criterion" in result.columns |
| 149 | + assert "weight_key" in result.columns |
| 150 | + assert "area_m2" in result.columns |
| 151 | + assert "suitability_value" in result.columns |
| 152 | + |
| 153 | + def test_get_statistics_one_reclassify_column(self, get_dummy_criterion): |
| 154 | + dummy_criterion = get_dummy_criterion |
| 155 | + dummy_criterion.columns_to_reclassify = ["col1"] |
| 156 | + dummy_criterion.layer_names = ["layer1"] |
| 157 | + gdf = gpd.GeoDataFrame( |
| 158 | + { |
| 159 | + "geometry": [shapely.Point(0, 0).buffer(10), shapely.Point(0, 0).buffer(10)], |
| 160 | + "col1": ["A", "B"], |
| 161 | + "suitability_value": [1, 2], |
| 162 | + } |
| 163 | + ) |
| 164 | + preprocessor = DummyPreprocessor() |
| 165 | + result = preprocessor.get_statistics(dummy_criterion, gdf) |
| 166 | + expected_df = pd.DataFrame( |
| 167 | + { |
| 168 | + "criterion": ["dummy_criterion", "dummy_criterion"], |
| 169 | + "weight_key": ["A", "B"], |
| 170 | + "suitability_value": [1, 2], |
| 171 | + "area_m2": [313.6548490545941, 313.6548490545941], |
| 172 | + } |
| 173 | + ) |
| 174 | + pd.testing.assert_frame_equal(result.reset_index(drop=True), expected_df, check_dtype=False) |
| 175 | + |
| 176 | + def test_get_statistics_two_reclassify_columns(self, get_dummy_criterion): |
| 177 | + dummy_criterion = get_dummy_criterion |
| 178 | + get_dummy_criterion.columns_to_reclassify = ["col1", "col2"] |
| 179 | + get_dummy_criterion.layer_names = ["layer1"] |
| 180 | + example_polygon1 = shapely.Polygon([(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)]) |
| 181 | + example_polygon2 = shapely.Polygon([(5, 5), (5, 15), (15, 15), (15, 5), (5, 5)]) |
| 182 | + gdf = gpd.GeoDataFrame( |
| 183 | + { |
| 184 | + "geometry": [example_polygon1, example_polygon1, example_polygon2], |
| 185 | + "col1": ["A", "B", "B"], |
| 186 | + "col2": ["X", "Y", "Y"], |
| 187 | + "suitability_value": [1, 2, 2], |
| 188 | + } |
| 189 | + ) |
| 190 | + preprocessor = DummyPreprocessor() |
| 191 | + result = preprocessor.get_statistics(dummy_criterion, gdf) |
| 192 | + |
| 193 | + expected_df = pd.DataFrame( |
| 194 | + { |
| 195 | + "criterion": ["dummy_criterion", "dummy_criterion"], |
| 196 | + "weight_key": ["A: X", "B: Y"], |
| 197 | + "suitability_value": [1, 2], |
| 198 | + "area_m2": [100, 175.0], |
| 199 | + } |
| 200 | + ) |
| 201 | + pd.testing.assert_frame_equal(result.reset_index(drop=True), expected_df, check_dtype=False) |
| 202 | + |
| 203 | + def test_get_statistics_three_reclassify_columns_raises(self, get_dummy_criterion): |
| 204 | + dummy_criterion = get_dummy_criterion |
| 205 | + dummy_criterion.columns_to_reclassify = ["col1", "col2", "col3"] |
| 206 | + dummy_criterion.layer_names = ["layer1"] |
| 207 | + gdf = gpd.GeoDataFrame( |
| 208 | + {"geometry": [None], "col1": ["A"], "col2": ["B"], "col3": ["C"], "suitability_value": [1]} |
| 209 | + ) |
| 210 | + preprocessor = DummyPreprocessor() |
| 211 | + with pytest.raises(ValueError): |
| 212 | + preprocessor.get_statistics(dummy_criterion, gdf) |
0 commit comments