refactor(go): simplify daily mileage storage
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
|
||||
)
|
||||
|
||||
func TestSamplesFromEnvelopeDerivesDailyMileageAndTotal(t *testing.T) {
|
||||
func TestSamplesFromEnvelopeDerivesDailyMileageSample(t *testing.T) {
|
||||
loc := time.FixedZone("Asia/Shanghai", 8*3600)
|
||||
env := envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolJT808,
|
||||
@@ -25,17 +25,14 @@ func TestSamplesFromEnvelopeDerivesDailyMileageAndTotal(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("SamplesFromEnvelope() error = %v", err)
|
||||
}
|
||||
if len(samples) != 2 {
|
||||
if len(samples) != 1 {
|
||||
t.Fatalf("sample count = %d", len(samples))
|
||||
}
|
||||
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])
|
||||
if samples[0].TotalMileageKM != 10241.2 {
|
||||
t.Fatalf("total mileage = %v", samples[0].TotalMileageKM)
|
||||
}
|
||||
if samples[0].StatDate != "2026-07-01" {
|
||||
t.Fatalf("stat date = %q", samples[0].StatDate)
|
||||
@@ -55,7 +52,7 @@ func TestSamplesFromEnvelopeUsesVehicleKeyWhenVINIsMissing(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("SamplesFromEnvelope() error = %v", err)
|
||||
}
|
||||
if len(samples) != 2 {
|
||||
if len(samples) != 1 {
|
||||
t.Fatalf("sample count = %d", len(samples))
|
||||
}
|
||||
if samples[0].VehicleKey != "JT808:13307811254" {
|
||||
@@ -110,7 +107,7 @@ func TestSamplesFromEnvelopeSkipsNonPositiveMileage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriterEnsuresSchemaAndUpsertsTwoMetrics(t *testing.T) {
|
||||
func TestWriterEnsuresSchemaAndUpsertsDailyMileage(t *testing.T) {
|
||||
exec := &recordingExec{}
|
||||
writer := NewWriter(exec, time.FixedZone("Asia/Shanghai", 8*3600))
|
||||
if err := writer.EnsureSchema(context.Background()); err != nil {
|
||||
@@ -127,18 +124,21 @@ func TestWriterEnsuresSchemaAndUpsertsTwoMetrics(t *testing.T) {
|
||||
t.Fatalf("Append() error = %v", err)
|
||||
}
|
||||
|
||||
if !strings.Contains(exec.calls[0].query, "CREATE TABLE IF NOT EXISTS vehicle_daily_metric") {
|
||||
if !strings.Contains(exec.calls[0].query, "CREATE TABLE IF NOT EXISTS vehicle_daily_mileage") {
|
||||
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 {
|
||||
if len(exec.calls) != 2 {
|
||||
t.Fatalf("exec calls = %d", len(exec.calls))
|
||||
}
|
||||
if !strings.Contains(exec.calls[1].query, "ON DUPLICATE KEY UPDATE") {
|
||||
t.Fatalf("unexpected upsert sql: %s", exec.calls[1].query)
|
||||
}
|
||||
if strings.Contains(exec.calls[1].query, "metric_key") || strings.Contains(exec.calls[1].query, "metric_unit") {
|
||||
t.Fatalf("daily mileage upsert should not use generic metric columns: %s", exec.calls[1].query)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user