step 1: lock public api and pure core

This commit is contained in:
2026-05-22 18:21:01 +02:00
parent 11fc1bb9bd
commit bd1ce7abff
14 changed files with 885 additions and 6 deletions

31
tests/test_cache.py Normal file
View File

@@ -0,0 +1,31 @@
from __future__ import annotations
from pathlib import Path
from dpg_map.cache import CacheStats, DiskCacheConfig, MemoryCacheConfig
def test_cache_stats_dataclass_construction() -> None:
stats = CacheStats(
memory_tiles=3,
memory_max_tiles=512,
memory_hits=10,
memory_misses=2,
disk_bytes=1024,
disk_max_bytes=None,
disk_hits=7,
disk_misses=1,
disk_path=Path("/tmp/dpg-map-cache"),
)
assert stats.memory_tiles == 3
assert stats.disk_max_bytes is None
assert stats.disk_path == Path("/tmp/dpg-map-cache")
def test_initial_cache_config_dataclasses() -> None:
memory_config = MemoryCacheConfig()
disk_config = DiskCacheConfig()
assert memory_config.max_tiles == 512
assert disk_config.max_bytes == 2_000_000_000

44
tests/test_projection.py Normal file
View File

@@ -0,0 +1,44 @@
from __future__ import annotations
import pytest
from dpg_map.projection import (
WEB_MERCATOR_MAX_LAT,
clamp_latitude,
latlon_to_tile,
latlon_to_world,
screen_to_world,
world_to_latlon,
world_to_screen,
)
@pytest.mark.parametrize(
("lat", "lon", "zoom"),
[
(0.0, 0.0, 0),
(47.9029, 1.9093, 15),
(-33.8688, 151.2093, 10),
(WEB_MERCATOR_MAX_LAT, 179.999, 4),
],
)
def test_projection_roundtrip(lat: float, lon: float, zoom: int) -> None:
x, y = latlon_to_world(lat, lon, zoom)
roundtrip_lat, roundtrip_lon = world_to_latlon(x, y, zoom)
assert roundtrip_lat == pytest.approx(clamp_latitude(lat), abs=1e-7)
assert roundtrip_lon == pytest.approx(lon, abs=1e-7)
def test_latlon_to_tile_returns_xyz_coordinate() -> None:
assert latlon_to_tile(0.0, 0.0, 1) == (1, 1, 1)
def test_world_screen_roundtrip() -> None:
center = (47.9029, 1.9093)
world = latlon_to_world(47.91, 1.92, 14)
screen = world_to_screen(*world, center=center, zoom=14, width=800, height=600)
assert screen_to_world(*screen, center=center, zoom=14, width=800, height=600) == pytest.approx(
world
)

71
tests/test_providers.py Normal file
View File

@@ -0,0 +1,71 @@
from __future__ import annotations
import pytest
import dpg_map as dpgm
from dpg_map.exceptions import InvalidProviderError, ProviderExistsError, ProviderNotFoundError
def test_default_provider_is_registered() -> None:
provider = dpgm.get_provider("osm")
assert provider.name == "osm"
assert "osm" in dpgm.list_providers()
assert provider.build_url(x=1, y=2, z=3) == "https://tile.openstreetmap.org/3/1/2.png"
def test_provider_registration_roundtrip() -> None:
provider = dpgm.TileProvider(
name="unit-test-provider",
url_template="https://tiles.example.test/{z}/{x}/{y}.png",
attribution="Example",
)
dpgm.register_provider(provider)
try:
assert dpgm.get_provider("unit-test-provider") == provider
assert "unit-test-provider" in dpgm.list_providers()
finally:
dpgm.unregister_provider("unit-test-provider")
with pytest.raises(ProviderNotFoundError):
dpgm.get_provider("unit-test-provider")
def test_duplicate_provider_registration_fails() -> None:
provider = dpgm.TileProvider(
name="unit-test-duplicate",
url_template="https://tiles.example.test/{z}/{x}/{y}.png",
)
dpgm.register_provider(provider)
try:
with pytest.raises(ProviderExistsError):
dpgm.register_provider(provider)
finally:
dpgm.unregister_provider("unit-test-duplicate")
def test_provider_url_building_with_subdomains_retina_and_extension() -> None:
provider = dpgm.TileProvider(
name="unit-test-template",
url_template="https://{s}.tiles.example.test/{z}/{x}/{y}{r}.{ext}",
subdomains=("a", "b", "c"),
retina=True,
file_extension="webp",
)
assert provider.build_url(x=1, y=2, z=3) == "https://a.tiles.example.test/3/1/2@2x.webp"
@pytest.mark.parametrize(
"template",
[
"https://tiles.example.test/{z}/{x}.png",
"https://tiles.example.test/{z}/{x}/{y}/{quadkey}.png",
"",
],
)
def test_invalid_provider_templates_fail(template: str) -> None:
with pytest.raises(InvalidProviderError):
dpgm.TileProvider(name="bad-template", url_template=template)

48
tests/test_public_api.py Normal file
View File

@@ -0,0 +1,48 @@
from __future__ import annotations
def test_package_exports_required_public_api() -> None:
import dpg_map as dpgm
expected = {
"configure",
"TileProvider",
"register_provider",
"unregister_provider",
"get_provider",
"list_providers",
"map_widget",
"set_center",
"get_center",
"set_zoom",
"get_zoom",
"set_view",
"fit_bounds",
"screen_to_latlon",
"latlon_to_screen",
"add_marker",
"add_polyline",
"add_trajectory",
"update_marker",
"update_polyline",
"update_trajectory",
"set_marker_position",
"set_marker_label",
"set_polyline_points",
"set_overlay_show",
"delete_overlay",
"add_layer",
"show_layer",
"hide_layer",
"clear_layer",
"clear_map",
"set_provider",
"clear_memory_cache",
"clear_disk_cache",
"get_cache_stats",
"get_map_debug_state",
}
assert set(dpgm.__all__) == expected
for name in expected:
assert hasattr(dpgm, name)