Use detailer mock and test GetDetails

This commit is contained in:
Nicolas Duhamel 2016-02-21 17:59:59 +01:00
parent 73785992dd
commit 276c9caa29
2 changed files with 40 additions and 119 deletions

View File

@ -46,6 +46,7 @@ var (
ErrNotFound = fmt.Errorf("Not found")
)
// Show represents a show
type Show struct {
sqly.BaseModel
polochon.Show
@ -120,8 +121,9 @@ func (s *Show) GetDetails(db *sqlx.DB, log *logrus.Entry) error {
s.Episodes = []*Episode{}
for _, pe := range s.Show.Episodes {
s.Episodes = append(s.Episodes, NewEpisodeFromPolochon(pe))
s.Episodes = append(s.Episodes, &Episode{ShowEpisode: *pe})
}
s.Add(db)
return nil
}
@ -145,7 +147,7 @@ func (s *Show) GetDetailsAsUser(db *sqlx.DB, user *users.User, log *logrus.Entry
s.Add(db)
s.Episodes = []*Episode{}
for _, pe := range s.Show.Episodes {
s.Episodes = append(s.Episodes, NewEpisodeFromPolochon(pe))
s.Episodes = append(s.Episodes, &Episode{ShowEpisode: *pe})
}
return nil
}
@ -159,6 +161,7 @@ func (s *Show) IsTracked() bool {
return false
}
// Add a show in the database
func (s *Show) Add(db *sqlx.DB) error {
var id string
r, err := db.NamedQuery(addShowQuery, s)
@ -189,6 +192,7 @@ func (s *Show) addEpisode(db *sqlx.DB, pEpisode *polochon.ShowEpisode) error {
return nil
}
// Delete show from database
func (s *Show) Delete(db *sqlx.DB) error {
r, err := db.Exec(deleteShowQuery, s.ID)
if err != nil {
@ -201,6 +205,7 @@ func (s *Show) Delete(db *sqlx.DB) error {
return nil
}
// GetEpisodes from database
func (s *Show) GetEpisodes(db *sqlx.DB) error {
// When retrive episode's info from database populate the s.Episodes member
// and not s.Show.Episodes
@ -212,25 +217,8 @@ func (s *Show) GetEpisodes(db *sqlx.DB) error {
return nil
}
// Episode represents an episode
type Episode struct {
sqly.BaseModel
polochon.ShowEpisode
}
// NewEpisodeFromPolochon returns a new episode from a polochon.ShowEpisode
func NewEpisodeFromPolochon(pe *polochon.ShowEpisode) *Episode {
e := &Episode{}
e.Title = pe.Title
e.ShowTitle = pe.ShowTitle
e.Season = pe.Season
e.Episode = pe.Episode
e.TvdbID = pe.TvdbID
e.Aired = pe.Aired
e.Plot = pe.Plot
e.Runtime = pe.Runtime
e.Rating = pe.Rating
e.ShowImdbID = pe.ShowImdbID
e.ShowTvdbID = pe.ShowTvdbID
e.EpisodeImdbID = pe.EpisodeImdbID
return e
}

View File

@ -3,70 +3,19 @@ package shows
import (
"fmt"
"os"
"strings"
"testing"
"gitlab.quimbo.fr/odwrtw/canape-sql/sqly"
"gitlab.quimbo.fr/odwrtw/canape-sql/users"
"github.com/Sirupsen/logrus"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
_ "github.com/mattes/migrate/driver/postgres"
"github.com/odwrtw/polochon/lib"
"github.com/odwrtw/polochon/modules/mock"
)
const showNFO1 = `
<tvshow>
<title>Marvel&#39;s Jessica Jones</title>
<showtitle>Marvel&#39;s Jessica Jones</showtitle>
<rating>8</rating>
<plot>Ever since her short-lived stint as a Super Hero ended in tragedy, Jessica Jones has been rebuilding her personal life and career as a hot-tempered, sardonic, badass private detective in Hell&#39;s Kitchen, New York City. Plagued by self-loathing, and a wicked case of PTSD, Jessica battles demons from within and without, using her extraordinary abilities as an unlikely champion for those in need... especially if they&#39;re willing to cut her a check.</plot>
<episodeguide>
<url>http://www.thetvdb.com/api/1D62F2F90030C444/series/284190/all/en.zip</url>
</episodeguide>
<tvdbid>284190</tvdbid>
<imdbid>tt2357547</imdbid>
<year>2015</year>
</tvshow>
`
const showNFO1E1 = `
<episodedetails>
<title>AKA Ladies Night</title>
<showtitle>Marvel&#39;s Jessica Jones</showtitle>
<season>1</season>
<episode>1</episode>
<uniqueid>5311261</uniqueid>
<aired>2015-11-20</aired>
<premiered>2015-11-20</premiered>
<plot>Jessica Jones is hired to find a pretty NYU student who&#39;s vanished, but it turns out to be more than a simple missing persons case.</plot>
<runtime>60</runtime>
<thumb>http://thetvdb.com/banners/episodes/284190/5311261.jpg</thumb>
<rating>7.5</rating>
<showimdbid>tt2357547</showimdbid>
<showtvdbid>284190</showtvdbid>
<episodeimdbid>tt4162058</episodeimdbid>
</episodedetails>
`
const showNFO1E2 = `
<episodedetails>
<title>AKA Crush Syndrome </title>
<showtitle>Marvel&#39;s Jessica Jones</showtitle>
<season>1</season>
<episode>2</episode>
<uniqueid>5311262</uniqueid>
<aired>2015-11-20</aired>
<premiered>2015-11-20</premiered>
<plot>Jessica vows to prove Hope&#39;s innocence, even though it means tracking down a terrifying figure from her own past.</plot>
<runtime>60</runtime>
<thumb>http://thetvdb.com/banners/episodes/284190/5311262.jpg</thumb>
<rating>7.7</rating>
<showimdbid>tt2357547</showimdbid>
<showtvdbid>284190</showtvdbid>
<episodeimdbid>tt4162062</episodeimdbid>
</episodedetails>
`
var db *sqlx.DB
var pgdsn string
@ -82,51 +31,30 @@ func init() {
}
}
func TestAddRemoveShow(t *testing.T) {
func TestIntegrate(t *testing.T) {
sqly.RunWithLastestMigration(db, pgdsn, t, func(db *sqlx.DB, t *testing.T) {
//Get unkown show
// sf := &Show{Show: polochon.Show{ImdbID: "polp", Title: "prout"}}
// sf, err := Get(db, "polp")
// if err != NotFound {
// t.Fatal("NotFound error expected here")
// }
nfo := strings.NewReader(showNFO1)
s := &polochon.Show{}
polochon.ReadNFO(nfo, s)
nfo = strings.NewReader(showNFO1E1)
ep1 := &polochon.ShowEpisode{}
polochon.ReadNFO(nfo, ep1)
nfo = strings.NewReader(showNFO1E2)
ep2 := &polochon.ShowEpisode{}
polochon.ReadNFO(nfo, ep2)
s.Episodes = append(s.Episodes, ep1)
s.Episodes = append(s.Episodes, ep2)
show := Show{Show: *s}
err := show.Add(db)
if err != nil {
t.Fatal(err)
detailer, _ := mock.NewDetailer(nil)
polochonConfig := polochon.ShowConfig{
Detailers: []polochon.Detailer{
detailer,
},
}
err = show.Get(db)
if err != nil {
t.Fatal(err)
show := Show{
Show: polochon.Show{
ShowConfig: polochonConfig,
ImdbID: "tt12345",
},
}
err = show.GetEpisodes(db)
if err != nil {
t.Fatal(err)
}
if len(show.Episodes) != 2 {
log := logrus.NewEntry(logrus.New())
show.GetDetails(db, log)
if len(show.Episodes) != 50 {
t.Fatalf("Unexpected number of episodes: %d", len(show.Episodes))
}
err = show.Delete(db)
err := show.Delete(db)
if err != nil {
t.Fatal(err)
}
@ -140,10 +68,20 @@ func TestAddRemoveShow(t *testing.T) {
}
func TestTrackedShow(t *testing.T) {
sqly.RunWithLastestMigration(db, pgdsn, t, func(db *sqlx.DB, t *testing.T) {
nfo := strings.NewReader(showNFO1)
s := &polochon.Show{}
polochon.ReadNFO(nfo, s)
show := Show{Show: *s}
detailer, _ := mock.NewDetailer(nil)
polochonConfig := polochon.ShowConfig{
Detailers: []polochon.Detailer{
detailer,
},
}
show := Show{
Show: polochon.Show{
ShowConfig: polochonConfig,
ImdbID: "tt12345",
},
}
show.Add(db)
u := &users.User{Name: "plop"}
@ -173,8 +111,3 @@ func TestTrackedShow(t *testing.T) {
}
})
}
func TestGetDetails(t *testing.T) {
sqly.RunWithLastestMigration(db, pgdsn, t, func(db *sqlx.DB, t *testing.T) {
})
}