refactor(go): simplify history location identity

This commit is contained in:
lingniu
2026-07-02 21:28:29 +08:00
parent ef2be36fce
commit e27af63025
7 changed files with 161 additions and 112 deletions

View File

@@ -22,6 +22,12 @@ func TestSchemaStatementsCreateCoreStables(t *testing.T) {
if strings.Contains(statements, "fields_json") {
t.Fatalf("raw schema should not create duplicate fields_json column:\n%s", statements)
}
locationStable := between(statements, "CREATE STABLE IF NOT EXISTS vehicle_locations", "}")
for _, field := range []string{"vehicle_key", "phone", "device_id"} {
if strings.Contains(locationStable, field) {
t.Fatalf("location stable should only keep protocol/vin identity tags, found %s:\n%s", field, locationStable)
}
}
}
func TestWriterAppendsRawAndLocationOnly(t *testing.T) {
@@ -61,6 +67,17 @@ func TestWriterAppendsRawAndLocationOnly(t *testing.T) {
if strings.Contains(rawInsert, "fields_json") {
t.Fatalf("raw insert should not write duplicate fields_json: %s", rawInsert)
}
locationChild := findSQL(exec.calls, "USING vehicle_locations")
for _, want := range []string{"'JT808'", "'LNBVIN00000000001'"} {
if !strings.Contains(locationChild, want) {
t.Fatalf("location child should tag by protocol and vin only: %s", locationChild)
}
}
for _, field := range []string{"'JT808:013307795425'", "'13307795425'", "device_id"} {
if strings.Contains(locationChild, field) {
t.Fatalf("location child should not carry raw identity fallback tag %s: %s", field, locationChild)
}
}
}
func TestWriterNormalizesPhoneTagAndRefreshesExistingChildTags(t *testing.T) {
@@ -130,6 +147,27 @@ func TestWriterSkipsSparseDerivedRows(t *testing.T) {
}
}
func TestWriterSkipsLocationWhenVINIsMissing(t *testing.T) {
exec := &recordingExec{}
writer := NewWriter(exec)
env := sampleEnvelope()
env.VIN = ""
if err := writer.AppendAll(context.Background(), env); err != nil {
t.Fatalf("AppendAll() error = %v", err)
}
if got := countSQL(exec.calls, "INSERT INTO raw_"); got != 1 {
t.Fatalf("raw insert count = %d", got)
}
if got := countSQL(exec.calls, "USING vehicle_locations"); got != 0 {
t.Fatalf("location child create count = %d", got)
}
if got := countSQL(exec.calls, "INSERT INTO loc_"); got != 0 {
t.Fatalf("location insert count = %d", got)
}
}
func TestRawSizeBytesUsesRawTextWhenHexIsEmpty(t *testing.T) {
env := envelope.FrameEnvelope{RawText: `{"code":"0F80","data":{"speed":12}}`}
if got, want := rawSizeBytes(env), len([]byte(env.RawText)); got != want {
@@ -195,6 +233,19 @@ func findSQL(calls []execCall, pattern string) string {
return ""
}
func between(value string, start string, end string) string {
startIndex := strings.Index(value, start)
if startIndex < 0 {
return ""
}
value = value[startIndex:]
endIndex := strings.Index(value, end)
if endIndex < 0 {
return value
}
return value[:endIndex]
}
type execCall struct {
query string
args []any