Skip to content

Commit

Permalink
Add mock to test go-experimental interface
Browse files Browse the repository at this point in the history
  • Loading branch information
arvindth committed Aug 23, 2020
1 parent fdc4599 commit 9d6ca29
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
89 changes: 89 additions & 0 deletions samples/client/petstore/go-experimental/mock/mock_api_pet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package mock

import (
"context"
"net/http"

sw "../go-petstore"
)

// MockPetApi is a mock of the PetApi interface
type MockPetApi struct {
}

// NewMockPetApi creates a new mock instance
func NewMockPetApi() *MockPetApi {
return &MockPetApi{}
}

func (m *MockPetApi) AddPet(ctx context.Context) sw.ApiAddPetRequest {
return sw.ApiAddPetRequest{ApiService: m}
}

func (m *MockPetApi) AddPetExecute(r sw.ApiAddPetRequest) (*http.Response, error) {
return &http.Response{StatusCode:200}, nil
}

func (m *MockPetApi) DeletePet(ctx context.Context, petId int64) sw.ApiDeletePetRequest {
return sw.ApiDeletePetRequest{ApiService: m}
}

func (m *MockPetApi) DeletePetExecute(r sw.ApiDeletePetRequest) (*http.Response, error) {
return &http.Response{StatusCode:200}, nil
}

func (m *MockPetApi) FindPetsByStatus(ctx context.Context) sw.ApiFindPetsByStatusRequest {
return sw.ApiFindPetsByStatusRequest{ApiService: m}
}

func (m *MockPetApi) FindPetsByStatusExecute(r sw.ApiFindPetsByStatusRequest) ([]sw.Pet, *http.Response, error) {
return []sw.Pet{}, &http.Response{StatusCode:200}, nil
}

func (m *MockPetApi) FindPetsByTags(ctx context.Context) sw.ApiFindPetsByTagsRequest {
return sw.ApiFindPetsByTagsRequest{ApiService: m}
}

func (m *MockPetApi) FindPetsByTagsExecute(r sw.ApiFindPetsByTagsRequest) ([]sw.Pet, *http.Response, error) {
return []sw.Pet{}, &http.Response{StatusCode:200}, nil
}

func (m *MockPetApi) GetPetById(ctx context.Context, petId int64) sw.ApiGetPetByIdRequest {
return sw.ApiGetPetByIdRequest{ApiService: m}
}

func (m *MockPetApi) GetPetByIdExecute(r sw.ApiGetPetByIdRequest) (sw.Pet, *http.Response, error) {
return sw.Pet{}, &http.Response{StatusCode:200}, nil
}

func (m *MockPetApi) UpdatePet(ctx context.Context) sw.ApiUpdatePetRequest {
return sw.ApiUpdatePetRequest{ApiService: m}
}

func (m *MockPetApi) UpdatePetExecute(r sw.ApiUpdatePetRequest) (*http.Response, error) {
return &http.Response{StatusCode:200}, nil
}

func (m *MockPetApi) UpdatePetWithForm(ctx context.Context, petId int64) sw.ApiUpdatePetWithFormRequest {
return sw.ApiUpdatePetWithFormRequest{ApiService: m}
}

func (m *MockPetApi) UpdatePetWithFormExecute(r sw.ApiUpdatePetWithFormRequest) (*http.Response, error) {
return &http.Response{StatusCode:200}, nil
}

func (m *MockPetApi) UploadFile(ctx context.Context, petId int64) sw.ApiUploadFileRequest {
return sw.ApiUploadFileRequest{ApiService: m}
}

func (m *MockPetApi) UploadFileExecute(r sw.ApiUploadFileRequest) (sw.ApiResponse, *http.Response, error) {
return sw.ApiResponse{}, &http.Response{StatusCode:200}, nil
}

func (m *MockPetApi) UploadFileWithRequiredFile(ctx context.Context, petId int64) sw.ApiUploadFileWithRequiredFileRequest {
return sw.ApiUploadFileWithRequiredFileRequest{ApiService: m}
}

func (m *MockPetApi) UploadFileWithRequiredFileExecute(r sw.ApiUploadFileWithRequiredFileRequest) (sw.ApiResponse, *http.Response, error) {
return sw.ApiResponse{}, &http.Response{StatusCode:200}, nil
}
10 changes: 10 additions & 0 deletions samples/client/petstore/go-experimental/pet_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"

sw "./go-petstore"
mock "./mock"
)

var client *sw.APIClient
Expand Down Expand Up @@ -41,6 +42,15 @@ func TestAddPet(t *testing.T) {
}
}

func TestAddPetMock(t *testing.T) {
actualApi := client.PetApi

mockApi := mock.NewMockPetApi()
client.PetApi = mockApi
TestAddPet(t)
client.PetApi = actualApi
}

func TestFindPetsByStatusWithMissingParam(t *testing.T) {
_, r, err := client.PetApi.FindPetsByStatus(context.Background()).Status(nil).Execute()

Expand Down

0 comments on commit 9d6ca29

Please sign in to comment.