gomdb/gomdb_test.go

207 lines
4.3 KiB
Go
Raw Normal View History

2015-06-26 20:07:46 +00:00
package gomdb
2015-06-26 17:36:33 +00:00
2017-05-24 12:33:12 +00:00
import (
"os"
"testing"
)
var apiKey = os.Getenv("OMDB_API_KEY")
func TestNoKey(t *testing.T) {
api := Init("")
2017-05-24 12:33:12 +00:00
_, err := api.Search(&QueryData{Title: "Her"})
if err == nil {
t.Errorf("Expected to fail")
}
if err != nil {
expectedErrorMsg := "Status Code 401 received from IMDB"
if err.Error() != expectedErrorMsg {
t.Errorf("Expected- %s, Got- %s", expectedErrorMsg, err)
}
}
}
2015-06-26 17:36:33 +00:00
2016-06-15 06:48:48 +00:00
func TestSearch(t *testing.T) {
tests := []struct {
query *QueryData
title string
year string
}{
{&QueryData{Title: "Fight Club", Year: "1999", SearchType: MovieSearch},
"Fight Club",
"1999",
},
{&QueryData{Title: "Her"},
"Her",
"2013",
},
{&QueryData{Title: "Macbeth", Year: "2015"},
"Macbeth",
"2015",
},
2015-06-26 17:36:33 +00:00
}
2017-05-24 12:33:12 +00:00
api := Init(apiKey)
2016-06-15 06:48:48 +00:00
for i, item := range tests {
2017-05-24 12:33:12 +00:00
resp, err := api.Search(item.query)
2016-06-15 06:48:48 +00:00
if err != nil {
t.Errorf("Test[%d]: %s", i, err)
continue
}
if resp.Search[0].Title != item.title {
t.Errorf("Test[%d]: Expected- %s, Got- %s", i, item.title, resp.Search[0].Title)
continue
}
if resp.Search[0].Year != item.year {
t.Errorf("Test[%d]: Expected- %s, Got- %s", i, item.year, resp.Search[0].Year)
continue
}
}
}
func TestFailSearch(t *testing.T) {
tests := []struct {
query *QueryData
}{
{&QueryData{Title: "Game of Thrones", Year: "2001"}},
{&QueryData{Title: "Dexter", SearchType: EpisodeSearch}},
}
2017-05-24 12:33:12 +00:00
api := Init(apiKey)
2016-06-15 06:48:48 +00:00
for i, item := range tests {
2017-05-24 12:33:12 +00:00
_, err := api.Search(item.query)
2016-06-15 06:48:48 +00:00
if err == nil {
t.Errorf("Test[%d]: Got nil error", i)
continue
}
// Checking for strings is bad. But the API might change.
if err.Error() != "Movie not found!" {
t.Errorf("Test[%d]: Unexpected value- %s", i, err)
2016-06-15 06:48:48 +00:00
continue
}
2015-06-26 17:36:33 +00:00
}
}
2016-06-15 06:59:01 +00:00
func TestInvalidCategory(t *testing.T) {
tests := []struct {
query *QueryData
}{
{&QueryData{Title: "Game of Thrones", Year: "2001", SearchType: "bad"}},
{&QueryData{Title: "Dexter", SearchType: "bad"}},
}
2017-05-24 12:33:12 +00:00
api := Init(apiKey)
2016-06-15 06:59:01 +00:00
for i, item := range tests {
2017-05-24 12:33:12 +00:00
_, err := api.Search(item.query)
2016-06-15 06:59:01 +00:00
if err == nil {
t.Errorf("Test[%d]: Got nil error", i)
continue
}
// Checking for strings is bad. But the error type is formatted
if err.Error() != "Invalid search category- bad" {
t.Errorf("Test[%d]: Unexpected value- %s", i, err)
2016-06-15 06:59:01 +00:00
continue
}
}
}
func TestMediaByTitle(t *testing.T) {
2016-06-15 06:48:48 +00:00
tests := []struct {
query *QueryData
title string
year string
}{
{&QueryData{Title: "Fight Club", Year: "1999", SearchType: MovieSearch},
"Fight Club",
"1999",
},
{&QueryData{Title: "Her"},
"Her",
"2013",
},
{&QueryData{Title: "Macbeth", Year: "2015"},
"Macbeth",
"2015",
},
{
&QueryData{Title: "Rick and Morty", Season: "1", SearchType: SeriesSearch},
"Rick and Morty",
"2013",
},
{
&QueryData{Title: "Rick and Morty", Season: "1", Episode: "8", SearchType: EpisodeSearch},
"Rixty Minutes",
"2014",
},
2015-06-26 17:36:33 +00:00
}
2016-06-15 06:48:48 +00:00
2017-05-24 12:33:12 +00:00
api := Init(apiKey)
2016-06-15 06:48:48 +00:00
for i, item := range tests {
2017-05-24 12:33:12 +00:00
resp, err := api.MovieByTitle(item.query)
2016-06-15 06:48:48 +00:00
if err != nil {
t.Errorf("Test[%d]: %s", i, err)
continue
}
if resp.Title != item.title {
t.Errorf("Test[%d]: Expected- %s, Got- %s", i, item.title, resp.Title)
continue
}
if resp.Year != item.year {
t.Errorf("Test[%d]: Expected- %s, Got- %s", i, item.year, resp.Year)
continue
}
2015-06-26 17:36:33 +00:00
}
}
func TestMediaByImdbID(t *testing.T) {
2016-06-15 06:48:48 +00:00
tests := []struct {
query *QueryData
2016-06-15 06:48:48 +00:00
title string
year string
}{
{&QueryData{ImdbId: "tt0137523", SearchType: MovieSearch},
2016-06-15 06:48:48 +00:00
"Fight Club",
"1999",
},
{&QueryData{ImdbId: "tt1798709", SearchType: MovieSearch},
2016-06-15 06:48:48 +00:00
"Her",
"2013",
},
{&QueryData{ImdbId: "tt2884018", SearchType: MovieSearch},
2016-06-15 06:48:48 +00:00
"Macbeth",
"2015",
},
{&QueryData{ImdbId: "tt3952222", Season: "1", SearchType: SeriesSearch},
"Killjoys",
"2015",
},
{&QueryData{ImdbId: "tt0944947", Season: "1", Episode: "1", SearchType: EpisodeSearch},
"Winter Is Coming",
"2011",
},
2015-06-26 17:36:33 +00:00
}
2016-06-15 06:48:48 +00:00
2017-05-24 12:33:12 +00:00
api := Init(apiKey)
2016-06-15 06:48:48 +00:00
for i, item := range tests {
resp, err := api.MovieByImdbID(item.query)
2016-06-15 06:48:48 +00:00
if err != nil {
t.Errorf("Test[%d]: %s", i, err)
continue
}
if resp.Title != item.title {
t.Errorf("Test[%d]: Expected- %s, Got- %s", i, item.title, resp.Title)
continue
}
if resp.Year != item.year {
t.Errorf("Test[%d]: Expected- %s, Got- %s", i, item.year, resp.Year)
continue
}
2015-06-26 17:36:33 +00:00
}
}