Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WithoutPants committed Sep 12, 2023
1 parent f381299 commit c0a8aa0
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pkg/gallery/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/gallery/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
Expand Down
23 changes: 23 additions & 0 deletions pkg/models/mocks/GalleryReaderWriter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 30 additions & 9 deletions pkg/sqlite/gallery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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{}),
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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),
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
17 changes: 14 additions & 3 deletions pkg/sqlite/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c0a8aa0

Please sign in to comment.