-
Notifications
You must be signed in to change notification settings - Fork 6
/
todoist_test.go
118 lines (103 loc) · 3.3 KB
/
todoist_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
package todoist
import (
"encoding/json"
"net/http"
"testing"
"fmt"
"time"
httpmock "gopkg.in/jarcoal/httpmock.v1"
)
func TestMakeRequest(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("GET", todoistURL+"test",
func(req *http.Request) (*http.Response, error) {
return httpmock.NewJsonResponse(200, nil)
})
httpmock.RegisterResponder("DELETE", todoistURL+"test",
func(req *http.Request) (*http.Response, error) {
return httpmock.NewJsonResponse(404, "not found")
})
httpmock.RegisterResponder("POST", todoistURL+"test",
func(req *http.Request) (*http.Response, error) {
var body map[string]interface{}
if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
return httpmock.NewJsonResponse(200, body)
})
res, err := makeRequest("GET", "test", nil)
if err != nil {
t.Error(err)
}
if res.StatusCode != 200 {
t.Errorf("Expected Status 200 != '%d'", res.StatusCode)
}
res, err = makeRequest("POST", "test", struct{ ID int }{1})
if err != nil {
t.Error(err)
}
if res.StatusCode != 200 {
t.Errorf("Expected Status 200 != '%d'", res.StatusCode)
}
var body struct{ ID int }
if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
t.Error(err)
}
if body.ID != 1 {
t.Errorf("Expected 1 != '%d'", body.ID)
}
res, err = makeRequest("DELETE", "test", nil)
if err.Error() != "\"not found\"" {
t.Error(err)
}
}
func TestTaskSaveMarshalJSON(t *testing.T) {
var ts taskSave
_, err := ts.MarshalJSON()
if err.Error() != "Content is empty" {
t.Error(err)
}
ts.Content = "test"
b, _ := ts.MarshalJSON()
if string(b) != "{\"content\":\"test\"}" {
t.Errorf("Expected '{\"content\":\"test\"}' != '%s'", string(b))
}
ts = taskSave{Content: "test", ProjectID: 1}
b, _ = ts.MarshalJSON()
if string(b) != "{\"content\":\"test\",\"project_id\":1}" {
t.Errorf("Expected '{\"content\":\"test\",\"project_id\":1}' != '%s'", string(b))
}
ts = taskSave{Content: "test", Order: 1}
b, _ = ts.MarshalJSON()
if string(b) != "{\"content\":\"test\",\"order\":1}" {
t.Errorf("Expected '{\"content\":\"test\",\"order\":1}' != '%s'", string(b))
}
ts = taskSave{Content: "test", Priority: 1}
b, _ = ts.MarshalJSON()
if string(b) != "{\"content\":\"test\",\"priority\":1}" {
t.Errorf("Expected '{\"content\":\"test\",\"priority\":1}' != '%s'", string(b))
}
ts = taskSave{Content: "test", DueString: "en"}
b, _ = ts.MarshalJSON()
if string(b) != "{\"content\":\"test\",\"due_string\":\"en\"}" {
t.Errorf("Expected '{\"content\":\"test\",\"due_string\":\"en\"}' != '%s'", string(b))
}
ts = taskSave{Content: "test", DueLang: "en"}
b, _ = ts.MarshalJSON()
if string(b) != "{\"content\":\"test\",\"due_lang\":\"en\"}" {
t.Errorf("Expected '{\"content\":\"test\",\"due_lang\":\"en\"}' != '%s'", string(b))
}
ts = taskSave{Content: "test", LabelIDs: []int{1, 2}}
b, _ = ts.MarshalJSON()
if string(b) != "{\"content\":\"test\",\"label_ids\":[1,2]}" {
t.Errorf("Expected '{\"content\":\"test\",\"label_ids\":[1,2]}' != '%s'", string(b))
}
now := time.Now()
ts = taskSave{Content: "test", DueDateTime: now}
s, _ := now.MarshalJSON()
b, _ = ts.MarshalJSON()
if string(b) != fmt.Sprintf("{\"content\":\"test\",\"due_datetime\":%s}", string(s)) {
t.Error("deu merda")
}
}