Files
GeoData/tests/test_river_lakes.py

71 lines
2.8 KiB
Python

import unittest
from unittest.mock import patch, MagicMock
from geodata_pipeline.river_erosion import _find_lake_depths
from geodata_pipeline.config import Config, LakeConfig
class TestRiverLakes(unittest.TestCase):
@patch("geodata_pipeline.river_erosion.ogr")
def test_find_lake_depths(self, mock_ogr):
# Mock OGR DataSource and Layer
mock_ds = MagicMock()
mock_layer = MagicMock()
mock_ogr.Open.return_value = mock_ds
mock_ds.GetLayer.return_value = mock_layer
# Mock Feature
mock_feat = MagicMock()
mock_feat.GetField.return_value = 15.5 # Depth_avg
mock_feat.GetFID.return_value = 123
# Mock Geometry
mock_geom = MagicMock()
mock_feat.GetGeometryRef.return_value = mock_geom
# When Distance is called on the feature geometry (transformed), return float
mock_transformed_geom = MagicMock()
mock_geom.Clone.return_value = mock_transformed_geom
mock_transformed_geom.Distance.return_value = 10.0
# Setup layer behavior
mock_layer.__iter__.return_value = [mock_feat]
mock_layer.GetNextFeature.side_effect = [mock_feat, None]
# Config
cfg = Config.default()
cfg.river_erosion.lakes.enabled = True
cfg.river_erosion.lakes.match_tolerance_m = 100.0
# Manual lake centroid (shapely point or similar, but here we pass tuple/object depending on implementation)
# Let's assume we pass a list of (x, y) tuples for centroids
manual_lakes = [(333000.0, 5517000.0)]
# Since _find_lake_depths expects OGR geometries for the manual lakes (or constructs them),
# let's assume we pass OGR geometries to it, or it creates them.
# Ideally, it should accept a list of OGR Geometries (Polygons from the mask).
# For this test, I'll assume we pass a list of ogr.Geometry objects.
mock_manual_geom = MagicMock()
mock_manual_geom.Centroid.return_value = mock_manual_geom
mock_manual_geom.Clone.return_value = mock_manual_geom # For transform
mock_manual_geom.GetPoint.return_value = (333000.0, 5517000.0)
mock_srs = MagicMock()
mock_srs.IsSame.return_value = True # Simplify: same CRS
results = _find_lake_depths(
[mock_manual_geom],
mock_srs,
cfg.river_erosion.lakes,
"raw/hydrolakes/HydroLAKES_polys_v10_shp/HydroLAKES_polys_v10.shp"
)
# Expected result: List of (depth, metadata)
# depth should be 15.5
self.assertEqual(len(results), 1)
depth, metadata = results[0]
self.assertEqual(depth, 15.5)
self.assertEqual(metadata["id"], 123)
self.assertEqual(metadata["distance"], 10.0)
if __name__ == "__main__":
unittest.main()