144 lines
4.6 KiB
Go
144 lines
4.6 KiB
Go
package handlers_test
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"otaupdate/handlers"
|
|
"otaupdate/services"
|
|
|
|
m "github.com/fiskerinc/cloud-services/pkg/common"
|
|
orm "github.com/fiskerinc/cloud-services/pkg/db/queries"
|
|
mo "github.com/fiskerinc/cloud-services/pkg/db/queries/mocks"
|
|
th "github.com/fiskerinc/cloud-services/pkg/testhelper"
|
|
)
|
|
|
|
func TestCarUpdatesGet(t *testing.T) {
|
|
mock := mo.MockCarUpdates{}
|
|
services.GetDB().SetCarUpdates(&mock)
|
|
expectedCarUpdate := `{"id":1,"vin":"1G1FP87S3GN100062","manifest_id":1,"updatemanifest":{"name":"TEST","version":"1.1","description":"description","release_notes":"http://releasenotes.com","rollback":false,"type":"forced","country":"US","powertrain":"MD23","restraint":"None","model":"Ocean","trim":"Sport","year":2022,"body_type":"truck"},"UpdateSource":"OTA"}`
|
|
expectedResp := fmt.Sprintf(`{"data":[%s],"total":1}`, expectedCarUpdate)
|
|
expectedRespNoTotal := fmt.Sprintf(`{"data":[%s]}`, expectedCarUpdate)
|
|
defaultOrder := "id DESC"
|
|
listData := []m.CarUpdate{
|
|
{
|
|
ID: 1,
|
|
VIN: "1G1FP87S3GN100062",
|
|
UpdateManifestID: 1,
|
|
UpdateManifest: &m.UpdateManifest{
|
|
Name: "TEST",
|
|
Description: "description",
|
|
Version: "1.1",
|
|
ReleaseNotes: "http://releasenotes.com",
|
|
RollbackEnabled: false,
|
|
Type: "forced",
|
|
Country: "US",
|
|
PowerTrain: "MD23",
|
|
Restraint: "None",
|
|
Model: "Ocean",
|
|
Trim: "Sport",
|
|
Year: 2022,
|
|
BodyType: "truck",
|
|
},
|
|
UpdateSource: "OTA",
|
|
},
|
|
}
|
|
tests := []mo.DBHttpTest{
|
|
{
|
|
Name: "No parameters",
|
|
Request: th.MakeTestRequest(http.MethodGet, "http://example.com/carupdates", nil),
|
|
ExpectedStatus: http.StatusOK,
|
|
ExpectedResponse: expectedResp,
|
|
DBTestCase: mo.DBTestCase{
|
|
ExpectedFilter: m.CarUpdate{},
|
|
ExpectedPage: &orm.PageQueryOptions{
|
|
Order: defaultOrder,
|
|
Limit: orm.PageQueryOptionsLimitMaximum,
|
|
Offset: 0,
|
|
},
|
|
MockListResponse: listData,
|
|
},
|
|
},
|
|
{
|
|
Name: "Id parameter",
|
|
Request: th.MakeTestRequest(http.MethodGet, "http://example.com/carupdates?id=1", nil),
|
|
ExpectedStatus: http.StatusOK,
|
|
ExpectedResponse: expectedRespNoTotal,
|
|
DBTestCase: mo.DBTestCase{
|
|
ExpectedFilter: m.CarUpdate{
|
|
ID: 1,
|
|
},
|
|
ExpectedPage: &orm.PageQueryOptions{
|
|
Order: defaultOrder,
|
|
Limit: orm.PageQueryOptionsLimitMaximum,
|
|
Offset: 0,
|
|
},
|
|
MockListResponse: listData,
|
|
},
|
|
},
|
|
{
|
|
Name: "VIN and UpdateManifestID parameters",
|
|
Request: th.MakeTestRequest(http.MethodGet, "http://example.com/carupdates?vin=1G1FP87S3GN100062&manifest_id=1", nil),
|
|
ExpectedStatus: http.StatusOK,
|
|
ExpectedResponse: expectedResp,
|
|
DBTestCase: mo.DBTestCase{
|
|
ExpectedFilter: m.CarUpdate{
|
|
VIN: "1G1FP87S3GN100062",
|
|
UpdateManifestID: 1,
|
|
},
|
|
ExpectedPage: &orm.PageQueryOptions{
|
|
Order: defaultOrder,
|
|
Limit: orm.PageQueryOptionsLimitMaximum,
|
|
Offset: 0,
|
|
},
|
|
MockListResponse: listData,
|
|
},
|
|
},
|
|
{
|
|
Name: "Paging parameters",
|
|
Request: th.MakeTestRequest(http.MethodGet, "http://example.com/carupdates?offset=10&limit=5", nil),
|
|
ExpectedStatus: http.StatusOK,
|
|
ExpectedResponse: expectedRespNoTotal,
|
|
DBTestCase: mo.DBTestCase{
|
|
ExpectedFilter: m.CarUpdate{},
|
|
ExpectedPage: &orm.PageQueryOptions{
|
|
Order: defaultOrder,
|
|
Limit: 5,
|
|
Offset: 10,
|
|
},
|
|
MockListResponse: listData,
|
|
},
|
|
},
|
|
{
|
|
Name: "Error",
|
|
Request: th.MakeTestRequest(http.MethodGet, "http://example.com/carupdates", nil),
|
|
ExpectedStatus: http.StatusServiceUnavailable,
|
|
ExpectedResponse: `{"message":"something went wrong","error":"Service Unavailable"}`,
|
|
DBTestCase: mo.DBTestCase{
|
|
ExpectedFilter: m.CarUpdate{},
|
|
ExpectedPage: &orm.PageQueryOptions{
|
|
Order: defaultOrder,
|
|
Limit: orm.PageQueryOptionsLimitMaximum,
|
|
Offset: 0,
|
|
},
|
|
MockError: fmt.Errorf("something went wrong"),
|
|
},
|
|
},
|
|
{
|
|
Name: "Wrong limit, -100",
|
|
Request: th.MakeTestRequest(http.MethodGet, "http://example.com/carupdates?limit=-100", nil),
|
|
ExpectedStatus: http.StatusBadRequest,
|
|
ExpectedResponse: `{"message":"Limit less than 0","error":"Bad Request"}`,
|
|
},
|
|
{
|
|
Name: "Wrong limit, 1000",
|
|
Request: th.MakeTestRequest(http.MethodGet, "http://example.com/carupdates?limit=1000", nil),
|
|
ExpectedStatus: http.StatusBadRequest,
|
|
ExpectedResponse: `{"message":"Limit greater than 100","error":"Bad Request"}`,
|
|
},
|
|
}
|
|
|
|
mo.RunDBTests(t, tests, handlers.HandleCarUpdatesGet, &mock)
|
|
}
|