Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix view history imported from o-history json #5127

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/scene/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
148 changes: 148 additions & 0 deletions pkg/scene/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down
Loading