2021-09-08 05:30:15 +00:00
|
|
|
//go:build integration
|
2021-01-18 01:23:20 +00:00
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package sqlite_test
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2021-01-18 01:23:20 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMovieFindByName(t *testing.T) {
|
2022-05-19 07:49:32 +00:00
|
|
|
withTxn(func(ctx context.Context) error {
|
2023-03-16 23:52:49 +00:00
|
|
|
mqb := db.Movie
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
name := movieNames[movieIdxWithScene] // find a movie by name
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
movie, err := mqb.FindByName(ctx, name, false)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error finding movies: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
assert.Equal(t, movieNames[movieIdxWithScene], movie.Name)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
name = movieNames[movieIdxWithDupName] // find a movie by name nocase
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
movie, err = mqb.FindByName(ctx, name, true)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
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
|
2023-06-15 02:46:09 +00:00
|
|
|
assert.Equal(t, movieNames[movieIdxWithScene], movie.Name)
|
2021-01-18 01:23:20 +00:00
|
|
|
//movie.Name should match with movieIdxWithDupName if the check is not case sensitive
|
2023-06-15 02:46:09 +00:00
|
|
|
assert.Equal(t, strings.ToLower(movieNames[movieIdxWithDupName]), strings.ToLower(movie.Name))
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMovieFindByNames(t *testing.T) {
|
2022-05-19 07:49:32 +00:00
|
|
|
withTxn(func(ctx context.Context) error {
|
2021-01-18 01:23:20 +00:00
|
|
|
var names []string
|
|
|
|
|
2023-03-16 23:52:49 +00:00
|
|
|
mqb := db.Movie
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
names = append(names, movieNames[movieIdxWithScene]) // find movies by names
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
movies, err := mqb.FindByNames(ctx, names, false)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error finding movies: %s", err.Error())
|
|
|
|
}
|
|
|
|
assert.Len(t, movies, 1)
|
2023-06-15 02:46:09 +00:00
|
|
|
assert.Equal(t, movieNames[movieIdxWithScene], movies[0].Name)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
movies, err = mqb.FindByNames(ctx, names, true) // find movies by names nocase
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error finding movies: %s", err.Error())
|
|
|
|
}
|
|
|
|
assert.Len(t, movies, 2) // movieIdxWithScene and movieIdxWithDupName
|
2023-06-15 02:46:09 +00:00
|
|
|
assert.Equal(t, strings.ToLower(movieNames[movieIdxWithScene]), strings.ToLower(movies[0].Name))
|
|
|
|
assert.Equal(t, strings.ToLower(movieNames[movieIdxWithScene]), strings.ToLower(movies[1].Name))
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-16 23:52:49 +00:00
|
|
|
func moviesToIDs(i []*models.Movie) []int {
|
|
|
|
ret := make([]int, len(i))
|
|
|
|
for i, v := range i {
|
|
|
|
ret[i] = v.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMovieQuery(t *testing.T) {
|
|
|
|
var (
|
|
|
|
frontImage = "front_image"
|
|
|
|
backImage = "back_image"
|
|
|
|
)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
findFilter *models.FindFilterType
|
|
|
|
filter *models.MovieFilterType
|
|
|
|
includeIdxs []int
|
|
|
|
excludeIdxs []int
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"is missing front image",
|
|
|
|
nil,
|
|
|
|
&models.MovieFilterType{
|
|
|
|
IsMissing: &frontImage,
|
|
|
|
},
|
|
|
|
// just ensure that it doesn't error
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"is missing back image",
|
|
|
|
nil,
|
|
|
|
&models.MovieFilterType{
|
|
|
|
IsMissing: &backImage,
|
|
|
|
},
|
|
|
|
// just ensure that it doesn't error
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
runWithRollbackTxn(t, tt.name, func(t *testing.T, ctx context.Context) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
results, _, err := db.Movie.Query(ctx, tt.filter, tt.findFilter)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("MovieQueryBuilder.Query() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := moviesToIDs(results)
|
|
|
|
include := indexesToIDs(performerIDs, tt.includeIdxs)
|
|
|
|
exclude := indexesToIDs(performerIDs, tt.excludeIdxs)
|
|
|
|
|
|
|
|
for _, i := range include {
|
|
|
|
assert.Contains(ids, i)
|
|
|
|
}
|
|
|
|
for _, e := range exclude {
|
|
|
|
assert.NotContains(ids, e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func TestMovieQueryStudio(t *testing.T) {
|
2022-05-19 07:49:32 +00:00
|
|
|
withTxn(func(ctx context.Context) error {
|
2023-03-16 23:52:49 +00:00
|
|
|
mqb := db.Movie
|
2021-06-03 10:52:19 +00:00
|
|
|
studioCriterion := models.HierarchicalMultiCriterionInput{
|
2021-01-18 01:23:20 +00:00
|
|
|
Value: []string{
|
|
|
|
strconv.Itoa(studioIDs[studioIdxWithMovie]),
|
|
|
|
},
|
|
|
|
Modifier: models.CriterionModifierIncludes,
|
|
|
|
}
|
|
|
|
|
|
|
|
movieFilter := models.MovieFilterType{
|
|
|
|
Studios: &studioCriterion,
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
movies, _, err := mqb.Query(ctx, &movieFilter, nil)
|
2021-01-18 01:23:20 +00:00
|
|
|
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)
|
|
|
|
|
2021-06-03 10:52:19 +00:00
|
|
|
studioCriterion = models.HierarchicalMultiCriterionInput{
|
2021-01-18 01:23:20 +00:00
|
|
|
Value: []string{
|
|
|
|
strconv.Itoa(studioIDs[studioIdxWithMovie]),
|
|
|
|
},
|
|
|
|
Modifier: models.CriterionModifierExcludes,
|
|
|
|
}
|
|
|
|
|
|
|
|
q := getMovieStringValue(movieIdxWithStudio, titleField)
|
|
|
|
findFilter := models.FindFilterType{
|
|
|
|
Q: &q,
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
movies, _, err = mqb.Query(ctx, &movieFilter, &findFilter)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error querying movie: %s", err.Error())
|
|
|
|
}
|
|
|
|
assert.Len(t, movies, 0)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-09 05:05:11 +00:00
|
|
|
func TestMovieQueryURL(t *testing.T) {
|
|
|
|
const sceneIdx = 1
|
|
|
|
movieURL := getMovieStringValue(sceneIdx, urlField)
|
|
|
|
|
|
|
|
urlCriterion := models.StringCriterionInput{
|
|
|
|
Value: movieURL,
|
|
|
|
Modifier: models.CriterionModifierEquals,
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := models.MovieFilterType{
|
|
|
|
URL: &urlCriterion,
|
|
|
|
}
|
|
|
|
|
|
|
|
verifyFn := func(n *models.Movie) {
|
|
|
|
t.Helper()
|
2023-06-15 02:46:09 +00:00
|
|
|
verifyString(t, n.URL, urlCriterion)
|
2021-04-09 05:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
verifyMovieQuery(t, filter, verifyFn)
|
|
|
|
|
|
|
|
urlCriterion.Modifier = models.CriterionModifierNotEquals
|
|
|
|
verifyMovieQuery(t, filter, verifyFn)
|
|
|
|
|
|
|
|
urlCriterion.Modifier = models.CriterionModifierMatchesRegex
|
|
|
|
urlCriterion.Value = "movie_.*1_URL"
|
|
|
|
verifyMovieQuery(t, filter, verifyFn)
|
|
|
|
|
|
|
|
urlCriterion.Modifier = models.CriterionModifierNotMatchesRegex
|
|
|
|
verifyMovieQuery(t, filter, verifyFn)
|
|
|
|
|
|
|
|
urlCriterion.Modifier = models.CriterionModifierIsNull
|
|
|
|
urlCriterion.Value = ""
|
|
|
|
verifyMovieQuery(t, filter, verifyFn)
|
|
|
|
|
|
|
|
urlCriterion.Modifier = models.CriterionModifierNotNull
|
|
|
|
verifyMovieQuery(t, filter, verifyFn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyMovieQuery(t *testing.T, filter models.MovieFilterType, verifyFn func(s *models.Movie)) {
|
2022-05-19 07:49:32 +00:00
|
|
|
withTxn(func(ctx context.Context) error {
|
2021-04-09 05:05:11 +00:00
|
|
|
t.Helper()
|
2023-03-16 23:52:49 +00:00
|
|
|
sqb := db.Movie
|
2021-04-09 05:05:11 +00:00
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
movies := queryMovie(ctx, t, sqb, &filter, nil)
|
2021-04-09 05:05:11 +00:00
|
|
|
|
|
|
|
// assume it should find at least one
|
|
|
|
assert.Greater(t, len(movies), 0)
|
|
|
|
|
|
|
|
for _, m := range movies {
|
|
|
|
verifyFn(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func queryMovie(ctx context.Context, t *testing.T, sqb models.MovieReader, movieFilter *models.MovieFilterType, findFilter *models.FindFilterType) []*models.Movie {
|
|
|
|
movies, _, err := sqb.Query(ctx, movieFilter, findFilter)
|
2021-04-09 05:05:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error querying movie: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return movies
|
|
|
|
}
|
|
|
|
|
2021-06-03 23:21:17 +00:00
|
|
|
func TestMovieQuerySorting(t *testing.T) {
|
|
|
|
sort := "scenes_count"
|
|
|
|
direction := models.SortDirectionEnumDesc
|
|
|
|
findFilter := models.FindFilterType{
|
|
|
|
Sort: &sort,
|
|
|
|
Direction: &direction,
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
withTxn(func(ctx context.Context) error {
|
2023-03-16 23:52:49 +00:00
|
|
|
sqb := db.Movie
|
2022-05-19 07:49:32 +00:00
|
|
|
movies := queryMovie(ctx, t, sqb, nil, &findFilter)
|
2021-06-03 23:21:17 +00:00
|
|
|
|
|
|
|
// scenes should be in same order as indexes
|
|
|
|
firstMovie := movies[0]
|
|
|
|
|
|
|
|
assert.Equal(t, movieIDs[movieIdxWithScene], firstMovie.ID)
|
|
|
|
|
|
|
|
// sort in descending order
|
|
|
|
direction = models.SortDirectionEnumAsc
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
movies = queryMovie(ctx, t, sqb, nil, &findFilter)
|
2021-06-03 23:21:17 +00:00
|
|
|
lastMovie := movies[len(movies)-1]
|
|
|
|
|
|
|
|
assert.Equal(t, movieIDs[movieIdxWithScene], lastMovie.ID)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-16 23:52:49 +00:00
|
|
|
func TestMovieUpdateFrontImage(t *testing.T) {
|
|
|
|
if err := withRollbackTxn(func(ctx context.Context) error {
|
|
|
|
qb := db.Movie
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
// create movie to test against
|
|
|
|
const name = "TestMovieUpdateMovieImages"
|
2023-06-15 02:46:09 +00:00
|
|
|
movie := models.Movie{
|
2023-09-01 00:04:56 +00:00
|
|
|
Name: name,
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2023-06-15 02:46:09 +00:00
|
|
|
err := qb.Create(ctx, &movie)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating movie: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2023-03-16 23:52:49 +00:00
|
|
|
return testUpdateImage(t, ctx, movie.ID, qb.UpdateFrontImage, qb.GetFrontImage)
|
2021-01-18 01:23:20 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 23:52:49 +00:00
|
|
|
func TestMovieUpdateBackImage(t *testing.T) {
|
|
|
|
if err := withRollbackTxn(func(ctx context.Context) error {
|
|
|
|
qb := db.Movie
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
// create movie to test against
|
2023-03-16 23:52:49 +00:00
|
|
|
const name = "TestMovieUpdateMovieImages"
|
2023-06-15 02:46:09 +00:00
|
|
|
movie := models.Movie{
|
2023-09-01 00:04:56 +00:00
|
|
|
Name: name,
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2023-06-15 02:46:09 +00:00
|
|
|
err := qb.Create(ctx, &movie)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating movie: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2023-03-16 23:52:49 +00:00
|
|
|
return testUpdateImage(t, ctx, movie.ID, qb.UpdateBackImage, qb.GetBackImage)
|
2021-01-18 01:23:20 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Update
|
2023-03-16 23:52:49 +00:00
|
|
|
// TODO Destroy - ensure image is destroyed
|
2021-01-18 01:23:20 +00:00
|
|
|
// TODO Find
|
|
|
|
// TODO Count
|
|
|
|
// TODO All
|
|
|
|
// TODO Query
|