Skip to content

Commit

Permalink
chore: increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jferrl committed Sep 8, 2022
1 parent 0e37128 commit deb66cd
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 1 deletion.
13 changes: 13 additions & 0 deletions electric_vehicle_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ func TestElectricVehicleStatusService_GetElectricVehicleStatus(t *testing.T) {
want []*ElectricVehicleStatus
wantErr bool
}{
{
name: "nil context error",
fields: fields{
mercedesAPIMock: createFakeServer(http.StatusOK, ""),
},
args: args{
ctx: nil,
opts: &Options{
VehicleID: fakeVehicleID,
},
},
wantErr: true,
},
{
name: "decoding response error",
fields: fields{
Expand Down
13 changes: 13 additions & 0 deletions fuel_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ func TestFuelStatusService_GetFuelStatus(t *testing.T) {
want []*FuelStatus
wantErr bool
}{
{
name: "nil context error",
fields: fields{
mercedesAPIMock: createFakeServer(http.StatusOK, ""),
},
args: args{
ctx: nil,
opts: &Options{
VehicleID: fakeVehicleID,
},
},
wantErr: true,
},
{
name: "decoding response error",
fields: fields{
Expand Down
65 changes: 64 additions & 1 deletion merche_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package merche

import (
"context"
"io"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -18,7 +19,7 @@ func (w *writer) Write(data []byte) (n int, err error) {
return len(data), nil
}

func TestClient_do(t *testing.T) {
func TestClient_Do(t *testing.T) {
type fakeResponse struct{}

type args struct {
Expand Down Expand Up @@ -96,6 +97,68 @@ func TestClient_do(t *testing.T) {
}
}

func TestClient_NewRequest(t *testing.T) {
invalidBaseURL, _ := url.Parse("https://api.mercedes-benz.com")

type fields struct {
BaseURL *url.URL
}
type args struct {
ctx context.Context
method string
path string
body io.Reader
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "invalid base url",
fields: fields{
BaseURL: invalidBaseURL,
},
args: args{
ctx: context.Background(),
method: http.MethodGet,
path: "",
body: nil,
},
wantErr: true,
},
{
name: "invalid method",
fields: fields{
BaseURL: nil,
},
args: args{
ctx: nil,
method: http.MethodGet,
path: "",
body: nil,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(nil)

if tt.fields.BaseURL != nil {
c.BaseURL = tt.fields.BaseURL
}

_, err := c.NewRequest(tt.args.ctx, tt.args.method, tt.args.path, tt.args.body)
if (err != nil) != tt.wantErr {
t.Errorf("Client.NewRequest() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}

func createFakeServer(statusCode int, res string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
Expand Down
13 changes: 13 additions & 0 deletions pay_as_you_drive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ func TestPayAsYouDriveService_GetPayAsYouDriveStatus(t *testing.T) {
want []*PayAsYouDriveStatus
wantErr bool
}{
{
name: "nil context error",
fields: fields{
mercedesAPIMock: createFakeServer(http.StatusOK, ""),
},
args: args{
ctx: nil,
opts: &Options{
VehicleID: fakeVehicleID,
},
},
wantErr: true,
},
{
name: "decoding response error",
fields: fields{
Expand Down
13 changes: 13 additions & 0 deletions resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ func TestVehicleStatusService_GetAvailableResources(t *testing.T) {
want []*ResourceMetaInfo
wantErr bool
}{
{
name: "nil context error",
fields: fields{
mercedesAPIMock: createFakeServer(http.StatusOK, ""),
},
args: args{
ctx: nil,
opts: &Options{
VehicleID: fakeVehicleID,
},
},
wantErr: true,
},
{
name: "decoding response error",
fields: fields{
Expand Down
13 changes: 13 additions & 0 deletions vehicle_lock_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ func TestVehicleLockStatusService_GetVehicleLockStatus(t *testing.T) {
want []*VehicleLockStatus
wantErr bool
}{
{
name: "nil context error",
fields: fields{
mercedesAPIMock: createFakeServer(http.StatusOK, ""),
},
args: args{
ctx: nil,
opts: &Options{
VehicleID: fakeVehicleID,
},
},
wantErr: true,
},
{
name: "decoding response error",
fields: fields{
Expand Down
13 changes: 13 additions & 0 deletions vehicle_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ func TestVehicleStatusService_GetVehicleStatus(t *testing.T) {
want []*VehicleStatus
wantErr bool
}{
{
name: "nil context error",
fields: fields{
mercedesAPIMock: createFakeServer(http.StatusOK, ""),
},
args: args{
ctx: nil,
opts: &Options{
VehicleID: fakeVehicleID,
},
},
wantErr: true,
},
{
name: "decoding response error",
fields: fields{
Expand Down

0 comments on commit deb66cd

Please sign in to comment.