Skip to content

Commit

Permalink
Test cleanup and support for multiple categories in searches
Browse files Browse the repository at this point in the history
* Make the test verbage more goconvey-y
* Rename the `category` Client.Searc* param to `categories`
* New `categories` param is now a slice of ints
* Add splitCats method to the Client
  • Loading branch information
mattsch committed Mar 20, 2016
1 parent be10dfb commit 4b64050
Show file tree
Hide file tree
Showing 4 changed files with 857 additions and 45 deletions.
25 changes: 17 additions & 8 deletions newznab/newznab.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"time"

log "github.com/Sirupsen/logrus"
Expand Down Expand Up @@ -70,45 +71,53 @@ func New(baseURL string, apikey string, insecure bool) Client {
}

// SearchWithTVRage returns NZBs for the given parameters
func (c Client) SearchWithTVRage(category int, tvRageID int, season int, episode int) ([]NZB, error) {
func (c Client) SearchWithTVRage(categories []int, tvRageID int, season int, episode int) ([]NZB, error) {
return c.search(url.Values{
"rid": []string{strconv.Itoa(tvRageID)},
"cat": []string{strconv.Itoa(category)},
"cat": c.splitCats(categories),
"season": []string{strconv.Itoa(season)},
"episode": []string{strconv.Itoa(episode)},
"t": []string{"tvsearch"},
})
}

// SearchWithTVDB returns NZBs for the given parameters
func (c Client) SearchWithTVDB(category int, tvDBID int, season int, episode int) ([]NZB, error) {
func (c Client) SearchWithTVDB(categories []int, tvDBID int, season int, episode int) ([]NZB, error) {
return c.search(url.Values{
"tvdbid": []string{strconv.Itoa(tvDBID)},
"cat": []string{strconv.Itoa(category)},
"cat": c.splitCats(categories),
"season": []string{strconv.Itoa(season)},
"episode": []string{strconv.Itoa(episode)},
"t": []string{"tvsearch"},
})
}

// SearchWithIMDB returns NZBs for the given parameters
func (c Client) SearchWithIMDB(category int, imdbID string) ([]NZB, error) {
func (c Client) SearchWithIMDB(categories []int, imdbID string) ([]NZB, error) {
return c.search(url.Values{
"imdbid": []string{imdbID},
"cat": []string{strconv.Itoa(category)},
"cat": c.splitCats(categories),
"t": []string{"movie"},
})
}

// SearchWithQuery returns NZBs for the given parameters
func (c Client) SearchWithQuery(category int, query string, searchType string) ([]NZB, error) {
func (c Client) SearchWithQuery(categories []int, query string, searchType string) ([]NZB, error) {
return c.search(url.Values{
"q": []string{query},
"cat": []string{strconv.Itoa(category)},
"cat": c.splitCats(categories),
"t": []string{searchType},
})
}

func (c Client) splitCats(cats []int) []string {
var categories, catsOut []string
for _, v := range cats {
categories = append(categories, strconv.Itoa(v))
}
catsOut = append(catsOut, strings.Join(categories, ","))
return catsOut
}
func (c Client) search(vals url.Values) ([]NZB, error) {
vals.Set("apikey", c.apikey)
//vals.Set("t", "tvsearch")
Expand Down
104 changes: 68 additions & 36 deletions newznab/newznab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestUsenetCrawlerClient(t *testing.T) {
filePath := fmt.Sprintf("../tests/fixtures%v/%v.xml", r.URL.Path, fixedPath)
f, err = ioutil.ReadFile(filePath)
}

if err != nil {
log.Error(err)
w.WriteHeader(http.StatusNotFound)
Expand All @@ -53,7 +54,8 @@ func TestUsenetCrawlerClient(t *testing.T) {
client := New(ts.URL+"/api", apiKey, true)

Convey("I can search using simple query", func() {
results, err := client.SearchWithQuery(CategoryTVHD, "Supernatural S11E01", "tvshows")
categories := []int{CategoryTVHD}
results, err := client.SearchWithQuery(categories, "Supernatural S11E01", "tvshows")
//for _, result := range results {
// log.Info(result.JSONString())
//}
Expand All @@ -65,34 +67,40 @@ func TestUsenetCrawlerClient(t *testing.T) {

Convey("I have setup a nzb client", t, func() {
client := New(ts.URL+"/api", apiKey, false)
categories := []int{CategoryTVSD}

Convey("Handle errors", func() {

Convey("Return an error for an invalid search", func() {
_, err := client.SearchWithTVDB(CategoryTVSD, 1234, 9, 2)
_, err := client.SearchWithTVDB(categories, 1234, 9, 2)
So(err, ShouldNotBeNil)
})
})

Convey("I can get TV show information", func() {
Convey("When getting TV show information", func() {

Convey("I can search using TheTVDB id", func() {
results, err := client.SearchWithTVDB(CategoryTVSD, 75682, 10, 1)
Convey("Given a category and a TheTVDB id", func() {
results, err := client.SearchWithTVDB(categories, 75682, 10, 1)

So(err, ShouldBeNil)
So(len(results), ShouldBeGreaterThan, 0)
Convey("A valid result is returned.", func() {
So(err, ShouldBeNil)
So(len(results), ShouldBeGreaterThan, 0)
})
})

Convey("I can search using tvrage id", func() {
results, err := client.SearchWithTVRage(CategoryTVSD, 2870, 10, 1)
Convey("When given a category and a tvrage id", func() {
results, err := client.SearchWithTVRage(categories, 2870, 10, 1)

//for _, result := range results {
// log.Info(result.JSONString())
//}

So(err, ShouldBeNil)
So(len(results), ShouldBeGreaterThan, 0)
Convey("A valid result is returned.", func() {
So(err, ShouldBeNil)
So(len(results), ShouldBeGreaterThan, 0)
})

Convey("I can populate the comments for an NZB", func() {
Convey("I can populate the comments for an NZB.", func() {
nzb := results[1]
So(len(nzb.Comments), ShouldEqual, 0)
So(nzb.NumComments, ShouldBeGreaterThan, 0)
Expand All @@ -104,57 +112,81 @@ func TestUsenetCrawlerClient(t *testing.T) {
}

So(len(nzb.Comments), ShouldBeGreaterThan, 0)
})

Convey("I can get the download url", func() {
url := client.NZBDownloadURL(results[0])
So(len(url), ShouldBeGreaterThan, 0)
log.Infof("URL: %s", url)
Convey("I can get the download url.", func() {
url := client.NZBDownloadURL(results[0])
So(len(url), ShouldBeGreaterThan, 0)
log.Infof("URL: %s", url)
})

Convey("I can download the NZB", func() {
bytes, err := client.DownloadNZB(results[0])
So(err, ShouldBeNil)
Convey("I can download the NZB.", func() {
bytes, err := client.DownloadNZB(results[0])
So(err, ShouldBeNil)

md5Sum := md5.Sum(bytes)
log.WithFields(log.Fields{
"num_bytes": len(bytes),
"md5": base64.StdEncoding.EncodeToString(md5Sum[:]),
}).Info("downloaded")
md5Sum := md5.Sum(bytes)
log.WithFields(log.Fields{
"num_bytes": len(bytes),
"md5": base64.StdEncoding.EncodeToString(md5Sum[:]),
}).Info("downloaded")

So(len(bytes), ShouldBeGreaterThan, 0)
})
})
So(len(bytes), ShouldBeGreaterThan, 0)
})
})
})
Convey("I can get movie information", func() {

Convey("I can search using an IMDB id", func() {
results, err := client.SearchWithIMDB(CategoryMovieHD, "0364569")
Convey("When getting movie information", func() {
categories := []int{CategoryMovieHD}

Convey("Given multiple categories and an IMDB id", func() {
cats := []int{
CategoryMovieHD,
CategoryMovieBluRay,
}
results, err := client.SearchWithIMDB(cats, "0371746")

So(err, ShouldBeNil)
So(len(results), ShouldBeGreaterThan, 0)

Convey("The results have different categories.", func() {
So(results[0].Category[1], ShouldEqual, "2040")
So(results[22].Category[1], ShouldEqual, "2050")
})
})

Convey("Given a single category and an IMDB id", func() {
results, err := client.SearchWithIMDB(categories, "0364569")

So(err, ShouldBeNil)
So(len(results), ShouldBeGreaterThan, 0)
Convey("Get movie specific fields from results", func() {
Convey("IMDB ID", func() {

Convey("I can get movie specific fields", func() {

Convey("An IMDB id.", func() {
imdbAttr := results[0].IMDBID

So(imdbAttr, ShouldEqual, "0364569")
})
Convey("IMDB Title", func() {

Convey("An IMDB title.", func() {
imdbAttr := results[0].IMDBTitle

So(imdbAttr, ShouldEqual, "Oldboy")
})
Convey("IMDB Year", func() {

Convey("An IMDB year.", func() {
imdbAttr := results[0].IMDBYear

So(imdbAttr, ShouldEqual, 2003)
})
Convey("IMDB Score", func() {

Convey("An IMDB score.", func() {
imdbAttr := results[0].IMDBScore

So(imdbAttr, ShouldEqual, 8.4)
})
Convey("IMDB Cover URL", func() {

Convey("A cover URL.", func() {
imdbAttr := results[0].CoverURL

So(imdbAttr, ShouldEqual, "https://dognzb.cr/content/covers/movies/thumbs/364569.jpg")
Expand Down
2 changes: 1 addition & 1 deletion newznab/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func (c Comment) JSONString() string {
return string(jsonString)
}

// SearchResponse is a RSS version of the response.
type SearchResponse struct {
// RSS version of the response.
Version string `xml:"version,attr"`
Channel struct {
Title string `xml:"title"`
Expand Down
Loading

0 comments on commit 4b64050

Please sign in to comment.