forked from andygrunwald/cachet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
components_test.go
186 lines (160 loc) · 5.11 KB
/
components_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
package cachet
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestComponentsService_GetAll(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/api/v1/components", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"meta":{"pagination":{"total":1,"count":1,"per_page":20,"current_page":1,"total_pages":1,"links":{"next_page":null,"previous_page":null}}},"data":[{"id":1,"name":"API","description":"This is the Cachet API.","link":"","status":1,"order":0,"group_id":0,"created_at":"2015-07-24 14:42:10","updated_at":"2015-07-24 14:42:10","deleted_at":null,"status_name":"Operational"}]}`)
})
queryParams := &ComponentsQueryParams{}
got, _, err := testClient.Components.GetAll(queryParams)
if err != nil {
t.Errorf("Components.GetAll returned error: %v", err)
}
expected := &ComponentResponse{
Meta: Meta{
Pagination: Pagination{
Total: 1,
Count: 1,
PerPage: 20,
CurrentPage: 1,
TotalPages: 1,
Links: Links{
NextPage: "",
PreviousPage: "",
},
},
},
Components: []Component{
{
ID: 1,
Name: "API",
Description: "This is the Cachet API.",
Link: "",
Status: 1,
Order: 0,
GroupID: 0,
CreatedAt: "2015-07-24 14:42:10",
UpdatedAt: "2015-07-24 14:42:10",
DeletedAt: "",
StatusName: "Operational",
},
},
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Components.GetAll returned %+v, want %+v", got, expected)
}
}
func TestComponentsService_Get(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/api/v1/components/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"data":{"id":1,"name":"API","description":"Used by third-parties to connect to us","link":"","status":1,"order":0,"group_id":0,"created_at":"2015-10-31 08:30:01","updated_at":"2015-10-31 08:30:01","deleted_at":null,"status_name":"Operational"}}`)
})
got, _, err := testClient.Components.Get(1)
if err != nil {
t.Errorf("Components.Get returned error: %v", err)
}
expected := &Component{
ID: 1,
Name: "API",
Description: "Used by third-parties to connect to us",
Link: "",
Status: 1,
Order: 0,
GroupID: 0,
CreatedAt: "2015-10-31 08:30:01",
UpdatedAt: "2015-10-31 08:30:01",
DeletedAt: "",
StatusName: "Operational",
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Components.Get returned %+v, want %+v", got, expected)
}
}
func TestComponentsService_Create(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/api/v1/components", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
fmt.Fprint(w, `{"data":{"id":1,"name":"Component Name","description":"Description","link":"","status":1,"order":0,"group_id":0,"created_at":"2015-08-01 12:00:00","updated_at":"2015-08-01 12:00:00","deleted_at":null,"status_name":"Operational"}}`)
})
co := &Component{
Name: "Go API (by Token) - Updated!",
Status: 1,
}
got, _, err := testClient.Components.Create(co)
if err != nil {
t.Errorf("Components.Create returned error: %v", err)
}
expected := &Component{
ID: 1,
Name: "Component Name",
Description: "Description",
Link: "",
Status: 1,
Order: 0,
GroupID: 0,
CreatedAt: "2015-08-01 12:00:00",
UpdatedAt: "2015-08-01 12:00:00",
DeletedAt: "",
StatusName: "Operational",
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Components.Create returned %+v, want %+v", got, expected)
}
}
func TestComponentsService_Update(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/api/v1/components/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
fmt.Fprint(w, `{"data":{"id":1,"name":"Component Name","description":"Description","link":"","status":1,"order":0,"group_id":0,"created_at":"2015-08-01 12:00:00","updated_at":"2015-08-01 12:00:00","deleted_at":null,"status_name":"Operational"}}`)
})
co := &Component{
Name: "Component Name",
Status: 1,
}
got, _, err := testClient.Components.Update(1, co)
if err != nil {
t.Errorf("Components.Update returned error: %v", err)
}
expected := &Component{
ID: 1,
Name: "Component Name",
Description: "Description",
Link: "",
Status: 1,
Order: 0,
GroupID: 0,
CreatedAt: "2015-08-01 12:00:00",
UpdatedAt: "2015-08-01 12:00:00",
DeletedAt: "",
StatusName: "Operational",
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Components.Update returned %+v, want %+v", got, expected)
}
}
func TestComponentsService_Delete(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/api/v1/components/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
w.WriteHeader(http.StatusNoContent)
})
resp, err := testClient.Components.Delete(1)
if err != nil {
t.Errorf("Components.Delete returned error: %v", err)
}
if resp.StatusCode != http.StatusNoContent {
t.Errorf("Components.Delete returned status %+v, want %+v", resp.StatusCode, http.StatusNoContent)
}
}