2021-10-28 03:25:17 +00:00
|
|
|
package scene
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2021-10-28 03:25:17 +00:00
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
"github.com/stashapp/stash/pkg/models/mocks"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/sliceutil/intslice"
|
2021-10-28 03:25:17 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUpdater_IsEmpty(t *testing.T) {
|
|
|
|
organized := true
|
|
|
|
ids := []int{1}
|
|
|
|
stashIDs := []models.StashID{
|
|
|
|
{},
|
|
|
|
}
|
|
|
|
cover := []byte{1}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
u *UpdateSet
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"empty",
|
|
|
|
&UpdateSet{},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"partial set",
|
|
|
|
&UpdateSet{
|
|
|
|
Partial: models.ScenePartial{
|
2022-07-13 06:30:54 +00:00
|
|
|
Organized: models.NewOptionalBool(organized),
|
2021-10-28 03:25:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"performer set",
|
|
|
|
&UpdateSet{
|
2022-07-13 06:30:54 +00:00
|
|
|
Partial: models.ScenePartial{
|
|
|
|
PerformerIDs: &models.UpdateIDs{
|
|
|
|
IDs: ids,
|
|
|
|
Mode: models.RelationshipUpdateModeSet,
|
|
|
|
},
|
|
|
|
},
|
2021-10-28 03:25:17 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tags set",
|
|
|
|
&UpdateSet{
|
2022-07-13 06:30:54 +00:00
|
|
|
Partial: models.ScenePartial{
|
|
|
|
TagIDs: &models.UpdateIDs{
|
|
|
|
IDs: ids,
|
|
|
|
Mode: models.RelationshipUpdateModeSet,
|
|
|
|
},
|
|
|
|
},
|
2021-10-28 03:25:17 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"performer set",
|
|
|
|
&UpdateSet{
|
2022-07-13 06:30:54 +00:00
|
|
|
Partial: models.ScenePartial{
|
|
|
|
StashIDs: &models.UpdateStashIDs{
|
|
|
|
StashIDs: stashIDs,
|
|
|
|
Mode: models.RelationshipUpdateModeSet,
|
|
|
|
},
|
|
|
|
},
|
2021-10-28 03:25:17 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cover set",
|
|
|
|
&UpdateSet{
|
|
|
|
CoverImage: cover,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := tt.u.IsEmpty(); got != tt.want {
|
|
|
|
t.Errorf("Updater.IsEmpty() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockScreenshotSetter struct{}
|
|
|
|
|
|
|
|
func (s *mockScreenshotSetter) SetScreenshot(scene *models.Scene, imageData []byte) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdater_Update(t *testing.T) {
|
|
|
|
const (
|
|
|
|
sceneID = iota + 1
|
|
|
|
badUpdateID
|
|
|
|
badPerformersID
|
|
|
|
badTagsID
|
|
|
|
badStashIDsID
|
|
|
|
badCoverID
|
|
|
|
performerID
|
|
|
|
tagID
|
|
|
|
)
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-10-28 03:25:17 +00:00
|
|
|
performerIDs := []int{performerID}
|
|
|
|
tagIDs := []int{tagID}
|
|
|
|
stashID := "stashID"
|
|
|
|
endpoint := "endpoint"
|
|
|
|
|
|
|
|
title := "title"
|
|
|
|
cover := []byte("cover")
|
|
|
|
|
|
|
|
validScene := &models.Scene{}
|
|
|
|
|
|
|
|
updateErr := errors.New("error updating")
|
|
|
|
|
|
|
|
qb := mocks.SceneReaderWriter{}
|
2022-07-13 06:30:54 +00:00
|
|
|
qb.On("UpdatePartial", ctx, mock.MatchedBy(func(id int) bool {
|
|
|
|
return id != badUpdateID
|
|
|
|
}), mock.Anything).Return(validScene, nil)
|
|
|
|
qb.On("UpdatePartial", ctx, badUpdateID, mock.Anything).Return(nil, updateErr)
|
2021-10-28 03:25:17 +00:00
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
qb.On("UpdateCover", ctx, sceneID, cover).Return(nil).Once()
|
|
|
|
qb.On("UpdateCover", ctx, badCoverID, cover).Return(updateErr).Once()
|
2021-10-28 03:25:17 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
u *UpdateSet
|
|
|
|
wantNil bool
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"empty",
|
|
|
|
&UpdateSet{
|
|
|
|
ID: sceneID,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"update all",
|
|
|
|
&UpdateSet{
|
2022-07-13 06:30:54 +00:00
|
|
|
ID: sceneID,
|
|
|
|
Partial: models.ScenePartial{
|
|
|
|
PerformerIDs: &models.UpdateIDs{
|
|
|
|
IDs: performerIDs,
|
|
|
|
Mode: models.RelationshipUpdateModeSet,
|
|
|
|
},
|
|
|
|
TagIDs: &models.UpdateIDs{
|
|
|
|
IDs: tagIDs,
|
|
|
|
Mode: models.RelationshipUpdateModeSet,
|
|
|
|
},
|
|
|
|
StashIDs: &models.UpdateStashIDs{
|
|
|
|
StashIDs: []models.StashID{
|
|
|
|
{
|
|
|
|
StashID: stashID,
|
|
|
|
Endpoint: endpoint,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Mode: models.RelationshipUpdateModeSet,
|
2021-10-28 03:25:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
CoverImage: cover,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"update fields only",
|
|
|
|
&UpdateSet{
|
|
|
|
ID: sceneID,
|
|
|
|
Partial: models.ScenePartial{
|
2022-07-13 06:30:54 +00:00
|
|
|
Title: models.NewOptionalString(title),
|
2021-10-28 03:25:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"error updating scene",
|
|
|
|
&UpdateSet{
|
|
|
|
ID: badUpdateID,
|
|
|
|
Partial: models.ScenePartial{
|
2022-07-13 06:30:54 +00:00
|
|
|
Title: models.NewOptionalString(title),
|
2021-10-28 03:25:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"error updating cover",
|
|
|
|
&UpdateSet{
|
|
|
|
ID: badCoverID,
|
|
|
|
CoverImage: cover,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-05-19 07:49:32 +00:00
|
|
|
got, err := tt.u.Update(ctx, &qb, &mockScreenshotSetter{})
|
2021-10-28 03:25:17 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("Updater.Update() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (got == nil) != tt.wantNil {
|
|
|
|
t.Errorf("Updater.Update() = %v, want %v", got, tt.wantNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
qb.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateSet_UpdateInput(t *testing.T) {
|
|
|
|
const (
|
|
|
|
sceneID = iota + 1
|
|
|
|
badUpdateID
|
|
|
|
badPerformersID
|
|
|
|
badTagsID
|
|
|
|
badStashIDsID
|
|
|
|
badCoverID
|
|
|
|
performerID
|
|
|
|
tagID
|
|
|
|
)
|
|
|
|
|
|
|
|
sceneIDStr := strconv.Itoa(sceneID)
|
|
|
|
|
|
|
|
performerIDs := []int{performerID}
|
2022-03-17 00:33:59 +00:00
|
|
|
performerIDStrs := intslice.IntSliceToStringSlice(performerIDs)
|
2021-10-28 03:25:17 +00:00
|
|
|
tagIDs := []int{tagID}
|
2022-03-17 00:33:59 +00:00
|
|
|
tagIDStrs := intslice.IntSliceToStringSlice(tagIDs)
|
2021-10-28 03:25:17 +00:00
|
|
|
stashID := "stashID"
|
|
|
|
endpoint := "endpoint"
|
|
|
|
stashIDs := []models.StashID{
|
|
|
|
{
|
|
|
|
StashID: stashID,
|
|
|
|
Endpoint: endpoint,
|
|
|
|
},
|
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
stashIDInputs := []models.StashID{
|
2021-10-28 03:25:17 +00:00
|
|
|
{
|
|
|
|
StashID: stashID,
|
|
|
|
Endpoint: endpoint,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
title := "title"
|
|
|
|
cover := []byte("cover")
|
|
|
|
coverB64 := "Y292ZXI="
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
u UpdateSet
|
|
|
|
want models.SceneUpdateInput
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"empty",
|
|
|
|
UpdateSet{
|
|
|
|
ID: sceneID,
|
|
|
|
},
|
|
|
|
models.SceneUpdateInput{
|
|
|
|
ID: sceneIDStr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"update all",
|
|
|
|
UpdateSet{
|
2022-07-13 06:30:54 +00:00
|
|
|
ID: sceneID,
|
|
|
|
Partial: models.ScenePartial{
|
|
|
|
PerformerIDs: &models.UpdateIDs{
|
|
|
|
IDs: performerIDs,
|
|
|
|
Mode: models.RelationshipUpdateModeSet,
|
|
|
|
},
|
|
|
|
TagIDs: &models.UpdateIDs{
|
|
|
|
IDs: tagIDs,
|
|
|
|
Mode: models.RelationshipUpdateModeSet,
|
|
|
|
},
|
|
|
|
StashIDs: &models.UpdateStashIDs{
|
|
|
|
StashIDs: stashIDs,
|
|
|
|
Mode: models.RelationshipUpdateModeSet,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CoverImage: cover,
|
2021-10-28 03:25:17 +00:00
|
|
|
},
|
|
|
|
models.SceneUpdateInput{
|
|
|
|
ID: sceneIDStr,
|
|
|
|
PerformerIds: performerIDStrs,
|
|
|
|
TagIds: tagIDStrs,
|
|
|
|
StashIds: stashIDInputs,
|
|
|
|
CoverImage: &coverB64,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"update fields only",
|
|
|
|
UpdateSet{
|
|
|
|
ID: sceneID,
|
|
|
|
Partial: models.ScenePartial{
|
2022-07-13 06:30:54 +00:00
|
|
|
Title: models.NewOptionalString(title),
|
2021-10-28 03:25:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
models.SceneUpdateInput{
|
|
|
|
ID: sceneIDStr,
|
|
|
|
Title: &title,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := tt.u.UpdateInput()
|
|
|
|
assert.Equal(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|