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

test(services): apply mock and naming conventions #391

Merged
Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 2 additions & 0 deletions service/amazonsns/amazon_sns.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
)

// SNSSendMessageAPI Basic interface to send messages through SNS.
//
//go:generate mockery --name=SNSSendMessageAPI --output=. --case=underscore --inpackage
type SNSSendMessageAPI interface {
nikoksr marked this conversation as resolved.
Show resolved Hide resolved
SendMessage(ctx context.Context,
params *sns.PublishInput,
Expand Down
39 changes: 19 additions & 20 deletions service/amazonsns/amazon_sns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ import (
"github.com/aws/aws-sdk-go-v2/service/sns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

type SNSSendMessageMock struct {
mock.Mock
}
func TestAmazonSNS_New(t *testing.T) {
t.Parallel()

assert := require.New(t)

func (m *SNSSendMessageMock) SendMessage(ctx context.Context,
params *sns.PublishInput,
optFns ...func(*sns.Options),
) (*sns.PublishOutput, error) {
args := m.Called(ctx, params, optFns)
return args.Get(0).(*sns.PublishOutput), args.Error(1)
service, err := New("", "", "")
assert.NotNil(service)
assert.Nil(err)
}

func TestAddReceivers(t *testing.T) {
func TestAmazonSNS_AddReceivers(t *testing.T) {
t.Parallel()

amazonSNS, err := New("", "", "")
Expand All @@ -32,10 +31,10 @@ func TestAddReceivers(t *testing.T) {
amazonSNS.AddReceivers("One topic")
}

func TestSendMessageWithNoTopicsConfigured(t *testing.T) {
func TestAmazonSNS_SendMessageWithNoTopicsConfigured(t *testing.T) {
t.Parallel()

mockSns := new(SNSSendMessageMock)
mockSns := new(MockSNSSendMessageAPI)
amazonSNS := AmazonSNS{
sendMessageClient: mockSns,
}
Expand All @@ -45,10 +44,10 @@ func TestSendMessageWithNoTopicsConfigured(t *testing.T) {
mockSns.AssertNotCalled(t, "SendMessage", mock.Anything, mock.Anything, mock.Anything)
}

func TestSendMessageWithSucessAndOneTopicCOnfigured(t *testing.T) {
func TestAmazonSNS_SendMessageWithSucessAndOneTopicConfigured(t *testing.T) {
t.Parallel()

mockSns := new(SNSSendMessageMock)
mockSns := new(MockSNSSendMessageAPI)
output := sns.PublishOutput{}
mockSns.On("SendMessage", mock.Anything, mock.Anything, mock.Anything).
Return(&output, nil)
Expand All @@ -64,10 +63,10 @@ func TestSendMessageWithSucessAndOneTopicCOnfigured(t *testing.T) {
assert.Equal(t, 1, len(mockSns.Calls))
}

func TestSendMessageWithSucessAndTwoTopicCOnfigured(t *testing.T) {
func TestAmazonSNS_SendMessageWithSucessAndTwoTopicsConfigured(t *testing.T) {
t.Parallel()

mockSns := new(SNSSendMessageMock)
mockSns := new(MockSNSSendMessageAPI)
output := sns.PublishOutput{}
mockSns.On("SendMessage", mock.Anything, mock.Anything, mock.Anything).
Return(&output, nil)
Expand All @@ -86,10 +85,10 @@ func TestSendMessageWithSucessAndTwoTopicCOnfigured(t *testing.T) {
assert.Equal(t, 2, len(mockSns.Calls))
}

func TestSendMessageWithErrorAndOneQueueConfiguredShouldReturnError(t *testing.T) {
func TestAmazonSNS_SendMessageWithErrorAndOneQueueConfiguredShouldReturnError(t *testing.T) {
t.Parallel()

mockSns := new(SNSSendMessageMock)
mockSns := new(MockSNSSendMessageAPI)
output := sns.PublishOutput{}
mockSns.On("SendMessage", mock.Anything, mock.Anything, mock.Anything).
Return(&output, errors.New("Error on SNS"))
Expand All @@ -107,10 +106,10 @@ func TestSendMessageWithErrorAndOneQueueConfiguredShouldReturnError(t *testing.T
assert.Equal(t, 1, len(mockSns.Calls))
}

func TestSendMessageWithErrorAndTwoQueueConfiguredShouldReturnErrorOnFirst(t *testing.T) {
func TestAmazonSNS_SendMessageWithErrorAndTwoQueueConfiguredShouldReturnErrorOnFirst(t *testing.T) {
t.Parallel()

mockSns := new(SNSSendMessageMock)
mockSns := new(MockSNSSendMessageAPI)
output := sns.PublishOutput{}
mockSns.On("SendMessage", mock.Anything, mock.Anything, mock.Anything).
Return(&output, errors.New("Error on SNS"))
Expand Down
60 changes: 60 additions & 0 deletions service/amazonsns/mock_sns_send_message_api.go

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

2 changes: 1 addition & 1 deletion service/discord/discord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestNew(t *testing.T) {
func TestDiscord_New(t *testing.T) {
t.Parallel()

assert := require.New(t)
Expand Down
2 changes: 2 additions & 0 deletions service/fcm/fcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type (
)

// fcmClient abstracts go-fcm for writing unit tests
//
//go:generate mockery --name=fcmClient --output=. --case=underscore --inpackage
type fcmClient interface {
SendWithRetry(*fcm.Message, int) (*fcm.Response, error)
}
Expand Down
28 changes: 19 additions & 9 deletions service/fcm/fcm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ import (
"github.com/stretchr/testify/require"
)

func TestAddReceivers(t *testing.T) {
func TestFCM_New(t *testing.T) {
t.Parallel()

assert := require.New(t)

service, err := New("server-api-key")
assert.NotNil(service)
assert.Nil(err)
}

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

assert := require.New(t)
Expand All @@ -23,7 +33,7 @@ func TestAddReceivers(t *testing.T) {
assert.Equal(svc.deviceTokens, deviceTokens)
}

func TestSend(t *testing.T) {
func TestFCM_Send(t *testing.T) {
t.Parallel()

assert := require.New(t)
Expand All @@ -38,7 +48,7 @@ func TestSend(t *testing.T) {
}

// test fcm client send
mockClient := newMockFCMClient(t)
mockClient := newMockFcmClient(t)
mockClient.On("SendWithRetry", &fcm.Message{
To: mockToken,
Notification: &fcm.Notification{
Expand All @@ -54,7 +64,7 @@ func TestSend(t *testing.T) {
mockClient.AssertExpectations(t)

// test fcm client send with data
mockClient = newMockFCMClient(t)
mockClient = newMockFcmClient(t)
mockClient.On("SendWithRetry", &fcm.Message{
To: mockToken,
Data: mockData,
Expand All @@ -72,7 +82,7 @@ func TestSend(t *testing.T) {
mockClient.AssertExpectations(t)

// test fcm client send with data and retries
mockClient = newMockFCMClient(t)
mockClient = newMockFcmClient(t)
mockClient.On("SendWithRetry", &fcm.Message{
To: mockToken,
Data: mockData,
Expand All @@ -91,7 +101,7 @@ func TestSend(t *testing.T) {
mockClient.AssertExpectations(t)

// test fcm client returning error
mockClient = newMockFCMClient(t)
mockClient = newMockFcmClient(t)
mockClient.On("SendWithRetry", &fcm.Message{
To: mockToken,
Data: mockData,
Expand All @@ -111,7 +121,7 @@ func TestSend(t *testing.T) {

// test fcm client multiple receivers
anotherMockToken := "another_device_token"
mockClient = newMockFCMClient(t)
mockClient = newMockFcmClient(t)
mockClient.On("SendWithRetry", &fcm.Message{
To: mockToken,
Data: mockData,
Expand All @@ -138,7 +148,7 @@ func TestSend(t *testing.T) {
mockClient.AssertExpectations(t)
}

func TestGetMessageData(t *testing.T) {
func TestFCM_GetMessageData(t *testing.T) {
t.Parallel()

assert := require.New(t)
Expand Down Expand Up @@ -179,7 +189,7 @@ func TestGetMessageData(t *testing.T) {
assert.True(ok)
}

func TestGetMessageRetryAttempts(t *testing.T) {
func TestFCM_GetMessageRetryAttempts(t *testing.T) {
t.Parallel()

assert := require.New(t)
Expand Down
48 changes: 0 additions & 48 deletions service/fcm/mock_fcmClient.go

This file was deleted.

51 changes: 51 additions & 0 deletions service/fcm/mock_fcm_client.go

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

4 changes: 4 additions & 0 deletions service/lark/common.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package lark

// sender is an interface for sending a message to an already defined receiver.
//
//go:generate mockery --name=sender --output=. --case=underscore --inpackage
type sender interface {
Send(subject, message string) error
}

// sender is an interface for sending a message to a specific receiver ID.
//
//go:generate mockery --name=sendToer --output=. --case=underscore --inpackage
type sendToer interface {
SendTo(subject, message, id, idType string) error
}
Expand Down
Loading