61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
import unittest
|
|
import tempfile
|
|
import io
|
|
import os
|
|
from unittest.mock import patch, Mock
|
|
|
|
from PIL import Image
|
|
|
|
from map_manager.engine import MapEngine
|
|
from map_manager.services import OpenStreetMapService
|
|
from map_manager.tile_manager import MapTileManager
|
|
|
|
|
|
class TestEngineZoomAndDownload(unittest.TestCase):
|
|
def test_choose_zoom_monotonic(self):
|
|
# Small bbox near Rome
|
|
bbox = (12.49, 41.89, 12.50, 41.90)
|
|
engine = MapEngine(service_name='osm', cache_dir=None, enable_online=True)
|
|
|
|
zoom_large = engine._choose_zoom_for_bbox(bbox, max_size_pixels=2000)
|
|
zoom_small = engine._choose_zoom_for_bbox(bbox, max_size_pixels=200)
|
|
|
|
# Expect that allowing larger max_size yields same-or-higher zoom
|
|
self.assertIsNotNone(zoom_large)
|
|
self.assertIsNotNone(zoom_small)
|
|
self.assertGreaterEqual(zoom_large, zoom_small)
|
|
|
|
def test_download_and_cache_mocked(self):
|
|
service = OpenStreetMapService()
|
|
with tempfile.TemporaryDirectory() as td:
|
|
mgr = MapTileManager(service, cache_root_directory=td, enable_online_tile_fetching=True, tile_pixel_size=64)
|
|
|
|
# Prepare a fake PNG image bytes for the mock response
|
|
fake_img = Image.new('RGB', (64, 64), (123, 222, 111))
|
|
bio = io.BytesIO()
|
|
fake_img.save(bio, format='PNG')
|
|
img_bytes = bio.getvalue()
|
|
|
|
# Create a mock response object
|
|
mock_resp = Mock()
|
|
mock_resp.content = img_bytes
|
|
mock_resp.raise_for_status = Mock()
|
|
|
|
# Patch requests.get used in the tile_manager module
|
|
with patch('map_manager.tile_manager.requests.get', return_value=mock_resp) as mock_get:
|
|
# Request a tile that is not cached
|
|
result = mgr.get_tile_image(5, 10, 12)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result.size, (64, 64))
|
|
|
|
# Check that the tile file was created on disk
|
|
tile_path = mgr._get_tile_cache_file_path(5, 10, 12)
|
|
self.assertTrue(tile_path.is_file())
|
|
|
|
# Ensure our mocked requests.get was called
|
|
mock_get.assert_called()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|