♻️ Extract test helpers

This commit is contained in:
OpenWaygate committed 2026-04-07 21:18:53 +08:00
1 parent a0c03f986a
commit 54734409c7
22 files changed
+328 -1637

No files matched your search

+13 -103
View File
@@ -4,12 +4,10 @@
package activity
import (
"bytes"
"fmt"
"io"
"math"
"net/http"
"reflect"
"strings"
"testing"
"github.com/eat-pray-ai/yutu/pkg/common"
@@ -325,32 +323,7 @@ func TestActivity_Get(t *testing.T) {
}
func TestActivity_Get_Pagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
pageToken := r.URL.Query().Get("pageToken")
w.Header().Set("Content-Type", "application/json")
if pageToken == "" {
items := make([]string, 20)
for i := range 20 {
items[i] = `{"id": "id"}`
}
jsonItems := "[" + strings.Join(items, ",") + "]"
_, _ = fmt.Fprintf(
w, `{
"items": %s,
"nextPageToken": "page-2"
}`, jsonItems,
)
} else if pageToken == "page-2" {
_, _ = w.Write(
[]byte(`{
"items": [{"id": "21"}, {"id": "22"}],
"nextPageToken": ""
}`),
)
}
}
svc := common.NewTestService(t, http.HandlerFunc(handler))
svc := common.NewTestService(t, common.PaginationHandler("activity"))
a := NewActivity(
WithService(svc),
WithChannelId("channel-id"),
@@ -366,79 +339,16 @@ func TestActivity_Get_Pagination(t *testing.T) {
}
func TestActivity_List(t *testing.T) {
mockResponse := `{
"items": [
{
"id": "activity-1",
"snippet": {
"title": "Activity 1",
"type": "upload",
"publishedAt": "2024-01-01T00:00:00Z"
}
}
],
"nextPageToken": ""
}`
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(mockResponse))
},
),
common.RunListTest(
t,
`{"items": [{"id": "activity-1", "snippet": {"title": "Activity 1", "type": "upload", "publishedAt": "2024-01-01T00:00:00Z"}}], "nextPageToken": ""}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
a := NewActivity(
WithService(svc),
WithOutput(output),
WithChannelId("channel-id"),
)
return a.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list activities json",
opts: []Option{
WithService(svc),
WithOutput("json"),
WithChannelId("channel-id"),
},
output: "json",
wantErr: false,
},
{
name: "list activities yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
WithChannelId("channel-id"),
},
output: "yaml",
wantErr: false,
},
{
name: "list activities table",
opts: []Option{
WithService(svc),
WithOutput("table"),
WithChannelId("channel-id"),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
a := NewActivity(tt.opts...)
var buf bytes.Buffer
if err := a.List(&buf); (err != nil) != tt.wantErr {
t.Errorf("Activity.List() error = %v, wantErr %v", err, tt.wantErr)
}
if buf.Len() == 0 {
t.Errorf("Activity.List() output is empty")
}
},
)
}
}
+1
View File
@@ -27,6 +27,7 @@ go_test(
embed = [":auth"],
deps = [
"//pkg",
"@org_golang_google_api//youtube/v3:youtube",
"@org_golang_x_oauth2//:oauth2",
],
)
+12 -73
View File
@@ -7,6 +7,7 @@ import (
"bytes"
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"os"
@@ -308,80 +309,18 @@ func TestCaption_Get(t *testing.T) {
}
func TestCaption_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
"items": [
{
"id": "caption-1",
"snippet": {
"videoId": "video-1",
"name": "English",
"language": "en"
}
}
]
}`),
)
},
),
common.RunListTest(
t,
`{"items": [{"id": "caption-1", "snippet": {"videoId": "video-1", "name": "English", "language": "en"}}]}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
c := NewCaption(
WithService(svc),
WithOutput(output),
WithVideoId("video-1"),
)
return c.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list captions json",
opts: []Option{
WithService(svc),
WithOutput("json"),
WithVideoId("video-1"),
},
output: "json",
wantErr: false,
},
{
name: "list captions yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
WithVideoId("video-1"),
},
output: "yaml",
wantErr: false,
},
{
name: "list captions table",
opts: []Option{
WithService(svc),
WithOutput("table"),
WithVideoId("video-1"),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
c := NewCaption(tt.opts...)
var buf bytes.Buffer
if err := c.List(&buf); (err != nil) != tt.wantErr {
t.Errorf("Caption.List() error = %v, wantErr %v", err, tt.wantErr)
}
if buf.Len() == 0 {
t.Errorf("Caption.List() output is empty")
}
},
)
}
}
func TestCaption_Insert(t *testing.T) {
+13 -102
View File
@@ -5,11 +5,10 @@ package channel
import (
"bytes"
"fmt"
"io"
"math"
"net/http"
"reflect"
"strings"
"testing"
"github.com/eat-pray-ai/yutu/pkg/common"
@@ -412,34 +411,7 @@ func TestChannel_Get(t *testing.T) {
}
func TestChannel_Get_Pagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
pageToken := r.URL.Query().Get("pageToken")
w.Header().Set("Content-Type", "application/json")
if pageToken == "" {
items := make([]string, 20)
for i := range 20 {
items[i] = fmt.Sprintf(`{"id": "channel-%d"}`, i)
}
_, _ = w.Write(
fmt.Appendf(
nil,
`{
"items": [%s],
"nextPageToken": "page-2"
}`, strings.Join(items, ","),
),
)
} else if pageToken == "page-2" {
_, _ = w.Write(
[]byte(`{
"items": [{"id": "channel-20"}, {"id": "channel-21"}],
"nextPageToken": ""
}`),
)
}
}
svc := common.NewTestService(t, http.HandlerFunc(handler))
svc := common.NewTestService(t, common.PaginationHandler("channel"))
c := NewChannel(
WithService(svc),
WithMaxResults(22),
@@ -454,79 +426,18 @@ func TestChannel_Get_Pagination(t *testing.T) {
}
func TestChannel_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
"items": [
{
"id": "channel-1",
"snippet": {
"title": "Channel 1",
"country": "US"
}
}
]
}`),
)
},
),
common.RunListTest(
t,
`{"items": [{"id": "channel-1", "snippet": {"title": "Channel 1", "country": "US"}}]}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
c := NewChannel(
WithService(svc),
WithOutput(output),
WithIds([]string{"channel-1"}),
)
return c.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list channels json",
opts: []Option{
WithService(svc),
WithOutput("json"),
WithIds([]string{"channel-1"}),
},
output: "json",
wantErr: false,
},
{
name: "list channels yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
WithIds([]string{"channel-1"}),
},
output: "yaml",
wantErr: false,
},
{
name: "list channels table",
opts: []Option{
WithService(svc),
WithOutput("table"),
WithIds([]string{"channel-1"}),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
c := NewChannel(tt.opts...)
var buf bytes.Buffer
if err := c.List(&buf); (err != nil) != tt.wantErr {
t.Errorf("Channel.List() error = %v, wantErr %v", err, tt.wantErr)
}
if buf.Len() == 0 {
t.Errorf("Channel.List() output is empty")
}
},
)
}
}
func TestChannel_Update(t *testing.T) {
+7 -65
View File
@@ -5,6 +5,7 @@ package channelSection
import (
"bytes"
"io"
"net/http"
"reflect"
"testing"
@@ -260,12 +261,7 @@ func TestChannelSection_Get(t *testing.T) {
}
func TestChannelSection_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
common.RunListTest(t, `{
"items": [
{
"id": "section-1",
@@ -275,66 +271,12 @@ func TestChannelSection_List(t *testing.T) {
}
}
]
}`),
)
},
),
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
cs := NewChannelSection(WithService(svc), WithOutput(output), WithChannelId("channel-1"))
return cs.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list channel sections json",
opts: []Option{
WithService(svc),
WithOutput("json"),
WithChannelId("channel-1"),
},
output: "json",
wantErr: false,
},
{
name: "list channel sections yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
WithChannelId("channel-1"),
},
output: "yaml",
wantErr: false,
},
{
name: "list channel sections table",
opts: []Option{
WithService(svc),
WithOutput("table"),
WithChannelId("channel-1"),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
cs := NewChannelSection(tt.opts...)
var buf bytes.Buffer
if err := cs.List(&buf); (err != nil) != tt.wantErr {
t.Errorf(
"ChannelSection.List() error = %v, wantErr %v", err, tt.wantErr,
)
}
if buf.Len() == 0 {
t.Errorf("ChannelSection.List() output is empty")
}
},
)
}
}
func TestChannelSection_Delete(t *testing.T) {
+15 -96
View File
@@ -5,11 +5,10 @@ package comment
import (
"bytes"
"fmt"
"io"
"math"
"net/http"
"reflect"
"strings"
"testing"
"github.com/eat-pray-ai/yutu/pkg/common"
@@ -278,34 +277,7 @@ func TestComment_Get(t *testing.T) {
}
func TestComment_Get_Pagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
pageToken := r.URL.Query().Get("pageToken")
w.Header().Set("Content-Type", "application/json")
if pageToken == "" {
items := make([]string, 20)
for i := range 20 {
items[i] = fmt.Sprintf(`{"id": "comment-%d"}`, i)
}
_, _ = w.Write(
fmt.Appendf(
nil,
`{
"items": [%s],
"nextPageToken": "page-2"
}`, strings.Join(items, ","),
),
)
} else if pageToken == "page-2" {
_, _ = w.Write(
[]byte(`{
"items": [{"id": "comment-20"}, {"id": "comment-21"}],
"nextPageToken": ""
}`),
)
}
}
svc := common.NewTestService(t, http.HandlerFunc(handler))
svc := common.NewTestService(t, common.PaginationHandler("comment"))
c := NewComment(
WithService(svc),
WithMaxResults(22),
@@ -320,12 +292,9 @@ func TestComment_Get_Pagination(t *testing.T) {
}
func TestComment_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
common.RunListTest(
t,
`{
"items": [
{
"id": "comment-1",
@@ -336,67 +305,17 @@ func TestComment_List(t *testing.T) {
}
}
]
}`),
)
},
),
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
c := NewComment(
WithService(svc),
WithOutput(output),
WithIds([]string{"comment-1"}),
WithMaxResults(1),
)
return c.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list comments json",
opts: []Option{
WithService(svc),
WithOutput("json"),
WithIds([]string{"comment-1"}),
WithMaxResults(1),
},
output: "json",
wantErr: false,
},
{
name: "list comments yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
WithIds([]string{"comment-1"}),
WithMaxResults(1),
},
output: "yaml",
wantErr: false,
},
{
name: "list comments table",
opts: []Option{
WithService(svc),
WithOutput("table"),
WithIds([]string{"comment-1"}),
WithMaxResults(1),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
c := NewComment(tt.opts...)
var buf bytes.Buffer
if err := c.List(&buf); (err != nil) != tt.wantErr {
t.Errorf("Comment.List() error = %v, wantErr %v", err, tt.wantErr)
}
if buf.Len() == 0 {
t.Errorf("Comment.List() output is empty")
}
},
)
}
}
func TestComment_Insert(t *testing.T) {
+15 -98
View File
@@ -5,11 +5,10 @@ package commentThread
import (
"bytes"
"fmt"
"io"
"math"
"net/http"
"reflect"
"strings"
"testing"
"github.com/eat-pray-ai/yutu/pkg/common"
@@ -308,34 +307,7 @@ func TestCommentThread_Get(t *testing.T) {
}
func TestCommentThread_Get_Pagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
pageToken := r.URL.Query().Get("pageToken")
w.Header().Set("Content-Type", "application/json")
if pageToken == "" {
items := make([]string, 20)
for i := range 20 {
items[i] = fmt.Sprintf(`{"id": "thread-%d"}`, i)
}
_, _ = w.Write(
fmt.Appendf(
nil,
`{
"items": [%s],
"nextPageToken": "page-2"
}`, strings.Join(items, ","),
),
)
} else if pageToken == "page-2" {
_, _ = w.Write(
[]byte(`{
"items": [{"id": "thread-20"}, {"id": "thread-21"}],
"nextPageToken": ""
}`),
)
}
}
svc := common.NewTestService(t, http.HandlerFunc(handler))
svc := common.NewTestService(t, common.PaginationHandler("thread"))
c := NewCommentThread(
WithService(svc),
WithMaxResults(22),
@@ -350,12 +322,9 @@ func TestCommentThread_Get_Pagination(t *testing.T) {
}
func TestCommentThread_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
common.RunListTest(
t,
`{
"items": [
{
"id": "thread-1",
@@ -370,69 +339,17 @@ func TestCommentThread_List(t *testing.T) {
}
}
]
}`),
)
},
),
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
c := NewCommentThread(
WithService(svc),
WithOutput(output),
WithIds([]string{"thread-1"}),
WithMaxResults(1),
)
return c.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list comment threads json",
opts: []Option{
WithService(svc),
WithOutput("json"),
WithIds([]string{"thread-1"}),
WithMaxResults(1),
},
output: "json",
wantErr: false,
},
{
name: "list comment threads yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
WithIds([]string{"thread-1"}),
WithMaxResults(1),
},
output: "yaml",
wantErr: false,
},
{
name: "list comment threads table",
opts: []Option{
WithService(svc),
WithOutput("table"),
WithIds([]string{"thread-1"}),
WithMaxResults(1),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
c := NewCommentThread(tt.opts...)
var buf bytes.Buffer
if err := c.List(&buf); (err != nil) != tt.wantErr {
t.Errorf(
"CommentThread.List() error = %v, wantErr %v", err, tt.wantErr,
)
}
if buf.Len() == 0 {
t.Errorf("CommentThread.List() output is empty")
}
},
)
}
}
func TestCommentThread_Insert(t *testing.T) {
+11 -1
View File
@@ -1,4 +1,4 @@
load("@rules_go//go:def.bzl", "go_library")
load("@rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "common",
@@ -18,3 +18,13 @@ go_library(
"@org_golang_google_api//youtube/v3:youtube",
],
)
go_test(
name = "common_test",
srcs = ["common_test.go"],
embed = [":common"],
deps = [
"@com_github_jedib0t_go_pretty_v6//table",
"@org_golang_google_api//youtube/v3:youtube",
],
)
+77
View File
@@ -4,9 +4,13 @@
package common
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"google.golang.org/api/option"
@@ -30,3 +34,76 @@ func NewTestService(t *testing.T, handler http.Handler) *youtube.Service {
}
return svc
}
// PaginationHandler returns an http.HandlerFunc that simulates 2-page pagination.
// idPrefix is used for item IDs (e.g., "channel", "video").
// An optional itemFmt function can override the default item JSON template;
// it receives the idPrefix and index, and returns the JSON for one item.
func PaginationHandler(idPrefix string, itemFmt ...func(string, int) string) http.HandlerFunc {
fmtItem := func(prefix string, i int) string {
return fmt.Sprintf(`{"id": "%s-%d"}`, prefix, i)
}
if len(itemFmt) > 0 && itemFmt[0] != nil {
fmtItem = itemFmt[0]
}
return func(w http.ResponseWriter, r *http.Request) {
pageToken := r.URL.Query().Get("pageToken")
w.Header().Set("Content-Type", "application/json")
if pageToken == "" {
items := make([]string, 20)
for i := range 20 {
items[i] = fmtItem(idPrefix, i)
}
_, _ = w.Write(
fmt.Appendf(
nil,
`{"items": [%s], "nextPageToken": "page-2"}`,
strings.Join(items, ","),
),
)
} else if pageToken == "page-2" {
_, _ = w.Write(
[]byte(
fmt.Sprintf(
`{"items": [%s, %s], "nextPageToken": ""}`,
fmtItem(idPrefix, 20), fmtItem(idPrefix, 21),
),
),
)
}
}
}
// RunListTest runs the standard json/yaml/table output test matrix.
// mockResponse is the JSON the mock server returns.
// listFn receives a *youtube.Service and output format, returns a function
// that calls List on the resource with a writer.
func RunListTest(
t *testing.T,
mockResponse string,
listFn func(svc *youtube.Service, output string) func(io.Writer) error,
) {
t.Helper()
svc := NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(mockResponse))
},
),
)
for _, output := range []string{"json", "yaml", "table"} {
t.Run(
"list "+output, func(t *testing.T) {
var buf bytes.Buffer
if err := listFn(svc, output)(&buf); err != nil {
t.Errorf("List(%s) error = %v", output, err)
}
if buf.Len() == 0 {
t.Errorf("List(%s) output is empty", output)
}
},
)
}
}
+7 -61
View File
@@ -4,7 +4,7 @@
package i18nLanguage
import (
"bytes"
"io"
"net/http"
"reflect"
"testing"
@@ -135,12 +135,7 @@ func TestI18nLanguage_Get(t *testing.T) {
}
func TestI18nLanguage_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
common.RunListTest(t, `{
"items": [
{
"id": "en",
@@ -150,59 +145,10 @@ func TestI18nLanguage_List(t *testing.T) {
}
}
]
}`),
)
},
),
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
i := NewI18nLanguage(WithService(svc), WithOutput(output))
return i.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list i18n languages json",
opts: []Option{
WithService(svc),
WithOutput("json"),
},
output: "json",
wantErr: false,
},
{
name: "list i18n languages yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
},
output: "yaml",
wantErr: false,
},
{
name: "list i18n languages table",
opts: []Option{
WithService(svc),
WithOutput("table"),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
i := NewI18nLanguage(tt.opts...)
var buf bytes.Buffer
if err := i.List(&buf); (err != nil) != tt.wantErr {
t.Errorf("I18nLanguage.List() error = %v, wantErr %v", err, tt.wantErr)
}
if buf.Len() == 0 {
t.Errorf("I18nLanguage.List() output is empty")
}
},
)
}
}
+7 -61
View File
@@ -4,7 +4,7 @@
package i18nRegion
import (
"bytes"
"io"
"net/http"
"reflect"
"testing"
@@ -135,12 +135,7 @@ func TestI18nRegion_Get(t *testing.T) {
}
func TestI18nRegion_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
common.RunListTest(t, `{
"items": [
{
"id": "US",
@@ -150,59 +145,10 @@ func TestI18nRegion_List(t *testing.T) {
}
}
]
}`),
)
},
),
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
i := NewI18nRegion(WithService(svc), WithOutput(output))
return i.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list i18n regions json",
opts: []Option{
WithService(svc),
WithOutput("json"),
},
output: "json",
wantErr: false,
},
{
name: "list i18n regions yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
},
output: "yaml",
wantErr: false,
},
{
name: "list i18n regions table",
opts: []Option{
WithService(svc),
WithOutput("table"),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
i := NewI18nRegion(tt.opts...)
var buf bytes.Buffer
if err := i.List(&buf); (err != nil) != tt.wantErr {
t.Errorf("I18nRegion.List() error = %v, wantErr %v", err, tt.wantErr)
}
if buf.Len() == 0 {
t.Errorf("I18nRegion.List() output is empty")
}
},
)
}
}
+23 -101
View File
@@ -4,12 +4,10 @@
package member
import (
"bytes"
"fmt"
"io"
"math"
"net/http"
"reflect"
"strings"
"testing"
"github.com/eat-pray-ai/yutu/pkg/common"
@@ -233,33 +231,7 @@ func TestMember_Get(t *testing.T) {
}
func TestMember_Get_Pagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
pageToken := r.URL.Query().Get("pageToken")
w.Header().Set("Content-Type", "application/json")
if pageToken == "" {
items := make([]string, 20)
for i := range 20 {
items[i] = `{"kind": "youtube#member"}`
}
_, _ = w.Write(
fmt.Appendf(
nil,
`{
"items": [%s],
"nextPageToken": "page-2"
}`, strings.Join(items, ","),
),
)
} else if pageToken == "page-2" {
_, _ = w.Write(
[]byte(`{
"items": [{"kind": "youtube#member"}, {"kind": "youtube#member"}],
"nextPageToken": ""
}`),
)
}
}
svc := common.NewTestService(t, http.HandlerFunc(handler))
svc := common.NewTestService(t, common.PaginationHandler("member"))
m := NewMember(
WithService(svc),
@@ -275,78 +247,28 @@ func TestMember_Get_Pagination(t *testing.T) {
}
func TestMember_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
"items": [
{
"snippet": {
"memberDetails": {
"channelId": "channel-1",
"displayName": "Member 1"
}
mockResponse := `{
"items": [
{
"snippet": {
"memberDetails": {
"channelId": "channel-1",
"displayName": "Member 1"
}
}
]
}`),
)
},
),
}
]
}`
common.RunListTest(
t, mockResponse,
func(svc *youtube.Service, output string) func(io.Writer) error {
m := NewMember(
WithService(svc),
WithOutput(output),
WithMaxResults(1),
)
return m.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list members json",
opts: []Option{
WithService(svc),
WithOutput("json"),
WithMaxResults(1),
},
output: "json",
wantErr: false,
},
{
name: "list members yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
WithMaxResults(1),
},
output: "yaml",
wantErr: false,
},
{
name: "list members table",
opts: []Option{
WithService(svc),
WithOutput("table"),
WithMaxResults(1),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
m := NewMember(tt.opts...)
var buf bytes.Buffer
if err := m.List(&buf); (err != nil) != tt.wantErr {
t.Errorf("Member.List() error = %v, wantErr %v", err, tt.wantErr)
}
if buf.Len() == 0 {
t.Errorf("Member.List() output is empty")
}
},
)
}
}
+7 -63
View File
@@ -4,7 +4,7 @@
package membershipsLevel
import (
"bytes"
"io"
"net/http"
"reflect"
"testing"
@@ -100,12 +100,7 @@ func TestMembershipsLevel_Get(t *testing.T) {
}
func TestMembershipsLevel_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
common.RunListTest(t, `{
"items": [
{
"id": "level-1",
@@ -116,61 +111,10 @@ func TestMembershipsLevel_List(t *testing.T) {
}
}
]
}`),
)
},
),
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
m := NewMembershipsLevel(WithService(svc), WithOutput(output))
return m.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list memberships levels json",
opts: []Option{
WithService(svc),
WithOutput("json"),
},
output: "json",
wantErr: false,
},
{
name: "list memberships levels yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
},
output: "yaml",
wantErr: false,
},
{
name: "list memberships levels table",
opts: []Option{
WithService(svc),
WithOutput("table"),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
m := NewMembershipsLevel(tt.opts...)
var buf bytes.Buffer
if err := m.List(&buf); (err != nil) != tt.wantErr {
t.Errorf(
"MembershipsLevel.List() error = %v, wantErr %v", err, tt.wantErr,
)
}
if buf.Len() == 0 {
t.Errorf("MembershipsLevel.List() output is empty")
}
},
)
}
}
+9 -96
View File
@@ -6,7 +6,7 @@ package playlist
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"reflect"
@@ -322,38 +322,8 @@ func TestPlaylist_Get(t *testing.T) {
}
func TestPlaylist_Get_Pagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
pageToken := r.URL.Query().Get("pageToken")
w.Header().Set("Content-Type", "application/json")
if pageToken == "" {
items := make([]string, 20)
for i := range 20 {
items[i] = fmt.Sprintf(`{"id": "playlist-%d"}`, i)
}
_, _ = w.Write(
fmt.Appendf(
nil,
`{
"items": [%s],
"nextPageToken": "page-2"
}`, strings.Join(items, ","),
),
)
} else if pageToken == "page-2" {
_, _ = w.Write(
[]byte(`{
"items": [{"id": "playlist-20"}, {"id": "playlist-21"}],
"nextPageToken": ""
}`),
)
}
}
svc := common.NewTestService(t, http.HandlerFunc(handler))
p := NewPlaylist(
WithService(svc),
WithMaxResults(22),
)
svc := common.NewTestService(t, common.PaginationHandler("playlist"))
p := NewPlaylist(WithService(svc), WithMaxResults(22))
got, err := p.Get()
if err != nil {
t.Errorf("Playlist.Get() error = %v", err)
@@ -364,12 +334,7 @@ func TestPlaylist_Get_Pagination(t *testing.T) {
}
func TestPlaylist_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
common.RunListTest(t, `{
"items": [
{
"id": "playlist-1",
@@ -379,64 +344,12 @@ func TestPlaylist_List(t *testing.T) {
}
}
]
}`),
)
},
),
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
p := NewPlaylist(WithService(svc), WithOutput(output), WithIds([]string{"playlist-1"}))
return p.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list playlists json",
opts: []Option{
WithService(svc),
WithOutput("json"),
WithIds([]string{"playlist-1"}),
},
output: "json",
wantErr: false,
},
{
name: "list playlists yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
WithIds([]string{"playlist-1"}),
},
output: "yaml",
wantErr: false,
},
{
name: "list playlists table",
opts: []Option{
WithService(svc),
WithOutput("table"),
WithIds([]string{"playlist-1"}),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
p := NewPlaylist(tt.opts...)
var buf bytes.Buffer
if err := p.List(&buf); (err != nil) != tt.wantErr {
t.Errorf("Playlist.List() error = %v, wantErr %v", err, tt.wantErr)
}
if buf.Len() == 0 {
t.Errorf("Playlist.List() output is empty")
}
},
)
}
}
func TestPlaylist_Insert(t *testing.T) {
+9 -95
View File
@@ -5,7 +5,7 @@ package playlistImage
import (
"bytes"
"fmt"
"io"
"math"
"net/http"
"os"
@@ -250,38 +250,8 @@ func TestPlaylistImage_Get(t *testing.T) {
}
func TestPlaylistImage_Get_Pagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
pageToken := r.URL.Query().Get("pageToken")
w.Header().Set("Content-Type", "application/json")
if pageToken == "" {
items := make([]string, 20)
for i := range 20 {
items[i] = fmt.Sprintf(`{"id": "image-%d"}`, i)
}
_, _ = w.Write(
fmt.Appendf(
nil,
`{
"items": [%s],
"nextPageToken": "page-2"
}`, strings.Join(items, ","),
),
)
} else if pageToken == "page-2" {
_, _ = w.Write(
[]byte(`{
"items": [{"id": "image-20"}, {"id": "image-21"}],
"nextPageToken": ""
}`),
)
}
}
svc := common.NewTestService(t, http.HandlerFunc(handler))
pi := NewPlaylistImage(
WithService(svc),
WithMaxResults(22),
)
svc := common.NewTestService(t, common.PaginationHandler("image"))
pi := NewPlaylistImage(WithService(svc), WithMaxResults(22))
got, err := pi.Get()
if err != nil {
t.Errorf("PlaylistImage.Get() error = %v", err)
@@ -292,12 +262,7 @@ func TestPlaylistImage_Get_Pagination(t *testing.T) {
}
func TestPlaylistImage_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
common.RunListTest(t, `{
"items": [
{
"id": "image-1",
@@ -308,63 +273,12 @@ func TestPlaylistImage_List(t *testing.T) {
}
}
]
}`),
)
},
),
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
pi := NewPlaylistImage(WithService(svc), WithOutput(output))
return pi.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list playlist images json",
opts: []Option{
WithService(svc),
WithOutput("json"),
},
output: "json",
wantErr: false,
},
{
name: "list playlist images yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
},
output: "yaml",
wantErr: false,
},
{
name: "list playlist images table",
opts: []Option{
WithService(svc),
WithOutput("table"),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
pi := NewPlaylistImage(tt.opts...)
var buf bytes.Buffer
if err := pi.List(&buf); (err != nil) != tt.wantErr {
t.Errorf(
"PlaylistImage.List() error = %v, wantErr %v", err, tt.wantErr,
)
}
if buf.Len() == 0 {
t.Errorf("PlaylistImage.List() output is empty")
}
},
)
}
}
func TestPlaylistImage_Insert(t *testing.T) {
+9 -96
View File
@@ -5,7 +5,7 @@ package playlistItem
import (
"bytes"
"fmt"
"io"
"math"
"net/http"
"reflect"
@@ -286,38 +286,8 @@ func TestPlaylistItem_Get(t *testing.T) {
}
func TestPlaylistItem_Get_Pagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
pageToken := r.URL.Query().Get("pageToken")
w.Header().Set("Content-Type", "application/json")
if pageToken == "" {
items := make([]string, 20)
for i := range 20 {
items[i] = fmt.Sprintf(`{"id": "item-%d"}`, i)
}
_, _ = w.Write(
fmt.Appendf(
nil,
`{
"items": [%s],
"nextPageToken": "page-2"
}`, strings.Join(items, ","),
),
)
} else if pageToken == "page-2" {
_, _ = w.Write(
[]byte(`{
"items": [{"id": "item-20"}, {"id": "item-21"}],
"nextPageToken": ""
}`),
)
}
}
svc := common.NewTestService(t, http.HandlerFunc(handler))
pi := NewPlaylistItem(
WithService(svc),
WithMaxResults(22),
)
svc := common.NewTestService(t, common.PaginationHandler("item"))
pi := NewPlaylistItem(WithService(svc), WithMaxResults(22))
got, err := pi.Get()
if err != nil {
t.Errorf("PlaylistItem.Get() error = %v", err)
@@ -328,12 +298,7 @@ func TestPlaylistItem_Get_Pagination(t *testing.T) {
}
func TestPlaylistItem_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
common.RunListTest(t, `{
"items": [
{
"id": "item-1",
@@ -346,64 +311,12 @@ func TestPlaylistItem_List(t *testing.T) {
}
}
]
}`),
)
},
),
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
pi := NewPlaylistItem(WithService(svc), WithOutput(output), WithIds([]string{"item-1"}))
return pi.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list playlist items json",
opts: []Option{
WithService(svc),
WithOutput("json"),
WithIds([]string{"item-1"}),
},
output: "json",
wantErr: false,
},
{
name: "list playlist items yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
WithIds([]string{"item-1"}),
},
output: "yaml",
wantErr: false,
},
{
name: "list playlist items table",
opts: []Option{
WithService(svc),
WithOutput("table"),
WithIds([]string{"item-1"}),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
pi := NewPlaylistItem(tt.opts...)
var buf bytes.Buffer
if err := pi.List(&buf); (err != nil) != tt.wantErr {
t.Errorf("PlaylistItem.List() error = %v, wantErr %v", err, tt.wantErr)
}
if buf.Len() == 0 {
t.Errorf("PlaylistItem.List() output is empty")
}
},
)
}
}
func TestPlaylistItem_Insert(t *testing.T) {
+20 -92
View File
@@ -4,8 +4,8 @@
package search
import (
"bytes"
"fmt"
"io"
"math"
"net/http"
"reflect"
@@ -446,34 +446,13 @@ func TestSearch_Get(t *testing.T) {
}
func TestSearch_Get_Pagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
pageToken := r.URL.Query().Get("pageToken")
w.Header().Set("Content-Type", "application/json")
if pageToken == "" {
items := make([]string, 20)
for i := range 20 {
items[i] = fmt.Sprintf(`{"id": {"videoId": "video-%d"}}`, i)
}
_, _ = w.Write(
fmt.Appendf(
nil,
`{
"items": [%s],
"nextPageToken": "page-2"
}`, strings.Join(items, ","),
),
)
} else if pageToken == "page-2" {
_, _ = w.Write(
[]byte(`{
"items": [{"id": {"videoId": "video-20"}}, {"id": {"videoId": "video-21"}}],
"nextPageToken": ""
}`),
)
}
}
svc := common.NewTestService(t, http.HandlerFunc(handler))
svc := common.NewTestService(
t, common.PaginationHandler(
"search", func(prefix string, i int) string {
return fmt.Sprintf(`{"id": {"videoId": "%s-%d"}}`, prefix, i)
},
),
)
s := NewSearch(
WithService(svc),
WithMaxResults(22),
@@ -488,12 +467,9 @@ func TestSearch_Get_Pagination(t *testing.T) {
}
func TestSearch_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
common.RunListTest(
t,
`{
"items": [
{
"id": {
@@ -505,62 +481,14 @@ func TestSearch_List(t *testing.T) {
}
}
]
}`),
)
},
),
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
s := NewSearch(
WithService(svc),
WithOutput(output),
WithQ("test"),
)
return s.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list search results json",
opts: []Option{
WithService(svc),
WithOutput("json"),
WithQ("test"),
},
output: "json",
wantErr: false,
},
{
name: "list search results yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
WithQ("test"),
},
output: "yaml",
wantErr: false,
},
{
name: "list search results table",
opts: []Option{
WithService(svc),
WithOutput("table"),
WithQ("test"),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
s := NewSearch(tt.opts...)
var buf bytes.Buffer
if err := s.List(&buf); (err != nil) != tt.wantErr {
t.Errorf("Search.List() error = %v, wantErr %v", err, tt.wantErr)
}
if buf.Len() == 0 {
t.Errorf("Search.List() output is empty")
}
},
)
}
}
+9 -97
View File
@@ -6,11 +6,10 @@ package subscription
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"reflect"
"strings"
"testing"
"github.com/eat-pray-ai/yutu/pkg/common"
@@ -351,38 +350,8 @@ func TestSubscription_Get(t *testing.T) {
}
func TestSubscription_Get_Pagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
pageToken := r.URL.Query().Get("pageToken")
w.Header().Set("Content-Type", "application/json")
if pageToken == "" {
items := make([]string, 20)
for i := range 20 {
items[i] = fmt.Sprintf(`{"id": "sub-%d"}`, i)
}
_, _ = w.Write(
fmt.Appendf(
nil,
`{
"items": [%s],
"nextPageToken": "page-2"
}`, strings.Join(items, ","),
),
)
} else if pageToken == "page-2" {
_, _ = w.Write(
[]byte(`{
"items": [{"id": "sub-20"}, {"id": "sub-21"}],
"nextPageToken": ""
}`),
)
}
}
svc := common.NewTestService(t, http.HandlerFunc(handler))
s := NewSubscription(
WithService(svc),
WithMaxResults(22),
)
svc := common.NewTestService(t, common.PaginationHandler("sub"))
s := NewSubscription(WithService(svc), WithMaxResults(22))
got, err := s.Get()
if err != nil {
t.Errorf("Subscription.Get() error = %v", err)
@@ -393,12 +362,7 @@ func TestSubscription_Get_Pagination(t *testing.T) {
}
func TestSubscription_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
common.RunListTest(t, `{
"items": [
{
"id": "sub-1",
@@ -411,64 +375,12 @@ func TestSubscription_List(t *testing.T) {
}
}
]
}`),
)
},
),
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
s := NewSubscription(WithService(svc), WithOutput(output), WithIds([]string{"sub-1"}))
return s.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list subscriptions json",
opts: []Option{
WithService(svc),
WithOutput("json"),
WithIds([]string{"sub-1"}),
},
output: "json",
wantErr: false,
},
{
name: "list subscriptions yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
WithIds([]string{"sub-1"}),
},
output: "yaml",
wantErr: false,
},
{
name: "list subscriptions table",
opts: []Option{
WithService(svc),
WithOutput("table"),
WithIds([]string{"sub-1"}),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
s := NewSubscription(tt.opts...)
var buf bytes.Buffer
if err := s.List(&buf); (err != nil) != tt.wantErr {
t.Errorf("Subscription.List() error = %v, wantErr %v", err, tt.wantErr)
}
if buf.Len() == 0 {
t.Errorf("Subscription.List() output is empty")
}
},
)
}
}
func TestSubscription_Insert(t *testing.T) {
+9 -97
View File
@@ -4,12 +4,10 @@
package superChatEvent
import (
"bytes"
"fmt"
"io"
"math"
"net/http"
"reflect"
"strings"
"testing"
"github.com/eat-pray-ai/yutu/pkg/common"
@@ -190,38 +188,8 @@ func TestSuperChatEvent_Get(t *testing.T) {
}
func TestSuperChatEvent_Get_Pagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
pageToken := r.URL.Query().Get("pageToken")
w.Header().Set("Content-Type", "application/json")
if pageToken == "" {
items := make([]string, 20)
for i := range 20 {
items[i] = fmt.Sprintf(`{"id": "event-%d"}`, i)
}
_, _ = w.Write(
fmt.Appendf(
nil,
`{
"items": [%s],
"nextPageToken": "page-2"
}`, strings.Join(items, ","),
),
)
} else if pageToken == "page-2" {
_, _ = w.Write(
[]byte(`{
"items": [{"id": "event-20"}, {"id": "event-21"}],
"nextPageToken": ""
}`),
)
}
}
svc := common.NewTestService(t, http.HandlerFunc(handler))
s := NewSuperChatEvent(
WithService(svc),
WithMaxResults(22),
)
svc := common.NewTestService(t, common.PaginationHandler("event"))
s := NewSuperChatEvent(WithService(svc), WithMaxResults(22))
got, err := s.Get()
if err != nil {
t.Errorf("SuperChatEvent.Get() error = %v", err)
@@ -232,12 +200,7 @@ func TestSuperChatEvent_Get_Pagination(t *testing.T) {
}
func TestSuperChatEvent_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
common.RunListTest(t, `{
"items": [
{
"id": "event-1",
@@ -250,61 +213,10 @@ func TestSuperChatEvent_List(t *testing.T) {
}
}
]
}`),
)
},
),
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
s := NewSuperChatEvent(WithService(svc), WithOutput(output))
return s.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list super chat events json",
opts: []Option{
WithService(svc),
WithOutput("json"),
},
output: "json",
wantErr: false,
},
{
name: "list super chat events yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
},
output: "yaml",
wantErr: false,
},
{
name: "list super chat events table",
opts: []Option{
WithService(svc),
WithOutput("table"),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
s := NewSuperChatEvent(tt.opts...)
var buf bytes.Buffer
if err := s.List(&buf); (err != nil) != tt.wantErr {
t.Errorf(
"SuperChatEvent.List() error = %v, wantErr %v", err, tt.wantErr,
)
}
if buf.Len() == 0 {
t.Errorf("SuperChatEvent.List() output is empty")
}
},
)
}
}
+26 -102
View File
@@ -6,7 +6,7 @@ package video
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math"
"mime"
"mime/multipart"
@@ -401,33 +401,7 @@ func TestVideo_Get(t *testing.T) {
}
func TestVideo_Get_Pagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
pageToken := r.URL.Query().Get("pageToken")
w.Header().Set("Content-Type", "application/json")
if pageToken == "" {
items := make([]string, 20)
for i := range 20 {
items[i] = fmt.Sprintf(`{"id": "video-%d"}`, i)
}
_, _ = w.Write(
fmt.Appendf(
nil,
`{
"items": [%s],
"nextPageToken": "page-2"
}`, strings.Join(items, ","),
),
)
} else if pageToken == "page-2" {
_, _ = w.Write(
[]byte(`{
"items": [{"id": "video-20"}, {"id": "video-21"}],
"nextPageToken": ""
}`),
)
}
}
svc := common.NewTestService(t, http.HandlerFunc(handler))
svc := common.NewTestService(t, common.PaginationHandler("video"))
v := NewVideo(
WithService(svc),
@@ -443,82 +417,32 @@ func TestVideo_Get_Pagination(t *testing.T) {
}
func TestVideo_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
"items": [
{
"id": "video-1",
"snippet": {
"channelId": "channel-1",
"title": "Video 1"
},
"statistics": {
"viewCount": 100
}
mockResponse := `{
"items": [
{
"id": "video-1",
"snippet": {
"channelId": "channel-1",
"title": "Video 1"
},
"statistics": {
"viewCount": 100
}
]
}`),
)
},
),
}
]
}`
common.RunListTest(
t, mockResponse,
func(svc *youtube.Service, output string) func(io.Writer) error {
v := NewVideo(
WithService(svc),
WithOutput(output),
WithIds([]string{"video-1"}),
)
return v.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list videos json",
opts: []Option{
WithService(svc),
WithOutput("json"),
WithIds([]string{"video-1"}),
},
output: "json",
wantErr: false,
},
{
name: "list videos yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
WithIds([]string{"video-1"}),
},
output: "yaml",
wantErr: false,
},
{
name: "list videos table",
opts: []Option{
WithService(svc),
WithOutput("table"),
WithIds([]string{"video-1"}),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
v := NewVideo(tt.opts...)
var buf bytes.Buffer
if err := v.List(&buf); (err != nil) != tt.wantErr {
t.Errorf("Video.List() error = %v, wantErr %v", err, tt.wantErr)
}
if buf.Len() == 0 {
t.Errorf("Video.List() output is empty")
}
},
)
}
}
func TestVideo_Insert(t *testing.T) {
@@ -4,7 +4,7 @@
package videoAbuseReportReason
import (
"bytes"
"io"
"net/http"
"reflect"
"testing"
@@ -165,12 +165,7 @@ func TestVideoAbuseReportReason_Get(t *testing.T) {
}
func TestVideoAbuseReportReason_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
common.RunListTest(t, `{
"items": [
{
"id": "reason-1",
@@ -179,62 +174,10 @@ func TestVideoAbuseReportReason_List(t *testing.T) {
}
}
]
}`),
)
},
),
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
va := NewVideoAbuseReportReason(WithService(svc), WithOutput(output))
return va.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list video abuse report reasons json",
opts: []Option{
WithService(svc),
WithOutput("json"),
},
output: "json",
wantErr: false,
},
{
name: "list video abuse report reasons yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
},
output: "yaml",
wantErr: false,
},
{
name: "list video abuse report reasons table",
opts: []Option{
WithService(svc),
WithOutput("table"),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
va := NewVideoAbuseReportReason(tt.opts...)
var buf bytes.Buffer
if err := va.List(&buf); (err != nil) != tt.wantErr {
t.Errorf(
"VideoAbuseReportReason.List() error = %v, wantErr %v", err,
tt.wantErr,
)
}
if buf.Len() == 0 {
t.Errorf("VideoAbuseReportReason.List() output is empty")
}
},
)
}
}
+22 -74
View File
@@ -4,7 +4,7 @@
package videoCategory
import (
"bytes"
"io"
"net/http"
"reflect"
"testing"
@@ -187,79 +187,27 @@ func TestVideoCategory_Get(t *testing.T) {
}
func TestVideoCategory_List(t *testing.T) {
svc := common.NewTestService(
t, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(
[]byte(`{
"items": [
{
"id": "category-1",
"snippet": {
"title": "Category 1",
"assignable": true
}
mockResponse := `{
"items": [
{
"id": "category-1",
"snippet": {
"title": "Category 1",
"assignable": true
}
]
}`),
)
},
),
}
]
}`
common.RunListTest(
t, mockResponse,
func(svc *youtube.Service, output string) func(io.Writer) error {
vc := NewVideoCategory(
WithService(svc),
WithOutput(output),
WithIds([]string{"category-1"}),
)
return vc.List
},
)
tests := []struct {
name string
opts []Option
output string
wantErr bool
}{
{
name: "list video categories json",
opts: []Option{
WithService(svc),
WithOutput("json"),
WithIds([]string{"category-1"}),
},
output: "json",
wantErr: false,
},
{
name: "list video categories yaml",
opts: []Option{
WithService(svc),
WithOutput("yaml"),
WithIds([]string{"category-1"}),
},
output: "yaml",
wantErr: false,
},
{
name: "list video categories table",
opts: []Option{
WithService(svc),
WithOutput("table"),
WithIds([]string{"category-1"}),
},
output: "table",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
vc := NewVideoCategory(tt.opts...)
var buf bytes.Buffer
if err := vc.List(&buf); (err != nil) != tt.wantErr {
t.Errorf(
"VideoCategory.List() error = %v, wantErr %v", err, tt.wantErr,
)
}
if buf.Len() == 0 {
t.Errorf("VideoCategory.List() output is empty")
}
},
)
}
}