Skip to content

Commit 0e87657

Browse files
TST: mock contextily tile fetches
1 parent 966cb8a commit 0e87657

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎tests/unit/simulation/test_monte_carlo_plots_background.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pylint: disable=unused-argument,assignment-from-no-return
2+
import math
23
import os
34
import urllib.error
45
from unittest.mock import MagicMock, patch
@@ -19,6 +20,33 @@
1920
)
2021

2122

23+
@pytest.fixture(autouse=True)
24+
def mock_background_tiles(monkeypatch):
25+
"""Return deterministic map tiles without contacting a tile provider."""
26+
contextily = import_optional_dependency("contextily")
27+
28+
def mock_bounds2img(west, south, east, north, **kwargs):
29+
earth_radius = 6378137.0
30+
31+
def to_mercator(longitude, latitude):
32+
x = earth_radius * math.radians(longitude)
33+
y = earth_radius * math.log(
34+
math.tan(math.pi / 4 + math.radians(latitude) / 2)
35+
)
36+
return x, y
37+
38+
min_x, min_y = to_mercator(west, south)
39+
max_x, max_y = to_mercator(east, north)
40+
return np.zeros((2, 2, 3), dtype=np.uint8), (
41+
min_x,
42+
max_x,
43+
min_y,
44+
max_y,
45+
)
46+
47+
monkeypatch.setattr(contextily, "bounds2img", mock_bounds2img)
48+
49+
2250
class MockMonteCarlo(MonteCarlo):
2351
"""Create a mock class to test the method without running a real simulation.
2452

0 commit comments

Comments
 (0)