Newer
Older
package tasklist_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"code.vereign.com/gaiax/tsa/golib/errors"
goatasklist "code.vereign.com/gaiax/tsa/task/gen/task_list"
"code.vereign.com/gaiax/tsa/task/internal/service/task"
"code.vereign.com/gaiax/tsa/task/internal/service/tasklist"
"code.vereign.com/gaiax/tsa/task/internal/service/tasklist/tasklistfakes"
)
func TestNew(t *testing.T) {
svc := tasklist.New(nil, nil, nil, zap.NewNop())
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
assert.Implements(t, (*goatasklist.Service)(nil), svc)
}
func Test_Create(t *testing.T) {
tests := []struct {
name string
req *goatasklist.CreateTaskListRequest
storage *tasklistfakes.FakeStorage
queue *tasklistfakes.FakeQueue
errkind errors.Kind
errtext string
}{
{
name: "empty taskList name",
req: &goatasklist.CreateTaskListRequest{},
errkind: errors.BadRequest,
errtext: "missing taskListName",
},
{
name: "taskList template not found",
req: &goatasklist.CreateTaskListRequest{TaskListName: "taskList name"},
storage: &tasklistfakes.FakeStorage{
TaskListTemplateStub: func(ctx context.Context, s string) (*tasklist.Template, error) {
return nil, errors.New(errors.NotFound)
},
},
errkind: errors.NotFound,
errtext: "not found",
},
{
name: "error getting task templates form storage",
req: &goatasklist.CreateTaskListRequest{TaskListName: "taskList name"},
storage: &tasklistfakes.FakeStorage{
TaskListTemplateStub: func(ctx context.Context, s string) (*tasklist.Template, error) {
return &tasklist.Template{}, nil
},
TaskTemplatesStub: func(ctx context.Context, strings []string) (map[string]*task.Task, error) {
return nil, errors.New(errors.Internal, "internal error")
},
},
errkind: errors.Internal,
errtext: "internal error",
},
{
name: "error creating tasks for a taskList, task template not found",
req: &goatasklist.CreateTaskListRequest{TaskListName: "taskList name"},
storage: &tasklistfakes.FakeStorage{
TaskListTemplateStub: func(ctx context.Context, s string) (*tasklist.Template, error) {
return &tasklist.Template{
Groups: []tasklist.GroupTemplate{
{
Tasks: []string{"non-existent task template"},
},
},
}, nil
},
TaskTemplatesStub: func(ctx context.Context, strings []string) (map[string]*task.Task, error) {
return map[string]*task.Task{"template": &task.Task{}}, nil
},
},
errkind: errors.NotFound,
errtext: "failed to find task template",
},
{
name: "failed to add taskList and tasks to queue",
req: &goatasklist.CreateTaskListRequest{TaskListName: "taskList name"},
storage: &tasklistfakes.FakeStorage{
TaskListTemplateStub: func(ctx context.Context, s string) (*tasklist.Template, error) {
return &tasklist.Template{}, nil
},
TaskTemplatesStub: func(ctx context.Context, strings []string) (map[string]*task.Task, error) {
return map[string]*task.Task{"template": &task.Task{}}, nil
},
},
queue: &tasklistfakes.FakeQueue{
AddTaskListStub: func(ctx context.Context, list *tasklist.TaskList, tasks []*task.Task) error {
return errors.New("storage error")
},
},
errkind: errors.Unknown,
errtext: "storage error",
},
{
name: "successfully add taskList and tasks to queue",
req: &goatasklist.CreateTaskListRequest{TaskListName: "taskList name"},
storage: &tasklistfakes.FakeStorage{
TaskListTemplateStub: func(ctx context.Context, s string) (*tasklist.Template, error) {
return &tasklist.Template{}, nil
},
TaskTemplatesStub: func(ctx context.Context, strings []string) (map[string]*task.Task, error) {
return map[string]*task.Task{"template": &task.Task{}}, nil
},
},
queue: &tasklistfakes.FakeQueue{
AddTaskListStub: func(ctx context.Context, list *tasklist.TaskList, tasks []*task.Task) error {
return nil
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
svc := tasklist.New(test.storage, test.queue, nil, zap.NewNop())
res, err := svc.Create(context.Background(), test.req)
if err != nil {
assert.NotEmpty(t, test.errtext)
e, ok := err.(*errors.Error)
assert.True(t, ok)
assert.Equal(t, test.errkind, e.Kind)
assert.Contains(t, e.Error(), test.errtext)
assert.Nil(t, res)
} else {
assert.Empty(t, test.errtext)
assert.NotNil(t, res)
assert.NotEmpty(t, res.TaskListID)
}
})
}
}
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
func Test_TaskListResult(t *testing.T) {
tests := []struct {
name string
req *goatasklist.TaskListResultRequest
storage *tasklistfakes.FakeStorage
queue *tasklistfakes.FakeQueue
cache *tasklistfakes.FakeCache
errkind errors.Kind
errtext string
}{
{
name: "missing taskList ID",
req: &goatasklist.TaskListResultRequest{},
errkind: errors.BadRequest,
errtext: "missing taskListID",
},
{
name: "error getting taskList form history collection",
req: &goatasklist.TaskListResultRequest{TaskListID: "d16996cd-1977-42a9-90b2-b4548a35c1b4"},
storage: &tasklistfakes.FakeStorage{
TaskListHistoryStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
return nil, errors.New("some error")
},
},
errkind: errors.Unknown,
errtext: "some error",
},
{
name: "taskList not found",
req: &goatasklist.TaskListResultRequest{TaskListID: "d16996cd-1977-42a9-90b2-b4548a35c1b4"},
storage: &tasklistfakes.FakeStorage{
TaskListHistoryStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
return nil, errors.New(errors.NotFound)
},
TaskListStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
return nil, errors.New(errors.NotFound)
},
},
errkind: errors.NotFound,
errtext: "taskList is not found",
},
{
name: "error getting taskList from taskLists collection",
req: &goatasklist.TaskListResultRequest{TaskListID: "d16996cd-1977-42a9-90b2-b4548a35c1b4"},
storage: &tasklistfakes.FakeStorage{
TaskListHistoryStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
return nil, errors.New(errors.NotFound)
},
TaskListStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
return nil, errors.New("some error")
},
},
errkind: errors.Unknown,
errtext: "some error",
},
{
name: "error calculating taskList state",
req: &goatasklist.TaskListResultRequest{TaskListID: "d16996cd-1977-42a9-90b2-b4548a35c1b4"},
storage: &tasklistfakes.FakeStorage{
TaskListHistoryStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
return pendingTaskList, nil
},
GetGroupTasksStub: func(ctx context.Context, group *tasklist.Group) ([]*task.Task, error) {
return nil, errors.New("some error")
},
},
errkind: errors.Unknown,
errtext: "some error",
},
{
name: "error getting taskList from cache",
req: &goatasklist.TaskListResultRequest{TaskListID: "d16996cd-1977-42a9-90b2-b4548a35c1b4"},
storage: &tasklistfakes.FakeStorage{
TaskListHistoryStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
return doneTaskList, nil
},
},
cache: &tasklistfakes.FakeCache{
GetStub: func(ctx context.Context, key, namespace, scope string) ([]byte, error) {
return nil, errors.New("some cache error")
},
},
errkind: errors.Unknown,
errtext: "some cache error",
},
{
name: "successfully get taskList state on pending task",
req: &goatasklist.TaskListResultRequest{TaskListID: "d16996cd-1977-42a9-90b2-b4548a35c1b4"},
storage: &tasklistfakes.FakeStorage{
TaskListHistoryStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
return pendingTaskList, nil
},
GetGroupTasksStub: func(ctx context.Context, group *tasklist.Group) ([]*task.Task, error) {
return []*task.Task{}, nil
},
},
},
{
name: "successfully get taskList state on executed task",
req: &goatasklist.TaskListResultRequest{TaskListID: "d16996cd-1977-42a9-90b2-b4548a35c1b4"},
storage: &tasklistfakes.FakeStorage{
TaskListHistoryStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
return doneTaskList, nil
},
},
cache: &tasklistfakes.FakeCache{
GetStub: func(ctx context.Context, key, namespace, scope string) ([]byte, error) {
return doneTaskState, nil
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
svc := tasklist.New(test.storage, test.queue, test.cache, zap.NewNop())
res, err := svc.TaskListResult(context.Background(), test.req)
if err != nil {
assert.NotEmpty(t, test.errtext)
e, ok := err.(*errors.Error)
assert.True(t, ok)
assert.Equal(t, test.errkind, e.Kind)
assert.Contains(t, e.Error(), test.errtext)
assert.Nil(t, res)
} else {
assert.Empty(t, test.errtext)
assert.NotNil(t, res)
assert.NotEmpty(t, res.ID)
assert.NotEmpty(t, res.Status)
assert.NotEmpty(t, res.Groups)
}
})
}
}
//nolint:gosec
var pendingTaskList = &tasklist.TaskList{
ID: "16996cd-1977-42a9-90b2-b4548a35c1b4",
State: "pending",
Groups: []tasklist.Group{
{
ID: "074076d5-c995-4d2d-8d38-da57360453d4",
Tasks: []string{"createdTask", "createdTask2"},
State: "created",
},
},
}
//nolint:gosec
var doneTaskList = &tasklist.TaskList{
ID: "16996cd-1977-42a9-90b2-b4548a35c1b4",
State: "done",
}
//nolint:gosec
var doneTaskState = []byte(`{
"id": "ad641603-1ca0-4342-ad73-d70a6b1ec502",
"status": "done",
"groups": [
{
"id": "ad641603-1ca0-4342-ad73-d70a6b1ec502",
"type": "sequential",
"status": "done",
"tasks": [
{
"id": "ad641603-1ca0-4342-ad73-d70a6b1ec502",
"status": "done"
},
{
"id": "ad641603-1ca0-4342-ad73-d70a6b1ec502",
"status": "done"
}
]
}
]
}`)