Strengthening unit tests

This commit is contained in:
Agniva De Sarker 2016-06-15 12:18:48 +05:30
parent 58cb9dcc45
commit 0658b245d2
2 changed files with 126 additions and 24 deletions

View file

@ -19,6 +19,7 @@ const (
EpisodeSearch = "episode"
)
// QueryData is the type to create the search query
type QueryData struct {
Title string
Year string
@ -160,11 +161,11 @@ func requestAPI(apiCategory string, params ...string) (resp *http.Response, err
case "search":
parameters.Add("s", params[0])
parameters.Add("y", params[1])
// TODO: validate params to only the right options
parameters.Add("type", params[2])
case "title":
parameters.Add("t", params[0])
parameters.Add("y", params[1])
parameters.Add("type", params[2])
parameters.Add("plot", plot)
parameters.Add("tomatoes", tomatoes)
case "id":

View file

@ -2,37 +2,138 @@ package gomdb
import "testing"
func TestImdbSearchMovies(t *testing.T) {
query := &QueryData{Title: "Fight Club", Year: "1999", SearchType: MovieSearch}
resp, err := Search(query)
if err != nil {
t.Error(err)
return
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",
},
}
for i, item := range tests {
resp, err := Search(item.query)
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
}
if resp.Search[0].Title != "Fight Club" {
t.Error("Wrong Movie")
}
}
func TestImdbGetMovieByTitle(t *testing.T) {
query := &QueryData{Title: "Fight Club", Year: "1999"}
resp, err := MovieByTitle(query)
if err != nil {
t.Error(err)
return
func TestFailSearch(t *testing.T) {
tests := []struct {
query *QueryData
}{
{&QueryData{Title: "Game of Thrones", Year: "2001"}},
{&QueryData{Title: "Dexter", SearchType: EpisodeSearch}},
}
for i, item := range tests {
_, err := Search(item.query)
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, Got- %s", i, err)
continue
}
if resp.Title != "Fight Club" {
t.Error("Wrong Movie")
}
}
func TestImdbGetMovieByImdbID(t *testing.T) {
resp, err := MovieByImdbID("tt0137523")
func TestMovieByTitle(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",
},
}
for i, item := range tests {
resp, err := MovieByTitle(item.query)
if err != nil {
t.Error(err)
return
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
}
}
}
func TestMovieByImdbID(t *testing.T) {
tests := []struct {
id string
title string
year string
}{
{
"tt0137523",
"Fight Club",
"1999",
},
{
"tt1798709",
"Her",
"2013",
},
{
"tt2884018",
"Macbeth",
"2015",
},
}
for i, item := range tests {
resp, err := MovieByImdbID(item.id)
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
}
if resp.Title != "Fight Club" {
t.Error("Wrong Movie")
}
}