补齐设备HTTP定位坐标归一化

This commit is contained in:
stu2not
2026-06-29 15:55:37 +08:00
parent cc67e8877a
commit 0283aa2fec
2 changed files with 135 additions and 21 deletions

View File

@@ -1,5 +1,6 @@
import pytest
from banban.schemas.location import DeviceLocationReportRequest
from banban.service.location import (
LocationService,
normalize_location_coordinate,
@@ -49,3 +50,68 @@ async def test_mqtt_location_report_ignores_zero_zero_before_normalization(monke
assert row is None
assert sessions == []
@pytest.mark.asyncio
async def test_http_location_report_without_coord_type_converts_before_persist(monkeypatch):
service = LocationService()
captured = {}
class FakeDeviceIdentity:
child_id = 9
child_name = "child"
class FakeSession:
async def close(self):
captured["closed"] = True
class FakeLocationDAO:
def __init__(self, session):
captured["session"] = session
async def report_device_location(self, *, device_id, child_id, payload):
captured["device_id"] = device_id
captured["child_id"] = child_id
captured["payload"] = payload
return {
"child_id": child_id,
"device_id": device_id,
"coord_type": payload.coord_type,
"lat": payload.lat,
"lng": payload.lng,
"should_resolve_address": False,
}
async def fake_authenticate_device_identity(*, device_id, serial_number):
captured["auth"] = (device_id, serial_number)
return FakeDeviceIdentity()
async def fake_get_session():
return FakeSession()
monkeypatch.setattr(
"banban.service.location.im_service.authenticate_device_identity",
fake_authenticate_device_identity,
)
monkeypatch.setattr("banban.service.location.LocationDAO", FakeLocationDAO)
monkeypatch.setattr(service, "get_session", fake_get_session)
_, row = await service.report_device_location(
device_id="TalkingQ_device001",
serial_number="SN001",
payload=DeviceLocationReportRequest(
lat=30.223122166666666,
lng=120.25837883333334,
source=0,
device_time="2026-06-29T00:00:00",
),
)
persisted_payload = captured["payload"]
assert captured["auth"] == ("TalkingQ_device001", "SN001")
assert captured["child_id"] == 9
assert captured["closed"] is True
assert row["coord_type"] == "gcj02"
assert persisted_payload.coord_type == "gcj02"
assert persisted_payload.lat == pytest.approx(30.22066658053894)
assert persisted_payload.lng == pytest.approx(120.26284572518867)