-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytsearch_test.go
100 lines (77 loc) · 2.2 KB
/
ytsearch_test.go
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
package ytsearch
import (
"context"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_Search(t *testing.T) {
t.Run("with default http client", func(t *testing.T) {
c := Client{}
res, err := c.Search("nocopyrightsounds")
// no errors
assert.NoError(t, err)
// response is not empty
assert.NotEmpty(t, res.Results, "results are empty")
// continuation key is not empty
assert.NotEmpty(t, res.Continuation, "continuation token is empty")
})
t.Run("with custom http client", func(t *testing.T) {
httpclient := &http.Client{
Timeout: time.Nanosecond * 1,
}
// client
c := Client{
HTTPClient: httpclient,
}
res, err := c.Search("nocopyrightsounds")
assert.ErrorContains(t, err, context.DeadlineExceeded.Error())
assert.Empty(t, res)
})
}
func Test_SearchWithContext(t *testing.T) {
t.Run("context with insufficent timeout", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*1)
// client
c := Client{}
res, err := c.SearchWithContext(ctx, "nocopyrightsounds")
assert.ErrorIs(t, err, context.DeadlineExceeded)
assert.Empty(t, res.Results)
// clean up
t.Cleanup(func() {
cancel()
})
})
t.Run("context with sufficent timeout", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
// client
c := Client{}
res, err := c.SearchWithContext(ctx, "nocopyrightsounds")
assert.NoError(t, err)
assert.NotEmpty(t, res.Results)
// clean up
t.Cleanup(func() {
cancel()
})
})
}
func Test_Next(t *testing.T) {
t.Run("valid continuation token", func(t *testing.T) {
c := Client{}
res, err := c.Search("hacker")
assert.NoError(t, err)
assert.NotEmpty(t, res.Results, "results")
assert.NotEmpty(t, res.Continuation, "continuation token")
// debug
t.Log("results length:", len(res.Results))
t.Log("continuation token:", res.Continuation)
res, err = c.Next(res.Continuation)
assert.NoError(t, err)
assert.NotEmpty(t, res.Results, "next results")
assert.NotEmpty(t, res.Continuation, "next continuation token")
// debug
t.Log("next results length:", len(res.Results))
t.Log("next continuation token:", res.Continuation)
})
}