-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtopics_service_test.go
189 lines (155 loc) · 4.84 KB
/
topics_service_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package service
import (
"context"
"errors"
"fontseca.dev/model"
"fontseca.dev/transfer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
type topicsRepositoryMockAPI struct {
topicsRepositoryAPI
t *testing.T
arguments []any
returns []any
errors error
called bool
}
type topicsRepositoryThenGetMock struct {
topicsRepositoryMockAPI
}
func (mock *topicsRepositoryThenGetMock) List(context.Context) ([]*model.Topic, error) {
return make([]*model.Topic, 2), nil
}
func (mock *topicsRepositoryThenGetMock) Create(_ context.Context, t *transfer.TopicCreation) error {
if nil != mock.errors {
return mock.errors
}
require.Equal(mock.t, mock.arguments[1], t)
return nil
}
func TestTopicsService_Add(t *testing.T) {
ctx := context.TODO()
creation := &transfer.TopicCreation{
Name: "Consectetur! Adipiscing... Quis nostrud: ELIT?",
ID: "consectetur-adipiscing-quis-nostrud-elit",
}
t.Run("success", func(t *testing.T) {
dirty := &transfer.TopicCreation{
Name: " \n\t " + creation.Name + " \n\t ",
}
r := &topicsRepositoryThenGetMock{
topicsRepositoryMockAPI{
t: t,
arguments: []any{ctx, creation},
},
}
err := NewTopicsService(r).Create(ctx, dirty)
assert.NoError(t, err)
})
t.Run("gets a repository failure", func(t *testing.T) {
unexpected := errors.New("unexpected error")
r := &topicsRepositoryThenGetMock{
topicsRepositoryMockAPI{
errors: unexpected,
},
}
err := NewTopicsService(r).Create(ctx, creation)
assert.ErrorIs(t, err, unexpected)
})
}
func (mock *topicsRepositoryMockAPI) List(context.Context) ([]*model.Topic, error) {
mock.called = true
return mock.returns[0].([]*model.Topic), mock.errors
}
func TestTopicsService_Get(t *testing.T) {
ctx := context.TODO()
t.Run("success without cache", func(t *testing.T) {
expectedTopics := []*model.Topic{{}, {}, {}}
r := &topicsRepositoryMockAPI{returns: []any{expectedTopics}}
s := NewTopicsService(r)
s.cache = nil
topics, err := s.List(ctx)
assert.Equal(t, expectedTopics, topics)
assert.NoError(t, err)
})
t.Run("success with cache", func(t *testing.T) {
expectedTopics := []*model.Topic{{}, {}, {}}
r := &topicsRepositoryMockAPI{}
s := NewTopicsService(r)
s.cache = expectedTopics
topics, err := s.List(ctx)
require.False(t, r.called)
assert.Equal(t, expectedTopics, topics)
assert.NoError(t, err)
})
t.Run("gets a repository failure", func(t *testing.T) {
unexpected := errors.New("unexpected error")
r := &topicsRepositoryMockAPI{returns: []any{([]*model.Topic)(nil)}, errors: unexpected}
topics, err := NewTopicsService(r).List(ctx)
assert.Nil(t, topics)
assert.ErrorIs(t, err, unexpected)
})
}
func (mock *topicsRepositoryThenGetMock) Update(_ context.Context, id string, t *transfer.TopicUpdate) error {
if nil != mock.errors {
return mock.errors
}
require.Equal(mock.t, mock.arguments[1], id)
require.Equal(mock.t, mock.arguments[2], t)
return nil
}
func TestTopicsService_Update(t *testing.T) {
ctx := context.TODO()
id := "consectetur-adipiscing-quis-nostrud-elit"
update := &transfer.TopicUpdate{
ID: "consectetur-adipiscing-quis-nostrud-elit",
Name: "Consectetur! Adipiscing... Quis nostrud: ELIT?",
}
t.Run("success", func(t *testing.T) {
dirty := &transfer.TopicUpdate{
Name: " \n\t " + update.Name + " \n\t ",
}
r := &topicsRepositoryThenGetMock{
topicsRepositoryMockAPI{
t: t,
arguments: []any{ctx, id, update},
},
}
err := NewTopicsService(r).Update(ctx, id, dirty)
assert.NoError(t, err)
})
t.Run("gets a repository failure", func(t *testing.T) {
unexpected := errors.New("unexpected error")
r := &topicsRepositoryThenGetMock{
topicsRepositoryMockAPI{
t: t,
arguments: []any{ctx, id, update},
errors: unexpected,
},
}
err := NewTopicsService(r).Update(ctx, id, update)
assert.ErrorIs(t, err, unexpected)
})
}
func (mock *topicsRepositoryThenGetMock) Remove(_ context.Context, id string) error {
if nil != mock.errors {
return mock.errors
}
require.Equal(mock.t, mock.arguments[1], id)
return nil
}
func TestTopicsService_Remove(t *testing.T) {
ctx := context.TODO()
id := "id"
t.Run("success", func(t *testing.T) {
r := &topicsRepositoryThenGetMock{topicsRepositoryMockAPI{t: t, arguments: []any{ctx, id}}}
assert.NoError(t, NewTopicsService(r).Remove(ctx, id))
})
t.Run("gets a repository failure", func(t *testing.T) {
unexpected := errors.New("unexpected error")
r := &topicsRepositoryThenGetMock{topicsRepositoryMockAPI{errors: unexpected}}
assert.ErrorIs(t, NewTopicsService(r).Remove(ctx, id), unexpected)
})
}