Skip to content

Commit

Permalink
Merge pull request #61 from evrone/feature/unit-tests
Browse files Browse the repository at this point in the history
Add unit tests for usecase
  • Loading branch information
sashamelentyev committed Sep 2, 2021
2 parents ad6c65a + 7b3cce4 commit 9e7a8fa
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ require (
github.com/Masterminds/squirrel v1.5.0
github.com/gin-gonic/gin v1.7.4
github.com/golang-migrate/migrate/v4 v4.14.1
github.com/golang/mock v1.4.4
github.com/google/uuid v1.3.0
github.com/ilyakaznacheev/cleanenv v1.2.5
github.com/jackc/pgx/v4 v4.13.0
github.com/rs/zerolog v1.24.0
github.com/streadway/amqp v1.0.0
github.com/stretchr/testify v1.7.0
github.com/swaggo/files v0.0.0-20210815190702-a29dd2bc99b2
github.com/swaggo/gin-swagger v1.3.1
github.com/swaggo/swag v1.7.1
Expand All @@ -26,6 +28,7 @@ require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/araddon/dateparse v0.0.0-20200409225146-d820a6159ab1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
Expand Down Expand Up @@ -63,6 +66,7 @@ require (
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tidwall/pretty v1.1.0 // indirect
github.com/ugorji/go/codec v1.1.13 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
Expand All @@ -73,5 +77,6 @@ require (
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
olympos.io/encoding/edn v0.0.0-20200308123125-93e3b8dd0e24 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFU
github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
2 changes: 2 additions & 0 deletions internal/usecase/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/evrone/go-clean-template/internal/entity"
)

//go:generate mockgen -source=interfaces.go -destination=./mocks_test.go -package=usecase_test

type (
// Translation -.
Translation interface {
Expand Down
156 changes: 156 additions & 0 deletions internal/usecase/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

126 changes: 126 additions & 0 deletions internal/usecase/translation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package usecase_test

import (
"context"
"errors"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/evrone/go-clean-template/internal/entity"
"github.com/evrone/go-clean-template/internal/usecase"
)

var errInternalServErr = errors.New("internal server error")

type test struct {
name string
mock func()
res interface{}
err error
}

func translation(t *testing.T) (*usecase.TranslationUseCase, *MockTranslationRepo, *MockTranslationWebAPI) {
t.Helper()

mockCtl := gomock.NewController(t)
defer mockCtl.Finish()

repo := NewMockTranslationRepo(mockCtl)
webAPI := NewMockTranslationWebAPI(mockCtl)

translation := usecase.New(repo, webAPI)

return translation, repo, webAPI
}

func TestHistory(t *testing.T) {
t.Parallel()

translation, repo, _ := translation(t)

tests := []test{
{
name: "empty result",
mock: func() {
repo.EXPECT().GetHistory(context.Background()).Return(nil, nil)
},
res: []entity.Translation(nil),
err: nil,
},
{
name: "result with error",
mock: func() {
repo.EXPECT().GetHistory(context.Background()).Return(nil, errInternalServErr)
},
res: []entity.Translation(nil),
err: errInternalServErr,
},
}

for _, tc := range tests {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

tc.mock()

res, err := translation.History(context.Background())

require.Equal(t, res, tc.res)
require.ErrorIs(t, err, tc.err)
})
}
}

func TestTranslate(t *testing.T) {
t.Parallel()

translation, repo, webAPI := translation(t)

tests := []test{
{
name: "empty result",
mock: func() {
webAPI.EXPECT().Translate(entity.Translation{}).Return(entity.Translation{}, nil)
repo.EXPECT().Store(context.Background(), entity.Translation{}).Return(nil)
},
res: entity.Translation{},
err: nil,
},
{
name: "web API error",
mock: func() {
webAPI.EXPECT().Translate(entity.Translation{}).Return(entity.Translation{}, errInternalServErr)
},
res: entity.Translation{},
err: errInternalServErr,
},
{
name: "repo error",
mock: func() {
webAPI.EXPECT().Translate(entity.Translation{}).Return(entity.Translation{}, nil)
repo.EXPECT().Store(context.Background(), entity.Translation{}).Return(errInternalServErr)
},
res: entity.Translation{},
err: errInternalServErr,
},
}

for _, tc := range tests {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

tc.mock()

res, err := translation.Translate(context.Background(), entity.Translation{})

require.EqualValues(t, res, tc.res)
require.ErrorIs(t, err, tc.err)
})
}
}

0 comments on commit 9e7a8fa

Please sign in to comment.