77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package handlers_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"otaupdate/handlers"
|
|
"otaupdate/services"
|
|
"testing"
|
|
|
|
"github.com/fiskerinc/cloud-services/pkg/clickhouse"
|
|
"github.com/julienschmidt/httprouter"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
var chExpectedResult = []handlers.ChGpsRow{
|
|
{"1G1FP87S1GN000445", 33.901499, -118.379155},
|
|
{"1G1FP87S1GN000445", 33.101599, -118.189155},
|
|
{"1G1FP87S1GN000446", 50.231084, 11.344868},
|
|
{"1G1FP87S1GN000446", 50.141084, 11.145868},
|
|
}
|
|
|
|
var expectedPathBody string = "" +
|
|
"{" +
|
|
`"1G1FP87S1GN000445":[[33.901499,-118.379155],[33.101599,-118.189155]],` +
|
|
`"1G1FP87S1GN000446":[[50.231084,11.344868],[50.141084,11.145868]]` +
|
|
"}"
|
|
|
|
func TestHandleVehiclePathPost(t *testing.T) {
|
|
validQuery := "?lookback_hours=24"
|
|
validBody := "{\"vins\":[\"1G1FP87S1GN000445\", \"1G1FP87S1GN000446\"]}"
|
|
validVins := `["1G1FP87S1GN000445","1G1FP87S1GN000446"]`
|
|
tests := map[string]struct {
|
|
q string
|
|
b string
|
|
conn clickhouse.ConnInterface
|
|
expStatus int
|
|
expBody string
|
|
}{
|
|
"correct": {
|
|
q: validQuery,
|
|
b: validBody,
|
|
conn: &clickhouse.MockConn{ExpectedResult: chExpectedResult},
|
|
expStatus: http.StatusOK,
|
|
expBody: expectedPathBody,
|
|
},
|
|
"missing_vins": {
|
|
q: "",
|
|
b: "",
|
|
expStatus: http.StatusBadRequest,
|
|
expBody: `{"message":"VINs required","error":"Bad Request"}`,
|
|
},
|
|
}
|
|
|
|
for tname, tt := range tests {
|
|
t.Run(tname, func(t *testing.T) {
|
|
services.SetClickhouseConn(tt.conn)
|
|
w := httptest.NewRecorder()
|
|
|
|
p := httprouter.Params{
|
|
{Key: "vins", Value: validVins},
|
|
}
|
|
ctx := context.WithValue(context.Background(), httprouter.ParamsKey, p)
|
|
|
|
reader := bytes.NewReader([]byte(tt.b))
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "http://example.com/ota_update/vehicle_paths"+tt.q, reader).
|
|
WithContext(ctx)
|
|
|
|
handlers.HandleVehiclePathsPost(w, r)
|
|
assert.Equal(t, tt.expStatus, w.Code)
|
|
assert.Equal(t, tt.expBody, w.Body.String())
|
|
})
|
|
}
|
|
}
|