Skip to content

Commit 5473440

Browse files
committed
♻️ Extract test helpers
1 parent a0c03f9 commit 5473440

File tree

22 files changed

+305
-1614
lines changed

22 files changed

+305
-1614
lines changed

pkg/activity/activity_test.go

Lines changed: 11 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
package activity
55

66
import (
7-
"bytes"
8-
"fmt"
7+
"io"
98
"math"
109
"net/http"
1110
"reflect"
12-
"strings"
1311
"testing"
1412

1513
"github.com/eat-pray-ai/yutu/pkg/common"
@@ -325,32 +323,7 @@ func TestActivity_Get(t *testing.T) {
325323
}
326324

327325
func TestActivity_Get_Pagination(t *testing.T) {
328-
handler := func(w http.ResponseWriter, r *http.Request) {
329-
pageToken := r.URL.Query().Get("pageToken")
330-
w.Header().Set("Content-Type", "application/json")
331-
if pageToken == "" {
332-
items := make([]string, 20)
333-
for i := range 20 {
334-
items[i] = `{"id": "id"}`
335-
}
336-
jsonItems := "[" + strings.Join(items, ",") + "]"
337-
_, _ = fmt.Fprintf(
338-
w, `{
339-
"items": %s,
340-
"nextPageToken": "page-2"
341-
}`, jsonItems,
342-
)
343-
} else if pageToken == "page-2" {
344-
_, _ = w.Write(
345-
[]byte(`{
346-
"items": [{"id": "21"}, {"id": "22"}],
347-
"nextPageToken": ""
348-
}`),
349-
)
350-
}
351-
}
352-
svc := common.NewTestService(t, http.HandlerFunc(handler))
353-
326+
svc := common.NewTestService(t, common.PaginationHandler("activity"))
354327
a := NewActivity(
355328
WithService(svc),
356329
WithChannelId("channel-id"),
@@ -366,79 +339,16 @@ func TestActivity_Get_Pagination(t *testing.T) {
366339
}
367340

368341
func TestActivity_List(t *testing.T) {
369-
mockResponse := `{
370-
"items": [
371-
{
372-
"id": "activity-1",
373-
"snippet": {
374-
"title": "Activity 1",
375-
"type": "upload",
376-
"publishedAt": "2024-01-01T00:00:00Z"
377-
}
378-
}
379-
],
380-
"nextPageToken": ""
381-
}`
382-
383-
svc := common.NewTestService(
384-
t, http.HandlerFunc(
385-
func(w http.ResponseWriter, r *http.Request) {
386-
w.Header().Set("Content-Type", "application/json")
387-
_, _ = w.Write([]byte(mockResponse))
388-
},
389-
),
390-
)
391-
392-
tests := []struct {
393-
name string
394-
opts []Option
395-
output string
396-
wantErr bool
397-
}{
398-
{
399-
name: "list activities json",
400-
opts: []Option{
401-
WithService(svc),
402-
WithOutput("json"),
403-
WithChannelId("channel-id"),
404-
},
405-
output: "json",
406-
wantErr: false,
407-
},
408-
{
409-
name: "list activities yaml",
410-
opts: []Option{
342+
common.RunListTest(
343+
t,
344+
`{"items": [{"id": "activity-1", "snippet": {"title": "Activity 1", "type": "upload", "publishedAt": "2024-01-01T00:00:00Z"}}], "nextPageToken": ""}`,
345+
func(svc *youtube.Service, output string) func(io.Writer) error {
346+
a := NewActivity(
411347
WithService(svc),
412-
WithOutput("yaml"),
348+
WithOutput(output),
413349
WithChannelId("channel-id"),
414-
},
415-
output: "yaml",
416-
wantErr: false,
417-
},
418-
{
419-
name: "list activities table",
420-
opts: []Option{
421-
WithService(svc),
422-
WithOutput("table"),
423-
WithChannelId("channel-id"),
424-
},
425-
output: "table",
426-
wantErr: false,
350+
)
351+
return a.List
427352
},
428-
}
429-
430-
for _, tt := range tests {
431-
t.Run(
432-
tt.name, func(t *testing.T) {
433-
a := NewActivity(tt.opts...)
434-
var buf bytes.Buffer
435-
if err := a.List(&buf); (err != nil) != tt.wantErr {
436-
t.Errorf("Activity.List() error = %v, wantErr %v", err, tt.wantErr)
437-
}
438-
if buf.Len() == 0 {
439-
t.Errorf("Activity.List() output is empty")
440-
}
441-
},
442-
)
443-
}
353+
)
444354
}

pkg/auth/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ go_test(
2727
embed = [":auth"],
2828
deps = [
2929
"//pkg",
30+
"@org_golang_google_api//youtube/v3:youtube",
3031
"@org_golang_x_oauth2//:oauth2",
3132
],
3233
)

pkg/caption/caption_test.go

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"context"
99
"errors"
10+
"io"
1011
"net/http"
1112
"net/http/httptest"
1213
"os"
@@ -308,80 +309,18 @@ func TestCaption_Get(t *testing.T) {
308309
}
309310

310311
func TestCaption_List(t *testing.T) {
311-
svc := common.NewTestService(
312-
t, http.HandlerFunc(
313-
func(w http.ResponseWriter, r *http.Request) {
314-
w.Header().Set("Content-Type", "application/json")
315-
_, _ = w.Write(
316-
[]byte(`{
317-
"items": [
318-
{
319-
"id": "caption-1",
320-
"snippet": {
321-
"videoId": "video-1",
322-
"name": "English",
323-
"language": "en"
324-
}
325-
}
326-
]
327-
}`),
328-
)
329-
},
330-
),
331-
)
332-
333-
tests := []struct {
334-
name string
335-
opts []Option
336-
output string
337-
wantErr bool
338-
}{
339-
{
340-
name: "list captions json",
341-
opts: []Option{
342-
WithService(svc),
343-
WithOutput("json"),
344-
WithVideoId("video-1"),
345-
},
346-
output: "json",
347-
wantErr: false,
348-
},
349-
{
350-
name: "list captions yaml",
351-
opts: []Option{
312+
common.RunListTest(
313+
t,
314+
`{"items": [{"id": "caption-1", "snippet": {"videoId": "video-1", "name": "English", "language": "en"}}]}`,
315+
func(svc *youtube.Service, output string) func(io.Writer) error {
316+
c := NewCaption(
352317
WithService(svc),
353-
WithOutput("yaml"),
318+
WithOutput(output),
354319
WithVideoId("video-1"),
355-
},
356-
output: "yaml",
357-
wantErr: false,
320+
)
321+
return c.List
358322
},
359-
{
360-
name: "list captions table",
361-
opts: []Option{
362-
WithService(svc),
363-
WithOutput("table"),
364-
WithVideoId("video-1"),
365-
},
366-
output: "table",
367-
wantErr: false,
368-
},
369-
}
370-
371-
for _, tt := range tests {
372-
t.Run(
373-
tt.name, func(t *testing.T) {
374-
c := NewCaption(tt.opts...)
375-
var buf bytes.Buffer
376-
if err := c.List(&buf); (err != nil) != tt.wantErr {
377-
t.Errorf("Caption.List() error = %v, wantErr %v", err, tt.wantErr)
378-
}
379-
if buf.Len() == 0 {
380-
t.Errorf("Caption.List() output is empty")
381-
}
382-
},
383-
)
384-
}
323+
)
385324
}
386325

387326
func TestCaption_Insert(t *testing.T) {

pkg/channel/channel_test.go

Lines changed: 11 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ package channel
55

66
import (
77
"bytes"
8-
"fmt"
8+
"io"
99
"math"
1010
"net/http"
1111
"reflect"
12-
"strings"
1312
"testing"
1413

1514
"github.com/eat-pray-ai/yutu/pkg/common"
@@ -412,34 +411,7 @@ func TestChannel_Get(t *testing.T) {
412411
}
413412

414413
func TestChannel_Get_Pagination(t *testing.T) {
415-
handler := func(w http.ResponseWriter, r *http.Request) {
416-
pageToken := r.URL.Query().Get("pageToken")
417-
w.Header().Set("Content-Type", "application/json")
418-
if pageToken == "" {
419-
items := make([]string, 20)
420-
for i := range 20 {
421-
items[i] = fmt.Sprintf(`{"id": "channel-%d"}`, i)
422-
}
423-
_, _ = w.Write(
424-
fmt.Appendf(
425-
nil,
426-
`{
427-
"items": [%s],
428-
"nextPageToken": "page-2"
429-
}`, strings.Join(items, ","),
430-
),
431-
)
432-
} else if pageToken == "page-2" {
433-
_, _ = w.Write(
434-
[]byte(`{
435-
"items": [{"id": "channel-20"}, {"id": "channel-21"}],
436-
"nextPageToken": ""
437-
}`),
438-
)
439-
}
440-
}
441-
svc := common.NewTestService(t, http.HandlerFunc(handler))
442-
414+
svc := common.NewTestService(t, common.PaginationHandler("channel"))
443415
c := NewChannel(
444416
WithService(svc),
445417
WithMaxResults(22),
@@ -454,79 +426,18 @@ func TestChannel_Get_Pagination(t *testing.T) {
454426
}
455427

456428
func TestChannel_List(t *testing.T) {
457-
svc := common.NewTestService(
458-
t, http.HandlerFunc(
459-
func(w http.ResponseWriter, r *http.Request) {
460-
w.Header().Set("Content-Type", "application/json")
461-
_, _ = w.Write(
462-
[]byte(`{
463-
"items": [
464-
{
465-
"id": "channel-1",
466-
"snippet": {
467-
"title": "Channel 1",
468-
"country": "US"
469-
}
470-
}
471-
]
472-
}`),
473-
)
474-
},
475-
),
476-
)
477-
478-
tests := []struct {
479-
name string
480-
opts []Option
481-
output string
482-
wantErr bool
483-
}{
484-
{
485-
name: "list channels json",
486-
opts: []Option{
487-
WithService(svc),
488-
WithOutput("json"),
489-
WithIds([]string{"channel-1"}),
490-
},
491-
output: "json",
492-
wantErr: false,
493-
},
494-
{
495-
name: "list channels yaml",
496-
opts: []Option{
497-
WithService(svc),
498-
WithOutput("yaml"),
499-
WithIds([]string{"channel-1"}),
500-
},
501-
output: "yaml",
502-
wantErr: false,
503-
},
504-
{
505-
name: "list channels table",
506-
opts: []Option{
429+
common.RunListTest(
430+
t,
431+
`{"items": [{"id": "channel-1", "snippet": {"title": "Channel 1", "country": "US"}}]}`,
432+
func(svc *youtube.Service, output string) func(io.Writer) error {
433+
c := NewChannel(
507434
WithService(svc),
508-
WithOutput("table"),
435+
WithOutput(output),
509436
WithIds([]string{"channel-1"}),
510-
},
511-
output: "table",
512-
wantErr: false,
437+
)
438+
return c.List
513439
},
514-
}
515-
516-
for _, tt := range tests {
517-
t.Run(
518-
tt.name, func(t *testing.T) {
519-
c := NewChannel(tt.opts...)
520-
var buf bytes.Buffer
521-
if err := c.List(&buf); (err != nil) != tt.wantErr {
522-
t.Errorf("Channel.List() error = %v, wantErr %v", err, tt.wantErr)
523-
}
524-
if buf.Len() == 0 {
525-
t.Errorf("Channel.List() output is empty")
526-
}
527-
},
528-
)
529-
}
440+
)
530441
}
531442

532443
func TestChannel_Update(t *testing.T) {

0 commit comments

Comments
 (0)