diff --git a/pkg/scene/import.go b/pkg/scene/import.go index 884d6d7e0..b36e1bd68 100644 --- a/pkg/scene/import.go +++ b/pkg/scene/import.go @@ -150,7 +150,7 @@ func (i *Importer) populateViewHistory() { } func (i *Importer) populateOHistory() { - i.viewHistory = getHistory( + i.oHistory = getHistory( i.Input.OHistory, i.Input.OCounter, i.Input.CreatedAt, // no last o count date diff --git a/pkg/scene/import_test.go b/pkg/scene/import_test.go index 26844f687..0e37dce16 100644 --- a/pkg/scene/import_test.go +++ b/pkg/scene/import_test.go @@ -4,10 +4,13 @@ import ( "context" "errors" "testing" + "time" "github.com/stashapp/stash/pkg/models" + "github.com/stashapp/stash/pkg/models/json" "github.com/stashapp/stash/pkg/models/jsonschema" "github.com/stashapp/stash/pkg/models/mocks" + "github.com/stashapp/stash/pkg/sliceutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) @@ -40,6 +43,151 @@ var ( var testCtx = context.Background() func TestImporterPreImport(t *testing.T) { + var ( + title = "title" + code = "code" + details = "details" + director = "director" + endpoint1 = "endpoint1" + stashID1 = "stashID1" + endpoint2 = "endpoint2" + stashID2 = "stashID2" + url1 = "url1" + url2 = "url2" + rating = 3 + organized = true + + createdAt = time.Now().Add(-time.Hour) + updatedAt = time.Now().Add(-time.Minute) + + resumeTime = 1.234 + playDuration = 2.345 + ) + tests := []struct { + name string + input jsonschema.Scene + output models.Scene + }{ + { + "basic", + jsonschema.Scene{ + Title: title, + Code: code, + Details: details, + Director: director, + StashIDs: []models.StashID{ + {Endpoint: endpoint1, StashID: stashID1}, + {Endpoint: endpoint2, StashID: stashID2}, + }, + URLs: []string{url1, url2}, + Rating: rating, + Organized: organized, + CreatedAt: json.JSONTime{Time: createdAt}, + UpdatedAt: json.JSONTime{Time: updatedAt}, + ResumeTime: resumeTime, + PlayDuration: playDuration, + }, + models.Scene{ + Title: title, + Code: code, + Details: details, + Director: director, + StashIDs: models.NewRelatedStashIDs([]models.StashID{ + {Endpoint: endpoint1, StashID: stashID1}, + {Endpoint: endpoint2, StashID: stashID2}, + }), + URLs: models.NewRelatedStrings([]string{url1, url2}), + Rating: &rating, + Organized: organized, + CreatedAt: createdAt.Truncate(0), + UpdatedAt: updatedAt.Truncate(0), + ResumeTime: resumeTime, + PlayDuration: playDuration, + + Files: models.NewRelatedVideoFiles([]*models.VideoFile{}), + GalleryIDs: models.NewRelatedIDs([]int{}), + TagIDs: models.NewRelatedIDs([]int{}), + PerformerIDs: models.NewRelatedIDs([]int{}), + Groups: models.NewRelatedGroups([]models.GroupsScenes{}), + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + i := Importer{ + Input: tt.input, + } + + if err := i.PreImport(testCtx); err != nil { + t.Errorf("PreImport() error = %v", err) + return + } + + assert.Equal(t, tt.output, i.scene) + }) + } +} + +func truncateTimes(t []time.Time) []time.Time { + return sliceutil.Map(t, func(t time.Time) time.Time { return t.Truncate(0) }) +} + +func TestImporterPreImportHistory(t *testing.T) { + var ( + playTime1 = time.Now().Add(-time.Hour * 2) + playTime2 = time.Now().Add(-time.Minute * 2) + oTime1 = time.Now().Add(-time.Hour * 3) + oTime2 = time.Now().Add(-time.Minute * 3) + ) + tests := []struct { + name string + input jsonschema.Scene + expectedPlayHistory []time.Time + expectedOHistory []time.Time + }{ + { + "basic", + jsonschema.Scene{ + PlayHistory: []json.JSONTime{ + {Time: playTime1}, + {Time: playTime2}, + }, + OHistory: []json.JSONTime{ + {Time: oTime1}, + {Time: oTime2}, + }, + }, + []time.Time{playTime1, playTime2}, + []time.Time{oTime1, oTime2}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + i := Importer{ + Input: tt.input, + } + + if err := i.PreImport(testCtx); err != nil { + t.Errorf("PreImport() error = %v", err) + return + } + + // convert histories to unix timestamps for comparison + eph := truncateTimes(tt.expectedPlayHistory) + vh := truncateTimes(i.viewHistory) + + eoh := truncateTimes(tt.expectedOHistory) + oh := truncateTimes(i.oHistory) + + assert.Equal(t, eph, vh, "view history mismatch") + assert.Equal(t, eoh, oh, "o history mismatch") + }) + } +} + +func TestImporterPreImportCoverImage(t *testing.T) { i := Importer{ Input: jsonschema.Scene{ Cover: invalidImage,