From 45342222b4b86cbbda0bf1a2f88b7c89b21729ea Mon Sep 17 00:00:00 2001 From: Herald-TUOS Date: Mon, 9 Mar 2026 13:45:51 +0000 Subject: [PATCH 1/2] mark tests that call external APIs as skipped --- Tests/test_geocode.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/test_geocode.py b/Tests/test_geocode.py index 3ab19d9..e4b1d04 100644 --- a/Tests/test_geocode.py +++ b/Tests/test_geocode.py @@ -31,6 +31,7 @@ def test_clear_cache(self): assert cache_dir.is_dir() assert len([c for c in cache_dir.glob("*.p") if "gmaps" not in c.name]) == 0 + @unittest.skip("Skipped to avoid external API calls") def test_force_setup(self): """Test the `force_setup()` method.""" with Geocoder() as geo: From 8094971ec12e04cb79445a014e576dab31d218d0 Mon Sep 17 00:00:00 2001 From: Herald-TUOS Date: Mon, 9 Mar 2026 13:49:30 +0000 Subject: [PATCH 2/2] add a mocked force setup test function --- Tests/test_geocode.py | 117 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/Tests/test_geocode.py b/Tests/test_geocode.py index e4b1d04..cdd8004 100644 --- a/Tests/test_geocode.py +++ b/Tests/test_geocode.py @@ -12,7 +12,10 @@ import sys import os import unittest +from unittest.mock import MagicMock +from shapely.geometry import Polygon from pathlib import Path +import geopandas as gpd from numpy.testing import assert_almost_equal, assert_equal @@ -40,6 +43,120 @@ def test_force_setup(self): assert cache_dir.is_dir() assert len([c for c in cache_dir.glob("*.p") if "gmaps" not in c.name]) == 17 + def test_force_setup_without_external_api_calls(self): + """Test the `force_setup()` method with mocked components.""" + with Geocoder() as geo: + + def mock_neso_force_setup(): + + gsp_boundaries = gpd.GeoDataFrame( + { + "GSPs": ["BRED_1", "DEWP"], + "GSPGroup": ["_G", "_N"], + "geometry": [ + Polygon( + [ + (-2.1, 53.3), + (-2.0, 53.3), + (-2.0, 53.4), + (-2.1, 53.4), + (-2.1, 53.3), + ] + ), + Polygon( + [ + (-3.2, 55.9), + (-3.1, 55.9), + (-3.1, 56.0), + (-3.2, 56.0), + (-3.2, 55.9), + ] + ), + ], + }, + crs="EPSG:4326", + ) + for version in ["20220314", "20250109", "20251204"]: + geo.cache_manager.write(f"gsp_boundaries_{version}", gsp_boundaries) + geo.cache_manager.write("dno_boundaries", gpd.GeoDataFrame({})) + + def mock_ons_nrs_force_setup(): + llsoa_boundaries = gpd.GeoDataFrame( + { + "region_id": [ + "E01012082", + "E01011214", + "E01002050", + "W01000323", + "S01008087", + ], + "geometry": [ + Polygon( + [ + (-1.2, 54.5), + (-1.19, 54.5), + (-1.19, 54.55), + (-1.2, 54.55), + (-1.2, 54.5), + ] + ), + Polygon( + [ + (-1.71, 53.66), + (-1.69, 53.66), + (-1.69, 53.67), + (-1.71, 53.67), + (-1.71, 53.66), + ] + ), + Polygon( + [ + (-0.07, 51.57), + (-0.06, 51.57), + (-0.06, 51.58), + (-0.07, 51.58), + (-0.07, 51.57), + ] + ), + Polygon( + [ + (-3.14, 53.20), + (-3.12, 53.20), + (-3.12, 53.21), + (-3.14, 53.21), + (-3.14, 53.20), + ] + ), + Polygon( + [ + (-4.23, 55.91), + (-4.21, 55.91), + (-4.21, 55.93), + (-4.23, 55.93), + (-4.23, 55.91), + ] + ), + ], + }, + crs="EPSG:4326", + ) + for version in ["2011", "2021"]: + geo.cache_manager.write( + f"llsoa_boundaries_{version}", llsoa_boundaries + ) + geo.ons_nrs._load_datazone_lookup(version) + geo.ons_nrs._load_constituency_lookup() + geo.ons_nrs._load_lad_lookup() + geo.ons_nrs._load_llsoa_lookup() + + geo.neso.force_setup = MagicMock(side_effect=mock_neso_force_setup) + geo.ons_nrs.force_setup = MagicMock(side_effect=mock_ons_nrs_force_setup) + + geo.force_setup() + + geo.neso.force_setup.assert_called_once() + geo.ons_nrs.force_setup.assert_called_once() + def test_geocode_llsoa(self): """ Test the `geocode_llsoa()` function with several test cases.