feat(platform): use vehicle keyword for data queries

This commit is contained in:
lingniu
2026-07-04 00:42:40 +08:00
parent f62fe13647
commit 20e2781f35
6 changed files with 74 additions and 27 deletions

View File

@@ -145,6 +145,27 @@ func TestHandlerVehicleRealtime(t *testing.T) {
}
}
func TestHandlerVehicleRealtimeAcceptsKeyword(t *testing.T) {
handler := NewHandler(NewService(NewMockStore()))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/realtime/vehicles?keyword=川AHTWO1&limit=10", nil)
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
}
var body struct {
Data struct {
Items []VehicleRealtimeRow `json:"items"`
} `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("response JSON should decode: %v body=%s", err, rec.Body.String())
}
if len(body.Data.Items) != 1 || body.Data.Items[0].VIN != "LNXNEGRR7SR318212" {
t.Fatalf("realtime vehicles should accept vehicle keyword, got %+v body=%s", body.Data.Items, rec.Body.String())
}
}
func TestHandlerHistoryMileageQualityOps(t *testing.T) {
cases := []struct {
path string
@@ -177,12 +198,16 @@ func TestHandlerHistoryMileageQualityOps(t *testing.T) {
func TestHandlerVehicleDataAPIsResolveVehicleKeyword(t *testing.T) {
handler := NewHandler(NewService(NewMockStore()))
cases := []struct {
name string
path string
name string
path string
wantVIN string
}{
{name: "history locations", path: "/api/history/locations?vin=粤AG18312&limit=10"},
{name: "raw frames", path: "/api/history/raw-frames?vin=粤AG18312&limit=1"},
{name: "daily mileage", path: "/api/mileage/daily?vin=粤AG18312&limit=10"},
{name: "history locations vin alias", path: "/api/history/locations?vin=粤AG18312&limit=10", wantVIN: "LB9A32A24R0LS1426"},
{name: "raw frames vin alias", path: "/api/history/raw-frames?vin=粤AG18312&limit=1", wantVIN: "LB9A32A24R0LS1426"},
{name: "daily mileage vin alias", path: "/api/mileage/daily?vin=粤AG18312&limit=10", wantVIN: "LB9A32A24R0LS1426"},
{name: "history locations keyword", path: "/api/history/locations?keyword=川AHTWO1&limit=10", wantVIN: "LNXNEGRR7SR318212"},
{name: "raw frames keyword", path: "/api/history/raw-frames?keyword=川AHTWO1&limit=1", wantVIN: "LNXNEGRR7SR318212"},
{name: "daily mileage keyword", path: "/api/mileage/daily?keyword=川AHTWO1&limit=10", wantVIN: "LNXNEGRR7SR318212"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
@@ -192,8 +217,21 @@ func TestHandlerVehicleDataAPIsResolveVehicleKeyword(t *testing.T) {
if rec.Code != http.StatusOK {
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
}
if !strings.Contains(rec.Body.String(), `"vin":"LB9A32A24R0LS1426"`) {
t.Fatalf("vehicle data API should resolve plate keyword to VIN: %s", rec.Body.String())
var body struct {
Data struct {
Items []struct {
VIN string `json:"vin"`
} `json:"items"`
} `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("response JSON should decode: %v body=%s", err, rec.Body.String())
}
if len(body.Data.Items) == 0 {
t.Fatalf("vehicle data API should return rows for resolved keyword: %s", rec.Body.String())
}
if body.Data.Items[0].VIN != tc.wantVIN {
t.Fatalf("vehicle data API should resolve keyword to VIN %s, got %s body=%s", tc.wantVIN, body.Data.Items[0].VIN, rec.Body.String())
}
})
}