stash/pkg/studio/export_test.go
SmallCoccinelle a5ca8fc678
Enable safe linters (#1786)
* Enable safe linters

Enable the linters dogsled, rowserrcheck, and sqlclosecheck.

These report no errors currently in the code base.

Enable misspell.

Misspell finds two spelling mistakes in comments, which are fixed by the
patch as well.

Add and sort linters which are relatively
safe to add over time. Comment them out for now.

* Close the response body

If we can get a HTTP response, it has a body which ought to be closed.

By doing so, we avoid potentially leaking connections.

* Enable the exportloopref linter

There are two places in the code with these warnings. Fix them while
enabling the linter.

* Remove redundant types in tests

If a slice already determines the type, the inner type declaration is
redundant. Remove the inner declarations.

* Mark autotag test cases as parallel

Autotag test cases is by far the outlier when it comes to test time.
While go test runs test cases in parallel,
it doesn't do so inside a given package, unless one marks the test cases
as parallel.

This change provides a significant speedup on a 8-core machine for test
runs.
2021-10-03 11:48:03 +11:00

197 lines
4.6 KiB
Go

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/stretchr/testify/assert"
"testing"
"time"
)
const (
studioID = 1
noImageID = 2
errImageID = 3
missingParentStudioID = 4
errStudioID = 5
errAliasID = 6
parentStudioID = 10
missingStudioID = 11
errParentStudioID = 12
)
const (
studioName = "testStudio"
url = "url"
details = "details"
rating = 5
parentStudioName = "parentStudio"
)
var parentStudio models.Studio = models.Studio{
Name: models.NullString(parentStudioName),
}
var imageBytes = []byte("imageBytes")
const image = "aW1hZ2VCeXRlcw=="
var (
createTime = time.Date(2001, 01, 01, 0, 0, 0, 0, time.Local)
updateTime = time.Date(2002, 01, 01, 0, 0, 0, 0, time.Local)
)
func createFullStudio(id int, parentID int) models.Studio {
ret := models.Studio{
ID: id,
Name: models.NullString(studioName),
URL: models.NullString(url),
Details: models.NullString(details),
CreatedAt: models.SQLiteTimestamp{
Timestamp: createTime,
},
UpdatedAt: models.SQLiteTimestamp{
Timestamp: updateTime,
},
Rating: models.NullInt64(rating),
}
if parentID != 0 {
ret.ParentID = models.NullInt64(int64(parentID))
}
return ret
}
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, aliases []string) *jsonschema.Studio {
return &jsonschema.Studio{
Name: studioName,
URL: url,
Details: details,
CreatedAt: models.JSONTime{
Time: createTime,
},
UpdatedAt: models.JSONTime{
Time: updateTime,
},
ParentStudio: parentStudio,
Image: image,
Rating: rating,
Aliases: aliases,
}
}
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{
{
createFullStudio(studioID, parentStudioID),
createFullJSONStudio(parentStudioName, image, []string{"alias"}),
false,
},
{
createEmptyStudio(noImageID),
createEmptyJSONStudio(),
false,
},
{
createFullStudio(errImageID, parentStudioID),
nil,
true,
},
{
createFullStudio(missingParentStudioID, missingStudioID),
createFullJSONStudio("", image, nil),
false,
},
{
createFullStudio(errStudioID, errParentStudioID),
nil,
true,
},
{
createFullStudio(errAliasID, parentStudioID),
nil,
true,
},
}
}
func TestToJSON(t *testing.T) {
initTestTable()
mockStudioReader := &mocks.StudioReaderWriter{}
imageErr := errors.New("error getting image")
mockStudioReader.On("GetImage", studioID).Return(imageBytes, nil).Once()
mockStudioReader.On("GetImage", noImageID).Return(nil, nil).Once()
mockStudioReader.On("GetImage", errImageID).Return(nil, imageErr).Once()
mockStudioReader.On("GetImage", missingParentStudioID).Return(imageBytes, nil).Maybe()
mockStudioReader.On("GetImage", errStudioID).Return(imageBytes, nil).Maybe()
mockStudioReader.On("GetImage", errAliasID).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)
aliasErr := errors.New("error getting aliases")
mockStudioReader.On("GetAliases", studioID).Return([]string{"alias"}, nil).Once()
mockStudioReader.On("GetAliases", noImageID).Return(nil, nil).Once()
mockStudioReader.On("GetAliases", errImageID).Return(nil, nil).Once()
mockStudioReader.On("GetAliases", missingParentStudioID).Return(nil, nil).Once()
mockStudioReader.On("GetAliases", errAliasID).Return(nil, aliasErr).Once()
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)
}