- 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 (
baseURL = "http://www.omdbapi.com/?"
baseURL = "http://www.omdbapi.com"
plot = "full"
tomatoes = "true"
@ -33,6 +33,8 @@ type QueryData struct {
Year string
ImdbId string
SearchType string
Season string
Episode string
}
//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
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 {
return nil, err
}
@ -133,8 +136,9 @@ func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) {
}
//MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381"
func (api *OmdbApi) MovieByImdbID(id string) (*MovieResult, error) {
resp, err := api.requestAPI("id", id)
func (api *OmdbApi) MovieByImdbID(query *QueryData) (*MovieResult, error) {
resp, err := api.requestAPI("id", query.ImdbId, query.Year, query.SearchType, query.Season,
query.Episode)
if err != nil {
return nil, err
}
@ -184,10 +188,15 @@ func (api *OmdbApi) requestAPI(apiCategory string, params ...string) (resp *http
parameters.Add("t", params[0])
parameters.Add("y", params[1])
parameters.Add("type", params[2])
parameters.Add("Season", params[3])
parameters.Add("Episode", params[4])
parameters.Add("plot", plot)
parameters.Add("tomatoes", tomatoes)
case "id":
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("tomatoes", tomatoes)
}

View file

@ -9,6 +9,7 @@ var apiKey = os.Getenv("OMDB_API_KEY")
func TestNoKey(t *testing.T) {
api := Init("")
_, err := api.Search(&QueryData{Title: "Her"})
if err == nil {
t.Errorf("Expected to fail")
@ -40,7 +41,9 @@ func TestSearch(t *testing.T) {
"2015",
},
}
api := Init(apiKey)
for i, item := range tests {
resp, err := api.Search(item.query)
if err != nil {
@ -67,6 +70,7 @@ func TestFailSearch(t *testing.T) {
}
api := Init(apiKey)
for i, item := range tests {
_, err := api.Search(item.query)
if err == nil {
@ -75,7 +79,7 @@ func TestFailSearch(t *testing.T) {
}
// 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)
t.Errorf("Test[%d]: Unexpected value- %s", i, err)
continue
}
}
@ -90,6 +94,7 @@ func TestInvalidCategory(t *testing.T) {
}
api := Init(apiKey)
for i, item := range tests {
_, err := api.Search(item.query)
if err == nil {
@ -98,13 +103,13 @@ func TestInvalidCategory(t *testing.T) {
}
// 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, Got- %s", i, err)
t.Errorf("Test[%d]: Unexpected value- %s", i, err)
continue
}
}
}
func TestMovieByTitle(t *testing.T) {
func TestMediaByTitle(t *testing.T) {
tests := []struct {
query *QueryData
title string
@ -122,6 +127,16 @@ func TestMovieByTitle(t *testing.T) {
"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",
},
}
api := Init(apiKey)
@ -143,33 +158,38 @@ func TestMovieByTitle(t *testing.T) {
}
}
func TestMovieByImdbID(t *testing.T) {
func TestMediaByImdbID(t *testing.T) {
tests := []struct {
id string
query *QueryData
title string
year string
}{
{
"tt0137523",
{&QueryData{ImdbId: "tt0137523", SearchType: MovieSearch},
"Fight Club",
"1999",
},
{
"tt1798709",
{&QueryData{ImdbId: "tt1798709", SearchType: MovieSearch},
"Her",
"2013",
},
{
"tt2884018",
{&QueryData{ImdbId: "tt2884018", SearchType: MovieSearch},
"Macbeth",
"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)
for i, item := range tests {
resp, err := api.MovieByImdbID(item.id)
resp, err := api.MovieByImdbID(item.query)
if err != nil {
t.Errorf("Test[%d]: %s", i, err)
continue