Unit testing for performers,studios,movies FindByName/s (#581)

This commit is contained in:
bnkai
2020-05-27 03:48:45 +03:00
committed by GitHub
parent 1110e9c311
commit dc5efb9e31
5 changed files with 230 additions and 23 deletions

View File

@@ -3,6 +3,7 @@
package models_test package models_test
import ( import (
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@@ -34,11 +35,60 @@ func TestMovieFindBySceneID(t *testing.T) {
assert.Equal(t, 0, len(movies)) assert.Equal(t, 0, len(movies))
} }
func TestMovieFindByName(t *testing.T) {
mqb := models.NewMovieQueryBuilder()
name := movieNames[movieIdxWithScene] // find a movie by name
movie, err := mqb.FindByName(name, nil, false)
if err != nil {
t.Fatalf("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, nil, true)
if err != nil {
t.Fatalf("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))
}
func TestMovieFindByNames(t *testing.T) {
var names []string
mqb := models.NewMovieQueryBuilder()
names = append(names, movieNames[movieIdxWithScene]) // find movies by names
movies, err := mqb.FindByNames(names, nil, false)
if err != nil {
t.Fatalf("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, nil, true) // find movies by names nocase
if err != nil {
t.Fatalf("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))
}
// TODO Update // TODO Update
// TODO Destroy // TODO Destroy
// TODO Find // TODO Find
// TODO FindByName
// TODO FindByNames
// TODO Count // TODO Count
// TODO All // TODO All
// TODO Query // TODO Query

View File

@@ -3,6 +3,7 @@
package models_test package models_test
import ( import (
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@@ -58,10 +59,53 @@ func TestPerformerFindNameBySceneID(t *testing.T) {
assert.Equal(t, 0, len(performers)) assert.Equal(t, 0, len(performers))
} }
func TestPerformerFindByNames(t *testing.T) {
var names []string
pqb := models.NewPerformerQueryBuilder()
names = append(names, performerNames[performerIdxWithScene]) // find performers by names
performers, err := pqb.FindByNames(names, nil, false)
if err != nil {
t.Fatalf("Error finding performers: %s", err.Error())
}
assert.Len(t, performers, 1)
assert.Equal(t, performerNames[performerIdxWithScene], performers[0].Name.String)
performers, err = pqb.FindByNames(names, nil, true) // find performers by names nocase
if err != nil {
t.Fatalf("Error finding performers: %s", err.Error())
}
assert.Len(t, performers, 2) // performerIdxWithScene and performerIdxWithDupName
assert.Equal(t, strings.ToLower(performerNames[performerIdxWithScene]), strings.ToLower(performers[0].Name.String))
assert.Equal(t, strings.ToLower(performerNames[performerIdxWithScene]), strings.ToLower(performers[1].Name.String))
names = append(names, performerNames[performerIdx1WithScene]) // find performers by names ( 2 names )
performers, err = pqb.FindByNames(names, nil, false)
if err != nil {
t.Fatalf("Error finding performers: %s", err.Error())
}
assert.Len(t, performers, 2) // performerIdxWithScene and performerIdx1WithScene
assert.Equal(t, performerNames[performerIdxWithScene], performers[0].Name.String)
assert.Equal(t, performerNames[performerIdx1WithScene], performers[1].Name.String)
performers, err = pqb.FindByNames(names, nil, true) // find performers by names ( 2 names nocase)
if err != nil {
t.Fatalf("Error finding performers: %s", err.Error())
}
assert.Len(t, performers, 4) // performerIdxWithScene and performerIdxWithDupName , performerIdx1WithScene and performerIdx1WithDupName
assert.Equal(t, performerNames[performerIdxWithScene], performers[0].Name.String)
assert.Equal(t, performerNames[performerIdx1WithScene], performers[1].Name.String)
assert.Equal(t, performerNames[performerIdx1WithDupName], performers[2].Name.String)
assert.Equal(t, performerNames[performerIdxWithDupName], performers[3].Name.String)
}
// TODO Update // TODO Update
// TODO Destroy // TODO Destroy
// TODO Find // TODO Find
// TODO FindByNames
// TODO Count // TODO Count
// TODO All // TODO All
// TODO AllSlim // TODO AllSlim

View File

@@ -0,0 +1,50 @@
// +build integration
package models_test
import (
"strings"
"testing"
"github.com/stashapp/stash/pkg/models"
"github.com/stretchr/testify/assert"
)
func TestStudioFindByName(t *testing.T) {
sqb := models.NewStudioQueryBuilder()
name := studioNames[studioIdxWithScene] // find a studio by name
studio, err := sqb.FindByName(name, nil, false)
if err != nil {
t.Fatalf("Error finding studios: %s", err.Error())
}
assert.Equal(t, studioNames[studioIdxWithScene], studio.Name.String)
name = studioNames[studioIdxWithDupName] // find a studio by name nocase
studio, err = sqb.FindByName(name, nil, true)
if err != nil {
t.Fatalf("Error finding studios: %s", err.Error())
}
// studioIdxWithDupName and studioIdxWithScene should have similar names ( only diff should be Name vs NaMe)
//studio.Name should match with studioIdxWithScene since its ID is before studioIdxWithDupName
assert.Equal(t, studioNames[studioIdxWithScene], studio.Name.String)
//studio.Name should match with studioIdxWithDupName if the check is not case sensitive
assert.Equal(t, strings.ToLower(studioNames[studioIdxWithDupName]), strings.ToLower(studio.Name.String))
}
// TODO Create
// TODO Update
// TODO Destroy
// TODO Find
// TODO FindBySceneID
// TODO Count
// TODO All
// TODO AllSlim
// TODO Query

View File

@@ -55,8 +55,9 @@ func TestTagFindByName(t *testing.T) {
t.Fatalf("Error finding tags: %s", err.Error()) t.Fatalf("Error finding tags: %s", err.Error())
} }
// tagIdxWithDupName and tagIdxWithScene should have similar names ( only diff should be Name vs NaMe) // tagIdxWithDupName and tagIdxWithScene should have similar names ( only diff should be Name vs NaMe)
// match (tag.Name) should be the tagIdxWithScene since its ID is first //tag.Name should match with tagIdxWithScene since its ID is before tagIdxWithDupName
assert.Equal(t, tagNames[tagIdxWithScene], tag.Name) assert.Equal(t, tagNames[tagIdxWithScene], tag.Name)
//tag.Name should match with tagIdxWithDupName if the check is not case sensitive
assert.Equal(t, strings.ToLower(tagNames[tagIdxWithDupName]), strings.ToLower(tag.Name)) assert.Equal(t, strings.ToLower(tagNames[tagIdxWithDupName]), strings.ToLower(tag.Name))
} }

View File

@@ -16,15 +16,19 @@ import (
"github.com/stashapp/stash/pkg/database" "github.com/stashapp/stash/pkg/database"
"github.com/stashapp/stash/pkg/models" "github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/utils"
) )
const totalScenes = 12 const totalScenes = 12
const totalPerformers = 3 const performersNameCase = 3
const totalMovies = 1 const performersNameNoCase = 2
const moviesNameCase = 1
const moviesNameNoCase = 1
const totalGalleries = 1 const totalGalleries = 1
const tagsNameNoCase = 2 const tagsNameNoCase = 2
const tagsNameCase = 5 const tagsNameCase = 5
const totalStudios = 1 const studiosNameCase = 1
const studiosNameNoCase = 1
var sceneIDs []int var sceneIDs []int
var performerIDs []int var performerIDs []int
@@ -35,6 +39,9 @@ var studioIDs []int
var markerIDs []int var markerIDs []int
var tagNames []string var tagNames []string
var studioNames []string
var movieNames []string
var performerNames []string
const sceneIdxWithMovie = 0 const sceneIdxWithMovie = 0
const sceneIdxWithGallery = 1 const sceneIdxWithGallery = 1
@@ -46,24 +53,35 @@ const sceneIdxWithStudio = 6
const sceneIdxWithMarker = 7 const sceneIdxWithMarker = 7
const performerIdxWithScene = 0 const performerIdxWithScene = 0
const performerIdx1WithScene = 1
const performerIdx2WithScene = 2
// performers with dup names start from the end
const performerIdx1WithDupName = 3
const performerIdxWithDupName = 4
const movieIdxWithScene = 0 const movieIdxWithScene = 0
const galleryIdxWithScene = 0 // movies with dup names start from the end
const movieIdxWithDupName = 1
const performerIdx1WithScene = 1 const galleryIdxWithScene = 0
const performerIdx2WithScene = 2
const tagIdxWithScene = 0 const tagIdxWithScene = 0
const tagIdx1WithScene = 1 const tagIdx1WithScene = 1
const tagIdx2WithScene = 2 const tagIdx2WithScene = 2
const tagIdxWithPrimaryMarker = 3 const tagIdxWithPrimaryMarker = 3
const tagIdxWithMarker = 4 const tagIdxWithMarker = 4
// tags with dup names start from the end
const tagIdx1WithDupName = 5 const tagIdx1WithDupName = 5
const tagIdxWithDupName = 6 const tagIdxWithDupName = 6
const studioIdxWithScene = 0 const studioIdxWithScene = 0
// studios with dup names start from the end
const studioIdxWithDupName = 1
const markerIdxWithScene = 0 const markerIdxWithScene = 0
const pathField = "Path" const pathField = "Path"
@@ -125,12 +143,12 @@ func populateDB() error {
return err return err
} }
if err := createMovies(tx, totalMovies); err != nil { if err := createMovies(tx, moviesNameCase, moviesNameNoCase); err != nil {
tx.Rollback() tx.Rollback()
return err return err
} }
if err := createPerformers(tx, totalPerformers); err != nil { if err := createPerformers(tx, performersNameCase, performersNameNoCase); err != nil {
tx.Rollback() tx.Rollback()
return err return err
} }
@@ -140,7 +158,7 @@ func populateDB() error {
return err return err
} }
if err := createStudios(tx, totalStudios); err != nil { if err := createStudios(tx, studiosNameCase, studiosNameNoCase); err != nil {
tx.Rollback() tx.Rollback()
return err return err
} }
@@ -288,13 +306,27 @@ func getMovieStringValue(index int, field string) string {
return "movie_" + strconv.FormatInt(int64(index), 10) + "_" + field return "movie_" + strconv.FormatInt(int64(index), 10) + "_" + field
} }
func createMovies(tx *sqlx.Tx, n int) error { //createMoviees creates n movies with plain Name and o movies with camel cased NaMe included
func createMovies(tx *sqlx.Tx, n int, o int) error {
mqb := models.NewMovieQueryBuilder() mqb := models.NewMovieQueryBuilder()
const namePlain = "Name"
const nameNoCase = "NaMe"
name := namePlain
for i := 0; i < n+o; i++ {
index := i
if i >= n { // i<n tags get normal names
name = nameNoCase // i>=n movies get dup names if case is not checked
index = n + o - (i + 1) // for the name to be the same the number (index) must be the same also
} // so count backwards to 0 as needed
// movies [ i ] and [ n + o - i - 1 ] should have similar names with only the Name!=NaMe part different
for i := 0; i < n; i++ {
movie := models.Movie{ movie := models.Movie{
Name: sql.NullString{String: getMovieStringValue(i, "Name"), Valid: true}, Name: sql.NullString{String: getMovieStringValue(index, name), Valid: true},
FrontImage: []byte(models.DefaultMovieImage), FrontImage: []byte(models.DefaultMovieImage),
Checksum: utils.MD5FromString(name),
} }
created, err := mqb.Create(movie, tx) created, err := mqb.Create(movie, tx)
@@ -304,6 +336,7 @@ func createMovies(tx *sqlx.Tx, n int) error {
} }
movieIDs = append(movieIDs, created.ID) movieIDs = append(movieIDs, created.ID)
movieNames = append(movieNames, created.Name.String)
} }
return nil return nil
@@ -318,12 +351,25 @@ func getPerformerBoolValue(index int) bool {
return index == 1 return index == 1
} }
func createPerformers(tx *sqlx.Tx, n int) error { //createPerformers creates n performers with plain Name and o performers with camel cased NaMe included
func createPerformers(tx *sqlx.Tx, n int, o int) error {
pqb := models.NewPerformerQueryBuilder() pqb := models.NewPerformerQueryBuilder()
const namePlain = "Name"
const nameNoCase = "NaMe"
name := namePlain
for i := 0; i < n+o; i++ {
index := i
if i >= n { // i<n tags get normal names
name = nameNoCase // i>=n performers get dup names if case is not checked
index = n + o - (i + 1) // for the name to be the same the number (index) must be the same also
} // so count backwards to 0 as needed
// performers [ i ] and [ n + o - i - 1 ] should have similar names with only the Name!=NaMe part different
for i := 0; i < n; i++ {
performer := models.Performer{ performer := models.Performer{
Name: sql.NullString{String: getPerformerStringValue(i, "Name"), Valid: true}, Name: sql.NullString{String: getPerformerStringValue(index, name), Valid: true},
Checksum: getPerformerStringValue(i, checksumField), Checksum: getPerformerStringValue(i, checksumField),
// just use movie image // just use movie image
Image: []byte(models.DefaultMovieImage), Image: []byte(models.DefaultMovieImage),
@@ -337,6 +383,7 @@ func createPerformers(tx *sqlx.Tx, n int) error {
} }
performerIDs = append(performerIDs, created.ID) performerIDs = append(performerIDs, created.ID)
performerNames = append(performerNames, created.Name.String)
} }
return nil return nil
@@ -385,13 +432,27 @@ func getStudioStringValue(index int, field string) string {
return "studio_" + strconv.FormatInt(int64(index), 10) + "_" + field return "studio_" + strconv.FormatInt(int64(index), 10) + "_" + field
} }
func createStudios(tx *sqlx.Tx, n int) error { //createStudios creates n studios with plain Name and o studios with camel cased NaMe included
func createStudios(tx *sqlx.Tx, n int, o int) error {
sqb := models.NewStudioQueryBuilder() sqb := models.NewStudioQueryBuilder()
const namePlain = "Name"
const nameNoCase = "NaMe"
name := namePlain
for i := 0; i < n+o; i++ {
index := i
if i >= n { // i<n studios get normal names
name = nameNoCase // i>=n studios get dup names if case is not checked
index = n + o - (i + 1) // for the name to be the same the number (index) must be the same also
} // so count backwards to 0 as needed
// studios [ i ] and [ n + o - i - 1 ] should have similar names with only the Name!=NaMe part different
for i := 0; i < n; i++ {
tag := models.Studio{ tag := models.Studio{
Name: sql.NullString{String: getStudioStringValue(i, "Name"), Valid: true}, Name: sql.NullString{String: getStudioStringValue(index, name), Valid: true},
Image: []byte(models.DefaultStudioImage), Image: []byte(models.DefaultStudioImage),
Checksum: utils.MD5FromString(name),
} }
created, err := sqb.Create(tag, tx) created, err := sqb.Create(tag, tx)
@@ -401,6 +462,7 @@ func createStudios(tx *sqlx.Tx, n int) error {
} }
studioIDs = append(studioIDs, created.ID) studioIDs = append(studioIDs, created.ID)
studioNames = append(studioNames, created.Name.String)
} }
return nil return nil