-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_test.go
200 lines (179 loc) · 5.2 KB
/
client_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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package tvdb_test
import (
"net/url"
"os"
"testing"
"github.com/pioz/tvdb"
"github.com/stretchr/testify/assert"
)
func TestClientLogin(t *testing.T) {
login(t)
}
func TestClientLoginFail(t *testing.T) {
c := tvdb.Client{Apikey: "WRONG APIKEY"}
err := c.Login()
if err == nil {
t.Fatal("Impossible!")
}
assert.True(t, tvdb.HaveCodeError(401, err))
}
func TestClientRefreshToken(t *testing.T) {
c := login(t)
err := c.RefreshToken()
if err != nil {
t.Fatal(err)
}
}
func TestClientGetLanguages(t *testing.T) {
c := login(t)
languages, err := c.GetLanguages()
if err != nil {
t.Fatal(err)
}
assert.True(t, len(languages) > 0, "Ensure more than 0 languages are returned.") //183 supported as of 08/03/2020
// assert.Equal(t, "English", languages[0].EnglishName) //disabled. English is not the firt language
}
func TestClientGetUpdates(t *testing.T) {
c := login(t)
updates, err := c.GetUpdates(1594509621) //Get all updates
assert.Nil(t, err)
t.Logf("%d shows need to be updated\n", len(updates))
}
func TestClientGetUpdatesOldDate(t *testing.T) {
c := login(t)
updates, err := c.GetUpdates(0) //Get all updates
assert.Nil(t, err)
t.Logf("%d shows need to be updated\n", len(updates))
assert.Equal(t, len(updates), 0, "TVDB does not return all shows updated since time 0, only works over past week")
}
func TestClientSearch(t *testing.T) {
c := login(t)
res, err := c.SearchByName("Game of Thrones")
if err != nil {
t.Fatal(err)
}
assert.GreaterOrEqual(t, len(res), 3, "A miniumum of 3 shows are found with 'Game of Thrones' in the name")
// assert.Equal(t, "Game of Thrones", res[0].SeriesName) //Flaky test, cannot ensure ordering of results
}
func TestClientSearchByImdbID(t *testing.T) {
c := login(t)
res, err := c.SearchByImdbID("tt0944947")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1, len(res))
assert.Equal(t, "Game of Thrones", res[0].SeriesName)
}
func TestClientBestSearch(t *testing.T) {
c := login(t)
res, err := c.BestSearch("Game of Thrones")
if err != nil {
t.Fatal(err)
}
assert.False(t, tvdb.HaveCodeError(404, err))
assert.Equal(t, "Game of Thrones", res.SeriesName)
res, err = c.BestSearch("kajdsfhasdkjhfsadkjhfasdkh")
assert.True(t, tvdb.HaveCodeError(404, err))
}
func TestClientGetSeries(t *testing.T) {
c := login(t)
s := getSerie(t, c, "Game of Thrones")
err := c.GetSeries(&s)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "tt0944947", s.ImdbID)
}
func TestClientGetSeriesActors(t *testing.T) {
c := login(t)
s := getSerie(t, c, "Game of Thrones")
err := c.GetSeriesActors(&s)
if err != nil {
t.Fatal(err)
}
assert.NotEqual(t, 0, len(s.Actors))
// assert.Equal(t, "Michelle Fairley", s.Actors[0].Name) //Flakey, cannot ensure ordering
}
func TestClientGetSeriesEpisodes(t *testing.T) {
c := login(t)
s := getSerie(t, c, "Game of Thrones")
err := c.GetSeriesEpisodes(&s, nil)
if err != nil {
t.Fatal(err)
}
assert.NotEqual(t, 0, len(s.Episodes))
assert.GreaterOrEqual(t, len(s.Episodes), 123) //Updated to cover the show continuing and having more eps
assert.Equal(t, "Winter Is Coming", s.GetEpisode(1, 1).EpisodeName)
assert.Equal(t, "The Mountain and the Viper", s.GetEpisode(4, 8).EpisodeName)
assert.Equal(t, "The Dragon and the Wolf", s.GetEpisode(7, 7).EpisodeName)
err = c.GetSeriesEpisodes(&s, url.Values{"airedSeason": {"2"}})
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 10, len(s.Episodes))
assert.Nil(t, s.GetEpisode(1, 1))
}
func TestClientGetSeriesSummary(t *testing.T) {
c := login(t)
s := getSerie(t, c, "Game of Thrones")
err := c.GetSeriesSummary(&s)
if err != nil {
t.Fatal(err)
}
assert.GreaterOrEqual(t, s.Summary.AiredEpisodes, "123", "Show must have at least 123 episodes") //Now at 127
assert.Equal(t, 9, len(s.Summary.AiredSeasons))
}
func TestClientGetSeriesPosterImages(t *testing.T) {
c := login(t)
s := getSerie(t, c, "Game of Thrones")
err := c.GetSeriesPosterImages(&s)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "posters/121361-1.jpg", s.Images[0].FileName)
assert.Equal(t, tvdb.ImageURL(s.Images[0].FileName), "https://thetvdb.com/banners/posters/121361-1.jpg")
}
func TestClientGetEpisode(t *testing.T) {
c := login(t)
s := getSerie(t, c, "Game of Thrones")
err := c.GetSeriesEpisodes(&s, url.Values{"airedSeason": {"1"}})
if err != nil {
t.Fatal(err)
}
err = c.GetEpisode(s.GetEpisode(1, 1))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "tt1480055", s.GetEpisode(1, 1).ImdbID)
}
func TestSeriesGetSeasonEpisodes(t *testing.T) {
c := login(t)
s := getSerie(t, c, "Game of Thrones")
assert.Equal(t, 0, len(s.GetSeasonEpisodes(2)))
err := c.GetSeriesEpisodes(&s, nil)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 10, len(s.GetSeasonEpisodes(2)))
}
func TestSeriesBannerURL(t *testing.T) {
t.Skip() //Image URL changed
c := login(t)
s := getSerie(t, c, "Game of Thrones")
assert.Equal(t, "https://thetvdb.com/banners/graphical/5c8c227dbd218.jpg", s.BannerURL())
}
func login(t *testing.T) tvdb.Client {
c := tvdb.Client{Apikey: os.Getenv("TVDB_APIKEY"), Language: "en"}
err := c.Login()
if err != nil {
t.Fatal(err)
}
return c
}
func getSerie(t *testing.T, c tvdb.Client, name string) tvdb.Series {
series, err := c.BestSearch(name)
if err != nil {
t.Fatal(err)
}
return series
}