From c0a8aa0c32b6b9b06ee6e482146f79cc208730f2 Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Thu, 10 Aug 2023 09:35:25 +1000 Subject: [PATCH] Fix unit tests --- pkg/gallery/export_test.go | 4 +-- pkg/gallery/import_test.go | 2 +- pkg/models/mocks/GalleryReaderWriter.go | 23 +++++++++++++++ pkg/sqlite/gallery_test.go | 39 +++++++++++++++++++------ pkg/sqlite/setup_test.go | 17 +++++++++-- 5 files changed, 70 insertions(+), 15 deletions(-) diff --git a/pkg/gallery/export_test.go b/pkg/gallery/export_test.go index 3a6ffa2ec55..eba08e4e513 100644 --- a/pkg/gallery/export_test.go +++ b/pkg/gallery/export_test.go @@ -59,7 +59,7 @@ func createFullGallery(id int) models.Gallery { Details: details, Rating: &rating, Organized: organized, - URL: url, + URLs: models.NewRelatedStrings([]string{url}), CreatedAt: createTime, UpdatedAt: updateTime, } @@ -85,7 +85,7 @@ func createFullJSONGallery() *jsonschema.Gallery { Details: details, Rating: rating, Organized: organized, - URL: url, + URLs: []string{url}, ZipFiles: []string{path}, CreatedAt: json.JSONTime{ Time: createTime, diff --git a/pkg/gallery/import_test.go b/pkg/gallery/import_test.go index 0997b4a57e2..8263f97870c 100644 --- a/pkg/gallery/import_test.go +++ b/pkg/gallery/import_test.go @@ -66,7 +66,7 @@ func TestImporterPreImport(t *testing.T) { Details: details, Rating: &rating, Organized: organized, - URL: url, + URLs: models.NewRelatedStrings([]string{url}), Files: models.NewRelatedFiles([]models.File{}), TagIDs: models.NewRelatedIDs([]int{}), PerformerIDs: models.NewRelatedIDs([]int{}), diff --git a/pkg/models/mocks/GalleryReaderWriter.go b/pkg/models/mocks/GalleryReaderWriter.go index 2b901466b19..bd1fbf0d2b9 100644 --- a/pkg/models/mocks/GalleryReaderWriter.go +++ b/pkg/models/mocks/GalleryReaderWriter.go @@ -533,6 +533,29 @@ func (_m *GalleryReaderWriter) GetTagIDs(ctx context.Context, relatedID int) ([] return r0, r1 } +// GetURLs provides a mock function with given fields: ctx, relatedID +func (_m *GalleryReaderWriter) GetURLs(ctx context.Context, relatedID int) ([]string, error) { + ret := _m.Called(ctx, relatedID) + + var r0 []string + if rf, ok := ret.Get(0).(func(context.Context, int) []string); ok { + r0 = rf(ctx, relatedID) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]string) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(context.Context, int) error); ok { + r1 = rf(ctx, relatedID) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // Query provides a mock function with given fields: ctx, galleryFilter, findFilter func (_m *GalleryReaderWriter) Query(ctx context.Context, galleryFilter *models.GalleryFilterType, findFilter *models.FindFilterType) ([]*models.Gallery, int, error) { ret := _m.Called(ctx, galleryFilter, findFilter) diff --git a/pkg/sqlite/gallery_test.go b/pkg/sqlite/gallery_test.go index c8dbe02762f..9b1075ecaed 100644 --- a/pkg/sqlite/gallery_test.go +++ b/pkg/sqlite/gallery_test.go @@ -17,6 +17,11 @@ import ( var invalidID = -1 func loadGalleryRelationships(ctx context.Context, expected models.Gallery, actual *models.Gallery) error { + if expected.URLs.Loaded() { + if err := actual.LoadURLs(ctx, db.Gallery); err != nil { + return err + } + } if expected.SceneIDs.Loaded() { if err := actual.LoadSceneIDs(ctx, db.Gallery); err != nil { return err @@ -72,7 +77,7 @@ func Test_galleryQueryBuilder_Create(t *testing.T) { "full", models.Gallery{ Title: title, - URL: url, + URLs: models.NewRelatedStrings([]string{url}), Date: &date, Details: details, Rating: &rating, @@ -90,7 +95,7 @@ func Test_galleryQueryBuilder_Create(t *testing.T) { "with file", models.Gallery{ Title: title, - URL: url, + URLs: models.NewRelatedStrings([]string{url}), Date: &date, Details: details, Rating: &rating, @@ -222,7 +227,7 @@ func Test_galleryQueryBuilder_Update(t *testing.T) { &models.Gallery{ ID: galleryIDs[galleryIdxWithScene], Title: title, - URL: url, + URLs: models.NewRelatedStrings([]string{url}), Date: &date, Details: details, Rating: &rating, @@ -243,6 +248,7 @@ func Test_galleryQueryBuilder_Update(t *testing.T) { "clear nullables", &models.Gallery{ ID: galleryIDs[galleryIdxWithImage], + URLs: models.NewRelatedStrings([]string{}), SceneIDs: models.NewRelatedIDs([]int{}), TagIDs: models.NewRelatedIDs([]int{}), PerformerIDs: models.NewRelatedIDs([]int{}), @@ -384,7 +390,7 @@ func clearGalleryPartial() models.GalleryPartial { return models.GalleryPartial{ Title: models.OptionalString{Set: true, Null: true}, Details: models.OptionalString{Set: true, Null: true}, - URL: models.OptionalString{Set: true, Null: true}, + URLs: &models.UpdateStrings{Mode: models.RelationshipUpdateModeSet}, Date: models.OptionalDate{Set: true, Null: true}, Rating: models.OptionalInt{Set: true, Null: true}, StudioID: models.OptionalInt{Set: true, Null: true}, @@ -416,9 +422,12 @@ func Test_galleryQueryBuilder_UpdatePartial(t *testing.T) { "full", galleryIDs[galleryIdxWithImage], models.GalleryPartial{ - Title: models.NewOptionalString(title), - Details: models.NewOptionalString(details), - URL: models.NewOptionalString(url), + Title: models.NewOptionalString(title), + Details: models.NewOptionalString(details), + URLs: &models.UpdateStrings{ + Values: []string{url}, + Mode: models.RelationshipUpdateModeSet, + }, Date: models.NewOptionalDate(date), Rating: models.NewOptionalInt(rating), Organized: models.NewOptionalBool(true), @@ -443,7 +452,7 @@ func Test_galleryQueryBuilder_UpdatePartial(t *testing.T) { ID: galleryIDs[galleryIdxWithImage], Title: title, Details: details, - URL: url, + URLs: models.NewRelatedStrings([]string{url}), Date: &date, Rating: &rating, Organized: true, @@ -1653,7 +1662,13 @@ func TestGalleryQueryURL(t *testing.T) { verifyFn := func(g *models.Gallery) { t.Helper() - verifyString(t, g.URL, urlCriterion) + urls := g.URLs.List() + var url string + if len(urls) > 0 { + url = urls[0] + } + + verifyString(t, url, urlCriterion) } verifyGalleryQuery(t, filter, verifyFn) @@ -1683,6 +1698,12 @@ func verifyGalleryQuery(t *testing.T, filter models.GalleryFilterType, verifyFn galleries := queryGallery(ctx, t, sqb, &filter, nil) + for _, g := range galleries { + if err := g.LoadURLs(ctx, sqb); err != nil { + t.Errorf("Error loading gallery URLs: %v", err) + } + } + // assume it should find at least one assert.Greater(t, len(galleries), 0) diff --git a/pkg/sqlite/setup_test.go b/pkg/sqlite/setup_test.go index e182ef99b5b..9ca886ac4d2 100644 --- a/pkg/sqlite/setup_test.go +++ b/pkg/sqlite/setup_test.go @@ -1213,7 +1213,16 @@ func getGalleryNullStringValue(index int, field string) sql.NullString { } func getGalleryNullStringPtr(index int, field string) *string { - return getStringPtr(getPrefixedStringValue("gallery", index, field)) + return getStringPtrFromNullString(getPrefixedNullStringValue("gallery", index, field)) +} + +func getGalleryEmptyString(index int, field string) string { + v := getGalleryNullStringPtr(index, field) + if v == nil { + return "" + } + + return *v } func getGalleryBasename(index int) string { @@ -1245,8 +1254,10 @@ func makeGallery(i int, includeScenes bool) *models.Gallery { tids := indexesToIDs(tagIDs, galleryTags[i]) ret := &models.Gallery{ - Title: getGalleryStringValue(i, titleField), - URL: getGalleryNullStringValue(i, urlField).String, + Title: getGalleryStringValue(i, titleField), + URLs: models.NewRelatedStrings([]string{ + getGalleryEmptyString(i, urlField), + }), Rating: getIntPtr(getRating(i)), Date: getObjectDate(i), StudioID: studioID,