feat: cache protocol realtime raw
This commit is contained in:
@@ -66,6 +66,69 @@ func TestRepositoryUpdatesMergedAndProtocolSnapshots(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepositoryStoresFullParsedProtocolSnapshotAndMergesGB32960Units(t *testing.T) {
|
||||
repo, closeFn := newTestRepository(t)
|
||||
defer closeFn()
|
||||
ctx := context.Background()
|
||||
|
||||
if err := repo.Update(ctx, envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolGB32960,
|
||||
VIN: "VIN001",
|
||||
EventTimeMS: 1000,
|
||||
ReceivedAtMS: 1100,
|
||||
Parsed: map[string]any{
|
||||
"header": map[string]any{"command": "0x02"},
|
||||
"data_units": []any{
|
||||
map[string]any{"type": "0x01", "name": "vehicle", "value": map[string]any{"soc_percent": 88.0}},
|
||||
},
|
||||
},
|
||||
Fields: map[string]any{envelope.FieldSOCPercent: 88.0},
|
||||
}); err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
}
|
||||
if err := repo.Update(ctx, envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolGB32960,
|
||||
VIN: "VIN001",
|
||||
EventTimeMS: 1100,
|
||||
ReceivedAtMS: 1200,
|
||||
Parsed: map[string]any{
|
||||
"data_units": []any{
|
||||
map[string]any{"type": "0x05", "name": "position", "value": map[string]any{"longitude": 121.1, "latitude": 30.2}},
|
||||
},
|
||||
},
|
||||
Fields: map[string]any{envelope.FieldLongitude: 121.1, envelope.FieldLatitude: 30.2},
|
||||
}); err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
}
|
||||
|
||||
protocol, err := repo.GetProtocol(ctx, "VIN001", envelope.ProtocolGB32960)
|
||||
if err != nil {
|
||||
t.Fatalf("GetProtocol() error = %v", err)
|
||||
}
|
||||
realtimeRaw, err := repo.GetRealtimeRaw(ctx, "VIN001", envelope.ProtocolGB32960)
|
||||
if err != nil {
|
||||
t.Fatalf("GetRealtimeRaw() error = %v", err)
|
||||
}
|
||||
units := protocol.Parsed["data_units"].([]any)
|
||||
if len(units) != 2 {
|
||||
t.Fatalf("expected merged gb32960 units, got %#v", protocol.Parsed)
|
||||
}
|
||||
if len(realtimeRaw["data_units"].([]any)) != 2 {
|
||||
t.Fatalf("realtime-raw did not keep merged parsed fields: %#v", realtimeRaw)
|
||||
}
|
||||
merged, err := repo.GetMerged(ctx, "VIN001")
|
||||
if err != nil {
|
||||
t.Fatalf("GetMerged() error = %v", err)
|
||||
}
|
||||
gb, ok := merged.ProtocolData[envelope.ProtocolGB32960]
|
||||
if !ok {
|
||||
t.Fatalf("merged protocol data missing: %#v", merged.ProtocolData)
|
||||
}
|
||||
if len(gb["data_units"].([]any)) != 2 {
|
||||
t.Fatalf("merged protocol data did not keep full parsed fields: %#v", gb)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepositoryOnlineStatus(t *testing.T) {
|
||||
repo, closeFn := newTestRepository(t)
|
||||
defer closeFn()
|
||||
@@ -104,7 +167,7 @@ func TestRepositoryUpdatesPhoneOnlyVehicleKey(t *testing.T) {
|
||||
|
||||
if err := repo.Update(ctx, envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolJT808,
|
||||
Phone: "013307811170",
|
||||
Phone: "13307811170",
|
||||
EventTimeMS: 1000,
|
||||
ReceivedAtMS: 1100,
|
||||
Fields: map[string]any{
|
||||
@@ -115,7 +178,7 @@ func TestRepositoryUpdatesPhoneOnlyVehicleKey(t *testing.T) {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
}
|
||||
|
||||
vehicleKey := "JT808:013307811170"
|
||||
vehicleKey := "JT808:13307811170"
|
||||
merged, err := repo.GetMerged(ctx, vehicleKey)
|
||||
if err != nil {
|
||||
t.Fatalf("GetMerged() error = %v", err)
|
||||
@@ -205,7 +268,7 @@ func TestHandlerReturnsPhoneOnlyVehicleKeySnapshot(t *testing.T) {
|
||||
defer closeFn()
|
||||
if err := repo.Update(context.Background(), envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolJT808,
|
||||
Phone: "013307811170",
|
||||
Phone: "13307811170",
|
||||
EventTimeMS: 1000,
|
||||
ReceivedAtMS: 1100,
|
||||
Fields: map[string]any{envelope.FieldSpeedKMH: 12.3},
|
||||
@@ -213,13 +276,41 @@ func TestHandlerReturnsPhoneOnlyVehicleKeySnapshot(t *testing.T) {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/realtime/vehicles/JT808:013307811170", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/realtime/vehicles/JT808:13307811170", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
NewHandler(repo).ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !stringsContains(rec.Body.String(), `"vehicle_key":"JT808:013307811170"`) {
|
||||
if !stringsContains(rec.Body.String(), `"vehicle_key":"JT808:13307811170"`) {
|
||||
t.Fatalf("unexpected body: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerReturnsRealtimeRaw(t *testing.T) {
|
||||
repo, closeFn := newTestRepository(t)
|
||||
defer closeFn()
|
||||
if err := repo.Update(context.Background(), envelope.FrameEnvelope{
|
||||
Protocol: envelope.ProtocolGB32960,
|
||||
VIN: "VIN001",
|
||||
EventTimeMS: 1000,
|
||||
ReceivedAtMS: 1100,
|
||||
Parsed: map[string]any{
|
||||
"data_units": []any{
|
||||
map[string]any{"type": "0x01", "name": "vehicle", "value": map[string]any{"soc_percent": 90.0}},
|
||||
},
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("Update() error = %v", err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/realtime/vehicles/VIN001/realtime-raw/GB32960", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
NewHandler(repo).ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !stringsContains(rec.Body.String(), `"soc_percent":90`) {
|
||||
t.Fatalf("unexpected body: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user