Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace mockery with moq #77

Merged
merged 1 commit into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions app/bot/anecdot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/radio-t/super-bot/app/bot/mocks"
Expand All @@ -22,67 +21,67 @@ func TestAnecdot_Help(t *testing.T) {
}

func TestAnecdot_ReactsOnJokeRequest(t *testing.T) {
mockHTTP := &mocks.HTTPClient{}
mockHTTP := &mocks.HTTPClient{DoFunc: func(req *http.Request) (*http.Response, error) {
return &http.Response{
Body: io.NopCloser(strings.NewReader(`{"content": "Добраться до вершины не так сложно, как пробраться через толпу у её основания."}`)),
}, nil
}}
b := NewAnecdote(mockHTTP)

mockHTTP.On("Do", mock.Anything).Return(&http.Response{
Body: io.NopCloser(strings.NewReader(`{"content": "Добраться до вершины не так сложно, как пробраться через толпу у её основания."}`)),
}, nil)

response := b.OnMessage(Message{Text: "joke!"})
require.True(t, response.Send)
require.Equal(t, "Добраться до вершины не так сложно, как пробраться через толпу у её основания", response.Text)
}

func TestAnecdot_ujokesrvRetursnNothingOnUnableToDoReq(t *testing.T) {
mockHTTP := &mocks.HTTPClient{}
mockHTTP := &mocks.HTTPClient{DoFunc: func(req *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("err")
}}
b := NewAnecdote(mockHTTP)

mockHTTP.On("Do", mock.Anything).Return(nil, fmt.Errorf("err"))

response := b.jokesrv("oneliners")
require.False(t, response.Send)
require.Empty(t, response.Text)
}

func TestAnecdotReactsOnUnexpectedMessage(t *testing.T) {
mockHTTP := &mocks.HTTPClient{}
mockHTTP := &mocks.HTTPClient{DoFunc: func(req *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("err")
}}
b := NewAnecdote(mockHTTP)

mockHTTP.On("Do", mock.Anything).Return(nil, fmt.Errorf("err"))

result := b.OnMessage(Message{Text: "unexpected msg"})
require.False(t, result.Send)
assert.Empty(t, result.Text)
}

func TestAnecdotReactsOnBadChuckMessage(t *testing.T) {
mockHTTP := &mocks.HTTPClient{}
mockHTTP := &mocks.HTTPClient{DoFunc: func(req *http.Request) (*http.Response, error) {
return &http.Response{
Body: io.NopCloser(bytes.NewReader([]byte(`not a json`))),
}, nil
}}
b := NewAnecdote(mockHTTP)

mockHTTP.On("Do", mock.Anything).Return(&http.Response{
Body: io.NopCloser(bytes.NewReader([]byte(`not a json`))),
}, nil)

require.Equal(t, Response{}, b.OnMessage(Message{Text: "chuck!"}))
}

func TestAnecdotReactsOnChuckMessageUnableToDoReq(t *testing.T) {
mockHTTP := &mocks.HTTPClient{}
mockHTTP := &mocks.HTTPClient{DoFunc: func(req *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("err")
}}
b := NewAnecdote(mockHTTP)

mockHTTP.On("Do", mock.Anything).Return(nil, fmt.Errorf("err"))

require.Equal(t, Response{}, b.OnMessage(Message{Text: "chuck!"}))
}

func TestAnecdotReactsOnChuckMessage(t *testing.T) {
mockHTTP := &mocks.HTTPClient{}
mockHTTP := &mocks.HTTPClient{DoFunc: func(req *http.Request) (*http.Response, error) {
return &http.Response{
Body: io.NopCloser(bytes.NewReader([]byte(`{"Value" : {"Joke" : ""joke""}}`))),
}, nil
}}
b := NewAnecdote(mockHTTP)

mockHTTP.On("Do", mock.Anything).Return(&http.Response{
Body: io.NopCloser(bytes.NewReader([]byte(`{"Value" : {"Joke" : ""joke""}}`))),
}, nil)

require.Equal(t, Response{Text: "- \"joke\"", Send: true}, b.OnMessage(Message{Text: "chuck!"}))
}
12 changes: 7 additions & 5 deletions app/bot/banhammer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ func TestBanhammer_parse(t *testing.T) {
}

func TestBanhammer_OnMessage(t *testing.T) {
su := &mocks.SuperUser{}
su := &mocks.SuperUser{IsSuperFunc: func(userName string) bool {
if userName == "admin" {
return true
}
return false
}}
tg := &mocks.TgBanClient{}
b := NewBanhammer(tg, su, 10)

su.On("IsSuper", "admin").Return(true).Times(2)
su.On("IsSuper", "user1").Return(false).Times(3)

tg.On("KickChatMember", mock.MatchedBy(func(u tbapi.KickChatMemberConfig) bool {
return u.UserID == 1 && u.ChatID == 123
})).Return(tbapi.APIResponse{}, nil)
Expand All @@ -72,6 +74,6 @@ func TestBanhammer_OnMessage(t *testing.T) {
resp = b.OnMessage(Message{Text: "unban! user1", From: User{Username: "admin"}, ChatID: 123})
assert.Equal(t, Response{Text: "амнистия для user1", Send: true}, resp)

su.AssertExpectations(t)
assert.Equal(t, 5, len(su.IsSuperCalls()))
tg.AssertExpectations(t)
}
6 changes: 3 additions & 3 deletions app/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"github.com/go-pkgz/syncs"
)

//go:generate mockery --name HTTPClient --case snake
//go:generate mockery --inpackage --name Interface --case snake
//go:generate mockery --name SuperUser --case snake
//go:generate moq --out mocks/http_client.go --pkg mocks --skip-ensure . HTTPClient:HTTPClient
//go:generate moq --out mock_interface.go . Interface
//go:generate moq --out mocks/super_user.go --pkg mocks --skip-ensure . SuperUser:SuperUser

// genHelpMsg construct help message from bot's ReactOn
func genHelpMsg(com []string, msg string) string {
Expand Down
41 changes: 22 additions & 19 deletions app/bot/bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,27 @@ func TestGenHelpMsg(t *testing.T) {
}

func TestMultiBotHelp(t *testing.T) {
b1 := &MockInterface{}
b1.On("Help").Return("b1 help")
b2 := &MockInterface{}
b2.On("Help").Return("b2 help")
b1 := &InterfaceMock{HelpFunc: func() string {
return "b1 help"
}}
b2 := &InterfaceMock{HelpFunc: func() string {
return "b2 help"
}}

// Must return concatenated b1 and b2 without space
// Line formatting only in genHelpMsg()
require.Equal(t, "b1 help\nb2 help\n", MultiBot{b1, b2}.Help())
}

func TestMultiBotReactsOnHelp(t *testing.T) {
b := &MockInterface{}
b.On("ReactOn").Return([]string{"help"})
b.On("Help").Return("help")
b := &InterfaceMock{
ReactOnFunc: func() []string {
return []string{"help"}
},
HelpFunc: func() string {
return "help"
},
}

mb := MultiBot{b}
resp := mb.OnMessage(Message{Text: "help"})
Expand All @@ -37,18 +44,14 @@ func TestMultiBotReactsOnHelp(t *testing.T) {
func TestMultiBotCombinesAllBotResponses(t *testing.T) {
msg := Message{Text: "cmd"}

b1 := &MockInterface{}
b1.On("ReactOn").Return([]string{"cmd"})
b1.On("OnMessage", msg).Return(Response{
Text: "b1 resp",
Send: true,
})
b2 := &MockInterface{}
b2.On("ReactOn").Return([]string{"cmd"})
b2.On("OnMessage", msg).Return(Response{
Text: "b2 resp",
Send: true,
})
b1 := &InterfaceMock{
ReactOnFunc: func() []string { return []string{"cmd"} },
OnMessageFunc: func(m Message) Response { return Response{Send: true, Text: "b1 resp"} },
}
b2 := &InterfaceMock{
ReactOnFunc: func() []string { return []string{"cmd"} },
OnMessageFunc: func(m Message) Response { return Response{Send: true, Text: "b2 resp"} },
}

mb := MultiBot{b1, b2}
resp := mb.OnMessage(msg)
Expand Down
11 changes: 5 additions & 6 deletions app/bot/duckduckgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/radio-t/super-bot/app/bot/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

Expand All @@ -18,13 +17,13 @@ func TestDuck_Help(t *testing.T) {
}

func TestDuck_OnMessage(t *testing.T) {
mockHTTP := &mocks.HTTPClient{}
mockHTTP := &mocks.HTTPClient{DoFunc: func(req *http.Request) (*http.Response, error) {
return &http.Response{
Body: io.NopCloser(bytes.NewReader([]byte(`{"AbstractText":"the answer", "AbstractSource":"test", "AbstractURL":"http://example.com"}`))),
}, nil
}}
d := NewDuck("key", mockHTTP)

mockHTTP.On("Do", mock.Anything).Return(&http.Response{
Body: io.NopCloser(bytes.NewReader([]byte(`{"AbstractText":"the answer", "AbstractSource":"test", "AbstractURL":"http://example.com"}`))),
}, nil)

assert.Equal(t, Response{Text: "- the answer\n[test](http://example.com)", Send: true}, d.OnMessage(Message{Text: "?? search"}))
}

Expand Down
Loading