fix: key daily metrics by vehicle identity
This commit is contained in:
@@ -31,6 +31,9 @@ func TestSamplesFromEnvelopeDerivesDailyMileageAndTotal(t *testing.T) {
|
||||
if samples[0].MetricKey != MetricDailyMileageKM || samples[0].MetricValue != 0 {
|
||||
t.Fatalf("unexpected daily mileage sample: %#v", samples[0])
|
||||
}
|
||||
if samples[0].VehicleKey != "LNBVIN00000000001" {
|
||||
t.Fatalf("vehicle key = %q", samples[0].VehicleKey)
|
||||
}
|
||||
if samples[1].MetricKey != MetricDailyTotalMileageKM || samples[1].MetricValue != 10241.2 {
|
||||
t.Fatalf("unexpected daily total sample: %#v", samples[1])
|
||||
}
|
||||
@@ -39,7 +42,31 @@ func TestSamplesFromEnvelopeDerivesDailyMileageAndTotal(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSamplesFromEnvelopeSkipsMissingVINOrMileage(t *testing.T) {
|
||||
func TestSamplesFromEnvelopeUsesVehicleKeyWhenVINIsMissing(t *testing.T) {
|
||||
loc := time.FixedZone("Asia/Shanghai", 8*3600)
|
||||
samples, err := SamplesFromEnvelope(envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolJT808,
|
||||
Phone: "013307811254",
|
||||
EventTimeMS: time.Date(2026, 7, 2, 0, 3, 0, 0, loc).UnixMilli(),
|
||||
Fields: map[string]any{
|
||||
envelope.FieldTotalMileageKM: 10985.7,
|
||||
},
|
||||
}, loc)
|
||||
if err != nil {
|
||||
t.Fatalf("SamplesFromEnvelope() error = %v", err)
|
||||
}
|
||||
if len(samples) != 2 {
|
||||
t.Fatalf("sample count = %d", len(samples))
|
||||
}
|
||||
if samples[0].VehicleKey != "JT808:013307811254" {
|
||||
t.Fatalf("vehicle key = %q", samples[0].VehicleKey)
|
||||
}
|
||||
if samples[0].VIN != "" {
|
||||
t.Fatalf("vin should stay empty for unresolved JT808 identity, got %q", samples[0].VIN)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSamplesFromEnvelopeSkipsMissingVehicleKeyOrMileage(t *testing.T) {
|
||||
samples, err := SamplesFromEnvelope(envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolJT808,
|
||||
Fields: map[string]any{envelope.FieldTotalMileageKM: 1.2},
|
||||
@@ -48,7 +75,7 @@ func TestSamplesFromEnvelopeSkipsMissingVINOrMileage(t *testing.T) {
|
||||
t.Fatalf("SamplesFromEnvelope() error = %v", err)
|
||||
}
|
||||
if len(samples) != 0 {
|
||||
t.Fatalf("expected no samples without vin, got %#v", samples)
|
||||
t.Fatalf("expected no samples without vehicle key, got %#v", samples)
|
||||
}
|
||||
|
||||
samples, err = SamplesFromEnvelope(envelope.FrameEnvelope{
|
||||
@@ -103,6 +130,9 @@ func TestWriterEnsuresSchemaAndUpsertsTwoMetrics(t *testing.T) {
|
||||
if !strings.Contains(exec.calls[0].query, "CREATE TABLE IF NOT EXISTS vehicle_daily_metric") {
|
||||
t.Fatalf("unexpected schema sql: %s", exec.calls[0].query)
|
||||
}
|
||||
if !strings.Contains(exec.calls[0].query, "vehicle_key") {
|
||||
t.Fatalf("schema should include vehicle_key: %s", exec.calls[0].query)
|
||||
}
|
||||
if len(exec.calls) != 3 {
|
||||
t.Fatalf("exec calls = %d", len(exec.calls))
|
||||
}
|
||||
@@ -112,6 +142,9 @@ func TestWriterEnsuresSchemaAndUpsertsTwoMetrics(t *testing.T) {
|
||||
if !strings.Contains(exec.calls[1].query, "first_total_mileage_km <= 0") {
|
||||
t.Fatalf("upsert should ignore legacy zero first mileage: %s", exec.calls[1].query)
|
||||
}
|
||||
if got := exec.calls[1].args[0]; got != "LNBVIN00000000002" {
|
||||
t.Fatalf("first upsert arg should be vehicle_key, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
type execCall struct {
|
||||
|
||||
Reference in New Issue
Block a user