Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WithoutPants committed Nov 8, 2024
1 parent 809ab6e commit 3c4203a
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 0 deletions.
91 changes: 91 additions & 0 deletions pkg/savedfilter/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package savedfilter

import (
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/models/jsonschema"
"github.com/stashapp/stash/pkg/models/mocks"
"github.com/stretchr/testify/assert"

"testing"
)

const (
savedFilterID = 1
noImageID = 2
errImageID = 3
errAliasID = 4
withParentsID = 5
errParentsID = 6
)

const (
filterName = "testFilter"
mode = models.FilterModeGalleries
)

var (
findFilter = models.FindFilterType{}
objectFilter = make(map[string]interface{})
uiOptions = make(map[string]interface{})
)

func createSavedFilter(id int) models.SavedFilter {
return models.SavedFilter{
ID: id,
Name: filterName,
Mode: mode,
FindFilter: &findFilter,
ObjectFilter: objectFilter,
UIOptions: uiOptions,
}
}

func createJSONSavedFilter() *jsonschema.SavedFilter {
return &jsonschema.SavedFilter{
Name: filterName,
Mode: mode,
FindFilter: &findFilter,
ObjectFilter: objectFilter,
UIOptions: uiOptions,
}
}

type testScenario struct {
savedFilter models.SavedFilter
expected *jsonschema.SavedFilter
err bool
}

var scenarios []testScenario

func initTestTable() {
scenarios = []testScenario{
{
createSavedFilter(savedFilterID),
createJSONSavedFilter(),
false,
},
}
}

func TestToJSON(t *testing.T) {
initTestTable()

db := mocks.NewDatabase()

for i, s := range scenarios {
savedFilter := s.savedFilter
json, err := ToJSON(testCtx, &savedFilter)

switch {
case !s.err && err != nil:
t.Errorf("[%d] unexpected error: %s", i, err.Error())
case s.err && err == nil:
t.Errorf("[%d] expected error not returned", i)
default:
assert.Equal(t, s.expected, json, "[%d]", i)
}
}

db.AssertExpectations(t)
}
124 changes: 124 additions & 0 deletions pkg/savedfilter/import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package savedfilter

import (
"context"
"errors"
"testing"

"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/models/jsonschema"
"github.com/stashapp/stash/pkg/models/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

const (
savedFilterNameErr = "savedFilterNameErr"
existingSavedFilterName = "existingSavedFilterName"

existingFilterID = 100
)

var testCtx = context.Background()

func TestImporterName(t *testing.T) {
i := Importer{
Input: jsonschema.SavedFilter{
Name: filterName,
},
}

assert.Equal(t, filterName, i.Name())
}

func TestImporterPreImport(t *testing.T) {
i := Importer{
Input: jsonschema.SavedFilter{
Name: filterName,
},
}

err := i.PreImport(testCtx)
assert.Nil(t, err)
}

func TestImporterPostImport(t *testing.T) {
db := mocks.NewDatabase()

i := Importer{
ReaderWriter: db.SavedFilter,
Input: jsonschema.SavedFilter{},
}

err := i.PostImport(testCtx, savedFilterID)
assert.Nil(t, err)

db.AssertExpectations(t)
}

func TestImporterFindExistingID(t *testing.T) {
db := mocks.NewDatabase()

i := Importer{
ReaderWriter: db.SavedFilter,
Input: jsonschema.SavedFilter{
Name: filterName,
},
}

id, err := i.FindExistingID(testCtx)
assert.Nil(t, id)
assert.Nil(t, err)
}

func TestCreate(t *testing.T) {
db := mocks.NewDatabase()

savedFilter := models.SavedFilter{
Name: filterName,
}

savedFilterErr := models.SavedFilter{
Name: savedFilterNameErr,
}

i := Importer{
ReaderWriter: db.SavedFilter,
savedFilter: savedFilter,
}

errCreate := errors.New("Create error")
db.SavedFilter.On("Create", testCtx, &savedFilter).Run(func(args mock.Arguments) {
t := args.Get(1).(*models.SavedFilter)
t.ID = savedFilterID
}).Return(nil).Once()
db.SavedFilter.On("Create", testCtx, &savedFilterErr).Return(errCreate).Once()

id, err := i.Create(testCtx)
assert.Equal(t, savedFilterID, *id)
assert.Nil(t, err)

i.savedFilter = savedFilterErr
id, err = i.Create(testCtx)
assert.Nil(t, id)
assert.NotNil(t, err)

db.AssertExpectations(t)
}

func TestUpdate(t *testing.T) {
db := mocks.NewDatabase()

savedFilterErr := models.SavedFilter{
Name: savedFilterNameErr,
}

i := Importer{
ReaderWriter: db.SavedFilter,
savedFilter: savedFilterErr,
}

// Update is not currently supported
err := i.Update(testCtx, existingFilterID)
assert.NotNil(t, err)
}

0 comments on commit 3c4203a

Please sign in to comment.