-
Notifications
You must be signed in to change notification settings - Fork 4
/
producer_test.go
100 lines (73 loc) · 2.88 KB
/
producer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package tasq_test
import (
"context"
"testing"
"github.com/greencoda/tasq"
"github.com/greencoda/tasq/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type ProducterTestSuite struct {
suite.Suite
mockRepository *mocks.IRepository
tasqClient *tasq.Client
tasqProducer *tasq.Producer
testTask *tasq.Task
}
func TestProducterTestSuite(t *testing.T) {
t.Parallel()
suite.Run(t, new(ProducterTestSuite))
}
func (s *ProducterTestSuite) SetupTest() {
s.mockRepository = mocks.NewIRepository(s.T())
s.tasqClient = tasq.NewClient(s.mockRepository)
require.NotNil(s.T(), s.tasqClient)
s.tasqProducer = s.tasqClient.NewProducer()
require.NotNil(s.T(), s.tasqProducer)
testArgs := "testData"
testTask, err := tasq.NewTask("testTask", testArgs, "testQueue", 100, 5)
require.Nil(s.T(), err)
s.testTask = testTask
}
func (s *ProducterTestSuite) TestNewProducer() {
assert.NotNil(s.T(), s.tasqProducer)
}
func (s *ProducterTestSuite) TestSubmitSuccessful() {
ctx := context.Background()
s.mockRepository.On("SubmitTask", ctx, mock.AnythingOfType("*tasq.Task")).Return(s.testTask, nil)
task, err := s.tasqProducer.Submit(ctx, s.testTask.Type, s.testTask.Args, s.testTask.Queue, s.testTask.Priority, s.testTask.MaxReceives)
assert.NotNil(s.T(), task)
assert.True(s.T(), s.mockRepository.AssertCalled(s.T(), "SubmitTask", ctx, mock.AnythingOfType("*tasq.Task")))
assert.Nil(s.T(), err)
}
func (s *ProducterTestSuite) TestSubmitUnsuccessful() {
var (
ctx = context.Background()
testArgs = "testData"
testTask, _ = tasq.NewTask("testTask", testArgs, "testQueue", 100, 5)
)
s.mockRepository.On("SubmitTask", ctx, mock.AnythingOfType("*tasq.Task")).Return(nil, errRepository)
task, err := s.tasqProducer.Submit(ctx, testTask.Type, testArgs, testTask.Queue, testTask.Priority, testTask.MaxReceives)
assert.Nil(s.T(), task)
assert.True(s.T(), s.mockRepository.AssertCalled(s.T(), "SubmitTask", ctx, mock.AnythingOfType("*tasq.Task")))
assert.NotNil(s.T(), err)
}
func (s *ProducterTestSuite) TestSubmitInvalidpriority() {
ctx := context.Background()
task, err := s.tasqProducer.Submit(ctx, "testData", nil, "testQueue", 100, 5)
assert.Nil(s.T(), task)
assert.NotNil(s.T(), err)
}
func (s *ProducterTestSuite) TestSubmitTask() {
ctx := context.Background()
s.mockRepository.On("SubmitTask", ctx, mock.AnythingOfType("*tasq.Task")).Return(s.testTask, nil)
task, err := tasq.NewTask(s.testTask.Type, s.testTask.Args, s.testTask.Queue, s.testTask.Priority, s.testTask.MaxReceives)
require.NotNil(s.T(), task)
require.Nil(s.T(), err)
submittedTask, err := s.tasqProducer.SubmitTask(ctx, task)
assert.NotNil(s.T(), submittedTask)
assert.True(s.T(), s.mockRepository.AssertCalled(s.T(), "SubmitTask", ctx, mock.AnythingOfType("*tasq.Task")))
assert.Nil(s.T(), err)
}