stash/pkg/sqlite/movies_test.go

242 lines
6.3 KiB
Go
Raw Normal View History

// +build integration
package sqlite_test
import (
"database/sql"
"fmt"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/utils"
)
func TestMovieFindByName(t *testing.T) {
withTxn(func(r models.Repository) error {
mqb := r.Movie()
name := movieNames[movieIdxWithScene] // find a movie by name
movie, err := mqb.FindByName(name, false)
if err != nil {
t.Errorf("Error finding movies: %s", err.Error())
}
assert.Equal(t, movieNames[movieIdxWithScene], movie.Name.String)
name = movieNames[movieIdxWithDupName] // find a movie by name nocase
movie, err = mqb.FindByName(name, true)
if err != nil {
t.Errorf("Error finding movies: %s", err.Error())
}
// movieIdxWithDupName and movieIdxWithScene should have similar names ( only diff should be Name vs NaMe)
//movie.Name should match with movieIdxWithScene since its ID is before moveIdxWithDupName
assert.Equal(t, movieNames[movieIdxWithScene], movie.Name.String)
//movie.Name should match with movieIdxWithDupName if the check is not case sensitive
assert.Equal(t, strings.ToLower(movieNames[movieIdxWithDupName]), strings.ToLower(movie.Name.String))
return nil
})
}
func TestMovieFindByNames(t *testing.T) {
withTxn(func(r models.Repository) error {
var names []string
mqb := r.Movie()
names = append(names, movieNames[movieIdxWithScene]) // find movies by names
movies, err := mqb.FindByNames(names, false)
if err != nil {
t.Errorf("Error finding movies: %s", err.Error())
}
assert.Len(t, movies, 1)
assert.Equal(t, movieNames[movieIdxWithScene], movies[0].Name.String)
movies, err = mqb.FindByNames(names, true) // find movies by names nocase
if err != nil {
t.Errorf("Error finding movies: %s", err.Error())
}
assert.Len(t, movies, 2) // movieIdxWithScene and movieIdxWithDupName
assert.Equal(t, strings.ToLower(movieNames[movieIdxWithScene]), strings.ToLower(movies[0].Name.String))
assert.Equal(t, strings.ToLower(movieNames[movieIdxWithScene]), strings.ToLower(movies[1].Name.String))
return nil
})
}
func TestMovieQueryStudio(t *testing.T) {
withTxn(func(r models.Repository) error {
mqb := r.Movie()
studioCriterion := models.MultiCriterionInput{
Value: []string{
strconv.Itoa(studioIDs[studioIdxWithMovie]),
},
Modifier: models.CriterionModifierIncludes,
}
movieFilter := models.MovieFilterType{
Studios: &studioCriterion,
}
movies, _, err := mqb.Query(&movieFilter, nil)
if err != nil {
t.Errorf("Error querying movie: %s", err.Error())
}
assert.Len(t, movies, 1)
// ensure id is correct
assert.Equal(t, movieIDs[movieIdxWithStudio], movies[0].ID)
studioCriterion = models.MultiCriterionInput{
Value: []string{
strconv.Itoa(studioIDs[studioIdxWithMovie]),
},
Modifier: models.CriterionModifierExcludes,
}
q := getMovieStringValue(movieIdxWithStudio, titleField)
findFilter := models.FindFilterType{
Q: &q,
}
movies, _, err = mqb.Query(&movieFilter, &findFilter)
if err != nil {
t.Errorf("Error querying movie: %s", err.Error())
}
assert.Len(t, movies, 0)
return nil
})
}
func TestMovieUpdateMovieImages(t *testing.T) {
if err := withTxn(func(r models.Repository) error {
mqb := r.Movie()
// create movie to test against
const name = "TestMovieUpdateMovieImages"
movie := models.Movie{
Name: sql.NullString{String: name, Valid: true},
Checksum: utils.MD5FromString(name),
}
created, err := mqb.Create(movie)
if err != nil {
return fmt.Errorf("Error creating movie: %s", err.Error())
}
frontImage := []byte("frontImage")
backImage := []byte("backImage")
err = mqb.UpdateImages(created.ID, frontImage, backImage)
if err != nil {
return fmt.Errorf("Error updating movie images: %s", err.Error())
}
// ensure images are set
storedFront, err := mqb.GetFrontImage(created.ID)
if err != nil {
return fmt.Errorf("Error getting front image: %s", err.Error())
}
assert.Equal(t, storedFront, frontImage)
storedBack, err := mqb.GetBackImage(created.ID)
if err != nil {
return fmt.Errorf("Error getting back image: %s", err.Error())
}
assert.Equal(t, storedBack, backImage)
// set front image only
newImage := []byte("newImage")
err = mqb.UpdateImages(created.ID, newImage, nil)
if err != nil {
return fmt.Errorf("Error updating movie images: %s", err.Error())
}
storedFront, err = mqb.GetFrontImage(created.ID)
if err != nil {
return fmt.Errorf("Error getting front image: %s", err.Error())
}
assert.Equal(t, storedFront, newImage)
// back image should be nil
storedBack, err = mqb.GetBackImage(created.ID)
if err != nil {
return fmt.Errorf("Error getting back image: %s", err.Error())
}
assert.Nil(t, nil)
// set back image only
err = mqb.UpdateImages(created.ID, nil, newImage)
if err == nil {
return fmt.Errorf("Expected error setting nil front image")
}
return nil
}); err != nil {
t.Error(err.Error())
}
}
func TestMovieDestroyMovieImages(t *testing.T) {
if err := withTxn(func(r models.Repository) error {
mqb := r.Movie()
// create movie to test against
const name = "TestMovieDestroyMovieImages"
movie := models.Movie{
Name: sql.NullString{String: name, Valid: true},
Checksum: utils.MD5FromString(name),
}
created, err := mqb.Create(movie)
if err != nil {
return fmt.Errorf("Error creating movie: %s", err.Error())
}
frontImage := []byte("frontImage")
backImage := []byte("backImage")
err = mqb.UpdateImages(created.ID, frontImage, backImage)
if err != nil {
return fmt.Errorf("Error updating movie images: %s", err.Error())
}
err = mqb.DestroyImages(created.ID)
if err != nil {
return fmt.Errorf("Error destroying movie images: %s", err.Error())
}
// front image should be nil
storedFront, err := mqb.GetFrontImage(created.ID)
if err != nil {
return fmt.Errorf("Error getting front image: %s", err.Error())
}
assert.Nil(t, storedFront)
// back image should be nil
storedBack, err := mqb.GetBackImage(created.ID)
if err != nil {
return fmt.Errorf("Error getting back image: %s", err.Error())
}
assert.Nil(t, storedBack)
return nil
}); err != nil {
t.Error(err.Error())
}
}
// TODO Update
// TODO Destroy
// TODO Find
// TODO Count
// TODO All
// TODO Query