|
| 1 | +import json |
| 2 | +import tempfile |
| 3 | +import unittest |
| 4 | +import geopandas as gpd |
| 5 | +from subprocess import run, PIPE |
| 6 | +from unittest.mock import patch, MagicMock |
| 7 | +from src.calculators.qm_fixed_calculator import QMFixedCalculator |
| 8 | +from src.calculators.qm_calculator import QualityMetricResult |
| 9 | + |
| 10 | + |
| 11 | +class TestQMFixedCalculator(unittest.TestCase): |
| 12 | + |
| 13 | + def setUp(self): |
| 14 | + self.edges_file_path = 'test_edges.geojson' |
| 15 | + self.output_file_path = 'test_output.geojson' |
| 16 | + self.polygon_file_path = 'test_polygon.geojson' |
| 17 | + self.calculator = QMFixedCalculator( |
| 18 | + self.edges_file_path, |
| 19 | + self.output_file_path, |
| 20 | + self.polygon_file_path |
| 21 | + ) |
| 22 | + |
| 23 | + def test_initialization(self): |
| 24 | + self.assertEqual(self.calculator.edges_file_path, self.edges_file_path) |
| 25 | + self.assertEqual(self.calculator.output_file_path, self.output_file_path) |
| 26 | + self.assertEqual(self.calculator.polygon_file_path, self.polygon_file_path) |
| 27 | + |
| 28 | + @patch('src.calculators.qm_fixed_calculator.gpd.read_file') |
| 29 | + @patch('src.calculators.qm_fixed_calculator.gpd.GeoDataFrame.to_file', autospec=True) |
| 30 | + @patch('src.calculators.qm_fixed_calculator.random.randint') |
| 31 | + def test_calculate_quality_metric(self, mock_randint, mock_to_file, mock_read_file): |
| 32 | + # Setup mocks |
| 33 | + mock_randint.return_value = 42 |
| 34 | + mock_gdf = MagicMock(spec=gpd.GeoDataFrame) |
| 35 | + mock_read_file.return_value = mock_gdf |
| 36 | + |
| 37 | + # Call the method under test |
| 38 | + result = self.calculator.calculate_quality_metric() |
| 39 | + |
| 40 | + # Assertions |
| 41 | + mock_read_file.assert_called_once_with(self.edges_file_path) |
| 42 | + mock_gdf.__setitem__.assert_called_once_with('fixed_score', 42) |
| 43 | + mock_gdf.to_file.assert_called_once_with(self.output_file_path) |
| 44 | + self.assertIsInstance(result, QualityMetricResult) |
| 45 | + self.assertTrue(result.success) |
| 46 | + self.assertEqual(result.message, 'QMFixedCalculator') |
| 47 | + self.assertEqual(result.output_file, self.output_file_path) |
| 48 | + |
| 49 | + def test_algorithm_name(self): |
| 50 | + self.assertEqual(self.calculator.algorithm_name(), 'QMFixedCalculator') |
| 51 | + |
| 52 | + def test_main_with_polygon(self): |
| 53 | + # Create temporary files for edges and polygon |
| 54 | + with tempfile.NamedTemporaryFile(suffix='.geojson') as edges_file, \ |
| 55 | + tempfile.NamedTemporaryFile(suffix='.geojson') as output_file, \ |
| 56 | + tempfile.NamedTemporaryFile(suffix='.geojson') as polygon_file: |
| 57 | + # Write dummy GeoJSON data to edges and polygon files |
| 58 | + dummy_geojson = { |
| 59 | + 'type': 'FeatureCollection', |
| 60 | + 'features': [ |
| 61 | + { |
| 62 | + 'type': 'Feature', |
| 63 | + 'geometry': {'type': 'LineString', 'coordinates': [[0, 0], [1, 1]]}, |
| 64 | + 'properties': {} |
| 65 | + } |
| 66 | + ] |
| 67 | + } |
| 68 | + |
| 69 | + edges_file.write(json.dumps(dummy_geojson).encode()) |
| 70 | + edges_file.flush() |
| 71 | + |
| 72 | + polygon_file.write(json.dumps(dummy_geojson).encode()) |
| 73 | + polygon_file.flush() |
| 74 | + |
| 75 | + # Simulate running the script as a standalone process |
| 76 | + result = run( |
| 77 | + [ |
| 78 | + 'python', |
| 79 | + 'src/calculators/qm_fixed_calculator.py', |
| 80 | + edges_file.name, |
| 81 | + output_file.name, |
| 82 | + polygon_file.name, |
| 83 | + ], |
| 84 | + stdout=PIPE, |
| 85 | + stderr=PIPE, |
| 86 | + text=True, |
| 87 | + ) |
| 88 | + |
| 89 | + # Debugging: Print stderr if test fails |
| 90 | + if result.returncode != 0: |
| 91 | + print('Error:', result.stderr) |
| 92 | + |
| 93 | + # Assertions |
| 94 | + self.assertEqual(result.returncode, 0) |
| 95 | + self.assertIn('QMFixedCalculator', result.stdout) |
| 96 | + |
| 97 | + def test_main_without_polygon(self): |
| 98 | + # Create temporary files for edges |
| 99 | + with tempfile.NamedTemporaryFile(suffix='.geojson') as edges_file, \ |
| 100 | + tempfile.NamedTemporaryFile(suffix='.geojson') as output_file: |
| 101 | + # Write dummy GeoJSON data to edges file |
| 102 | + dummy_geojson = { |
| 103 | + 'type': 'FeatureCollection', |
| 104 | + 'features': [] |
| 105 | + } |
| 106 | + |
| 107 | + edges_file.write(json.dumps(dummy_geojson).encode()) |
| 108 | + edges_file.flush() |
| 109 | + |
| 110 | + # Simulate running the script as a standalone process |
| 111 | + result = run( |
| 112 | + [ |
| 113 | + 'python', |
| 114 | + 'src/calculators/qm_fixed_calculator.py', |
| 115 | + edges_file.name, |
| 116 | + output_file.name, |
| 117 | + ], |
| 118 | + stdout=PIPE, |
| 119 | + stderr=PIPE, |
| 120 | + text=True, |
| 121 | + ) |
| 122 | + |
| 123 | + # Assertions |
| 124 | + self.assertEqual(result.returncode, 0) |
| 125 | + self.assertIn('QMFixedCalculator', result.stdout) |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == '__main__': |
| 130 | + unittest.main() |
0 commit comments