Add basic shows
This commit is contained in:
parent
b7c7e32d1d
commit
9212318c23
71
shows.go
Normal file
71
shows.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package shows
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
"github.com/odwrtw/polochon/lib"
|
||||||
|
)
|
||||||
|
|
||||||
|
const showsCreate = `
|
||||||
|
CREATE TABLE shows (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
imdbid text NOT NULL UNIQUE,
|
||||||
|
title text NOT NULL,
|
||||||
|
updated timestamp DEFAULT current_timestamp,
|
||||||
|
created timestamp DEFAULT current_timestamp
|
||||||
|
);
|
||||||
|
`
|
||||||
|
|
||||||
|
const (
|
||||||
|
addShowQuery = `INSERT INTO shows (imdbid, title) VALUES (:imdbid, :title) RETURNING id;`
|
||||||
|
getShowQuery = `SELECT * FROM shows WHERE imdbid=$1;`
|
||||||
|
deleteShowQuery = `DELETE FROM shows WHERE id=$1;`
|
||||||
|
)
|
||||||
|
|
||||||
|
// BaseTable have to be embeded in all your struct which reflect a table
|
||||||
|
type BaseTable struct {
|
||||||
|
Updated time.Time
|
||||||
|
Created time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type Show struct {
|
||||||
|
ID int
|
||||||
|
polochon.Show
|
||||||
|
BaseTable
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get(db *sqlx.DB, imdbID string) (*Show, error) {
|
||||||
|
s := &Show{}
|
||||||
|
err := db.QueryRowx(getShowQuery, imdbID).StructScan(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Show) Add(db *sqlx.DB) error {
|
||||||
|
var id int
|
||||||
|
r, err := db.NamedQuery(addShowQuery, s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for r.Next() {
|
||||||
|
r.Scan(&id)
|
||||||
|
}
|
||||||
|
s.ID = id
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Show) Delete(db *sqlx.DB) error {
|
||||||
|
r, err := db.Exec(deleteShowQuery, s.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
count, _ := r.RowsAffected()
|
||||||
|
if count != 1 {
|
||||||
|
return fmt.Errorf("Unexpected number of row deleted: %d", count)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
87
shows_test.go
Normal file
87
shows_test.go
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
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'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>
|
||||||
|
`
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user