From 6ed1762e681898229e908fdf6a3f721eecf4362c Mon Sep 17 00:00:00 2001 From: Dobson Date: Mon, 25 Mar 2024 11:02:08 +0000 Subject: [PATCH 1/4] Update test_metric_utilities.py fix metric tests --- tests/test_metric_utilities.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_metric_utilities.py b/tests/test_metric_utilities.py index 692518a8..180fc7b3 100644 --- a/tests/test_metric_utilities.py +++ b/tests/test_metric_utilities.py @@ -310,7 +310,8 @@ def test_design_params(): real_G = G, real_subs = subs, real_results = results, - metric_list = design_results.keys()) + metric_list = design_results.keys(), + metric_evaluation = MetricEvaluation()) for metric, val in metrics.items(): assert metric in design_results assert np.isclose(val, 0) @@ -326,7 +327,8 @@ def test_design_params(): real_G = G, real_subs = subs, real_results = results, - metric_list = design_results.keys()) + metric_list = design_results.keys(), + metric_evaluation = MetricEvaluation()) for metric, val in metrics.items(): assert metric in design_results From 0c5fd2955596e9b6dcea41684bcdbcf2492fd0d3 Mon Sep 17 00:00:00 2001 From: Dobson Date: Mon, 25 Mar 2024 11:13:29 +0000 Subject: [PATCH 2/4] Fix graph test bug --- tests/test_graph_utilities.py | 2 +- tests/test_logging.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_graph_utilities.py b/tests/test_graph_utilities.py index ccaa3a72..002a30f6 100644 --- a/tests/test_graph_utilities.py +++ b/tests/test_graph_utilities.py @@ -16,7 +16,6 @@ from swmmanywhere.graph_utilities import graphfcns as gu from swmmanywhere.graph_utilities import iterate_graphfcns, load_graph, save_graph -os.environ['SWMMANYWHERE_VERBOSE'] = "false" def load_street_network(): """Load a street network.""" @@ -252,6 +251,7 @@ def test_iterate_graphfcns(): project_name = None, bbox_number = None, model_number = None) + os.environ['SWMMANYWHERE_VERBOSE'] = "false" G = iterate_graphfcns(G, ['assign_id', 'format_osmnx_lanes'], diff --git a/tests/test_logging.py b/tests/test_logging.py index f228ebfc..69695db9 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -29,6 +29,7 @@ def test_logger(): assert temp_file.read() != b"" logger.remove() fid.unlink() + os.environ["SWMMANYWHERE_VERBOSE"] = "false" def test_logger_disable(): """Test the disable function.""" @@ -67,4 +68,5 @@ def test_logger_again(): logger.test_logger() assert temp_file.read() != b"" logger.remove() - fid.unlink() \ No newline at end of file + fid.unlink() + os.environ["SWMMANYWHERE_VERBOSE"] = "false" \ No newline at end of file From 6021815c629098bf34efc638ec3b00342cc6444b Mon Sep 17 00:00:00 2001 From: Dobson Date: Mon, 25 Mar 2024 11:16:30 +0000 Subject: [PATCH 3/4] Update logging.py ignore doctest for logging --- swmmanywhere/logging.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/swmmanywhere/logging.py b/swmmanywhere/logging.py index 6ea63140..0ad1a655 100644 --- a/swmmanywhere/logging.py +++ b/swmmanywhere/logging.py @@ -5,11 +5,9 @@ >>> os.environ["SWMMANYWHERE_VERBOSE"] = "true" >>> # logging is now enabled in any swmmanywhere module >>> from swmmanywhere.logging import logger # You can now log yourself ->>> logger.info("This is an info message.") # Write to stdout -This is an info message. ->>> logger.add("file.log") # Add a log file ->>> logger.info("This is an info message.") # Write to stdout and file.log +>>> logger.info("This is an info message.") # Write to stdout # doctest: +SKIP This is an info message. +>>> logger.add("file.log") # Add a log file # doctest: +SKIP >>> os.environ["SWMMANYWHERE_VERBOSE"] = "false" # Disable logging """ import os From 4ce5f679c9fd3f6e18a8f1a0c5a988b6a8d80f8d Mon Sep 17 00:00:00 2001 From: barneydobson Date: Tue, 26 Mar 2024 15:43:37 +0000 Subject: [PATCH 4/4] Update test_prepare_data.py Fix nominatim call in building_downloader --- tests/test_prepare_data.py | 44 +++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/tests/test_prepare_data.py b/tests/test_prepare_data.py index 084a0134..293999be 100644 --- a/tests/test_prepare_data.py +++ b/tests/test_prepare_data.py @@ -117,32 +117,42 @@ def test_elevation_downloader_download(): # Test some property of data (not sure if they may change this # data) assert data.max().max() > 25, "Elevation data should be higher." + +@pytest.fixture +def setup_mocks(): + """Set up get_country mock for the tests.""" + # Mock for geolocator.reverse + mock_location = mock.Mock() + mock_location.raw = {'address': {'country_code': 'gb'}} -def test_get_uk(): + # Mock Nominatim + nominatim_patch = mock.patch.object(Nominatim, + 'reverse', + return_value=mock_location) + # Mock yaml.safe_load + yaml_patch = mock.patch.object(yaml, 'safe_load', return_value={'GB': 'GBR'}) + + with nominatim_patch, yaml_patch: + yield + +def test_get_uk(setup_mocks): """Check a UK point.""" # Coordinates for London, UK x = -0.1276 y = 51.5074 - - # Create a mock response for geolocator.reverse - mock_location = mock.Mock() - mock_location.raw = {'address': {'country_code': 'gb'}} - - # Mock Nominatim - with mock.patch.object(Nominatim, 'reverse', return_value=mock_location): - # Mock yaml.safe_load - with mock.patch.object(yaml, 'safe_load', return_value={'GB': 'GBR'}): - # Call get_country - result = downloaders.get_country(x, y) + + # Call get_country + result = downloaders.get_country(x, y) assert result[2] == 'GB' assert result[3] == 'GBR' -def test_building_downloader(): +def test_building_downloader(setup_mocks): """Check buildings are downloaded.""" - # Coordinates for small country (VAT) - x = 7.41839 - y = 43.73205 + # Coordinates + x = -0.1276 + y = 51.5074 + with tempfile.TemporaryDirectory() as temp_dir: temp_fid = Path(temp_dir) / 'temp.parquet' mock_response = mock.Mock() @@ -154,7 +164,7 @@ def test_building_downloader(): response = downloaders.download_buildings(temp_fid, x, y) # Assert that requests.get was called with the right arguments - mock_get.assert_called_once_with('https://data.source.coop/vida/google-microsoft-open-buildings/geoparquet/by_country/country_iso=MCO/MCO.parquet') + mock_get.assert_called_once_with('https://data.source.coop/vida/google-microsoft-open-buildings/geoparquet/by_country/country_iso=GBR/GBR.parquet') # Check response assert response == 200