Skip to content

Commit

Permalink
test: add router unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoclair committed Jan 10, 2024
1 parent 56ec6be commit 7314713
Showing 1 changed file with 86 additions and 2 deletions.
88 changes: 86 additions & 2 deletions application/rest/routes/accountroute/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type mock struct {
}

func getServerTest(t *testing.T) (accountMock mock, server *echo.Echo, ctrl *gomock.Controller) {

ctrl = gomock.NewController(t)
accountMock = mock{
mapper: mapper.New(),
Expand All @@ -40,6 +39,7 @@ func getServerTest(t *testing.T) (accountMock mock, server *echo.Echo, ctrl *gom
server = echo.New()
appGroup := server.Group("/")
transferRoute.RegisterRoutes(appGroup, nil)

return
}

Expand Down Expand Up @@ -202,15 +202,16 @@ func TestController_handleAddAccount(t *testing.T) {
func buildAccoununtByID(id int) entity.Account {
return entity.Account{UUID: "random", Name: "diego" + strconv.Itoa(id)}
}

func buildAccountsByQuantity(qtd int) (accounts []entity.Account) {
for i := 0; i < qtd; i++ {
accounts = append(accounts, buildAccoununtByID(i))
}

return
}

func TestController_GetAccounts(t *testing.T) {

type args struct {
page int
quantity int
Expand Down Expand Up @@ -288,3 +289,86 @@ func TestController_GetAccounts(t *testing.T) {
})
}
}

func TestController_GetAccountByID(t *testing.T) {
type args struct {
accountUUID string
}

tests := []struct {
name string
args args
buildMocks func(ctx context.Context, mocks mock, args args)
checkResponse func(t *testing.T, recorder *httptest.ResponseRecorder, mock mock, args args)
}{
{
name: "Should complete request with no error",
args: args{
accountUUID: "random",
},
buildMocks: func(ctx context.Context, mock mock, args args) {
account := buildAccoununtByID(1)
mock.accountService.EXPECT().GetAccountByUUID(ctx, args.accountUUID).Times(1).Return(account, nil)
},
checkResponse: func(t *testing.T, resp *httptest.ResponseRecorder, mock mock, args args) {
require.Equal(t, http.StatusOK, resp.Code)
account := buildAccoununtByID(1)

response := viewmodel.Account{}
err := mock.mapper.From(account).To(&response)
require.NoError(t, err)

expectedResp, err := json.Marshal(response)
require.NoError(t, err)
require.Contains(t, resp.Body.String(), string(expectedResp))
},
},
{
name: "Should return error if we have some error with service",
args: args{
accountUUID: "random",
},
buildMocks: func(ctx context.Context, mock mock, args args) {
mock.accountService.EXPECT().GetAccountByUUID(ctx, args.accountUUID).Times(1).Return(entity.Account{}, fmt.Errorf("some service error"))
},
checkResponse: func(t *testing.T, resp *httptest.ResponseRecorder, mock mock, args args) {
require.Equal(t, http.StatusServiceUnavailable, resp.Code)
require.Contains(t, resp.Body.String(), "some service error")
},
},
{
name: "Should return error if we have an invalid uuid",
args: args{accountUUID: " "},
checkResponse: func(t *testing.T, resp *httptest.ResponseRecorder, mock mock, args args) {
require.Equal(t, http.StatusUnprocessableEntity, resp.Code)
require.Contains(t, resp.Body.String(), "Invalid account_uuid")
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

accountMock, server, ctrl := getServerTest(t)
defer ctrl.Finish()

recorder := httptest.NewRecorder()
url := fmt.Sprintf("/%s/%s", RouteName, tt.args.accountUUID)

req, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)

if tt.buildMocks != nil {
e := echo.New()
ctx := routeutils.GetContext(e.NewContext(req, recorder))
tt.buildMocks(ctx, accountMock, tt.args)
}

req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
server.ServeHTTP(recorder, req)
if tt.checkResponse != nil {
tt.checkResponse(t, recorder, accountMock, tt.args)
}
})
}
}

0 comments on commit 7314713

Please sign in to comment.