Skip to content

Commit

Permalink
test uc/articlesFeed_test uc/articlesRecent_test
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Jacquot committed Aug 10, 2018
1 parent d299704 commit 56b00dc
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
55 changes: 55 additions & 0 deletions uc/articlesFeed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package uc_test
import (
"testing"

"errors"

"github.com/err0r500/go-realworld-clean/domain"
"github.com/err0r500/go-realworld-clean/implem/uc.mock"
"github.com/err0r500/go-realworld-clean/testData"
Expand Down Expand Up @@ -50,4 +52,57 @@ func TestInteractor_ArticlesFeed_happycases(t *testing.T) {
assert.Equal(t, rick, *user)
})

t.Run("empty count", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

i := mock.NewMockedInteractor(mockCtrl)
user, _, count, err := i.GetUCHandler().ArticlesFeed(rick.Name, -2, 0)
assert.NoError(t, err)
assert.Equal(t, 0, count)
assert.Nil(t, user)
})
}

func TestInteractor_ArticlesFeed_fails(t *testing.T) {
mutations := map[string]mock.Tester{
"shouldPass": {
Calls: func(i *mock.Interactor) { // change nothing
},
ShouldPass: true},
"error return on uRW.GetByName": {
Calls: func(i *mock.Interactor) {
i.UserRW.EXPECT().GetByName(gomock.Any()).Return(nil, errors.New(""))
}},
"error return on GetByAuthorsNameOrderedByMostRecentAsc": {
Calls: func(i *mock.Interactor) {
i.ArticleRW.EXPECT().GetByAuthorsNameOrderedByMostRecentAsc(gomock.Any()).Return(nil, errors.New(""))
}},
}
rick := testData.User("rick")
rick.FollowIDs = []string{testData.User("jane").Name}
expectedArticles := domain.ArticleCollection{art1, art2, art3, art4}

// same as the happy case but with any parameter and called any number of times (including 0)
validCalls := func(i *mock.Interactor) {
i.UserRW.EXPECT().GetByName(gomock.Any()).Return(&rick, nil).AnyTimes()
i.ArticleRW.EXPECT().GetByAuthorsNameOrderedByMostRecentAsc(gomock.Any()).Return(expectedArticles, nil).AnyTimes()
}

for testName, mutation := range mutations {
t.Run(testName, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
i := mock.NewMockedInteractor(mockCtrl)
mutation.Calls(&i) // put the tested call first (important)
validCalls(&i) // then fill the gaps with valid calls

_, _, _, err := i.GetUCHandler().ArticlesFeed(rick.Name, 2, 0)
if mutation.ShouldPass {
assert.NoError(t, err)
return
}
assert.Error(t, err)
})
}
}
59 changes: 58 additions & 1 deletion uc/articlesRecent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package uc_test
import (
"testing"

"errors"

"github.com/err0r500/go-realworld-clean/domain"
"github.com/err0r500/go-realworld-clean/implem/uc.mock"
"github.com/err0r500/go-realworld-clean/testData"
Expand All @@ -19,7 +21,7 @@ func TestInteractor_GetArticles(t *testing.T) {
defer mockCtrl.Finish()

offset := 2
filters := uc.NewFilters("jane", "", "")
filters := uc.NewFilters("jane", "bla", "true")
testUser := testData.User("rick")
i := mock.NewMockedInteractor(mockCtrl)
i.ArticleRW.EXPECT().GetRecentFiltered(filters).Return(expectedArticles, nil).Times(1)
Expand All @@ -30,6 +32,61 @@ func TestInteractor_GetArticles(t *testing.T) {
assert.Equal(t, 4, count)
assert.Equal(t, expectedArticles[offset:], articles)
assert.Equal(t, testData.User("rick"), *user)
})

t.Run("empty", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

testUser := testData.User("rick")
i := mock.NewMockedInteractor(mockCtrl)

user, _, count, err := i.GetUCHandler().GetArticles(testUser.Name, -10, 2, nil)
assert.NoError(t, err)
assert.Nil(t, user)
assert.Equal(t, 0, count)
})
}

func TestInteractor_ArticlesRecent_fails(t *testing.T) {
expectedArticles := domain.ArticleCollection{art1, art2, art3, art4}

mutations := map[string]mock.Tester{
"shouldPass": {
Calls: func(i *mock.Interactor) { // change nothing
},
ShouldPass: true},
"error return on uRW.GetByName": {
Calls: func(i *mock.Interactor) {
i.UserRW.EXPECT().GetByName(gomock.Any()).Return(nil, errors.New(""))
}},
"error return on GetByAuthorsNameOrderedByMostRecentAsc": {
Calls: func(i *mock.Interactor) {
i.ArticleRW.EXPECT().GetRecentFiltered(gomock.Any()).Return(nil, errors.New(""))
}},
}
testUser := testData.User("rick")

// same as the happy case but with any parameter and called any number of times (including 0)
validCalls := func(i *mock.Interactor) {
i.ArticleRW.EXPECT().GetRecentFiltered(gomock.Any()).Return(expectedArticles, nil).AnyTimes()
i.UserRW.EXPECT().GetByName(gomock.Any()).Return(&testUser, nil).AnyTimes()
}

for testName, mutation := range mutations {
t.Run(testName, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
i := mock.NewMockedInteractor(mockCtrl)
mutation.Calls(&i) // put the tested call first (important)
validCalls(&i) // then fill the gaps with valid calls

_, _, _, err := i.GetUCHandler().GetArticles(testUser.Name, 10, 2, nil)
if mutation.ShouldPass {
assert.NoError(t, err)
return
}
assert.Error(t, err)
})
}
}

0 comments on commit 56b00dc

Please sign in to comment.