Skip to content

Commit

Permalink
test: Cover notification service
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricioabreu committed Apr 13, 2024
1 parent 48b381c commit 6c794b8
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 4 deletions.
59 changes: 59 additions & 0 deletions internal/mocks/queue.go

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

6 changes: 5 additions & 1 deletion internal/queue/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"github.com/learn-video/dionysia/internal/config"
)

func NewClient(cfg *config.Config) *asynq.Client {
type Client interface {
Enqueue(*asynq.Task, ...asynq.Option) (*asynq.TaskInfo, error)
}

func NewClient(cfg *config.Config) Client {
return asynq.NewClient(asynq.RedisClientOpt{Addr: cfg.RedisAddr})
}
6 changes: 3 additions & 3 deletions internal/service/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"

"github.com/google/uuid"
"github.com/hibiken/asynq"
"github.com/learn-video/dionysia/internal/queue"
"github.com/learn-video/dionysia/internal/task"
)

Expand All @@ -13,10 +13,10 @@ type NotificationHandler interface {
}

type notificationHandler struct {
client *asynq.Client
client queue.Client
}

func NewNotificationHandler(c *asynq.Client) NotificationHandler {
func NewNotificationHandler(c queue.Client) NotificationHandler {
return &notificationHandler{
client: c,
}
Expand Down
49 changes: 49 additions & 0 deletions internal/service/notification_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package service_test

import (
"testing"

"github.com/google/uuid"
"github.com/hibiken/asynq"
"github.com/learn-video/dionysia/internal/mocks"
"github.com/learn-video/dionysia/internal/service"
"github.com/learn-video/dionysia/internal/task"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

func TestPackageStreamSuccess(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockClient := mocks.NewMockClient(ctrl)
handler := service.NewNotificationHandler(mockClient)

taskID := uuid.New()
expectedTask, _ := task.NewPackageTask(taskID)

expectedInfo := &asynq.TaskInfo{ID: "1", Queue: "default"}

mockClient.EXPECT().Enqueue(expectedTask).Return(expectedInfo, nil).Times(1)

err := handler.PackageStream(taskID)

assert.NoError(t, err)
}

func TestPackageStreamEnqueueFailure(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockClient := mocks.NewMockClient(ctrl)
handler := service.NewNotificationHandler(mockClient)

taskID := uuid.New()
expectedTask, _ := task.NewPackageTask(taskID)

mockClient.EXPECT().Enqueue(expectedTask).Return(nil, assert.AnError).Times(1)

err := handler.PackageStream(taskID)

assert.Error(t, err)
}

0 comments on commit 6c794b8

Please sign in to comment.