-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks_test.go
388 lines (340 loc) · 9.6 KB
/
tasks_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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package ticktick
import (
"fmt"
"testing"
"time"
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
)
// ********* test utils ********* //
func BuildSampleClient() *Client {
NewSignInTestServer()
syncResponse := BuildSyncResponse()
NewSyncTestServer(syncResponse)
client, _ := NewClient("testuser", "testpass", "test")
return client
}
// ********* test part ********* //
func TestNewTask(t *testing.T) {
defer gock.Off()
assert := assert.New(t)
client := BuildSampleClient()
// normal case
exampleTime := time.Date(2023, 01, 01, 11, 30, 59, 0, time.UTC)
task, err := NewTask(client, "test", "testcontent", exampleTime, "pname1")
assert.Nil(err)
assert.Equal(
TaskItem{
Title: "test",
Content: "testcontent",
StartDate: exampleTime.Format(TemplateTime),
ProjectId: "pid1",
ProjectName: "pname1",
},
*task,
)
// empty time
task, err = NewTask(client, "test", "testcontent", time.Time{}, "pname1")
assert.Nil(err)
assert.Equal(
TaskItem{
Title: "test",
Content: "testcontent",
StartDate: "",
ProjectId: "pid1",
ProjectName: "pname1",
},
*task,
)
// project name not found
task, err = NewTask(client, "test", "testcontent", time.Time{}, "randomProject")
assert.Nil(task)
if assert.NotNil(err) {
assert.Contains(fmt.Sprint(err), "not found")
}
}
func TestCreateTask(t *testing.T) {
defer gock.Off()
assert := assert.New(t)
client := BuildSampleClient()
task, _ := NewTask(client, "test", "testcontent", time.Date(2023, 01, 01, 11, 30, 59, 0, time.UTC), "pname1")
// test server
taskWithId := *task
taskWithId.Id = "testid"
gock.New(baseUrlV2Test).
Post(taskCreateUrlEndpoint).
MatchHeader("Cookie", "t=testtoken").
MatchType("json").
JSON(task).
Reply(200).
JSON(&taskWithId)
// normal case
newtask, err := client.CreateTask(task)
assert.Nil(err)
assert.Equal(taskWithId, *newtask)
// if the task already has an Id
newtask, err = client.CreateTask(&taskWithId)
assert.Nil(newtask)
if assert.NotNil(err) {
assert.Contains(fmt.Sprint(err), "the task has already been created with id")
}
// if no response due to the server error
gock.New(baseUrlV2Test).
Post(taskCreateUrlEndpoint).
Reply(404)
newtask, err = client.CreateTask(task)
assert.Nil(newtask)
assert.NotNil(err)
}
func TestDeleteTask(t *testing.T) {
defer gock.Off()
assert := assert.New(t)
client := BuildSampleClient()
task, _ := NewTask(client, "test", "testcontent", time.Date(2023, 01, 01, 11, 30, 59, 0, time.UTC), "pname1")
// test server
type deleteElement struct {
ProjectId string `json:"projectId"`
TaskId string `json:"taskId"`
}
body := struct {
Delete []deleteElement `json:"delete"`
}{
Delete: []deleteElement{
{
ProjectId: "pid1",
TaskId: "1",
},
},
}
gock.New(baseUrlV2Test).
Post(taskDeleteUrlEndpoint).
MatchHeader("Cookie", "t=testtoken").
MatchType("json").
JSON(body).
Reply(200)
// normal case
task.Id = "1" // mimic createTask
ntask, err := client.DeleteTask(task)
assert.NotNil(ntask)
assert.Nil(err)
assert.Empty(ntask.ProjectId)
assert.Empty(ntask.ProjectName)
assert.Empty(ntask.Id)
// No Id
task.Id = ""
ntask, err = client.DeleteTask(task)
assert.Nil(ntask)
if assert.NotNil(err) {
assert.Contains(fmt.Sprint(err), "the task has not been created, thus not deleted")
}
// server error
task.Id = "1"
gock.New(baseUrlV2Test).
Post(taskDeleteUrlEndpoint).
Reply(404)
ntask, err = client.DeleteTask(task)
assert.Nil(ntask)
assert.NotNil(err)
}
func TestSearchTask(t *testing.T) {
defer gock.Off()
assert := assert.New(t)
client := BuildSampleClient()
// normal case
tasks, err := client.SearchTask("1", "pname1", "a", "", time.Time{}, time.Time{}, -1)
assert.Nil(err)
assert.True(len(tasks) == 1)
tasks, err = client.SearchTask("", "", "", "", time.Time{}, time.Time{}, -1)
assert.Nil(err)
assert.True(len(tasks) > 1)
// project not found
tasks, err = client.SearchTask("1", "pnameRandom", "a", "", time.Time{}, time.Time{}, -1)
assert.Nil(err)
assert.True(len(tasks) == 0)
// tags not found
tasks, err = client.SearchTask("1", "pname1", "random", "", time.Time{}, time.Time{}, -1)
assert.Nil(err)
assert.True(len(tasks) == 0)
// Id not found
tasks, err = client.SearchTask("1", "pname1", "", "r", time.Time{}, time.Time{}, -1)
assert.Nil(err)
assert.True(len(tasks) == 0)
// time not in the range
tasks, err = client.SearchTask("1", "pname1", "", "", time.Date(2023, 01, 01, 11, 30, 59, 0, time.UTC), time.Date(2023, 01, 02, 11, 30, 59, 0, time.UTC), -1)
assert.Nil(err)
assert.True(len(tasks) == 0)
// priority not correct
tasks, err = client.SearchTask("1", "pname1", "", "", time.Time{}, time.Time{}, 3)
assert.Nil(err)
assert.True(len(tasks) == 0)
}
func TestUpdateTask(t *testing.T) {
defer gock.Off()
assert := assert.New(t)
client := BuildSampleClient()
task, _ := NewTask(client, "test", "testcontent", time.Date(2023, 01, 01, 11, 30, 59, 0, time.UTC), "pname1")
task.Id = "1" // mimic createTask
// test server
taskUpdated := *task
taskUpdated.Priority = 5
gock.New(baseUrlV2Test).
Post(fmt.Sprintf(taskUpdateUrlEndpoint, task.Id)).
MatchHeader("Cookie", "t=testtoken").
MatchType("json").
JSON(taskUpdated).
Reply(200).
JSON(&taskUpdated)
// normal case
ntask, err := client.UpdateTask(&taskUpdated)
assert.Equal(&taskUpdated, ntask)
assert.Nil(err)
// Empty Id
taskUpdated.Id = ""
ntask, err = client.UpdateTask(&taskUpdated)
assert.Nil(ntask)
if assert.NotNil(err) {
assert.Contains(fmt.Sprint(err), "task Id is empty")
}
// server error
taskUpdated.Id = "1"
gock.New(baseUrlV2Test).
Post(fmt.Sprintf(taskUpdateUrlEndpoint, task.Id)).
Reply(404)
ntask, err = client.UpdateTask(&taskUpdated)
assert.Nil(ntask)
assert.NotNil(err)
}
func TestCompleteTask(t *testing.T) {
defer gock.Off()
assert := assert.New(t)
client := BuildSampleClient()
task, _ := NewTask(client, "test", "testcontent", time.Date(2023, 01, 01, 11, 30, 59, 0, time.UTC), "pname1")
task.Id = "1" // mimic createTask
// test server
gock.New(baseUrlV2Test).
Post(fmt.Sprintf(taskUpdateUrlEndpoint, task.Id)).
MatchHeader("Cookie", "t=testtoken").
Reply(200).
JSON(task)
// normal case
ntask, err := client.CompleteTask(task)
assert.NotNil(ntask)
assert.Nil(err)
}
func TestMakeSubtask(t *testing.T) {
defer gock.Off()
assert := assert.New(t)
client := BuildSampleClient()
// test server: subtask
type bodyElement struct {
ParentId string `json:"parentId"`
ProjectId string `json:"projectId"`
TaskId string `json:"taskId"`
}
var body []bodyElement
body = append(body, bodyElement{
ParentId: "p",
ProjectId: "pid1",
TaskId: "c",
})
gock.New(baseUrlV2Test).
Post(MakeSubtaskUrlEndpoint).
MatchHeader("Cookie", "t=testtoken").
MatchType("json").
JSON(body).
Reply(200)
// normal case, when both are in the same dir
taskp, _ := client.SearchTask("1", "", "", "", time.Time{}, time.Time{}, -1)
taskc, _ := client.SearchTask("2", "", "", "", time.Time{}, time.Time{}, -1)
newp, newt, err := client.MakeSubtask(&taskp[0], &taskc[0])
assert.NotNil(newp)
assert.NotNil(newt)
assert.Nil(err)
// when taskp has no Id
taskp[0].Id = ""
_, _, err = client.MakeSubtask(&taskp[0], &taskc[0])
if assert.NotNil(err) {
assert.Contains(fmt.Sprint(err), "the parent has not been created")
}
taskp[0].Id = "1"
// when taskc has no Id
taskc[0].Id = ""
_, _, err = client.MakeSubtask(&taskp[0], &taskc[0])
if assert.NotNil(err) {
assert.Contains(fmt.Sprint(err), "the child has not been created")
}
taskc[0].Id = "1"
// when we have to move project
// test server: move project
taskc, _ = client.SearchTask("3", "", "", "", time.Time{}, time.Time{}, -1)
taskcUpdated := taskc[0]
taskcUpdated.ProjectId = "pid1"
gock.New(baseUrlV2Test).
Post(MoveTaskUrlEndpoint).
MatchHeader("Cookie", "t=testtoken").
Reply(200).
JSON(taskcUpdated)
gock.New(baseUrlV2Test).
Post(MakeSubtaskUrlEndpoint).
MatchHeader("Cookie", "t=testtoken").
Reply(200)
newp, newt, err = client.MakeSubtask(&taskp[0], &taskc[0])
assert.NotNil(newp)
assert.NotNil(newt)
assert.Nil(err)
// if server error
gock.New(baseUrlV2Test).
Post(MakeSubtaskUrlEndpoint).
Reply(404)
taskp, _ = client.SearchTask("1", "", "", "", time.Time{}, time.Time{}, -1)
taskc, _ = client.SearchTask("2", "", "", "", time.Time{}, time.Time{}, -1)
newp, newt, err = client.MakeSubtask(&taskp[0], &taskc[0])
assert.Nil(newp)
assert.Nil(newt)
assert.NotNil(err)
}
func TestMoveTask(t *testing.T) {
defer gock.Off()
assert := assert.New(t)
client := BuildSampleClient()
task, _ := NewTask(client, "test", "testcontent", time.Date(2023, 01, 01, 11, 30, 59, 0, time.UTC), "pname1")
task.Id = "test"
// test server
type bodyElement struct {
FromProjectId string `json:"fromProjectId"`
TaskId string `json:"taskId"`
ToProjectId string `json:"toProjectId"`
}
var body []bodyElement
body = append(body, bodyElement{
FromProjectId: "pid1",
TaskId: "test",
ToProjectId: "pid2",
})
gock.New(baseUrlV2Test).
Post(MoveTaskUrlEndpoint).
MatchHeader("Cookie", "t=testtoken").
MatchType("json").
JSON(body).
Reply(200)
// normal case
newt, err := client.MoveTask(task, "pname2")
assert.NotNil(newt)
assert.Nil(err)
// if move to the same project
newt, err = client.MoveTask(task, "pname1")
assert.NotNil(newt)
assert.Nil(err)
assert.Equal(newt, task)
// if the new project not exists
newt, err = client.MoveTask(task, "pnameRandom")
assert.Nil(newt)
assert.NotNil(err)
// if server error
gock.New(baseUrlV2Test).
Post(MoveTaskUrlEndpoint).
Reply(404)
newt, err = client.MoveTask(task, "pname2")
assert.Nil(newt)
assert.NotNil(err)
}