Guard geo_service.init_geoip against repeated initialization
This commit is contained in:
@@ -48,6 +48,31 @@ def _make_session(response_json: dict[str, object], status: int = 200) -> MagicM
|
||||
async def clear_geo_cache() -> None:
|
||||
"""Flush the module-level geo cache before every test."""
|
||||
await geo_service.clear_cache()
|
||||
geo_service._geoip_reader = None
|
||||
geo_service._geoip_initialized = False
|
||||
|
||||
|
||||
def test_init_geoip_is_startup_only(tmp_path) -> None:
|
||||
"""A second init_geoip() call raises when the reader was already loaded."""
|
||||
path = tmp_path / "GeoLite2-Country.mmdb"
|
||||
path.write_text("dummy")
|
||||
|
||||
with patch("geoip2.database.Reader", MagicMock(name="Reader")) as mock_reader:
|
||||
geo_service.init_geoip(str(path))
|
||||
assert geo_service._geoip_reader is not None
|
||||
assert geo_service._geoip_initialized is True
|
||||
|
||||
with pytest.raises(RuntimeError, match="already initialised"):
|
||||
geo_service.init_geoip(str(path))
|
||||
|
||||
assert mock_reader.call_count == 1
|
||||
|
||||
|
||||
def test_init_geoip_no_path_leaves_reader_uninitialised() -> None:
|
||||
"""No active reader is created when no path is supplied."""
|
||||
geo_service.init_geoip("")
|
||||
assert geo_service._geoip_reader is None
|
||||
assert geo_service._geoip_initialized is False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user