1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "encoding/json"
11 "fmt"
12 "net/http"
13 "testing"
14
15 "github.com/google/go-cmp/cmp"
16 )
17
18 func TestIssuesService_ListLabels(t *testing.T) {
19 client, mux, _, teardown := setup()
20 defer teardown()
21
22 mux.HandleFunc("/repos/o/r/labels", func(w http.ResponseWriter, r *http.Request) {
23 testMethod(t, r, "GET")
24 testFormValues(t, r, values{"page": "2"})
25 fmt.Fprint(w, `[{"name": "a"},{"name": "b"}]`)
26 })
27
28 opt := &ListOptions{Page: 2}
29 ctx := context.Background()
30 labels, _, err := client.Issues.ListLabels(ctx, "o", "r", opt)
31 if err != nil {
32 t.Errorf("Issues.ListLabels returned error: %v", err)
33 }
34
35 want := []*Label{{Name: String("a")}, {Name: String("b")}}
36 if !cmp.Equal(labels, want) {
37 t.Errorf("Issues.ListLabels returned %+v, want %+v", labels, want)
38 }
39
40 const methodName = "ListLabels"
41 testBadOptions(t, methodName, func() (err error) {
42 _, _, err = client.Issues.ListLabels(ctx, "\n", "\n", opt)
43 return err
44 })
45
46 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
47 got, resp, err := client.Issues.ListLabels(ctx, "o", "r", opt)
48 if got != nil {
49 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
50 }
51 return resp, err
52 })
53 }
54
55 func TestIssuesService_ListLabels_invalidOwner(t *testing.T) {
56 client, _, _, teardown := setup()
57 defer teardown()
58
59 ctx := context.Background()
60 _, _, err := client.Issues.ListLabels(ctx, "%", "%", nil)
61 testURLParseError(t, err)
62 }
63
64 func TestIssuesService_GetLabel(t *testing.T) {
65 client, mux, _, teardown := setup()
66 defer teardown()
67
68 mux.HandleFunc("/repos/o/r/labels/n", func(w http.ResponseWriter, r *http.Request) {
69 testMethod(t, r, "GET")
70 fmt.Fprint(w, `{"url":"u", "name": "n", "color": "c", "description": "d"}`)
71 })
72
73 ctx := context.Background()
74 label, _, err := client.Issues.GetLabel(ctx, "o", "r", "n")
75 if err != nil {
76 t.Errorf("Issues.GetLabel returned error: %v", err)
77 }
78
79 want := &Label{URL: String("u"), Name: String("n"), Color: String("c"), Description: String("d")}
80 if !cmp.Equal(label, want) {
81 t.Errorf("Issues.GetLabel returned %+v, want %+v", label, want)
82 }
83
84 const methodName = "GetLabel"
85 testBadOptions(t, methodName, func() (err error) {
86 _, _, err = client.Issues.GetLabel(ctx, "\n", "\n", "\n")
87 return err
88 })
89
90 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
91 got, resp, err := client.Issues.GetLabel(ctx, "o", "r", "n")
92 if got != nil {
93 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
94 }
95 return resp, err
96 })
97 }
98
99 func TestIssuesService_GetLabel_invalidOwner(t *testing.T) {
100 client, _, _, teardown := setup()
101 defer teardown()
102
103 ctx := context.Background()
104 _, _, err := client.Issues.GetLabel(ctx, "%", "%", "%")
105 testURLParseError(t, err)
106 }
107
108 func TestIssuesService_CreateLabel(t *testing.T) {
109 client, mux, _, teardown := setup()
110 defer teardown()
111
112 input := &Label{Name: String("n")}
113
114 mux.HandleFunc("/repos/o/r/labels", func(w http.ResponseWriter, r *http.Request) {
115 v := new(Label)
116 json.NewDecoder(r.Body).Decode(v)
117
118 testMethod(t, r, "POST")
119 if !cmp.Equal(v, input) {
120 t.Errorf("Request body = %+v, want %+v", v, input)
121 }
122
123 fmt.Fprint(w, `{"url":"u"}`)
124 })
125
126 ctx := context.Background()
127 label, _, err := client.Issues.CreateLabel(ctx, "o", "r", input)
128 if err != nil {
129 t.Errorf("Issues.CreateLabel returned error: %v", err)
130 }
131
132 want := &Label{URL: String("u")}
133 if !cmp.Equal(label, want) {
134 t.Errorf("Issues.CreateLabel returned %+v, want %+v", label, want)
135 }
136
137 const methodName = "CreateLabel"
138 testBadOptions(t, methodName, func() (err error) {
139 _, _, err = client.Issues.CreateLabel(ctx, "\n", "\n", input)
140 return err
141 })
142
143 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
144 got, resp, err := client.Issues.CreateLabel(ctx, "o", "r", input)
145 if got != nil {
146 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
147 }
148 return resp, err
149 })
150 }
151
152 func TestIssuesService_CreateLabel_invalidOwner(t *testing.T) {
153 client, _, _, teardown := setup()
154 defer teardown()
155
156 ctx := context.Background()
157 _, _, err := client.Issues.CreateLabel(ctx, "%", "%", nil)
158 testURLParseError(t, err)
159 }
160
161 func TestIssuesService_EditLabel(t *testing.T) {
162 client, mux, _, teardown := setup()
163 defer teardown()
164
165 input := &Label{Name: String("z")}
166
167 mux.HandleFunc("/repos/o/r/labels/n", func(w http.ResponseWriter, r *http.Request) {
168 v := new(Label)
169 json.NewDecoder(r.Body).Decode(v)
170
171 testMethod(t, r, "PATCH")
172 if !cmp.Equal(v, input) {
173 t.Errorf("Request body = %+v, want %+v", v, input)
174 }
175
176 fmt.Fprint(w, `{"url":"u"}`)
177 })
178
179 ctx := context.Background()
180 label, _, err := client.Issues.EditLabel(ctx, "o", "r", "n", input)
181 if err != nil {
182 t.Errorf("Issues.EditLabel returned error: %v", err)
183 }
184
185 want := &Label{URL: String("u")}
186 if !cmp.Equal(label, want) {
187 t.Errorf("Issues.EditLabel returned %+v, want %+v", label, want)
188 }
189
190 const methodName = "EditLabel"
191 testBadOptions(t, methodName, func() (err error) {
192 _, _, err = client.Issues.EditLabel(ctx, "\n", "\n", "\n", input)
193 return err
194 })
195
196 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
197 got, resp, err := client.Issues.EditLabel(ctx, "o", "r", "n", input)
198 if got != nil {
199 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
200 }
201 return resp, err
202 })
203 }
204
205 func TestIssuesService_EditLabel_invalidOwner(t *testing.T) {
206 client, _, _, teardown := setup()
207 defer teardown()
208
209 ctx := context.Background()
210 _, _, err := client.Issues.EditLabel(ctx, "%", "%", "%", nil)
211 testURLParseError(t, err)
212 }
213
214 func TestIssuesService_DeleteLabel(t *testing.T) {
215 client, mux, _, teardown := setup()
216 defer teardown()
217
218 mux.HandleFunc("/repos/o/r/labels/n", func(w http.ResponseWriter, r *http.Request) {
219 testMethod(t, r, "DELETE")
220 })
221
222 ctx := context.Background()
223 _, err := client.Issues.DeleteLabel(ctx, "o", "r", "n")
224 if err != nil {
225 t.Errorf("Issues.DeleteLabel returned error: %v", err)
226 }
227
228 const methodName = "DeleteLabel"
229 testBadOptions(t, methodName, func() (err error) {
230 _, err = client.Issues.DeleteLabel(ctx, "\n", "\n", "\n")
231 return err
232 })
233
234 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
235 return client.Issues.DeleteLabel(ctx, "o", "r", "n")
236 })
237 }
238
239 func TestIssuesService_DeleteLabel_invalidOwner(t *testing.T) {
240 client, _, _, teardown := setup()
241 defer teardown()
242
243 ctx := context.Background()
244 _, err := client.Issues.DeleteLabel(ctx, "%", "%", "%")
245 testURLParseError(t, err)
246 }
247
248 func TestIssuesService_ListLabelsByIssue(t *testing.T) {
249 client, mux, _, teardown := setup()
250 defer teardown()
251
252 mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) {
253 testMethod(t, r, "GET")
254 testFormValues(t, r, values{"page": "2"})
255 fmt.Fprint(w, `[{"name":"a","id":1},{"name":"b","id":2}]`)
256 })
257
258 opt := &ListOptions{Page: 2}
259 ctx := context.Background()
260 labels, _, err := client.Issues.ListLabelsByIssue(ctx, "o", "r", 1, opt)
261 if err != nil {
262 t.Errorf("Issues.ListLabelsByIssue returned error: %v", err)
263 }
264
265 want := []*Label{
266 {Name: String("a"), ID: Int64(1)},
267 {Name: String("b"), ID: Int64(2)},
268 }
269 if !cmp.Equal(labels, want) {
270 t.Errorf("Issues.ListLabelsByIssue returned %+v, want %+v", labels, want)
271 }
272
273 const methodName = "ListLabelsByIssue"
274 testBadOptions(t, methodName, func() (err error) {
275 _, _, err = client.Issues.ListLabelsByIssue(ctx, "\n", "\n", -1, opt)
276 return err
277 })
278
279 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
280 got, resp, err := client.Issues.ListLabelsByIssue(ctx, "o", "r", 1, opt)
281 if got != nil {
282 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
283 }
284 return resp, err
285 })
286 }
287
288 func TestIssuesService_ListLabelsByIssue_invalidOwner(t *testing.T) {
289 client, _, _, teardown := setup()
290 defer teardown()
291
292 ctx := context.Background()
293 _, _, err := client.Issues.ListLabelsByIssue(ctx, "%", "%", 1, nil)
294 testURLParseError(t, err)
295 }
296
297 func TestIssuesService_AddLabelsToIssue(t *testing.T) {
298 client, mux, _, teardown := setup()
299 defer teardown()
300
301 input := []string{"a", "b"}
302
303 mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) {
304 var v []string
305 json.NewDecoder(r.Body).Decode(&v)
306
307 testMethod(t, r, "POST")
308 if !cmp.Equal(v, input) {
309 t.Errorf("Request body = %+v, want %+v", v, input)
310 }
311
312 fmt.Fprint(w, `[{"url":"u"}]`)
313 })
314
315 ctx := context.Background()
316 labels, _, err := client.Issues.AddLabelsToIssue(ctx, "o", "r", 1, input)
317 if err != nil {
318 t.Errorf("Issues.AddLabelsToIssue returned error: %v", err)
319 }
320
321 want := []*Label{{URL: String("u")}}
322 if !cmp.Equal(labels, want) {
323 t.Errorf("Issues.AddLabelsToIssue returned %+v, want %+v", labels, want)
324 }
325
326 const methodName = "AddLabelsToIssue"
327 testBadOptions(t, methodName, func() (err error) {
328 _, _, err = client.Issues.AddLabelsToIssue(ctx, "\n", "\n", -1, input)
329 return err
330 })
331
332 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
333 got, resp, err := client.Issues.AddLabelsToIssue(ctx, "o", "r", 1, input)
334 if got != nil {
335 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
336 }
337 return resp, err
338 })
339 }
340
341 func TestIssuesService_AddLabelsToIssue_invalidOwner(t *testing.T) {
342 client, _, _, teardown := setup()
343 defer teardown()
344
345 ctx := context.Background()
346 _, _, err := client.Issues.AddLabelsToIssue(ctx, "%", "%", 1, nil)
347 testURLParseError(t, err)
348 }
349
350 func TestIssuesService_RemoveLabelForIssue(t *testing.T) {
351 client, mux, _, teardown := setup()
352 defer teardown()
353
354 mux.HandleFunc("/repos/o/r/issues/1/labels/l", func(w http.ResponseWriter, r *http.Request) {
355 testMethod(t, r, "DELETE")
356 })
357
358 ctx := context.Background()
359 _, err := client.Issues.RemoveLabelForIssue(ctx, "o", "r", 1, "l")
360 if err != nil {
361 t.Errorf("Issues.RemoveLabelForIssue returned error: %v", err)
362 }
363
364 const methodName = "RemoveLabelForIssue"
365 testBadOptions(t, methodName, func() (err error) {
366 _, err = client.Issues.RemoveLabelForIssue(ctx, "\n", "\n", -1, "\n")
367 return err
368 })
369
370 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
371 return client.Issues.RemoveLabelForIssue(ctx, "o", "r", 1, "l")
372 })
373 }
374
375 func TestIssuesService_RemoveLabelForIssue_invalidOwner(t *testing.T) {
376 client, _, _, teardown := setup()
377 defer teardown()
378
379 ctx := context.Background()
380 _, err := client.Issues.RemoveLabelForIssue(ctx, "%", "%", 1, "%")
381 testURLParseError(t, err)
382 }
383
384 func TestIssuesService_ReplaceLabelsForIssue(t *testing.T) {
385 client, mux, _, teardown := setup()
386 defer teardown()
387
388 input := []string{"a", "b"}
389
390 mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) {
391 var v []string
392 json.NewDecoder(r.Body).Decode(&v)
393
394 testMethod(t, r, "PUT")
395 if !cmp.Equal(v, input) {
396 t.Errorf("Request body = %+v, want %+v", v, input)
397 }
398
399 fmt.Fprint(w, `[{"url":"u"}]`)
400 })
401
402 ctx := context.Background()
403 labels, _, err := client.Issues.ReplaceLabelsForIssue(ctx, "o", "r", 1, input)
404 if err != nil {
405 t.Errorf("Issues.ReplaceLabelsForIssue returned error: %v", err)
406 }
407
408 want := []*Label{{URL: String("u")}}
409 if !cmp.Equal(labels, want) {
410 t.Errorf("Issues.ReplaceLabelsForIssue returned %+v, want %+v", labels, want)
411 }
412
413 const methodName = "ReplaceLabelsForIssue"
414 testBadOptions(t, methodName, func() (err error) {
415 _, _, err = client.Issues.ReplaceLabelsForIssue(ctx, "\n", "\n", -1, input)
416 return err
417 })
418
419 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
420 got, resp, err := client.Issues.ReplaceLabelsForIssue(ctx, "o", "r", 1, input)
421 if got != nil {
422 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
423 }
424 return resp, err
425 })
426 }
427
428 func TestIssuesService_ReplaceLabelsForIssue_invalidOwner(t *testing.T) {
429 client, _, _, teardown := setup()
430 defer teardown()
431
432 ctx := context.Background()
433 _, _, err := client.Issues.ReplaceLabelsForIssue(ctx, "%", "%", 1, nil)
434 testURLParseError(t, err)
435 }
436
437 func TestIssuesService_RemoveLabelsForIssue(t *testing.T) {
438 client, mux, _, teardown := setup()
439 defer teardown()
440
441 mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) {
442 testMethod(t, r, "DELETE")
443 })
444
445 ctx := context.Background()
446 _, err := client.Issues.RemoveLabelsForIssue(ctx, "o", "r", 1)
447 if err != nil {
448 t.Errorf("Issues.RemoveLabelsForIssue returned error: %v", err)
449 }
450
451 const methodName = "RemoveLabelsForIssue"
452 testBadOptions(t, methodName, func() (err error) {
453 _, err = client.Issues.RemoveLabelsForIssue(ctx, "\n", "\n", -1)
454 return err
455 })
456
457 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
458 return client.Issues.RemoveLabelsForIssue(ctx, "o", "r", 1)
459 })
460 }
461
462 func TestIssuesService_RemoveLabelsForIssue_invalidOwner(t *testing.T) {
463 client, _, _, teardown := setup()
464 defer teardown()
465
466 ctx := context.Background()
467 _, err := client.Issues.RemoveLabelsForIssue(ctx, "%", "%", 1)
468 testURLParseError(t, err)
469 }
470
471 func TestIssuesService_ListLabelsForMilestone(t *testing.T) {
472 client, mux, _, teardown := setup()
473 defer teardown()
474
475 mux.HandleFunc("/repos/o/r/milestones/1/labels", func(w http.ResponseWriter, r *http.Request) {
476 testMethod(t, r, "GET")
477 testFormValues(t, r, values{"page": "2"})
478 fmt.Fprint(w, `[{"name": "a"},{"name": "b"}]`)
479 })
480
481 opt := &ListOptions{Page: 2}
482 ctx := context.Background()
483 labels, _, err := client.Issues.ListLabelsForMilestone(ctx, "o", "r", 1, opt)
484 if err != nil {
485 t.Errorf("Issues.ListLabelsForMilestone returned error: %v", err)
486 }
487
488 want := []*Label{{Name: String("a")}, {Name: String("b")}}
489 if !cmp.Equal(labels, want) {
490 t.Errorf("Issues.ListLabelsForMilestone returned %+v, want %+v", labels, want)
491 }
492
493 const methodName = "ListLabelsForMilestone"
494 testBadOptions(t, methodName, func() (err error) {
495 _, _, err = client.Issues.ListLabelsForMilestone(ctx, "\n", "\n", -1, opt)
496 return err
497 })
498
499 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
500 got, resp, err := client.Issues.ListLabelsForMilestone(ctx, "o", "r", 1, opt)
501 if got != nil {
502 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
503 }
504 return resp, err
505 })
506 }
507
508 func TestIssuesService_ListLabelsForMilestone_invalidOwner(t *testing.T) {
509 client, _, _, teardown := setup()
510 defer teardown()
511
512 ctx := context.Background()
513 _, _, err := client.Issues.ListLabelsForMilestone(ctx, "%", "%", 1, nil)
514 testURLParseError(t, err)
515 }
516
517 func TestLabel_Marshal(t *testing.T) {
518 testJSONMarshal(t, &Label{}, "{}")
519
520 u := &Label{
521 ID: Int64(1),
522 URL: String("url"),
523 Name: String("name"),
524 Color: String("color"),
525 Description: String("desc"),
526 Default: Bool(false),
527 NodeID: String("nid"),
528 }
529
530 want := `{
531 "id": 1,
532 "url": "url",
533 "name": "name",
534 "color": "color",
535 "description": "desc",
536 "default": false,
537 "node_id": "nid"
538 }`
539
540 testJSONMarshal(t, u, want)
541 }
542
View as plain text