From 0658b245d2006dfda9c68bc892700e5ef7eec048 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Wed, 15 Jun 2016 12:18:48 +0530 Subject: [PATCH] Strengthening unit tests --- gomdb.go | 3 +- gomdb_test.go | 147 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 126 insertions(+), 24 deletions(-) diff --git a/gomdb.go b/gomdb.go index a8ff877..4a72693 100644 --- a/gomdb.go +++ b/gomdb.go @@ -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": diff --git a/gomdb_test.go b/gomdb_test.go index 09226f4..42a53fd 100644 --- a/gomdb_test.go +++ b/gomdb_test.go @@ -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", + }, } - if resp.Search[0].Title != "Fight Club" { - t.Error("Wrong Movie") + + 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 + } } } -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}}, } - if resp.Title != "Fight Club" { - t.Error("Wrong Movie") + + 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 + } } } -func TestImdbGetMovieByImdbID(t *testing.T) { - resp, err := MovieByImdbID("tt0137523") - if err != nil { - t.Error(err) - return +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", + }, } - if resp.Title != "Fight Club" { - t.Error("Wrong Movie") + + for i, item := range tests { + resp, err := MovieByTitle(item.query) + 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 + } + } +} + +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 + } } }