Skip to content

Commit cdebdc4

Browse files
refactor: unit test file
1 parent a4911aa commit cdebdc4

4 files changed

Lines changed: 63 additions & 67 deletions

File tree

services/mqtt/client/logger_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
)
1212

13-
// testHook captures log entries emitted by logrus.
14-
type testHook struct {
15-
entries []logrus.Entry
16-
}
17-
18-
func (h *testHook) Levels() []logrus.Level {
19-
return logrus.AllLevels
20-
}
21-
22-
func (h *testHook) Fire(e *logrus.Entry) error {
23-
h.entries = append(h.entries, *e)
24-
return nil
25-
}
26-
2713
func TestLogger_InfoAndError(t *testing.T) {
2814
// Arrange
2915
baseLogger := logrus.New()
@@ -77,3 +63,17 @@ func TestNewLogger(t *testing.T) {
7763
assert.NotNil(t, l.log)
7864
assert.Equal(t, logger.GetLogger(), l.log)
7965
}
66+
67+
// testHook captures log entries emitted by logrus.
68+
type testHook struct {
69+
entries []logrus.Entry
70+
}
71+
72+
func (h *testHook) Levels() []logrus.Level {
73+
return logrus.AllLevels
74+
}
75+
76+
func (h *testHook) Fire(e *logrus.Entry) error {
77+
h.entries = append(h.entries, *e)
78+
return nil
79+
}

services/mqtt/client/pubsub_test.go

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,6 @@ import (
1313
"github.com/stretchr/testify/mock"
1414
)
1515

16-
// ---- Mock Definitions ----
17-
18-
type mockCourierClient struct {
19-
mock.Mock
20-
}
21-
22-
func (m *mockCourierClient) Start() error {
23-
args := m.Called()
24-
return args.Error(0)
25-
}
26-
func (m *mockCourierClient) Stop() {
27-
m.Called()
28-
}
29-
func (m *mockCourierClient) IsConnected() bool {
30-
args := m.Called()
31-
return args.Bool(0)
32-
}
33-
34-
type mockResolver struct {
35-
mock.Mock
36-
wg *sync.WaitGroup
37-
}
38-
39-
func (m *mockResolver) Start() {
40-
defer m.wg.Done()
41-
m.Called()
42-
}
43-
44-
// ---- Tests ----
45-
4616
func TestMqttPubSubClient_Start(t *testing.T) {
4717

4818
t.Run("Start success", func(t *testing.T) {
@@ -141,6 +111,34 @@ func TestNewMqttPubSubClient(t *testing.T) {
141111

142112
}
143113

114+
// ---- Mock Definitions ----
115+
116+
type mockCourierClient struct {
117+
mock.Mock
118+
}
119+
120+
func (m *mockCourierClient) Start() error {
121+
args := m.Called()
122+
return args.Error(0)
123+
}
124+
func (m *mockCourierClient) Stop() {
125+
m.Called()
126+
}
127+
func (m *mockCourierClient) IsConnected() bool {
128+
args := m.Called()
129+
return args.Bool(0)
130+
}
131+
132+
type mockResolver struct {
133+
mock.Mock
134+
wg *sync.WaitGroup
135+
}
136+
137+
func (m *mockResolver) Start() {
138+
defer m.wg.Done()
139+
m.Called()
140+
}
141+
144142
type MockPubSub struct {
145143
mock.Mock
146144
}

services/mqtt/handler_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"github.com/goto/raccoon/collection"
1717
)
1818

19-
// --- Tests ---
20-
2119
func TestHandler_MQTTHandler(t *testing.T) {
2220
req := pb.SendEventRequest{
2321
ReqGuid: "test-1",

services/mqtt/service_test.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,6 @@ import (
1212
"github.com/stretchr/testify/mock"
1313
)
1414

15-
// -------------------- Mock types --------------------
16-
17-
type MockPubSubClient struct {
18-
mock.Mock
19-
}
20-
21-
func (m *MockPubSubClient) Start() error {
22-
args := m.Called()
23-
return args.Error(0)
24-
}
25-
26-
func (m *MockPubSubClient) Stop() error {
27-
args := m.Called()
28-
return args.Error(0)
29-
}
30-
31-
func (m *MockPubSubClient) IsConnected() bool {
32-
args := m.Called()
33-
return args.Bool(0)
34-
}
35-
3615
func TestService_NewMQTTService(t *testing.T) {
3716
ctx := context.Background()
3817
mockCollector := new(collection.MockCollector)
@@ -134,3 +113,24 @@ func TestService_Name(t *testing.T) {
134113
s := &Service{}
135114
assert.Equal(t, "MQTT", s.Name())
136115
}
116+
117+
// -------------------- Mock types --------------------
118+
119+
type MockPubSubClient struct {
120+
mock.Mock
121+
}
122+
123+
func (m *MockPubSubClient) Start() error {
124+
args := m.Called()
125+
return args.Error(0)
126+
}
127+
128+
func (m *MockPubSubClient) Stop() error {
129+
args := m.Called()
130+
return args.Error(0)
131+
}
132+
133+
func (m *MockPubSubClient) IsConnected() bool {
134+
args := m.Called()
135+
return args.Bool(0)
136+
}

0 commit comments

Comments
 (0)