1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "encoding/json"
11 "fmt"
12 "net/http"
13 "testing"
14 "time"
15
16 "github.com/google/go-cmp/cmp"
17 )
18
19 func TestGistComments_Marshal(t *testing.T) {
20 testJSONMarshal(t, &GistComment{}, "{}")
21
22 createdAt := time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)
23
24 u := &GistComment{
25 ID: Int64(1),
26 URL: String("u"),
27 Body: String("test gist comment"),
28 User: &User{
29 Login: String("ll"),
30 ID: Int64(123),
31 AvatarURL: String("a"),
32 GravatarID: String("g"),
33 Name: String("n"),
34 Company: String("c"),
35 Blog: String("b"),
36 Location: String("l"),
37 Email: String("e"),
38 Hireable: Bool(true),
39 PublicRepos: Int(1),
40 Followers: Int(1),
41 Following: Int(1),
42 CreatedAt: &Timestamp{referenceTime},
43 URL: String("u"),
44 },
45 CreatedAt: &Timestamp{createdAt},
46 }
47
48 want := `{
49 "id": 1,
50 "url": "u",
51 "body": "test gist comment",
52 "user": {
53 "login": "ll",
54 "id": 123,
55 "avatar_url": "a",
56 "gravatar_id": "g",
57 "name": "n",
58 "company": "c",
59 "blog": "b",
60 "location": "l",
61 "email": "e",
62 "hireable": true,
63 "public_repos": 1,
64 "followers": 1,
65 "following": 1,
66 "created_at": ` + referenceTimeStr + `,
67 "url": "u"
68 },
69 "created_at": "2002-02-10T15:30:00Z"
70 }`
71
72 testJSONMarshal(t, u, want)
73 }
74 func TestGistsService_ListComments(t *testing.T) {
75 client, mux, _, teardown := setup()
76 defer teardown()
77
78 mux.HandleFunc("/gists/1/comments", func(w http.ResponseWriter, r *http.Request) {
79 testMethod(t, r, "GET")
80 testFormValues(t, r, values{"page": "2"})
81 fmt.Fprint(w, `[{"id": 1}]`)
82 })
83
84 opt := &ListOptions{Page: 2}
85 ctx := context.Background()
86 comments, _, err := client.Gists.ListComments(ctx, "1", opt)
87 if err != nil {
88 t.Errorf("Gists.Comments returned error: %v", err)
89 }
90
91 want := []*GistComment{{ID: Int64(1)}}
92 if !cmp.Equal(comments, want) {
93 t.Errorf("Gists.ListComments returned %+v, want %+v", comments, want)
94 }
95
96 const methodName = "ListComments"
97 testBadOptions(t, methodName, func() (err error) {
98 _, _, err = client.Gists.ListComments(ctx, "\n", opt)
99 return err
100 })
101
102 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
103 got, resp, err := client.Gists.ListComments(ctx, "1", opt)
104 if got != nil {
105 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
106 }
107 return resp, err
108 })
109 }
110
111 func TestGistsService_ListComments_invalidID(t *testing.T) {
112 client, _, _, teardown := setup()
113 defer teardown()
114
115 ctx := context.Background()
116 _, _, err := client.Gists.ListComments(ctx, "%", nil)
117 testURLParseError(t, err)
118 }
119
120 func TestGistsService_GetComment(t *testing.T) {
121 client, mux, _, teardown := setup()
122 defer teardown()
123
124 mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) {
125 testMethod(t, r, "GET")
126 fmt.Fprint(w, `{"id": 1}`)
127 })
128
129 ctx := context.Background()
130 comment, _, err := client.Gists.GetComment(ctx, "1", 2)
131 if err != nil {
132 t.Errorf("Gists.GetComment returned error: %v", err)
133 }
134
135 want := &GistComment{ID: Int64(1)}
136 if !cmp.Equal(comment, want) {
137 t.Errorf("Gists.GetComment returned %+v, want %+v", comment, want)
138 }
139
140 const methodName = "GetComment"
141 testBadOptions(t, methodName, func() (err error) {
142 _, _, err = client.Gists.GetComment(ctx, "\n", -2)
143 return err
144 })
145
146 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
147 got, resp, err := client.Gists.GetComment(ctx, "1", 2)
148 if got != nil {
149 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
150 }
151 return resp, err
152 })
153 }
154
155 func TestGistsService_GetComment_invalidID(t *testing.T) {
156 client, _, _, teardown := setup()
157 defer teardown()
158
159 ctx := context.Background()
160 _, _, err := client.Gists.GetComment(ctx, "%", 1)
161 testURLParseError(t, err)
162 }
163
164 func TestGistsService_CreateComment(t *testing.T) {
165 client, mux, _, teardown := setup()
166 defer teardown()
167
168 input := &GistComment{ID: Int64(1), Body: String("b")}
169
170 mux.HandleFunc("/gists/1/comments", func(w http.ResponseWriter, r *http.Request) {
171 v := new(GistComment)
172 json.NewDecoder(r.Body).Decode(v)
173
174 testMethod(t, r, "POST")
175 if !cmp.Equal(v, input) {
176 t.Errorf("Request body = %+v, want %+v", v, input)
177 }
178
179 fmt.Fprint(w, `{"id":1}`)
180 })
181
182 ctx := context.Background()
183 comment, _, err := client.Gists.CreateComment(ctx, "1", input)
184 if err != nil {
185 t.Errorf("Gists.CreateComment returned error: %v", err)
186 }
187
188 want := &GistComment{ID: Int64(1)}
189 if !cmp.Equal(comment, want) {
190 t.Errorf("Gists.CreateComment returned %+v, want %+v", comment, want)
191 }
192
193 const methodName = "CreateComment"
194 testBadOptions(t, methodName, func() (err error) {
195 _, _, err = client.Gists.CreateComment(ctx, "\n", input)
196 return err
197 })
198
199 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
200 got, resp, err := client.Gists.CreateComment(ctx, "1", input)
201 if got != nil {
202 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
203 }
204 return resp, err
205 })
206 }
207
208 func TestGistsService_CreateComment_invalidID(t *testing.T) {
209 client, _, _, teardown := setup()
210 defer teardown()
211
212 ctx := context.Background()
213 _, _, err := client.Gists.CreateComment(ctx, "%", nil)
214 testURLParseError(t, err)
215 }
216
217 func TestGistsService_EditComment(t *testing.T) {
218 client, mux, _, teardown := setup()
219 defer teardown()
220
221 input := &GistComment{ID: Int64(1), Body: String("b")}
222
223 mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) {
224 v := new(GistComment)
225 json.NewDecoder(r.Body).Decode(v)
226
227 testMethod(t, r, "PATCH")
228 if !cmp.Equal(v, input) {
229 t.Errorf("Request body = %+v, want %+v", v, input)
230 }
231
232 fmt.Fprint(w, `{"id":1}`)
233 })
234
235 ctx := context.Background()
236 comment, _, err := client.Gists.EditComment(ctx, "1", 2, input)
237 if err != nil {
238 t.Errorf("Gists.EditComment returned error: %v", err)
239 }
240
241 want := &GistComment{ID: Int64(1)}
242 if !cmp.Equal(comment, want) {
243 t.Errorf("Gists.EditComment returned %+v, want %+v", comment, want)
244 }
245
246 const methodName = "EditComment"
247 testBadOptions(t, methodName, func() (err error) {
248 _, _, err = client.Gists.EditComment(ctx, "\n", -2, input)
249 return err
250 })
251
252 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
253 got, resp, err := client.Gists.EditComment(ctx, "1", 2, input)
254 if got != nil {
255 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
256 }
257 return resp, err
258 })
259 }
260
261 func TestGistsService_EditComment_invalidID(t *testing.T) {
262 client, _, _, teardown := setup()
263 defer teardown()
264
265 ctx := context.Background()
266 _, _, err := client.Gists.EditComment(ctx, "%", 1, nil)
267 testURLParseError(t, err)
268 }
269
270 func TestGistsService_DeleteComment(t *testing.T) {
271 client, mux, _, teardown := setup()
272 defer teardown()
273
274 mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) {
275 testMethod(t, r, "DELETE")
276 })
277
278 ctx := context.Background()
279 _, err := client.Gists.DeleteComment(ctx, "1", 2)
280 if err != nil {
281 t.Errorf("Gists.Delete returned error: %v", err)
282 }
283
284 const methodName = "DeleteComment"
285 testBadOptions(t, methodName, func() (err error) {
286 _, err = client.Gists.DeleteComment(ctx, "\n", -2)
287 return err
288 })
289
290 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
291 return client.Gists.DeleteComment(ctx, "1", 2)
292 })
293 }
294
295 func TestGistsService_DeleteComment_invalidID(t *testing.T) {
296 client, _, _, teardown := setup()
297 defer teardown()
298
299 ctx := context.Background()
300 _, err := client.Gists.DeleteComment(ctx, "%", 1)
301 testURLParseError(t, err)
302 }
303
View as plain text