181 lines
4.6 KiB
Go
181 lines
4.6 KiB
Go
package shows
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitlab.quimbo.fr/odwrtw/canape-sql/sqly"
|
|
"gitlab.quimbo.fr/odwrtw/canape-sql/users"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
_ "github.com/lib/pq"
|
|
_ "github.com/mattes/migrate/driver/postgres"
|
|
"github.com/odwrtw/polochon/lib"
|
|
)
|
|
|
|
const showNFO1 = `
|
|
<tvshow>
|
|
<title>Marvel's Jessica Jones</title>
|
|
<showtitle>Marvel'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'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'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'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'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'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'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
|
|
|
|
func init() {
|
|
var err error
|
|
|
|
pgdsn = os.Getenv("POSTGRES_DSN")
|
|
db, err = sqlx.Connect("postgres", pgdsn)
|
|
|
|
if err != nil {
|
|
fmt.Printf("Unavailable PG tests:\n %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func TestAddRemoveShow(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)
|
|
}
|
|
|
|
err = show.Get(db)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = show.GetEpisodes(db)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(show.Episodes) != 2 {
|
|
t.Fatalf("Unexpected number of episodes: %d", len(show.Episodes))
|
|
}
|
|
|
|
err = show.Delete(db)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = show.Get(db)
|
|
if err != ErrNotFound {
|
|
t.Fatalf("Unexpected error: %q", err)
|
|
}
|
|
})
|
|
|
|
}
|
|
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}
|
|
show.Add(db)
|
|
|
|
u := &users.User{Name: "plop"}
|
|
err := u.Add(db)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = show.GetAsUser(db, u)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if show.IsTracked() {
|
|
t.Fatal("Tracked must be false here")
|
|
}
|
|
|
|
q := `INSERT INTO shows_tracked (shows_id, users_id, season, episode) VALUES ($1, $2, $3, $4);`
|
|
_, err = db.Exec(q, show.ID, u.ID, 1, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = show.GetAsUser(db, u)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !show.IsTracked() {
|
|
t.Fatal("Tracked must be true here")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestGetDetails(t *testing.T) {
|
|
sqly.RunWithLastestMigration(db, pgdsn, t, func(db *sqlx.DB, t *testing.T) {
|
|
})
|
|
}
|