64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
package stats
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
|
|
)
|
|
|
|
func TestSourceKeyUsesProtocolDeviceAndSourceIP(t *testing.T) {
|
|
got := SourceKey(envelope.ProtocolJT808, "13307765812", "", "115.231.168.135")
|
|
if got != "JT808:13307765812@115.231.168.135" {
|
|
t.Fatalf("source key = %q", got)
|
|
}
|
|
|
|
got = SourceKey(envelope.ProtocolGB32960, "", "iccid-1", "202.98.117.132")
|
|
if got != "GB32960:iccid-1@202.98.117.132" {
|
|
t.Fatalf("source key = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestUpsertSourceMileageWritesCandidateRow(t *testing.T) {
|
|
exec := &recordingExec{}
|
|
sample := SourceMileageSample{
|
|
VIN: "LA9GG64L7PBAF4001",
|
|
StatDate: "2026-07-08",
|
|
Protocol: envelope.ProtocolJT808,
|
|
SourceKey: "JT808:13307765812@115.231.168.135",
|
|
SourceIP: "115.231.168.135",
|
|
SourceEndpoint: "115.231.168.135:20215",
|
|
Phone: "13307765812",
|
|
FirstTotalKM: 4100.8,
|
|
LatestTotalKM: 4123.9,
|
|
DailyKM: 23.1,
|
|
SampleCount: 2,
|
|
FirstEventTime: time.Date(2026, 7, 8, 0, 1, 0, 0, time.FixedZone("Asia/Shanghai", 8*3600)),
|
|
LatestEventTime: time.Date(2026, 7, 8, 23, 59, 0, 0, time.FixedZone("Asia/Shanghai", 8*3600)),
|
|
QualityStatus: QualityOK,
|
|
QualityReason: "same_source_continuous",
|
|
}
|
|
if err := UpsertSourceMileage(context.Background(), exec, sample); err != nil {
|
|
t.Fatalf("UpsertSourceMileage() error = %v", err)
|
|
}
|
|
if len(exec.calls) != 1 {
|
|
t.Fatalf("exec calls = %d", len(exec.calls))
|
|
}
|
|
sql := exec.calls[0].query
|
|
for _, want := range []string{
|
|
"INSERT INTO vehicle_daily_mileage_source",
|
|
"ON DUPLICATE KEY UPDATE",
|
|
"daily_mileage_km = VALUES(daily_mileage_km)",
|
|
"quality_status = VALUES(quality_status)",
|
|
} {
|
|
if !strings.Contains(sql, want) {
|
|
t.Fatalf("candidate upsert missing %q: %s", want, sql)
|
|
}
|
|
}
|
|
if got := exec.calls[0].args[0]; got != "LA9GG64L7PBAF4001" {
|
|
t.Fatalf("first arg = %#v", got)
|
|
}
|
|
}
|