canape/shows_test.go
2016-02-11 20:57:38 +01:00

88 lines
2.0 KiB
Go

package shows
import (
"fmt"
"os"
"strings"
"testing"
"gitlab.quimbo.fr/odwrtw/canape-sql/sqltest"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"github.com/odwrtw/polochon/lib"
)
const drop = `
DROP TABLE shows;
`
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>
`
var db *sqlx.DB
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) {
sqltest.RunWithSchema(db, showsCreate, drop, t, func(db *sqlx.DB, t *testing.T) {
nfo := strings.NewReader(showNFO1)
s := &polochon.Show{}
err := polochon.ReadNFO(nfo, s)
if err != nil {
t.Fatal(err)
}
show := Show{Show: *s}
err = show.Add(db)
if err != nil {
t.Fatal(err)
}
show1, err := Get(db, "tt2357547")
if err != nil {
t.Fatal(err)
}
err = show1.Delete(db)
if err != nil {
t.Fatal(err)
}
show2, err := Get(db, "tt2357547")
if err == nil {
t.Fatal("We expect an error here, the show didn't exist anymore")
}
if err.Error() != "sql: no rows in result set" {
t.Fatalf("Unexpected error: %q", err)
}
if show2 != nil {
t.Fatal("Show have to be nil here")
}
})
}