feat: build vehicle data platform and production pipeline
This commit is contained in:
@@ -37,26 +37,30 @@ func TestSnapshotWriterEnsuresSchemaAndUpsertsCoreSnapshot(t *testing.T) {
|
||||
map[string]any{"name": "gd_fc_vendor_tlv", "type": "vendor", "value": map[string]any{"foo": "bar"}},
|
||||
},
|
||||
},
|
||||
ParsedFields: map[string]any{
|
||||
"gb32960.vehicle.soc_percent": "90",
|
||||
"gb32960.gd_fc_vendor_tlv.foo": "bar",
|
||||
},
|
||||
Fields: map[string]any{envelope.FieldSOCPercent: 90},
|
||||
}); err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
}
|
||||
|
||||
if len(exec.calls) != 6 {
|
||||
t.Fatalf("exec calls = %d, want 6", len(exec.calls))
|
||||
if len(exec.calls) != 19 {
|
||||
t.Fatalf("exec calls = %d, want 19", len(exec.calls))
|
||||
}
|
||||
if !strings.Contains(exec.calls[0].query, "CREATE TABLE IF NOT EXISTS vehicle_realtime_snapshot") {
|
||||
t.Fatalf("schema query = %s", exec.calls[0].query)
|
||||
}
|
||||
if !strings.Contains(exec.calls[4].query, "CREATE TABLE IF NOT EXISTS vehicle_realtime_location") {
|
||||
t.Fatalf("location schema query = %s", exec.calls[4].query)
|
||||
if !strings.Contains(exec.calls[11].query, "CREATE TABLE IF NOT EXISTS vehicle_realtime_location") {
|
||||
t.Fatalf("location schema query = %s", exec.calls[11].query)
|
||||
}
|
||||
for _, call := range exec.calls {
|
||||
if strings.Contains(call.query, "vehicle_realtime_kv") {
|
||||
t.Fatalf("snapshot writer should not create or write MySQL realtime kv: %s", call.query)
|
||||
}
|
||||
}
|
||||
for _, call := range []snapshotExecCall{exec.calls[0], exec.calls[4]} {
|
||||
for _, call := range []snapshotExecCall{exec.calls[0], exec.calls[11]} {
|
||||
if strings.Contains(call.query, "fields_json") {
|
||||
t.Fatalf("realtime schema should not contain fields_json: %s", call.query)
|
||||
}
|
||||
@@ -74,10 +78,21 @@ func TestSnapshotWriterEnsuresSchemaAndUpsertsCoreSnapshot(t *testing.T) {
|
||||
t.Fatalf("snapshot schema should contain %s: %s", want, exec.calls[0].query)
|
||||
}
|
||||
}
|
||||
if strings.Contains(exec.calls[4].query, "idx_location") {
|
||||
t.Fatalf("realtime location table should not keep unused geo index: %s", exec.calls[4].query)
|
||||
if strings.Contains(exec.calls[11].query, "idx_location") {
|
||||
t.Fatalf("realtime location table should not keep unused geo index: %s", exec.calls[11].query)
|
||||
}
|
||||
upsert := exec.calls[5]
|
||||
if !strings.Contains(exec.calls[12].query, "total_mileage_event_time") {
|
||||
t.Fatalf("location compatibility migration should add total mileage event time: %s", exec.calls[12].query)
|
||||
}
|
||||
for _, call := range exec.calls[13:17] {
|
||||
if !strings.Contains(call.query, "total_mileage") {
|
||||
t.Fatalf("schema bootstrap should clean invalid realtime mileage: %s", call.query)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(exec.calls[17].query, "snapshot_backfill") {
|
||||
t.Fatalf("access projection baseline should be backfilled explicitly: %s", exec.calls[17].query)
|
||||
}
|
||||
upsert := exec.calls[18]
|
||||
if !strings.Contains(upsert.query, "ON DUPLICATE KEY UPDATE") {
|
||||
t.Fatalf("upsert query = %s", upsert.query)
|
||||
}
|
||||
@@ -101,8 +116,13 @@ func TestSnapshotWriterEnsuresSchemaAndUpsertsCoreSnapshot(t *testing.T) {
|
||||
if got := upsert.args[5]; !strings.Contains(got.(string), `"gb32960.vehicle.soc_percent":"90"`) {
|
||||
t.Fatalf("parsed json arg = %#v", got)
|
||||
}
|
||||
if len(upsert.args) != 9 {
|
||||
t.Fatalf("snapshot upsert args = %d, want 9", len(upsert.args))
|
||||
if len(upsert.args) != 12 {
|
||||
t.Fatalf("snapshot upsert args = %d, want 12", len(upsert.args))
|
||||
}
|
||||
for _, want := range []string{"access_first_seen_at", "access_previous_received_at", "access_latest_received_at", "access_report_interval_ms", "access_sample_count", "access_latest_event_id", "live_writer", "TIMESTAMPDIFF(MICROSECOND"} {
|
||||
if !strings.Contains(upsert.query, want) {
|
||||
t.Fatalf("access projection upsert missing %q: %s", want, upsert.query)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,8 +142,24 @@ func TestSnapshotWriterEnsureSchemaOnlyCreatesTargetTables(t *testing.T) {
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec("ALTER TABLE vehicle_realtime_snapshot ADD COLUMN parsed_json").
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
for _, column := range []string{"access_first_seen_at", "access_previous_received_at", "access_latest_received_at", "access_report_interval_ms", "access_sample_count", "access_latest_event_id", "access_first_seen_source"} {
|
||||
mock.ExpectExec("ALTER TABLE vehicle_realtime_snapshot ADD COLUMN " + column).
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
}
|
||||
mock.ExpectExec("CREATE TABLE IF NOT EXISTS vehicle_realtime_location").
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec("ALTER TABLE vehicle_realtime_location ADD COLUMN total_mileage_event_time").
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec("UPDATE vehicle_realtime_location").
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec("UPDATE vehicle_realtime_snapshot").
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec("UPDATE vehicle_realtime_snapshot").
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec("UPDATE vehicle_realtime_snapshot").
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec("UPDATE vehicle_realtime_snapshot").
|
||||
WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
|
||||
if err := writer.EnsureSchema(context.Background()); err != nil {
|
||||
t.Fatalf("EnsureSchema() error = %v", err)
|
||||
@@ -133,6 +169,41 @@ func TestSnapshotWriterEnsureSchemaOnlyCreatesTargetTables(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRealtimeAccessProjectionSQLProtectsDuplicateAndOutOfOrderReceipts(t *testing.T) {
|
||||
accessStart := strings.Index(upsertRealtimeSnapshotSQL, "access_previous_received_at =")
|
||||
if accessStart < 0 {
|
||||
t.Fatal("access projection assignments are missing")
|
||||
}
|
||||
accessSQL := upsertRealtimeSnapshotSQL[accessStart:]
|
||||
for _, want := range []string{
|
||||
"VALUES(access_latest_received_at) > vehicle_realtime_snapshot.access_latest_received_at",
|
||||
"VALUES(access_latest_event_id) <> vehicle_realtime_snapshot.access_latest_event_id",
|
||||
"TIMESTAMPDIFF(MICROSECOND",
|
||||
"access_sample_count + 1",
|
||||
} {
|
||||
if !strings.Contains(accessSQL, want) {
|
||||
t.Fatalf("access projection SQL missing %q:\n%s", want, accessSQL)
|
||||
}
|
||||
}
|
||||
if strings.Contains(accessSQL, "VALUES(event_time)") {
|
||||
t.Fatalf("access receipt projection must not be gated by device event time:\n%s", accessSQL)
|
||||
}
|
||||
ordered := []string{"access_previous_received_at =", "access_report_interval_ms =", "access_sample_count =", "access_latest_received_at =", "access_latest_event_id ="}
|
||||
previous := -1
|
||||
for _, assignment := range ordered {
|
||||
position := strings.Index(accessSQL, assignment)
|
||||
if position <= previous {
|
||||
t.Fatalf("access assignment %q must preserve old latest receipt before advancing it: %s", assignment, accessSQL)
|
||||
}
|
||||
previous = position
|
||||
}
|
||||
for _, want := range []string{"COALESCE(access_first_seen_at, received_at, updated_at)", "snapshot_backfill", "access_sample_count = 0"} {
|
||||
if !strings.Contains(backfillRealtimeAccessProjectionSQL, want) {
|
||||
t.Fatalf("access baseline backfill missing %q: %s", want, backfillRealtimeAccessProjectionSQL)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotWriterUpsertsRealtimeLocationWhenCoordinatesExist(t *testing.T) {
|
||||
exec := &recordingSnapshotExec{}
|
||||
writer := NewSnapshotWriter(exec)
|
||||
@@ -150,16 +221,15 @@ func TestSnapshotWriterUpsertsRealtimeLocationWhenCoordinatesExist(t *testing.T)
|
||||
ReceivedAtMS: 1782918601000,
|
||||
EventID: "event-1",
|
||||
Parsed: map[string]any{"location": map[string]any{"latitude": 30.590151, "longitude": 121.069881}},
|
||||
Fields: map[string]any{
|
||||
envelope.FieldLatitude: 30.590151,
|
||||
envelope.FieldLongitude: 121.069881,
|
||||
envelope.FieldSpeedKMH: 23.0,
|
||||
envelope.FieldTotalMileageKM: 10241.2,
|
||||
envelope.FieldSOCPercent: 88,
|
||||
"altitude_m": uint16(5),
|
||||
"direction_deg": uint16(79),
|
||||
"alarm_flag": uint32(0),
|
||||
"status_flag": uint32(786435),
|
||||
ParsedFields: map[string]any{
|
||||
"jt808.location.latitude": 30.590151,
|
||||
"jt808.location.longitude": 121.069881,
|
||||
"jt808.location.speed_kmh": 23.0,
|
||||
"jt808.location.total_mileage_km": "10241.2",
|
||||
"jt808.location.altitude_m": uint16(5),
|
||||
"jt808.location.direction_deg": uint16(79),
|
||||
"jt808.location.alarm_flag": uint32(0),
|
||||
"jt808.location.status_flag": uint32(786435),
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
@@ -198,15 +268,190 @@ func TestSnapshotWriterUpsertsRealtimeLocationWhenCoordinatesExist(t *testing.T)
|
||||
if got, want := locationUpsert.args[7], 10241.2; got != want {
|
||||
t.Fatalf("mileage arg = %#v, want %v", got, want)
|
||||
}
|
||||
if got, want := locationUpsert.args[8], 88.0; got != want {
|
||||
t.Fatalf("soc arg = %#v, want %v", got, want)
|
||||
if _, ok := locationUpsert.args[8].(time.Time); !ok {
|
||||
t.Fatalf("total mileage event time arg = %#v, want time.Time", locationUpsert.args[8])
|
||||
}
|
||||
if len(locationUpsert.args) != 15 {
|
||||
t.Fatalf("location upsert args = %d, want 15", len(locationUpsert.args))
|
||||
if got := locationUpsert.args[9]; got != nil {
|
||||
t.Fatalf("JT808 location should not invent SOC, got %#v", got)
|
||||
}
|
||||
if len(locationUpsert.args) != 16 {
|
||||
t.Fatalf("location upsert args = %d, want 16", len(locationUpsert.args))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotWriterMergesGB32960ParsedJSONAcrossSplitRealtimeFrames(t *testing.T) {
|
||||
func TestSnapshotWriterDropsNonPositiveTotalMileageFromRealtimeStores(t *testing.T) {
|
||||
exec := &recordingSnapshotExec{}
|
||||
writer := NewSnapshotWriter(exec)
|
||||
|
||||
if err := writer.Update(context.Background(), envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolJT808,
|
||||
MessageID: "0x0200",
|
||||
VIN: "VIN001",
|
||||
EventTimeMS: 1782918600000,
|
||||
ReceivedAtMS: 1782918601000,
|
||||
ParsedFields: map[string]any{
|
||||
"jt808.location.latitude": "30.590151",
|
||||
"jt808.location.longitude": "121.069881",
|
||||
"jt808.location.speed_kmh": "23",
|
||||
"jt808.location.total_mileage_km": "0",
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
}
|
||||
|
||||
if len(exec.calls) != 2 {
|
||||
t.Fatalf("exec calls = %d, want snapshot + location", len(exec.calls))
|
||||
}
|
||||
parsedJSON, ok := exec.calls[0].args[5].(string)
|
||||
if !ok {
|
||||
t.Fatalf("snapshot parsed_json arg = %#v", exec.calls[0].args[5])
|
||||
}
|
||||
if strings.Contains(parsedJSON, "total_mileage_km") {
|
||||
t.Fatalf("snapshot parsed_json should drop non-positive mileage: %s", parsedJSON)
|
||||
}
|
||||
if !strings.Contains(parsedJSON, "jt808.location.speed_kmh") {
|
||||
t.Fatalf("snapshot parsed_json should keep valid fields: %s", parsedJSON)
|
||||
}
|
||||
if exec.calls[1].args[7] != nil {
|
||||
t.Fatalf("location total mileage arg = %#v, want nil", exec.calls[1].args[7])
|
||||
}
|
||||
if exec.calls[1].args[8] != nil {
|
||||
t.Fatalf("location total mileage time arg = %#v, want nil", exec.calls[1].args[8])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotWriterNormalizesFarFutureEventTime(t *testing.T) {
|
||||
exec := &recordingSnapshotExec{}
|
||||
writer := NewSnapshotWriter(exec)
|
||||
received := time.Date(2026, 7, 12, 9, 30, 0, 0, time.UTC)
|
||||
futureEvent := time.Date(2026, 7, 13, 9, 30, 0, 0, time.UTC)
|
||||
|
||||
if err := writer.Update(context.Background(), envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolJT808,
|
||||
MessageID: "0x0200",
|
||||
VIN: "VIN001",
|
||||
EventTimeMS: futureEvent.UnixMilli(),
|
||||
ReceivedAtMS: received.UnixMilli(),
|
||||
Parsed: map[string]any{"location": map[string]any{"latitude": 30.590151, "longitude": 121.069881}},
|
||||
ParsedFields: map[string]any{
|
||||
"jt808.location.latitude": 30.590151,
|
||||
"jt808.location.longitude": 121.069881,
|
||||
"jt808.location.total_mileage_km": "10241.2",
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
}
|
||||
|
||||
if len(exec.calls) != 2 {
|
||||
t.Fatalf("exec calls = %d, want snapshot + location", len(exec.calls))
|
||||
}
|
||||
if got, ok := exec.calls[0].args[6].(time.Time); !ok || !got.Equal(received) {
|
||||
t.Fatalf("snapshot event_time arg = %#v, want received %s", exec.calls[0].args[6], received)
|
||||
}
|
||||
if got, ok := exec.calls[1].args[3].(time.Time); !ok || !got.Equal(received) {
|
||||
t.Fatalf("location event_time arg = %#v, want received %s", exec.calls[1].args[3], received)
|
||||
}
|
||||
if got, ok := exec.calls[1].args[8].(time.Time); !ok || !got.Equal(received) {
|
||||
t.Fatalf("total mileage event time arg = %#v, want received %s", exec.calls[1].args[8], received)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRealtimeLocationUsesProtocolMileageMapping(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
protocol envelope.Protocol
|
||||
fields map[string]any
|
||||
wantMileage float64
|
||||
}{
|
||||
{
|
||||
name: "gb32960 kilometers",
|
||||
protocol: envelope.ProtocolGB32960,
|
||||
fields: map[string]any{
|
||||
"gb32960.position.latitude": 30.590151,
|
||||
"gb32960.position.longitude": 121.069881,
|
||||
"gb32960.vehicle.total_mileage_km": "4123.9",
|
||||
},
|
||||
wantMileage: 4123.9,
|
||||
},
|
||||
{
|
||||
name: "jt808 kilometers",
|
||||
protocol: envelope.ProtocolJT808,
|
||||
fields: map[string]any{
|
||||
"jt808.location.latitude": 30.590151,
|
||||
"jt808.location.longitude": 121.069881,
|
||||
"jt808.location.total_mileage_km": json.Number("10241.2"),
|
||||
},
|
||||
wantMileage: 10241.2,
|
||||
},
|
||||
{
|
||||
name: "yutong meters",
|
||||
protocol: envelope.ProtocolYutongMQTT,
|
||||
fields: map[string]any{
|
||||
"yutong_mqtt.data.latitude": 30.590151,
|
||||
"yutong_mqtt.data.longitude": 121.069881,
|
||||
"yutong_mqtt.data.total_mileage": "86737000",
|
||||
},
|
||||
wantMileage: 86737,
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
row, ok := realtimeLocationFromEnvelope(envelope.FrameEnvelope{
|
||||
Protocol: tc.protocol,
|
||||
EventTimeMS: 1782918600000,
|
||||
ReceivedAtMS: 1782918601000,
|
||||
ParsedFields: tc.fields,
|
||||
}, "VIN001", "")
|
||||
if !ok {
|
||||
t.Fatal("realtimeLocationFromEnvelope() should produce a location")
|
||||
}
|
||||
if got, ok := row.TotalMileageKM.(float64); !ok || got != tc.wantMileage {
|
||||
t.Fatalf("total mileage = %#v, want %v", row.TotalMileageKM, tc.wantMileage)
|
||||
}
|
||||
if _, ok := row.TotalMileageAt.(time.Time); !ok {
|
||||
t.Fatalf("total mileage event time = %#v, want time.Time", row.TotalMileageAt)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRealtimeLocationDoesNotAdvanceMileageFromCoreFieldOnSparseFrame(t *testing.T) {
|
||||
row, ok := realtimeLocationFromEnvelope(envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolYutongMQTT,
|
||||
EventTimeMS: 1782918600000,
|
||||
ReceivedAtMS: 1782918601000,
|
||||
ParsedFields: map[string]any{
|
||||
"yutong_mqtt.data.latitude": "30.590151",
|
||||
"yutong_mqtt.data.longitude": "121.069881",
|
||||
},
|
||||
}, "VIN001", "")
|
||||
if !ok {
|
||||
t.Fatal("realtimeLocationFromEnvelope() should produce a location")
|
||||
}
|
||||
if row.TotalMileageKM != nil {
|
||||
t.Fatalf("sparse frame total mileage = %#v, want nil", row.TotalMileageKM)
|
||||
}
|
||||
if row.TotalMileageAt != nil {
|
||||
t.Fatalf("sparse frame total mileage event time = %#v, want nil", row.TotalMileageAt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRealtimeLocationRejectsBareStandardizedFields(t *testing.T) {
|
||||
_, ok := realtimeLocationFromEnvelope(envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolJT808,
|
||||
EventTimeMS: 1782918600000,
|
||||
ReceivedAtMS: 1782918601000,
|
||||
Fields: map[string]any{
|
||||
envelope.FieldLatitude: 30.590151,
|
||||
envelope.FieldLongitude: 121.069881,
|
||||
},
|
||||
}, "VIN001", "")
|
||||
if ok {
|
||||
t.Fatal("bare standardized fields must not drive the canonical location projection")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotWriterMergesGB32960ParsedJSONAcrossSplitRealtimeFramesInUpsert(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -214,10 +459,6 @@ func TestSnapshotWriterMergesGB32960ParsedJSONAcrossSplitRealtimeFrames(t *testi
|
||||
defer db.Close()
|
||||
writer := NewSnapshotWriter(db)
|
||||
|
||||
existing := `{"data_units":[{"type":"0x01","name":"vehicle","value":{"soc_percent":88}}]}`
|
||||
mock.ExpectQuery("SELECT parsed_json FROM vehicle_realtime_snapshot WHERE protocol = \\? AND vin = \\?").
|
||||
WithArgs("GB32960", "VIN001").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"parsed_json"}).AddRow(existing))
|
||||
mock.ExpectExec("INSERT INTO vehicle_realtime_snapshot").
|
||||
WithArgs(
|
||||
"GB32960",
|
||||
@@ -226,14 +467,15 @@ func TestSnapshotWriterMergesGB32960ParsedJSONAcrossSplitRealtimeFrames(t *testi
|
||||
"",
|
||||
"",
|
||||
jsonFlatFieldsArg{fields: map[string]string{
|
||||
"gb32960.vehicle.soc_percent": "88",
|
||||
"gb32960.vehicle.type": "0x01",
|
||||
"gb32960.gd_fc_stack.stack_count": "1",
|
||||
"gb32960.gd_fc_stack.type": "0x30",
|
||||
}},
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
@@ -248,6 +490,10 @@ func TestSnapshotWriterMergesGB32960ParsedJSONAcrossSplitRealtimeFrames(t *testi
|
||||
map[string]any{"type": "0x30", "name": "gd_fc_stack", "value": map[string]any{"stack_count": 1}},
|
||||
},
|
||||
},
|
||||
ParsedFields: map[string]any{
|
||||
"gb32960.gd_fc_stack.stack_count": "1",
|
||||
"gb32960.gd_fc_stack.type": "0x30",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
@@ -265,9 +511,6 @@ func TestSnapshotWriterUsesEnvelopeParsedFieldsWithoutReflattening(t *testing.T)
|
||||
defer db.Close()
|
||||
writer := NewSnapshotWriter(db)
|
||||
|
||||
mock.ExpectQuery("SELECT parsed_json FROM vehicle_realtime_snapshot WHERE protocol = \\? AND vin = \\?").
|
||||
WithArgs("GB32960", "VIN001").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"parsed_json"}))
|
||||
mock.ExpectExec("INSERT INTO vehicle_realtime_snapshot").
|
||||
WithArgs(
|
||||
"GB32960",
|
||||
@@ -281,6 +524,9 @@ func TestSnapshotWriterUsesEnvelopeParsedFieldsWithoutReflattening(t *testing.T)
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
@@ -316,10 +562,6 @@ func TestSnapshotWriterCollapsesGB32960StackFragmentsWhenMergingParsedJSON(t *te
|
||||
defer db.Close()
|
||||
writer := NewSnapshotWriter(db)
|
||||
|
||||
existing := `{"data_units":[{"type":"0x30","name":"gd_fc_stack","value":{"stack_count":1,"summaries":[{"cell_count":432,"stack_water_outlet_temp_c":65,"frame_cell_start":201,"frame_cell_count":200}]}}]}`
|
||||
mock.ExpectQuery("SELECT parsed_json FROM vehicle_realtime_snapshot WHERE protocol = \\? AND vin = \\?").
|
||||
WithArgs("GB32960", "VIN001").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"parsed_json"}).AddRow(existing))
|
||||
mock.ExpectExec("INSERT INTO vehicle_realtime_snapshot").
|
||||
WithArgs(
|
||||
"GB32960",
|
||||
@@ -341,6 +583,9 @@ func TestSnapshotWriterCollapsesGB32960StackFragmentsWhenMergingParsedJSON(t *te
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
sqlmock.AnyArg(),
|
||||
).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
@@ -364,6 +609,9 @@ func TestSnapshotWriterCollapsesGB32960StackFragmentsWhenMergingParsedJSON(t *te
|
||||
},
|
||||
},
|
||||
},
|
||||
ParsedFields: map[string]any{
|
||||
"gb32960.gd_fc_stack.stack_water_outlet_temp_c": "63",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
@@ -397,6 +645,18 @@ func TestRealtimeUpsertsDoNotOverwriteWithOlderEventTime(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRealtimeSnapshotUpsertMergesParsedJSONInSQL(t *testing.T) {
|
||||
for _, want := range []string{
|
||||
"JSON_MERGE_PATCH",
|
||||
"COALESCE(NULLIF(vehicle_realtime_snapshot.parsed_json, ''), JSON_OBJECT())",
|
||||
"VALUES(parsed_json)",
|
||||
} {
|
||||
if !strings.Contains(upsertRealtimeSnapshotSQL, want) {
|
||||
t.Fatalf("snapshot upsert should merge parsed_json in SQL, missing %q:\n%s", want, upsertRealtimeSnapshotSQL)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRealtimeLocationUpsertKeepsExistingSparseFieldsWhenMQTTOnlySendsCoordinates(t *testing.T) {
|
||||
for _, column := range []string{
|
||||
"speed_kmh",
|
||||
@@ -412,6 +672,10 @@ func TestRealtimeLocationUpsertKeepsExistingSparseFieldsWhenMQTTOnlySendsCoordin
|
||||
t.Fatalf("location upsert should keep existing %s when incoming sparse MQTT frame omits it; missing:\n%s\nin:\n%s", column, want, upsertRealtimeLocationSQL)
|
||||
}
|
||||
}
|
||||
wantMileageAt := "total_mileage_event_time = IF(VALUES(event_time) IS NOT NULL AND (vehicle_realtime_location.event_time IS NULL OR VALUES(event_time) >= vehicle_realtime_location.event_time), IF(VALUES(total_mileage_km) IS NOT NULL, COALESCE(VALUES(total_mileage_event_time), total_mileage_event_time), total_mileage_event_time), total_mileage_event_time)"
|
||||
if !strings.Contains(upsertRealtimeLocationSQL, wantMileageAt) {
|
||||
t.Fatalf("location upsert should only advance total_mileage_event_time when the incoming frame carries mileage; missing:\n%s\nin:\n%s", wantMileageAt, upsertRealtimeLocationSQL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotWriterBackfillsPlateFromBindingByVIN(t *testing.T) {
|
||||
@@ -426,10 +690,7 @@ func TestSnapshotWriterBackfillsPlateFromBindingByVIN(t *testing.T) {
|
||||
EventTimeMS: 1782918600000,
|
||||
ReceivedAtMS: 1782918601000,
|
||||
Parsed: sampleGB32960RealtimeParsed(),
|
||||
Fields: map[string]any{
|
||||
envelope.FieldLatitude: 30.590151,
|
||||
envelope.FieldLongitude: 121.069881,
|
||||
},
|
||||
ParsedFields: sampleGB32960LocationFields(),
|
||||
}); err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
}
|
||||
@@ -481,10 +742,7 @@ func TestSnapshotWriterCachesBindingPlateByVIN(t *testing.T) {
|
||||
EventTimeMS: 1782918600000,
|
||||
ReceivedAtMS: 1782918601000,
|
||||
Parsed: sampleGB32960RealtimeParsed(),
|
||||
Fields: map[string]any{
|
||||
envelope.FieldLatitude: 30.590151,
|
||||
envelope.FieldLongitude: 121.069881,
|
||||
},
|
||||
ParsedFields: sampleGB32960LocationFields(),
|
||||
}
|
||||
|
||||
if err := writer.Update(context.Background(), event); err != nil {
|
||||
@@ -509,6 +767,61 @@ func TestSnapshotWriterCachesBindingPlateByVIN(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachedPlateResolverEvictsOldestEntryWhenLimitExceeded(t *testing.T) {
|
||||
delegate := &mapPlateResolver{plates: map[string]string{
|
||||
"VIN001": "沪A00001",
|
||||
"VIN002": "沪A00002",
|
||||
"VIN003": "沪A00003",
|
||||
}}
|
||||
resolver := NewCachedPlateResolverWithMaxEntries(delegate, time.Hour, 2)
|
||||
now := time.Date(2026, 7, 12, 10, 0, 0, 0, time.UTC)
|
||||
resolver.now = func() time.Time { return now }
|
||||
var observed PlateCacheStats
|
||||
resolver.SetStatsObserver(func(stats PlateCacheStats) {
|
||||
observed = stats
|
||||
})
|
||||
for _, vin := range []string{"VIN001", "VIN002", "VIN003"} {
|
||||
if _, err := resolver.PlateByVIN(context.Background(), vin); err != nil {
|
||||
t.Fatalf("PlateByVIN(%s) error = %v", vin, err)
|
||||
}
|
||||
now = now.Add(time.Second)
|
||||
}
|
||||
|
||||
stats := resolver.CacheStats()
|
||||
if stats.Entries != 2 || stats.MaxEntries != 2 || stats.Evictions != 1 {
|
||||
t.Fatalf("cache stats = %+v, want entries=2 max=2 evictions=1", stats)
|
||||
}
|
||||
if observed != stats {
|
||||
t.Fatalf("observed stats = %+v, want %+v", observed, stats)
|
||||
}
|
||||
if _, ok := resolver.entries["VIN001"]; ok {
|
||||
t.Fatalf("oldest VIN should be evicted")
|
||||
}
|
||||
if _, ok := resolver.entries["VIN002"]; !ok {
|
||||
t.Fatalf("VIN002 should remain cached")
|
||||
}
|
||||
if _, ok := resolver.entries["VIN003"]; !ok {
|
||||
t.Fatalf("VIN003 should remain cached")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachedPlateResolverCanDisableEntryLimit(t *testing.T) {
|
||||
delegate := &mapPlateResolver{plates: map[string]string{
|
||||
"VIN001": "沪A00001",
|
||||
"VIN002": "沪A00002",
|
||||
}}
|
||||
resolver := NewCachedPlateResolverWithMaxEntries(delegate, time.Hour, 0)
|
||||
for _, vin := range []string{"VIN001", "VIN002"} {
|
||||
if _, err := resolver.PlateByVIN(context.Background(), vin); err != nil {
|
||||
t.Fatalf("PlateByVIN(%s) error = %v", vin, err)
|
||||
}
|
||||
}
|
||||
stats := resolver.CacheStats()
|
||||
if stats.Entries != 2 || stats.MaxEntries != 0 || stats.Evictions != 0 {
|
||||
t.Fatalf("cache stats = %+v, want unlimited cache with two entries", stats)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotWriterKeepsEventPlateWhenPresent(t *testing.T) {
|
||||
exec := &recordingSnapshotExec{}
|
||||
resolver := &recordingPlateResolver{plate: "沪B99999"}
|
||||
@@ -521,9 +834,9 @@ func TestSnapshotWriterKeepsEventPlateWhenPresent(t *testing.T) {
|
||||
Plate: "沪A12345",
|
||||
EventTimeMS: 1782918600000,
|
||||
ReceivedAtMS: 1782918601000,
|
||||
Fields: map[string]any{
|
||||
envelope.FieldLatitude: 30.590151,
|
||||
envelope.FieldLongitude: 121.069881,
|
||||
ParsedFields: map[string]any{
|
||||
"jt808.location.latitude": 30.590151,
|
||||
"jt808.location.longitude": 121.069881,
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
@@ -548,7 +861,7 @@ func TestSnapshotWriterIgnoresMissingBindingPlate(t *testing.T) {
|
||||
EventTimeMS: 1782918600000,
|
||||
ReceivedAtMS: 1782918601000,
|
||||
Parsed: sampleGB32960RealtimeParsed(),
|
||||
Fields: map[string]any{envelope.FieldSOCPercent: 90},
|
||||
ParsedFields: map[string]any{"gb32960.vehicle.soc_percent": 90},
|
||||
}); err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
}
|
||||
@@ -568,7 +881,7 @@ func TestSnapshotWriterReturnsUnexpectedPlateLookupError(t *testing.T) {
|
||||
EventTimeMS: 1782918600000,
|
||||
ReceivedAtMS: 1782918601000,
|
||||
Parsed: sampleGB32960RealtimeParsed(),
|
||||
Fields: map[string]any{envelope.FieldSOCPercent: 90},
|
||||
ParsedFields: map[string]any{"gb32960.vehicle.soc_percent": 90},
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), "db down") {
|
||||
t.Fatalf("Update() error = %v, want db down", err)
|
||||
@@ -701,6 +1014,9 @@ func TestSnapshotWriterSkipsMQTTWithoutActualDataFields(t *testing.T) {
|
||||
"topic": "/vehicle/VIN001/state",
|
||||
"data": map[string]any{},
|
||||
},
|
||||
ParsedFields: map[string]any{
|
||||
"yutong_mqtt.metadata.topic": "/vehicle/VIN001/state",
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
}
|
||||
@@ -709,6 +1025,27 @@ func TestSnapshotWriterSkipsMQTTWithoutActualDataFields(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotWriterAcceptsGB32960RetransmissionFields(t *testing.T) {
|
||||
exec := &recordingSnapshotExec{}
|
||||
writer := NewSnapshotWriter(exec)
|
||||
|
||||
if err := writer.Update(context.Background(), envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolGB32960,
|
||||
MessageID: "0x03",
|
||||
VIN: "VIN001",
|
||||
EventTimeMS: 1782918600000,
|
||||
ReceivedAtMS: 1782918601000,
|
||||
ParsedFields: map[string]any{
|
||||
"gb32960.vehicle.soc_percent": 90,
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
}
|
||||
if len(exec.calls) != 1 || !strings.Contains(exec.calls[0].query, "vehicle_realtime_snapshot") {
|
||||
t.Fatalf("GB32960 retransmission should update snapshot, calls=%#v", exec.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotWriterSkipsEmptyDataUnitsWithoutCoreFields(t *testing.T) {
|
||||
exec := &recordingSnapshotExec{}
|
||||
writer := NewSnapshotWriter(exec)
|
||||
@@ -740,6 +1077,14 @@ func sampleGB32960RealtimeParsed() map[string]any {
|
||||
}
|
||||
}
|
||||
|
||||
func sampleGB32960LocationFields() map[string]any {
|
||||
return map[string]any{
|
||||
"gb32960.position.latitude": 30.590151,
|
||||
"gb32960.position.longitude": 121.069881,
|
||||
"gb32960.vehicle.soc_percent": 90,
|
||||
}
|
||||
}
|
||||
|
||||
type snapshotExecCall struct {
|
||||
query string
|
||||
args []any
|
||||
@@ -763,6 +1108,18 @@ func (r *recordingPlateResolver) PlateByVIN(_ context.Context, vin string) (stri
|
||||
return r.plate, r.err
|
||||
}
|
||||
|
||||
type mapPlateResolver struct {
|
||||
plates map[string]string
|
||||
}
|
||||
|
||||
func (r *mapPlateResolver) PlateByVIN(_ context.Context, vin string) (string, error) {
|
||||
plate := strings.TrimSpace(r.plates[strings.TrimSpace(vin)])
|
||||
if plate == "" {
|
||||
return "", sql.ErrNoRows
|
||||
}
|
||||
return plate, nil
|
||||
}
|
||||
|
||||
type jsonFlatFieldsArg struct {
|
||||
fields map[string]string
|
||||
forbidden []string
|
||||
|
||||
Reference in New Issue
Block a user