Skip to content

Commit

Permalink
test: for get all teams handler
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrMatsko committed Nov 19, 2024
1 parent 86b6314 commit 78c35c5
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/controller/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func GetTeam(dataBase moira.Database, teamID string) (dto.TeamModel, *api.ErrorR
func GetAllTeams(dataBase moira.Database, page, size int64, textRegexp *regexp.Regexp, sortOrder api.SortOrder) (dto.TeamsList, *api.ErrorResponse) {
teams, err := dataBase.GetAllTeams()
if err != nil {
return dto.TeamsList{}, api.ErrorInternalServer(fmt.Errorf("cannot get teams fron database: %w", err))
return dto.TeamsList{}, api.ErrorInternalServer(fmt.Errorf("cannot get teams from database: %w", err))
}

filteredTeams := make([]moira.Team, 0)
Expand Down
2 changes: 1 addition & 1 deletion api/controller/team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func TestGetAllTeams(t *testing.T) {

response, err := GetAllTeams(dataBase, firstPage, allTeamsSize, anyText, api.NoSortOrder)

So(err, ShouldResemble, api.ErrorInternalServer(fmt.Errorf("cannot get teams fron database: %w", dbErr)))
So(err, ShouldResemble, api.ErrorInternalServer(fmt.Errorf("cannot get teams from database: %w", dbErr)))
So(response, ShouldResemble, dto.TeamsList{})
})

Expand Down
139 changes: 139 additions & 0 deletions api/handler/team_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package handler

import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/moira-alert/moira"

Check failure on line 8 in api/handler/team_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
"github.com/moira-alert/moira/api"
"github.com/moira-alert/moira/api/dto"
"github.com/moira-alert/moira/api/middleware"
mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert"
"io"
"net/http"
"net/http/httptest"
"regexp"
"strconv"
"testing"

Check failure on line 18 in api/handler/team_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

Check failure on line 19 in api/handler/team_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
. "github.com/smartystreets/goconvey/convey"
"go.uber.org/mock/gomock"
)

func fillContextForGetAllTeams(ctx context.Context, testPage, testSize int64, searchText *regexp.Regexp, sort api.SortOrder) context.Context {
ctx = middleware.SetContextValueForTest(ctx, "page", testPage)
ctx = middleware.SetContextValueForTest(ctx, "size", testSize)
ctx = middleware.SetContextValueForTest(ctx, "searchText", searchText)
ctx = middleware.SetContextValueForTest(ctx, "sort", sort)

return ctx
}

func TestGetAllTeams(t *testing.T) {
Convey("Test getting all teams", t, func() {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
responseWriter := httptest.NewRecorder()
mockDb := mock_moira_alert.NewMockDatabase(mockCtrl)
database = mockDb

var (
defaultTestPage int64 = getAllTeamsDefaultPage
defaultTestSize int64 = getAllTeamsDefaultSize
defaultTestSearchText = regexp.MustCompile(getAllTeamsDefaultRegexTemplate)
defaultTestSortOrder = api.NoSortOrder
)

testTeamsCount := 7
testTeams := make([]moira.Team, 0, testTeamsCount)
for i := 0; i < testTeamsCount; i++ {
iStr := strconv.FormatInt(int64(i), 10)

testTeams = append(testTeams, moira.Team{
ID: "team-" + iStr,
Name: "Test team " + iStr,
})
}

Convey("when everything ok returns ok", func() {
mockDb.EXPECT().GetAllTeams().Return(testTeams, nil)

testRequest := httptest.NewRequest(http.MethodGet, "/api/teams/all", nil)

testRequest = testRequest.WithContext(
fillContextForGetAllTeams(
testRequest.Context(),
defaultTestPage,
defaultTestSize,
defaultTestSearchText,
defaultTestSortOrder))
testRequest.Header.Add("content-type", "application/json")

total := int64(len(testTeams))

expectedDTO := dto.NewTeamsList(testTeams)
expectedDTO.Page = &defaultTestPage
expectedDTO.Size = &defaultTestSize
expectedDTO.Total = &total

getAllTeams(responseWriter, testRequest)

response := responseWriter.Result()
defer response.Body.Close()

So(response.StatusCode, ShouldEqual, http.StatusOK)

content, err := io.ReadAll(response.Body)
So(err, ShouldBeNil)

var gotDTO dto.TeamsList
err = json.Unmarshal(content, &gotDTO)
So(err, ShouldBeNil)
So(gotDTO, ShouldResemble, expectedDTO)
})

Convey("when db returns error returns internal server error", func() {
dbErr := errors.New("some error from db")

mockDb.EXPECT().GetAllTeams().Return(nil, dbErr)

testRequest := httptest.NewRequest(http.MethodGet, "/api/teams/all", nil)

testRequest = testRequest.WithContext(
fillContextForGetAllTeams(
testRequest.Context(),
defaultTestPage,
defaultTestSize,
defaultTestSearchText,
defaultTestSortOrder))
testRequest.Header.Add("content-type", "application/json")

type errorResponse struct {
StatusText string `json:"status"`
ErrorText string `json:"error,omitempty"`
}

expectedErrResponseFromController := api.ErrorInternalServer(fmt.Errorf("cannot get teams from database: %w", dbErr))
expectedDTO := errorResponse{
StatusText: expectedErrResponseFromController.StatusText,
ErrorText: expectedErrResponseFromController.ErrorText,
}

getAllTeams(responseWriter, testRequest)

response := responseWriter.Result()
defer response.Body.Close()

So(response.StatusCode, ShouldEqual, http.StatusInternalServerError)

content, err := io.ReadAll(response.Body)
So(err, ShouldBeNil)

var gotDTO errorResponse
err = json.Unmarshal(content, &gotDTO)
So(err, ShouldBeNil)
So(gotDTO, ShouldResemble, expectedDTO)
})
})
}

0 comments on commit 78c35c5

Please sign in to comment.