File size: 4,987 Bytes
ca7217f | 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 | package testkit
import (
"fmt"
"io"
"math/rand"
"net/http"
"os"
"reflect"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/metatube-community/metatube-sdk-go/model"
mt "github.com/metatube-community/metatube-sdk-go/provider"
)
type internalTestSuite struct {
t *testing.T
}
func (s *internalTestSuite) T() *testing.T {
return s.t
}
func (s *internalTestSuite) testItems(items []string, call func(*testing.T, string)) {
for i, item := range items {
s.T().Run(item, func(t *testing.T) {
call(t, item)
})
if i < len(items)-1 {
// Add random delay milliseconds.
time.Sleep(time.Duration(rand.Intn(500)+500) * time.Millisecond)
}
}
}
func (s *internalTestSuite) testGetInfo(f any, items []string, vfs ...ValidateFunc) {
ff := func(item string) (info interface{ IsValid() bool }, err error) {
switch v := f.(type) {
case func(string) (*model.ActorInfo, error):
info, err = v(item)
case func(string) (*model.MovieInfo, error):
info, err = v(item)
default:
return nil, fmt.Errorf("invalid function type: %T", v)
}
return
}
s.testItems(items, func(t *testing.T, item string) {
info, err := ff(item)
require.NoError(t, err)
require.NotNil(t, info)
for _, vf := range append([]ValidateFunc{
logJSONContent(),
assertIsValid(),
}, vfs...) {
vf(t, info)
}
})
}
func (s *internalTestSuite) TestGetActorInfoByID(p mt.ActorProvider, items []string, vfs ...ValidateFunc) {
s.testGetInfo(p.GetActorInfoByID, items, vfs...)
}
func (s *internalTestSuite) TestGetActorInfoByURL(p mt.ActorProvider, items []string, vfs ...ValidateFunc) {
s.testGetInfo(p.GetActorInfoByURL, items, vfs...)
}
func (s *internalTestSuite) TestGetMovieInfoByID(p mt.MovieProvider, items []string, vfs ...ValidateFunc) {
s.testGetInfo(p.GetMovieInfoByID, items, vfs...)
}
func (s *internalTestSuite) TestGetMovieInfoByURL(p mt.MovieProvider, items []string, vfs ...ValidateFunc) {
s.testGetInfo(p.GetMovieInfoByURL, items, vfs...)
}
func (s *internalTestSuite) testGetMovieReviews(f func(string) ([]*model.MovieReviewDetail, error), items []string, vfs ...ValidateFunc) {
s.testItems(items, func(t *testing.T, item string) {
reviews, err := f(item)
require.NoError(t, err)
require.NotEmpty(t, reviews)
for _, vf := range append([]ValidateFunc{
logJSONContent(),
assertIsValid(),
}, vfs...) {
vf(t, reviews)
}
})
}
func (s *internalTestSuite) TestGetMovieReviewsByID(p mt.MovieReviewer, items []string, vfs ...ValidateFunc) {
s.testGetMovieReviews(p.GetMovieReviewsByID, items, vfs...)
}
func (s *internalTestSuite) TestGetMovieReviewsByURL(p mt.MovieReviewer, items []string, vfs ...ValidateFunc) {
s.testGetMovieReviews(p.GetMovieReviewsByURL, items, vfs...)
}
func (s *internalTestSuite) TestSearchActor(p mt.ActorSearcher, items []string, vfs ...ValidateFunc) {
s.testItems(items, func(t *testing.T, item string) {
results, err := p.SearchActor(item)
require.NoError(t, err)
require.NotEmpty(t, results)
for _, vf := range append([]ValidateFunc{
logJSONContent(),
assertIsValid(),
}, vfs...) {
vf(t, results)
}
})
}
func (s *internalTestSuite) TestSearchMovie(p mt.MovieSearcher, items []string, vfs ...ValidateFunc) {
s.testItems(items, func(t *testing.T, item string) {
results, err := p.SearchMovie(p.NormalizeMovieKeyword(item))
require.NoError(t, err)
require.NotEmpty(t, results)
for _, vf := range append([]ValidateFunc{
logJSONContent(),
assertIsValid(),
}, vfs...) {
vf(t, results)
}
})
}
func (s *internalTestSuite) TestFetch(p mt.Fetcher, items []string, vfs ...ValidateFunc) {
s.testItems(items, func(t *testing.T, item string) {
resp, err := p.Fetch(item)
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, http.StatusOK, resp.StatusCode)
data, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
assert.NotEmpty(t, data)
for _, vf := range append([]ValidateFunc{
logJSONContent(),
}, vfs...) {
vf(t, data)
}
})
}
func Test[T mt.Provider](t *testing.T, new func() T, items []string, vfs ...ValidateFunc) {
if ci, _ := strconv.ParseBool(os.Getenv("GITHUB_ACTIONS")); ci {
t.SkipNow() // Skip in GitHub Actions
}
functionName := getFrame(1).Function
providerName, testMethod, err := parseTestFunction(functionName)
require.NoError(t, err)
provider := new()
structName := reflect.TypeOf(provider).Elem().Name()
require.Equal(t, providerName, structName)
s := &internalTestSuite{t}
m := reflect.ValueOf(s).MethodByName("Test" + testMethod)
require.Truef(t, m.IsValid(), "invalid test method: %s", testMethod)
// Build test function args.
args := []reflect.Value{
reflect.ValueOf(provider),
reflect.ValueOf(items),
}
for _, vf := range vfs {
args = append(args, reflect.ValueOf(vf))
}
// Make the test function call.
results := m.Call(args)
require.Empty(t, results)
}
|