修正设备定位坐标系归一化

This commit is contained in:
stu2not
2026-06-29 15:50:48 +08:00
parent a2b873a3c2
commit cc67e8877a
2 changed files with 121 additions and 3 deletions

View File

@@ -0,0 +1,51 @@
import pytest
from banban.service.location import (
LocationService,
normalize_location_coordinate,
)
def test_normalize_default_device_location_from_wgs84_to_gcj02():
lat, lng, coord_type = normalize_location_coordinate(
latitude=30.223122166666666,
longitude=120.25837883333334,
coord_type=None,
)
assert coord_type == "gcj02"
assert lat == pytest.approx(30.22066658053894)
assert lng == pytest.approx(120.26284572518867)
def test_normalize_explicit_gcj02_does_not_convert_again():
lat, lng, coord_type = normalize_location_coordinate(
latitude=30.223122166666666,
longitude=120.25837883333334,
coord_type="gcj02",
)
assert coord_type == "gcj02"
assert lat == pytest.approx(30.223122166666666)
assert lng == pytest.approx(120.25837883333334)
@pytest.mark.asyncio
async def test_mqtt_location_report_ignores_zero_zero_before_normalization(monkeypatch):
service = LocationService()
sessions = []
async def fail_get_session():
sessions.append("called")
raise AssertionError("invalid location should not open a database session")
monkeypatch.setattr(service, "get_session", fail_get_session)
row = await service.report_mqtt_device_location(
device_id="TalkingQ_device001",
latitude=0,
longitude=0,
)
assert row is None
assert sessions == []