mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 04:14:39 +03:00
Selective export (#770)
This commit is contained in:
47
pkg/studio/export.go
Normal file
47
pkg/studio/export.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package studio
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/stashapp/stash/pkg/manager/jsonschema"
|
||||
"github.com/stashapp/stash/pkg/models"
|
||||
"github.com/stashapp/stash/pkg/utils"
|
||||
)
|
||||
|
||||
// ToJSON converts a Studio object into its JSON equivalent.
|
||||
func ToJSON(reader models.StudioReader, studio *models.Studio) (*jsonschema.Studio, error) {
|
||||
newStudioJSON := jsonschema.Studio{
|
||||
CreatedAt: models.JSONTime{Time: studio.CreatedAt.Timestamp},
|
||||
UpdatedAt: models.JSONTime{Time: studio.UpdatedAt.Timestamp},
|
||||
}
|
||||
|
||||
if studio.Name.Valid {
|
||||
newStudioJSON.Name = studio.Name.String
|
||||
}
|
||||
|
||||
if studio.URL.Valid {
|
||||
newStudioJSON.URL = studio.URL.String
|
||||
}
|
||||
|
||||
if studio.ParentID.Valid {
|
||||
parent, err := reader.Find(int(studio.ParentID.Int64))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting parent studio: %s", err.Error())
|
||||
}
|
||||
|
||||
if parent != nil {
|
||||
newStudioJSON.ParentStudio = parent.Name.String
|
||||
}
|
||||
}
|
||||
|
||||
image, err := reader.GetStudioImage(studio.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting studio image: %s", err.Error())
|
||||
}
|
||||
|
||||
if len(image) > 0 {
|
||||
newStudioJSON.Image = utils.GetBase64StringFromData(image)
|
||||
}
|
||||
|
||||
return &newStudioJSON, nil
|
||||
}
|
||||
168
pkg/studio/export_test.go
Normal file
168
pkg/studio/export_test.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package studio
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/stashapp/stash/pkg/manager/jsonschema"
|
||||
"github.com/stashapp/stash/pkg/models"
|
||||
"github.com/stashapp/stash/pkg/models/mocks"
|
||||
"github.com/stashapp/stash/pkg/models/modelstest"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
studioID = 1
|
||||
noImageID = 2
|
||||
errImageID = 3
|
||||
missingParentStudioID = 4
|
||||
errStudioID = 5
|
||||
|
||||
parentStudioID = 10
|
||||
missingStudioID = 11
|
||||
errParentStudioID = 12
|
||||
)
|
||||
|
||||
const studioName = "testStudio"
|
||||
const url = "url"
|
||||
|
||||
const parentStudioName = "parentStudio"
|
||||
|
||||
var parentStudio models.Studio = models.Studio{
|
||||
Name: modelstest.NullString(parentStudioName),
|
||||
}
|
||||
|
||||
var imageBytes = []byte("imageBytes")
|
||||
|
||||
const image = "aW1hZ2VCeXRlcw=="
|
||||
|
||||
var createTime time.Time = time.Date(2001, 01, 01, 0, 0, 0, 0, time.UTC)
|
||||
var updateTime time.Time = time.Date(2002, 01, 01, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
func createFullStudio(id int, parentID int) models.Studio {
|
||||
return models.Studio{
|
||||
ID: id,
|
||||
Name: modelstest.NullString(studioName),
|
||||
URL: modelstest.NullString(url),
|
||||
ParentID: modelstest.NullInt64(int64(parentID)),
|
||||
CreatedAt: models.SQLiteTimestamp{
|
||||
Timestamp: createTime,
|
||||
},
|
||||
UpdatedAt: models.SQLiteTimestamp{
|
||||
Timestamp: updateTime,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func createEmptyStudio(id int) models.Studio {
|
||||
return models.Studio{
|
||||
ID: id,
|
||||
CreatedAt: models.SQLiteTimestamp{
|
||||
Timestamp: createTime,
|
||||
},
|
||||
UpdatedAt: models.SQLiteTimestamp{
|
||||
Timestamp: updateTime,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func createFullJSONStudio(parentStudio, image string) *jsonschema.Studio {
|
||||
return &jsonschema.Studio{
|
||||
Name: studioName,
|
||||
URL: url,
|
||||
CreatedAt: models.JSONTime{
|
||||
Time: createTime,
|
||||
},
|
||||
UpdatedAt: models.JSONTime{
|
||||
Time: updateTime,
|
||||
},
|
||||
ParentStudio: parentStudio,
|
||||
Image: image,
|
||||
}
|
||||
}
|
||||
|
||||
func createEmptyJSONStudio() *jsonschema.Studio {
|
||||
return &jsonschema.Studio{
|
||||
CreatedAt: models.JSONTime{
|
||||
Time: createTime,
|
||||
},
|
||||
UpdatedAt: models.JSONTime{
|
||||
Time: updateTime,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type testScenario struct {
|
||||
input models.Studio
|
||||
expected *jsonschema.Studio
|
||||
err bool
|
||||
}
|
||||
|
||||
var scenarios []testScenario
|
||||
|
||||
func initTestTable() {
|
||||
scenarios = []testScenario{
|
||||
testScenario{
|
||||
createFullStudio(studioID, parentStudioID),
|
||||
createFullJSONStudio(parentStudioName, image),
|
||||
false,
|
||||
},
|
||||
testScenario{
|
||||
createEmptyStudio(noImageID),
|
||||
createEmptyJSONStudio(),
|
||||
false,
|
||||
},
|
||||
testScenario{
|
||||
createFullStudio(errImageID, parentStudioID),
|
||||
nil,
|
||||
true,
|
||||
},
|
||||
testScenario{
|
||||
createFullStudio(missingParentStudioID, missingStudioID),
|
||||
createFullJSONStudio("", image),
|
||||
false,
|
||||
},
|
||||
testScenario{
|
||||
createFullStudio(errStudioID, errParentStudioID),
|
||||
nil,
|
||||
true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestToJSON(t *testing.T) {
|
||||
initTestTable()
|
||||
|
||||
mockStudioReader := &mocks.StudioReaderWriter{}
|
||||
|
||||
imageErr := errors.New("error getting image")
|
||||
|
||||
mockStudioReader.On("GetStudioImage", studioID).Return(imageBytes, nil).Once()
|
||||
mockStudioReader.On("GetStudioImage", noImageID).Return(nil, nil).Once()
|
||||
mockStudioReader.On("GetStudioImage", errImageID).Return(nil, imageErr).Once()
|
||||
mockStudioReader.On("GetStudioImage", missingParentStudioID).Return(imageBytes, nil).Maybe()
|
||||
mockStudioReader.On("GetStudioImage", errStudioID).Return(imageBytes, nil).Maybe()
|
||||
|
||||
parentStudioErr := errors.New("error getting parent studio")
|
||||
|
||||
mockStudioReader.On("Find", parentStudioID).Return(&parentStudio, nil)
|
||||
mockStudioReader.On("Find", missingStudioID).Return(nil, nil)
|
||||
mockStudioReader.On("Find", errParentStudioID).Return(nil, parentStudioErr)
|
||||
|
||||
for i, s := range scenarios {
|
||||
studio := s.input
|
||||
json, err := ToJSON(mockStudioReader, &studio)
|
||||
|
||||
if !s.err && err != nil {
|
||||
t.Errorf("[%d] unexpected error: %s", i, err.Error())
|
||||
} else if s.err && err == nil {
|
||||
t.Errorf("[%d] expected error not returned", i)
|
||||
} else {
|
||||
assert.Equal(t, s.expected, json, "[%d]", i)
|
||||
}
|
||||
}
|
||||
|
||||
mockStudioReader.AssertExpectations(t)
|
||||
}
|
||||
Reference in New Issue
Block a user