- Cleanup baseURL

- Extend QueryData to be able to use s and i queries with Season and
  Episode parameters
- Exnted MovieByImdbID() to include Season and Episode where
  appropriate,
- Refactor MovieByImdbID() to use QueryData as per standard API which
  also permits SearchType, Season and Episode filters
- Update unit tests for API changes
This commit is contained in:
Dinko Korunic 2019-03-19 10:41:02 +01:00
parent 8a1e0abd44
commit 4319c85503
2 changed files with 45 additions and 16 deletions

View file

@ -10,7 +10,7 @@ import (
) )
const ( const (
baseURL = "http://www.omdbapi.com/?" baseURL = "http://www.omdbapi.com"
plot = "full" plot = "full"
tomatoes = "true" tomatoes = "true"
@ -33,6 +33,8 @@ type QueryData struct {
Year string Year string
ImdbId string ImdbId string
SearchType string SearchType string
Season string
Episode string
} }
//SearchResult is the type for the search results //SearchResult is the type for the search results
@ -114,7 +116,8 @@ func (api *OmdbApi) Search(query *QueryData) (*SearchResponse, error) {
//MovieByTitle returns a MovieResult given Title //MovieByTitle returns a MovieResult given Title
func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) { func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) {
resp, err := api.requestAPI("title", query.Title, query.Year, query.SearchType) resp, err := api.requestAPI("title", query.Title, query.Year, query.SearchType, query.Season,
query.Episode)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -133,8 +136,9 @@ func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) {
} }
//MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381" //MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381"
func (api *OmdbApi) MovieByImdbID(id string) (*MovieResult, error) { func (api *OmdbApi) MovieByImdbID(query *QueryData) (*MovieResult, error) {
resp, err := api.requestAPI("id", id) resp, err := api.requestAPI("id", query.ImdbId, query.Year, query.SearchType, query.Season,
query.Episode)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -184,10 +188,15 @@ func (api *OmdbApi) requestAPI(apiCategory string, params ...string) (resp *http
parameters.Add("t", params[0]) parameters.Add("t", params[0])
parameters.Add("y", params[1]) parameters.Add("y", params[1])
parameters.Add("type", params[2]) parameters.Add("type", params[2])
parameters.Add("Season", params[3])
parameters.Add("Episode", params[4])
parameters.Add("plot", plot) parameters.Add("plot", plot)
parameters.Add("tomatoes", tomatoes) parameters.Add("tomatoes", tomatoes)
case "id": case "id":
parameters.Add("i", params[0]) parameters.Add("i", params[0])
parameters.Add("type", params[2])
parameters.Add("Season", params[3])
parameters.Add("Episode", params[4])
parameters.Add("plot", plot) parameters.Add("plot", plot)
parameters.Add("tomatoes", tomatoes) parameters.Add("tomatoes", tomatoes)
} }

View file

@ -9,6 +9,7 @@ var apiKey = os.Getenv("OMDB_API_KEY")
func TestNoKey(t *testing.T) { func TestNoKey(t *testing.T) {
api := Init("") api := Init("")
_, err := api.Search(&QueryData{Title: "Her"}) _, err := api.Search(&QueryData{Title: "Her"})
if err == nil { if err == nil {
t.Errorf("Expected to fail") t.Errorf("Expected to fail")
@ -40,7 +41,9 @@ func TestSearch(t *testing.T) {
"2015", "2015",
}, },
} }
api := Init(apiKey) api := Init(apiKey)
for i, item := range tests { for i, item := range tests {
resp, err := api.Search(item.query) resp, err := api.Search(item.query)
if err != nil { if err != nil {
@ -67,6 +70,7 @@ func TestFailSearch(t *testing.T) {
} }
api := Init(apiKey) api := Init(apiKey)
for i, item := range tests { for i, item := range tests {
_, err := api.Search(item.query) _, err := api.Search(item.query)
if err == nil { if err == nil {
@ -75,7 +79,7 @@ func TestFailSearch(t *testing.T) {
} }
// Checking for strings is bad. But the API might change. // Checking for strings is bad. But the API might change.
if err.Error() != "Movie not found!" { if err.Error() != "Movie not found!" {
t.Errorf("Test[%d]: Unexpected value- %s, Got- %s", i, err) t.Errorf("Test[%d]: Unexpected value- %s", i, err)
continue continue
} }
} }
@ -90,6 +94,7 @@ func TestInvalidCategory(t *testing.T) {
} }
api := Init(apiKey) api := Init(apiKey)
for i, item := range tests { for i, item := range tests {
_, err := api.Search(item.query) _, err := api.Search(item.query)
if err == nil { if err == nil {
@ -98,13 +103,13 @@ func TestInvalidCategory(t *testing.T) {
} }
// Checking for strings is bad. But the error type is formatted // Checking for strings is bad. But the error type is formatted
if err.Error() != "Invalid search category- bad" { if err.Error() != "Invalid search category- bad" {
t.Errorf("Test[%d]: Unexpected value- %s, Got- %s", i, err) t.Errorf("Test[%d]: Unexpected value- %s", i, err)
continue continue
} }
} }
} }
func TestMovieByTitle(t *testing.T) { func TestMediaByTitle(t *testing.T) {
tests := []struct { tests := []struct {
query *QueryData query *QueryData
title string title string
@ -122,6 +127,16 @@ func TestMovieByTitle(t *testing.T) {
"Macbeth", "Macbeth",
"2015", "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",
},
} }
api := Init(apiKey) api := Init(apiKey)
@ -143,33 +158,38 @@ func TestMovieByTitle(t *testing.T) {
} }
} }
func TestMovieByImdbID(t *testing.T) { func TestMediaByImdbID(t *testing.T) {
tests := []struct { tests := []struct {
id string query *QueryData
title string title string
year string year string
}{ }{
{ {&QueryData{ImdbId: "tt0137523", SearchType: MovieSearch},
"tt0137523",
"Fight Club", "Fight Club",
"1999", "1999",
}, },
{ {&QueryData{ImdbId: "tt1798709", SearchType: MovieSearch},
"tt1798709",
"Her", "Her",
"2013", "2013",
}, },
{ {&QueryData{ImdbId: "tt2884018", SearchType: MovieSearch},
"tt2884018",
"Macbeth", "Macbeth",
"2015", "2015",
}, },
{&QueryData{ImdbId: "tt3952222", Season: "1", SearchType: SeriesSearch},
"Killjoys",
"2015",
},
{&QueryData{ImdbId: "tt0944947", Season: "1", Episode: "1", SearchType: EpisodeSearch},
"Winter Is Coming",
"2011",
},
} }
api := Init(apiKey) api := Init(apiKey)
for i, item := range tests { for i, item := range tests {
resp, err := api.MovieByImdbID(item.id) resp, err := api.MovieByImdbID(item.query)
if err != nil { if err != nil {
t.Errorf("Test[%d]: %s", i, err) t.Errorf("Test[%d]: %s", i, err)
continue continue