功能:完善车辆地图合作站信息与缓存刷新
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -52,6 +54,20 @@ class DashboardTest(unittest.TestCase):
|
||||
self.assertEqual(server.DASHBOARD_CACHE_SECONDS, 120)
|
||||
self.assertEqual(server.STATION_CACHE_SECONDS, 120)
|
||||
|
||||
def test_public_station_directory_limits_partner_contact_and_price_to_cooperative_stations(self):
|
||||
stations = [
|
||||
{"id": "partner", "cooperative": True, "contactPerson": "张工", "contactPhone": "13800000000", "unitPrice": 12.5},
|
||||
{"id": "external", "cooperative": False, "contactPerson": "李工", "contactPhone": "13900000000", "unitPrice": 10.0},
|
||||
]
|
||||
with patch.object(server, "_load_stations", return_value=stations):
|
||||
directory = server._public_station_directory()["stations"]
|
||||
self.assertEqual(directory[0]["contactPerson"], "张工")
|
||||
self.assertEqual(directory[0]["contactPhone"], "13800000000")
|
||||
self.assertEqual(directory[0]["unitPrice"], 12.5)
|
||||
self.assertNotIn("contactPerson", directory[1])
|
||||
self.assertNotIn("contactPhone", directory[1])
|
||||
self.assertNotIn("unitPrice", directory[1])
|
||||
|
||||
def test_cache_reuses_dashboard_snapshot_within_window(self):
|
||||
calls = []
|
||||
with patch.object(server, "_cache", {}):
|
||||
@@ -60,6 +76,45 @@ class DashboardTest(unittest.TestCase):
|
||||
self.assertEqual(calls, ["load"])
|
||||
self.assertEqual(first, second)
|
||||
|
||||
def test_expired_cache_returns_stale_value_while_refreshing_in_background(self):
|
||||
started = threading.Event()
|
||||
release = threading.Event()
|
||||
|
||||
def loader():
|
||||
started.set()
|
||||
release.wait(1)
|
||||
return {"version": 2}
|
||||
|
||||
cache = {"dashboard": (time.time() - 121, {"version": 1})}
|
||||
with patch.object(server, "_cache", cache), \
|
||||
patch.object(server, "_cache_refreshing", set()), \
|
||||
patch.object(server, "_cache_load_locks", {}):
|
||||
result = server._cached("dashboard", 120, loader)
|
||||
self.assertEqual(result, {"version": 1})
|
||||
self.assertTrue(started.wait(1))
|
||||
release.set()
|
||||
for _ in range(100):
|
||||
if cache["dashboard"][1] == {"version": 2}:
|
||||
break
|
||||
time.sleep(0.01)
|
||||
self.assertEqual(cache["dashboard"][1], {"version": 2})
|
||||
|
||||
def test_expired_station_region_is_served_while_refresh_is_scheduled(self):
|
||||
station = {"longitude": 113.2, "latitude": 23.1, "province": "错误省份"}
|
||||
coordinate = server._station_coordinate_key(station)
|
||||
stale_region = {"province": "广东省", "city": "广州市", "district": "黄埔区"}
|
||||
cache = {coordinate: {"updatedAt": time.time() - server.STATION_GEOCODE_CACHE_SECONDS - 1, "region": stale_region}}
|
||||
with patch.object(server, "_station_geocode_cache", cache), \
|
||||
patch.object(server, "_station_geocode_cache_dirty", False), \
|
||||
patch.object(server, "_schedule_station_region_refresh") as schedule, \
|
||||
patch.object(server, "_reverse_geocode_station") as reverse:
|
||||
result = server._stations_with_gps_regions([station])
|
||||
self.assertEqual(result[0]["province"], "广东省")
|
||||
self.assertEqual(result[0]["city"], "广州市")
|
||||
reverse.assert_not_called()
|
||||
schedule.assert_called_once()
|
||||
self.assertEqual(len(schedule.call_args[0][0]), 1)
|
||||
|
||||
def test_station_region_comes_from_gps_reverse_geocode_and_persists_by_coordinate(self):
|
||||
station = {
|
||||
"longitude": 113.200001,
|
||||
|
||||
Reference in New Issue
Block a user