Skip to content

Commit

Permalink
refactor: replace custom mock implementations
Browse files Browse the repository at this point in the history
with mockery
  • Loading branch information
wischoepke committed Jan 22, 2025
1 parent 899e6b9 commit 6583419
Show file tree
Hide file tree
Showing 5 changed files with 537 additions and 199 deletions.
2 changes: 2 additions & 0 deletions server/src/.mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ packages:
all: true
Notes:
all: true
Votings:
all: true
75 changes: 23 additions & 52 deletions server/src/api/notes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,17 @@ func TestNotesTestSuite(t *testing.T) {

func (suite *NotesTestSuite) TestCreateNote() {

tests := []struct {
name string
expectedCode int
err error
}{
{
name: "all ok",
expectedCode: http.StatusCreated,
},
{
name: "api err",
expectedCode: http.StatusConflict,
err: &common.APIError{
Err: errors.New("test"),
StatusCode: http.StatusConflict,
StatusText: "no",
ErrorText: "way",
},
},
{
name: "unexpected err",
expectedCode: http.StatusInternalServerError,
err: errors.New("oops"),
},
}
for _, tt := range tests {
testParameterBundles := *TestParameterBundles{}.
Append("all ok", http.StatusCreated, nil, false, false, nil).
Append("api err", http.StatusConflict, &common.APIError{
Err: errors.New("test"),
StatusCode: http.StatusConflict,
StatusText: "no",
ErrorText: "way",
}, false, false, nil).
Append("unexpected err", http.StatusInternalServerError, errors.New("oops"), false, false, nil)

for _, tt := range testParameterBundles {
suite.Run(tt.name, func() {
s := new(Server)
noteMock := services.NewMockNotes(suite.T())
Expand Down Expand Up @@ -93,32 +78,17 @@ func (suite *NotesTestSuite) TestCreateNote() {
}
func (suite *NotesTestSuite) TestGetNote() {

tests := []struct {
name string
expectedCode int
err error
}{
{
name: "all ok",
expectedCode: http.StatusOK,
},
{
name: "api err",
expectedCode: http.StatusConflict,
err: &common.APIError{
Err: errors.New("foo"),
StatusCode: http.StatusConflict,
StatusText: "no",
ErrorText: "way",
},
},
{
name: "unexpected err",
expectedCode: http.StatusInternalServerError,
err: errors.New("oops"),
},
}
for _, tt := range tests {
testParameterBundles := *TestParameterBundles{}.
Append("all ok", http.StatusOK, nil, false, false, nil).
Append("api err", http.StatusConflict, &common.APIError{
Err: errors.New("test"),
StatusCode: http.StatusConflict,
StatusText: "no",
ErrorText: "way",
}, false, false, nil).
Append("unexpected err", http.StatusInternalServerError, errors.New("oops"), false, false, nil)

for _, tt := range testParameterBundles {
suite.Run(tt.name, func() {
s := new(Server)
noteMock := services.NewMockNotes(suite.T())
Expand All @@ -142,6 +112,7 @@ func (suite *NotesTestSuite) TestGetNote() {
}
}
func (suite *NotesTestSuite) TestDeleteNote() {

tests := []struct {
name string
expectedCode int
Expand Down
59 changes: 23 additions & 36 deletions server/src/api/votes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"scrumlr.io/server/common/dto"
"scrumlr.io/server/identifiers"
"scrumlr.io/server/logger"
"scrumlr.io/server/mocks/services"
"strings"
"testing"

Expand All @@ -27,48 +28,26 @@ func TestVoteTestSuite(t *testing.T) {

func (suite *VoteTestSuite) TestAddVote() {

tests := []struct {
name string
expectedCode int
err error
}{
{
name: "all ok",
expectedCode: http.StatusCreated,
},
{
name: "specific error",
expectedCode: http.StatusTeapot,
err: &common.APIError{
Err: errors.New("check"),
StatusCode: http.StatusTeapot,
StatusText: "teapot",
ErrorText: "Error",
},
},
{
name: "unexpected error",
expectedCode: http.StatusInternalServerError,
err: errors.New("teapot?"),
},
}
for _, tt := range tests {
testParameterBundles := *TestParameterBundles{}.
Append("all ok", http.StatusCreated, nil, false, false, nil).
Append("specific error", http.StatusTeapot, &common.APIError{
Err: errors.New("check"),
StatusCode: http.StatusTeapot,
StatusText: "teapot",
ErrorText: "Error",
}, false, false, nil).
Append("unexpected error", http.StatusInternalServerError, errors.New("teapot?"), false, false, nil)

for _, tt := range testParameterBundles {
suite.Run(tt.name, func() {
s := new(Server)
mock := new(VotingMock)
votingMock := services.NewMockVotings(suite.T())

boardId, _ := uuid.NewRandom()
userId, _ := uuid.NewRandom()
noteId, _ := uuid.NewRandom()
mock.On("AddVote", dto.VoteRequest{
Board: boardId,
User: userId,
Note: noteId,
}).Return(&dto.Vote{
Note: noteId,
}, tt.err)

s.votings = mock
s.votings = votingMock

req := NewTestRequestBuilder("POST", "/", strings.NewReader(fmt.Sprintf(`{
"note": "%s"
Expand All @@ -77,10 +56,18 @@ func (suite *VoteTestSuite) TestAddVote() {
req.AddToContext(identifiers.BoardIdentifier, boardId).
AddToContext(identifiers.UserIdentifier, userId)

votingMock.EXPECT().AddVote(req.req.Context(), dto.VoteRequest{
Board: boardId,
User: userId,
Note: noteId,
}).Return(&dto.Vote{
Note: noteId,
}, tt.err)

rr := httptest.NewRecorder()
s.addVote(rr, req.Request())
suite.Equal(tt.expectedCode, rr.Result().StatusCode)
mock.AssertExpectations(suite.T())
votingMock.AssertExpectations(suite.T())
})
}

Expand Down
Loading

0 comments on commit 6583419

Please sign in to comment.