From dc5efb9e31d0b0c1544510f7d170618faa6ccbfd Mon Sep 17 00:00:00 2001 From: bnkai <48220860+bnkai@users.noreply.github.com> Date: Wed, 27 May 2020 03:48:45 +0300 Subject: [PATCH] Unit testing for performers,studios,movies FindByName/s (#581) --- pkg/models/querybuilder_movies_test.go | 54 +++++++++++- pkg/models/querybuilder_performer_test.go | 46 +++++++++- pkg/models/querybuilder_studio_test.go | 50 +++++++++++ pkg/models/querybuilder_tag_test.go | 3 +- pkg/models/setup_test.go | 100 ++++++++++++++++++---- 5 files changed, 230 insertions(+), 23 deletions(-) create mode 100644 pkg/models/querybuilder_studio_test.go diff --git a/pkg/models/querybuilder_movies_test.go b/pkg/models/querybuilder_movies_test.go index 90116b199..a11f94f18 100644 --- a/pkg/models/querybuilder_movies_test.go +++ b/pkg/models/querybuilder_movies_test.go @@ -3,6 +3,7 @@ package models_test import ( + "strings" "testing" "github.com/stretchr/testify/assert" @@ -34,11 +35,60 @@ func TestMovieFindBySceneID(t *testing.T) { 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 Destroy // TODO Find -// TODO FindByName -// TODO FindByNames // TODO Count // TODO All // TODO Query diff --git a/pkg/models/querybuilder_performer_test.go b/pkg/models/querybuilder_performer_test.go index a371e3c57..1c2b26979 100644 --- a/pkg/models/querybuilder_performer_test.go +++ b/pkg/models/querybuilder_performer_test.go @@ -3,6 +3,7 @@ package models_test import ( + "strings" "testing" "github.com/stretchr/testify/assert" @@ -58,10 +59,53 @@ func TestPerformerFindNameBySceneID(t *testing.T) { 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 Destroy // TODO Find -// TODO FindByNames // TODO Count // TODO All // TODO AllSlim diff --git a/pkg/models/querybuilder_studio_test.go b/pkg/models/querybuilder_studio_test.go new file mode 100644 index 000000000..5f7460844 --- /dev/null +++ b/pkg/models/querybuilder_studio_test.go @@ -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 diff --git a/pkg/models/querybuilder_tag_test.go b/pkg/models/querybuilder_tag_test.go index faf736284..052c89fc3 100644 --- a/pkg/models/querybuilder_tag_test.go +++ b/pkg/models/querybuilder_tag_test.go @@ -55,8 +55,9 @@ func TestTagFindByName(t *testing.T) { t.Fatalf("Error finding tags: %s", err.Error()) } // 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) + //tag.Name should match with tagIdxWithDupName if the check is not case sensitive assert.Equal(t, strings.ToLower(tagNames[tagIdxWithDupName]), strings.ToLower(tag.Name)) } diff --git a/pkg/models/setup_test.go b/pkg/models/setup_test.go index 63412047b..0cfb25328 100644 --- a/pkg/models/setup_test.go +++ b/pkg/models/setup_test.go @@ -16,15 +16,19 @@ import ( "github.com/stashapp/stash/pkg/database" "github.com/stashapp/stash/pkg/models" + "github.com/stashapp/stash/pkg/utils" ) const totalScenes = 12 -const totalPerformers = 3 -const totalMovies = 1 +const performersNameCase = 3 +const performersNameNoCase = 2 +const moviesNameCase = 1 +const moviesNameNoCase = 1 const totalGalleries = 1 const tagsNameNoCase = 2 const tagsNameCase = 5 -const totalStudios = 1 +const studiosNameCase = 1 +const studiosNameNoCase = 1 var sceneIDs []int var performerIDs []int @@ -35,6 +39,9 @@ var studioIDs []int var markerIDs []int var tagNames []string +var studioNames []string +var movieNames []string +var performerNames []string const sceneIdxWithMovie = 0 const sceneIdxWithGallery = 1 @@ -46,24 +53,35 @@ const sceneIdxWithStudio = 6 const sceneIdxWithMarker = 7 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 galleryIdxWithScene = 0 +// movies with dup names start from the end +const movieIdxWithDupName = 1 -const performerIdx1WithScene = 1 -const performerIdx2WithScene = 2 +const galleryIdxWithScene = 0 const tagIdxWithScene = 0 const tagIdx1WithScene = 1 const tagIdx2WithScene = 2 const tagIdxWithPrimaryMarker = 3 const tagIdxWithMarker = 4 + +// tags with dup names start from the end const tagIdx1WithDupName = 5 const tagIdxWithDupName = 6 const studioIdxWithScene = 0 +// studios with dup names start from the end +const studioIdxWithDupName = 1 + const markerIdxWithScene = 0 const pathField = "Path" @@ -125,12 +143,12 @@ func populateDB() error { return err } - if err := createMovies(tx, totalMovies); err != nil { + if err := createMovies(tx, moviesNameCase, moviesNameNoCase); err != nil { tx.Rollback() return err } - if err := createPerformers(tx, totalPerformers); err != nil { + if err := createPerformers(tx, performersNameCase, performersNameNoCase); err != nil { tx.Rollback() return err } @@ -140,7 +158,7 @@ func populateDB() error { return err } - if err := createStudios(tx, totalStudios); err != nil { + if err := createStudios(tx, studiosNameCase, studiosNameNoCase); err != nil { tx.Rollback() return err } @@ -288,13 +306,27 @@ func getMovieStringValue(index int, field string) string { 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() + const namePlain = "Name" + const nameNoCase = "NaMe" + + name := namePlain + + for i := 0; i < n+o; i++ { + index := i + + if i >= n { // 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{ - Name: sql.NullString{String: getMovieStringValue(i, "Name"), Valid: true}, + Name: sql.NullString{String: getMovieStringValue(index, name), Valid: true}, FrontImage: []byte(models.DefaultMovieImage), + Checksum: utils.MD5FromString(name), } created, err := mqb.Create(movie, tx) @@ -304,6 +336,7 @@ func createMovies(tx *sqlx.Tx, n int) error { } movieIDs = append(movieIDs, created.ID) + movieNames = append(movieNames, created.Name.String) } return nil @@ -318,12 +351,25 @@ func getPerformerBoolValue(index int) bool { 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() + const namePlain = "Name" + const nameNoCase = "NaMe" + + name := namePlain + + for i := 0; i < n+o; i++ { + index := i + + if i >= n { // 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{ - Name: sql.NullString{String: getPerformerStringValue(i, "Name"), Valid: true}, + Name: sql.NullString{String: getPerformerStringValue(index, name), Valid: true}, Checksum: getPerformerStringValue(i, checksumField), // just use movie image Image: []byte(models.DefaultMovieImage), @@ -337,6 +383,7 @@ func createPerformers(tx *sqlx.Tx, n int) error { } performerIDs = append(performerIDs, created.ID) + performerNames = append(performerNames, created.Name.String) } return nil @@ -385,13 +432,27 @@ func getStudioStringValue(index int, field string) string { 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() + const namePlain = "Name" + const nameNoCase = "NaMe" + + name := namePlain + + for i := 0; i < n+o; i++ { + index := i + + if i >= n { // 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{ - Name: sql.NullString{String: getStudioStringValue(i, "Name"), Valid: true}, - Image: []byte(models.DefaultStudioImage), + Name: sql.NullString{String: getStudioStringValue(index, name), Valid: true}, + Image: []byte(models.DefaultStudioImage), + Checksum: utils.MD5FromString(name), } created, err := sqb.Create(tag, tx) @@ -401,6 +462,7 @@ func createStudios(tx *sqlx.Tx, n int) error { } studioIDs = append(studioIDs, created.ID) + studioNames = append(studioNames, created.Name.String) } return nil