262 lines
8.3 KiB
Go
262 lines
8.3 KiB
Go
package history
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"strings"
|
|
"testing"
|
|
|
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
|
|
)
|
|
|
|
func TestSchemaStatementsCreateCoreStables(t *testing.T) {
|
|
statements := strings.Join(SchemaStatements("test_ts"), "\n")
|
|
for _, want := range []string{"CREATE DATABASE IF NOT EXISTS test_ts", "raw_frames", "vehicle_locations"} {
|
|
if !strings.Contains(statements, want) {
|
|
t.Fatalf("schema missing %q:\n%s", want, statements)
|
|
}
|
|
}
|
|
if strings.Contains(statements, "vehicle_mileage_points") {
|
|
t.Fatalf("schema should not create duplicate mileage stable:\n%s", statements)
|
|
}
|
|
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) {
|
|
exec := &recordingExec{}
|
|
writer := NewWriter(exec)
|
|
env := sampleEnvelope()
|
|
|
|
if err := writer.AppendAll(context.Background(), env); err != nil {
|
|
t.Fatalf("AppendAll() error = %v", err)
|
|
}
|
|
|
|
if got := countSQL(exec.calls, "USING raw_frames"); got != 1 {
|
|
t.Fatalf("raw child create count = %d", got)
|
|
}
|
|
if got := countSQL(exec.calls, "USING vehicle_locations"); got != 1 {
|
|
t.Fatalf("location child create count = %d", got)
|
|
}
|
|
if got := countSQL(exec.calls, "USING vehicle_mileage_points"); got != 0 {
|
|
t.Fatalf("duplicate mileage child create count = %d", got)
|
|
}
|
|
if got := countSQL(exec.calls, "INSERT INTO raw_"); got != 1 {
|
|
t.Fatalf("raw insert count = %d", got)
|
|
}
|
|
if got := countSQL(exec.calls, "INSERT INTO loc_"); got != 1 {
|
|
t.Fatalf("location insert count = %d", got)
|
|
}
|
|
if got := countSQL(exec.calls, "INSERT INTO mil_"); got != 0 {
|
|
t.Fatalf("duplicate mileage insert count = %d", got)
|
|
}
|
|
rawInsert := findSQL(exec.calls, "INSERT INTO raw_")
|
|
if !strings.Contains(rawInsert, "'go_") || !strings.Contains(rawInsert, "'2e") && !strings.Contains(rawInsert, "'") {
|
|
t.Fatalf("raw insert should quote string literals: %s", rawInsert)
|
|
}
|
|
if strings.Contains(rawInsert, "VALUES (?,") {
|
|
t.Fatalf("raw insert should not use placeholders for tdengine websocket plain insert: %s", rawInsert)
|
|
}
|
|
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) {
|
|
exec := &recordingExec{}
|
|
writer := NewWriter(exec)
|
|
env := sampleEnvelope()
|
|
|
|
if err := writer.AppendRawFrame(context.Background(), env); err != nil {
|
|
t.Fatalf("AppendRawFrame() error = %v", err)
|
|
}
|
|
|
|
createChild := findSQL(exec.calls, "USING raw_frames")
|
|
if !strings.Contains(createChild, "'13307795425'") {
|
|
t.Fatalf("child table phone tag should be normalized: %s", createChild)
|
|
}
|
|
if strings.Contains(createChild, "'013307795425'") {
|
|
t.Fatalf("child table phone tag should not keep leading zero: %s", createChild)
|
|
}
|
|
refreshTag := findSQL(exec.calls, "SET TAG phone")
|
|
if !strings.Contains(refreshTag, "'13307795425'") {
|
|
t.Fatalf("phone tag refresh should use normalized phone: %s", refreshTag)
|
|
}
|
|
}
|
|
|
|
func TestWriterChunksOversizedParsedJSON(t *testing.T) {
|
|
exec := &recordingExec{}
|
|
writer := NewWriter(exec)
|
|
env := sampleEnvelope()
|
|
env.Parsed = map[string]any{
|
|
"large_payload": strings.Repeat("x", 16_128),
|
|
}
|
|
|
|
if err := writer.AppendRawFrame(context.Background(), env); err != nil {
|
|
t.Fatalf("AppendRawFrame() error = %v", err)
|
|
}
|
|
|
|
rawInsert := findSQL(exec.calls, "INSERT INTO raw_")
|
|
if !strings.Contains(rawInsert, `"chunked":true`) || !strings.Contains(rawInsert, `"payload_kind":"parsed_json"`) {
|
|
t.Fatalf("raw insert should store a parsed_json chunk manifest: %s", rawInsert)
|
|
}
|
|
if got := countSQL(exec.calls, "USING raw_frame_payload_chunks"); got != 1 {
|
|
t.Fatalf("chunk child create count = %d", got)
|
|
}
|
|
if got := countSQL(exec.calls, "INSERT INTO chunk_"); got < 2 {
|
|
t.Fatalf("chunk insert count = %d, calls=%v", got, exec.calls)
|
|
}
|
|
}
|
|
|
|
func TestWriterSkipsSparseDerivedRows(t *testing.T) {
|
|
exec := &recordingExec{}
|
|
writer := NewWriter(exec)
|
|
env := sampleEnvelope()
|
|
env.Fields = map[string]any{}
|
|
|
|
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, "INSERT INTO loc_"); got != 0 {
|
|
t.Fatalf("location insert count = %d", got)
|
|
}
|
|
if got := countSQL(exec.calls, "INSERT INTO mil_"); got != 0 {
|
|
t.Fatalf("mileage insert count = %d", got)
|
|
}
|
|
}
|
|
|
|
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 {
|
|
t.Fatalf("raw text size = %d, want %d", got, want)
|
|
}
|
|
|
|
env.RawHex = "7E0200"
|
|
if got, want := rawSizeBytes(env), 3; got != want {
|
|
t.Fatalf("raw hex size = %d, want %d", got, want)
|
|
}
|
|
}
|
|
|
|
func TestSchemaStatementsCreateRawFramePayloadChunks(t *testing.T) {
|
|
statements := strings.Join(SchemaStatements("test_ts"), "\n")
|
|
for _, want := range []string{"raw_frame_payload_chunks", "payload_kind NCHAR(32)", "chunk_index INT", "chunk_text BINARY(16000)"} {
|
|
if !strings.Contains(statements, want) {
|
|
t.Fatalf("schema missing %q:\n%s", want, statements)
|
|
}
|
|
}
|
|
}
|
|
|
|
func sampleEnvelope() envelope.FrameEnvelope {
|
|
return envelope.FrameEnvelope{
|
|
Protocol: envelope.ProtocolJT808,
|
|
MessageID: "0x0200",
|
|
Sequence: 1,
|
|
VIN: "LNBVIN00000000001",
|
|
Phone: "013307795425",
|
|
SourceEndpoint: "127.0.0.1:18080",
|
|
EventTimeMS: 1782745114000,
|
|
ReceivedAtMS: 1782745114999,
|
|
RawHex: "7E0200",
|
|
Parsed: map[string]any{"message": "location"},
|
|
Fields: map[string]any{
|
|
envelope.FieldLongitude: 121.069881,
|
|
envelope.FieldLatitude: 30.590151,
|
|
envelope.FieldSpeedKMH: 23.0,
|
|
envelope.FieldTotalMileageKM: 10241.2,
|
|
"direction_deg": uint16(79),
|
|
"alarm_flag": uint32(0),
|
|
"status_flag": uint32(72),
|
|
},
|
|
ParseStatus: envelope.ParseOK,
|
|
}
|
|
}
|
|
|
|
func countSQL(calls []execCall, pattern string) int {
|
|
var count int
|
|
for _, call := range calls {
|
|
if strings.Contains(call.query, pattern) {
|
|
count++
|
|
}
|
|
}
|
|
return count
|
|
}
|
|
|
|
func findSQL(calls []execCall, pattern string) string {
|
|
for _, call := range calls {
|
|
if strings.Contains(call.query, pattern) {
|
|
return call.query
|
|
}
|
|
}
|
|
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
|
|
}
|
|
|
|
type recordingExec struct {
|
|
calls []execCall
|
|
}
|
|
|
|
func (e *recordingExec) ExecContext(_ context.Context, query string, args ...any) (sql.Result, error) {
|
|
e.calls = append(e.calls, execCall{query: query, args: args})
|
|
return nil, nil
|
|
}
|