mirror of
https://github.com/nikoksr/notify.git
synced 2024-11-28 08:39:13 +02:00
test(service): add mocks and tests for slack
This commit is contained in:
parent
dfcad9d78d
commit
2ee90ea8db
65
service/slack/mock_slack_client.go
Normal file
65
service/slack/mock_slack_client.go
Normal file
@ -0,0 +1,65 @@
|
||||
// Code generated by mockery v2.14.0. DO NOT EDIT.
|
||||
|
||||
package slack
|
||||
|
||||
import (
|
||||
context "context"
|
||||
|
||||
slack_goslack "github.com/slack-go/slack"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// mockSlackClient is an autogenerated mock type for the slackClient type
|
||||
type mockSlackClient struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// PostMessageContext provides a mock function with given fields: ctx, channelID, options
|
||||
func (_m *mockSlackClient) PostMessageContext(ctx context.Context, channelID string, options ...slack_goslack.MsgOption) (string, string, error) {
|
||||
_va := make([]interface{}, len(options))
|
||||
for _i := range options {
|
||||
_va[_i] = options[_i]
|
||||
}
|
||||
var _ca []interface{}
|
||||
_ca = append(_ca, ctx, channelID)
|
||||
_ca = append(_ca, _va...)
|
||||
ret := _m.Called(_ca...)
|
||||
|
||||
var r0 string
|
||||
if rf, ok := ret.Get(0).(func(context.Context, string, ...slack_goslack.MsgOption) string); ok {
|
||||
r0 = rf(ctx, channelID, options...)
|
||||
} else {
|
||||
r0 = ret.Get(0).(string)
|
||||
}
|
||||
|
||||
var r1 string
|
||||
if rf, ok := ret.Get(1).(func(context.Context, string, ...slack_goslack.MsgOption) string); ok {
|
||||
r1 = rf(ctx, channelID, options...)
|
||||
} else {
|
||||
r1 = ret.Get(1).(string)
|
||||
}
|
||||
|
||||
var r2 error
|
||||
if rf, ok := ret.Get(2).(func(context.Context, string, ...slack_goslack.MsgOption) error); ok {
|
||||
r2 = rf(ctx, channelID, options...)
|
||||
} else {
|
||||
r2 = ret.Error(2)
|
||||
}
|
||||
|
||||
return r0, r1, r2
|
||||
}
|
||||
|
||||
type mockConstructorTestingTnewMockSlackClient interface {
|
||||
mock.TestingT
|
||||
Cleanup(func())
|
||||
}
|
||||
|
||||
// newMockSlackClient creates a new instance of mockSlackClient. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
|
||||
func newMockSlackClient(t mockConstructorTestingTnewMockSlackClient) *mockSlackClient {
|
||||
mock := &mockSlackClient{}
|
||||
mock.Mock.Test(t)
|
||||
|
||||
t.Cleanup(func() { mock.AssertExpectations(t) })
|
||||
|
||||
return mock
|
||||
}
|
@ -7,9 +7,17 @@ import (
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
//go:generate mockery --name=slackClient --output=. --case=underscore --inpackage
|
||||
type slackClient interface {
|
||||
PostMessageContext(ctx context.Context, channelID string, options ...slack.MsgOption) (string, string, error)
|
||||
}
|
||||
|
||||
// Compile-time check to ensure that slack.Client implements the slackClient interface.
|
||||
var _ slackClient = new(slack.Client)
|
||||
|
||||
// Slack struct holds necessary data to communicate with the Slack API.
|
||||
type Slack struct {
|
||||
client *slack.Client
|
||||
client slackClient
|
||||
channelIDs []string
|
||||
}
|
||||
|
||||
|
84
service/slack/slack_test.go
Normal file
84
service/slack/slack_test.go
Normal file
@ -0,0 +1,84 @@
|
||||
package slack
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := require.New(t)
|
||||
|
||||
service := New("")
|
||||
assert.NotNil(service)
|
||||
}
|
||||
|
||||
func TestSlack_AddReceivers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := require.New(t)
|
||||
|
||||
service := New("")
|
||||
assert.NotNil(service)
|
||||
|
||||
service.AddReceivers("")
|
||||
assert.Len(service.channelIDs, 1)
|
||||
|
||||
service.AddReceivers("", "")
|
||||
assert.Len(service.channelIDs, 3)
|
||||
|
||||
service.channelIDs = []string{}
|
||||
receivers := []string{"", ""}
|
||||
service.AddReceivers(receivers...)
|
||||
|
||||
diff := cmp.Diff(service.channelIDs, receivers)
|
||||
assert.Equal("", diff)
|
||||
}
|
||||
|
||||
func TestSlack_Send(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := require.New(t)
|
||||
|
||||
service := New("")
|
||||
assert.NotNil(service)
|
||||
|
||||
// No receivers added
|
||||
ctx := context.Background()
|
||||
err := service.Send(ctx, "subject", "message")
|
||||
assert.Nil(err)
|
||||
|
||||
// Test error response
|
||||
mockClient := newMockSlackClient(t)
|
||||
mockClient.
|
||||
On("PostMessageContext", ctx, "1234", mock.AnythingOfType("MsgOption")).
|
||||
Return("", "", errors.New("some error"))
|
||||
|
||||
service.client = mockClient
|
||||
service.AddReceivers("1234")
|
||||
err = service.Send(ctx, "subject", "message")
|
||||
assert.NotNil(err)
|
||||
mockClient.AssertExpectations(t)
|
||||
|
||||
// Test success response
|
||||
mockClient = newMockSlackClient(t)
|
||||
mockClient.
|
||||
On("PostMessageContext", ctx, "1234", mock.AnythingOfType("MsgOption")).
|
||||
Return("", "", nil)
|
||||
|
||||
mockClient.
|
||||
On("PostMessageContext", ctx, "5678", mock.AnythingOfType("MsgOption")).
|
||||
Return("", "", nil)
|
||||
|
||||
service.client = mockClient
|
||||
service.AddReceivers("5678")
|
||||
err = service.Send(ctx, "subject", "message")
|
||||
assert.Nil(err)
|
||||
mockClient.AssertExpectations(t)
|
||||
}
|
Loading…
Reference in New Issue
Block a user