-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter_test.go
246 lines (213 loc) · 5.86 KB
/
adapter_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package interactor_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/screwyprof/interactor/v2"
)
func TestFunc(t *testing.T) {
t.Parallel()
testCases := []testCase{
{
name: "a use case runner must be a function",
runner: struct{}{},
wantErr: interactor.ErrUseCaseRunnerIsNotAFunction,
},
{
name: "a use case runner must have 3 arguments",
runner: func(ctx context.Context, req TestRequest) error {
return nil
},
wantErr: interactor.ErrInvalidUseCaseRunnerSignature,
},
{
name: "first input param must be context.Context",
runner: func(ctx struct{}, req TestRequest, resp *TestResponse) error {
return nil
},
wantErr: interactor.ErrFirstArgHasInvalidType,
},
{
name: "second input param must be a struct implementing Request",
runner: func(ctx context.Context, req int, resp *TestResponse) error {
return nil
},
wantErr: interactor.ErrSecondArgHasInvalidType,
},
{
name: "third input param must be a pointer type implementing Response",
runner: func(ctx context.Context, req TestRequest, resp struct{}) error {
return nil
},
wantErr: interactor.ErrThirdArgHasInvalidType,
},
{
name: "provided response type must match expected response type",
runner: func(ctx context.Context, req TestRequest, resp *TestResponse) error {
return nil
},
request: TestRequest{},
response: &struct {
interactor.Response
}{},
wantRunnerErr: interactor.ErrResultTypeMismatch,
},
{
name: "a use case returns an error",
runner: func(ctx context.Context, req TestRequest, resp *TestResponse) error {
return errSomeErr
},
request: TestRequest{id: 123},
response: &TestResponse{},
wantRunnerErr: errSomeErr,
},
{
name: "second input param maybe be a pointer to a struct implementing Request",
runner: func(ctx context.Context, req *TestRequest, resp *TestResponse) error {
return nil
},
request: &TestRequest{},
response: &TestResponse{},
wantResult: &TestResponse{},
},
{
name: "provided use case successfully adapted to comply with UseCaseRunner interface",
runner: func(ctx context.Context, req TestRequest, resp *TestResponse) error {
resp.result = req.id
return nil
},
request: TestRequest{id: 123},
response: &TestResponse{},
wantResult: &TestResponse{result: 123},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
runFuncTest(t, tc)
})
}
}
func TestAdapt(t *testing.T) {
t.Parallel()
testCases := []testCase{
{
name: "a use case must have a Run method",
runner: struct{}{},
wantErr: interactor.ErrUseCaseRunnerHasNoRunMethod,
},
{
name: "method must have 3 arguments",
runner: InvalidUseCaseWrongSignature{},
wantErr: interactor.ErrInvalidUseCaseRunnerSignature,
},
{
name: "first input param must be context.Context",
runner: InvalidUseCaseWrongContext{},
wantErr: interactor.ErrFirstArgHasInvalidType,
},
{
name: "second input param must be a struct implementing Request",
runner: InvalidUseCaseWrongRequest{},
wantErr: interactor.ErrSecondArgHasInvalidType,
},
{
name: "third input param must be a pointer type implementing Response",
runner: InvalidUseCaseWrongResponse{},
wantErr: interactor.ErrThirdArgHasInvalidType,
},
{
name: "response types must match",
runner: ConcreteUseCase{},
response: &AnotherResponse{},
wantRunnerErr: interactor.ErrResultTypeMismatch,
},
{
name: "a use case returns an error",
runner: ConcreteUseCase{err: errSomeErr},
request: TestRequest{id: 123},
response: &TestResponse{},
wantRunnerErr: errSomeErr,
},
{
name: "second input param maybe be a pointer type implementing Request",
runner: ValidUseCasePointerRequest{},
request: &TestRequest{},
response: &TestResponse{},
wantResult: &TestResponse{},
},
{
name: "provided use case successfully adapted to comply with UseCaseRunner interface",
runner: ConcreteUseCase{},
request: TestRequest{id: 123},
response: &TestResponse{},
wantResult: &TestResponse{result: 123},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
runAdaptTest(t, tc)
})
}
}
func TestMust(t *testing.T) {
t.Parallel()
t.Run("it panics if it cannot adapt a use case runner", func(t *testing.T) {
t.Parallel()
assert.Panics(t, func() {
interactor.Must(interactor.Func(struct{}{}))
})
})
t.Run("it adapts a use case runner function", func(t *testing.T) {
t.Parallel()
// act
runner := interactor.Must(interactor.Func(ConcreteUseCase{}.Run))
// assert
assert.NotNil(t, runner)
})
}
type testCase struct {
name string
runner interface{}
request interactor.Request
response interactor.Response
wantErr error
wantRunnerErr error
wantResult interactor.Response
}
func runFuncTest(t *testing.T, tc testCase) {
t.Helper()
runner, err := interactor.Func(tc.runner)
if tc.wantErr != nil {
require.ErrorIs(t, err, tc.wantErr)
return
}
require.NoError(t, err)
err = runner(context.Background(), tc.request, tc.response)
if tc.wantRunnerErr != nil {
require.ErrorIs(t, err, tc.wantRunnerErr)
return
}
require.NoError(t, err)
assert.Equal(t, tc.wantResult, tc.response)
}
func runAdaptTest(t *testing.T, tc testCase) {
t.Helper()
runner, err := interactor.Adapt(tc.runner)
if tc.wantErr != nil {
require.ErrorIs(t, err, tc.wantErr)
return
}
require.NoError(t, err)
err = runner(context.Background(), tc.request, tc.response)
if tc.wantRunnerErr != nil {
require.ErrorIs(t, err, tc.wantRunnerErr)
return
}
require.NoError(t, err)
assert.Equal(t, tc.wantResult, tc.response)
}