stash/pkg/sqlite/folder_test.go

242 lines
4.5 KiB
Go
Raw Normal View History

File storage rewrite (#2676) * Restructure data layer part 2 (#2599) * Refactor and separate image model * Refactor image query builder * Handle relationships in image query builder * Remove relationship management methods * Refactor gallery model/query builder * Add scenes to gallery model * Convert scene model * Refactor scene models * Remove unused methods * Add unit tests for gallery * Add image tests * Add scene tests * Convert unnecessary scene value pointers to values * Convert unnecessary pointer values to values * Refactor scene partial * Add scene partial tests * Refactor ImagePartial * Add image partial tests * Refactor gallery partial update * Add partial gallery update tests * Use zero/null package for null values * Add files and scan system * Add sqlite implementation for files/folders * Add unit tests for files/folders * Image refactors * Update image data layer * Refactor gallery model and creation * Refactor scene model * Refactor scenes * Don't set title from filename * Allow galleries to freely add/remove images * Add multiple scene file support to graphql and UI * Add multiple file support for images in graphql/UI * Add multiple file for galleries in graphql/UI * Remove use of some deprecated fields * Remove scene path usage * Remove gallery path usage * Remove path from image * Move funscript to video file * Refactor caption detection * Migrate existing data * Add post commit/rollback hook system * Lint. Comment out import/export tests * Add WithDatabase read only wrapper * Prepend tasks to list * Add 32 pre-migration * Add warnings in release and migration notes
2022-07-13 06:30:54 +00:00
//go:build integration
// +build integration
package sqlite_test
import (
"context"
"reflect"
"testing"
"time"
"github.com/stashapp/stash/pkg/file"
"github.com/stretchr/testify/assert"
)
var (
invalidFolderID = file.FolderID(invalidID)
invalidFileID = file.ID(invalidID)
)
func Test_FolderStore_Create(t *testing.T) {
var (
path = "path"
fileModTime = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
createdAt = time.Date(2001, 1, 1, 0, 0, 0, 0, time.UTC)
updatedAt = time.Date(2001, 1, 1, 0, 0, 0, 0, time.UTC)
)
tests := []struct {
name string
newObject file.Folder
wantErr bool
}{
{
"full",
file.Folder{
DirEntry: file.DirEntry{
ZipFileID: &fileIDs[fileIdxZip],
ZipFile: makeZipFileWithID(fileIdxZip),
ModTime: fileModTime,
},
Path: path,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
},
false,
},
{
"invalid parent folder id",
file.Folder{
Path: path,
ParentFolderID: &invalidFolderID,
},
true,
},
{
"invalid zip file id",
file.Folder{
DirEntry: file.DirEntry{
ZipFileID: &invalidFileID,
},
Path: path,
},
true,
},
}
qb := db.Folder
for _, tt := range tests {
runWithRollbackTxn(t, tt.name, func(t *testing.T, ctx context.Context) {
assert := assert.New(t)
s := tt.newObject
if err := qb.Create(ctx, &s); (err != nil) != tt.wantErr {
t.Errorf("FolderStore.Create() error = %v, wantErr = %v", err, tt.wantErr)
}
if tt.wantErr {
assert.Zero(s.ID)
return
}
assert.NotZero(s.ID)
copy := tt.newObject
copy.ID = s.ID
assert.Equal(copy, s)
// ensure can find the folder
found, err := qb.FindByPath(ctx, path)
if err != nil {
t.Errorf("FolderStore.Find() error = %v", err)
}
assert.Equal(copy, *found)
})
}
}
func Test_FolderStore_Update(t *testing.T) {
var (
path = "path"
fileModTime = time.Date(2000, 1, 2, 3, 4, 5, 6, time.UTC)
createdAt = time.Date(2001, 1, 2, 3, 4, 5, 6, time.UTC)
updatedAt = time.Date(2002, 1, 2, 3, 4, 5, 6, time.UTC)
)
tests := []struct {
name string
updatedObject *file.Folder
wantErr bool
}{
{
"full",
&file.Folder{
ID: folderIDs[folderIdxWithParentFolder],
DirEntry: file.DirEntry{
ZipFileID: &fileIDs[fileIdxZip],
ZipFile: makeZipFileWithID(fileIdxZip),
ModTime: fileModTime,
},
Path: path,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
},
false,
},
{
"clear zip",
&file.Folder{
ID: folderIDs[folderIdxInZip],
Path: path,
},
false,
},
{
"clear folder",
&file.Folder{
ID: folderIDs[folderIdxWithParentFolder],
Path: path,
},
false,
},
{
"invalid parent folder id",
&file.Folder{
ID: folderIDs[folderIdxWithParentFolder],
Path: path,
ParentFolderID: &invalidFolderID,
},
true,
},
{
"invalid zip file id",
&file.Folder{
ID: folderIDs[folderIdxWithParentFolder],
DirEntry: file.DirEntry{
ZipFileID: &invalidFileID,
},
Path: path,
},
true,
},
}
qb := db.Folder
for _, tt := range tests {
runWithRollbackTxn(t, tt.name, func(t *testing.T, ctx context.Context) {
assert := assert.New(t)
copy := *tt.updatedObject
if err := qb.Update(ctx, tt.updatedObject); (err != nil) != tt.wantErr {
t.Errorf("FolderStore.Update() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr {
return
}
s, err := qb.FindByPath(ctx, path)
if err != nil {
t.Errorf("FolderStore.Find() error = %v", err)
}
assert.Equal(copy, *s)
return
})
}
}
func makeFolderWithID(index int) *file.Folder {
ret := makeFolder(index)
ret.ID = folderIDs[index]
return &ret
}
func Test_FolderStore_FindByPath(t *testing.T) {
getPath := func(index int) string {
return folderPaths[index]
}
tests := []struct {
name string
path string
want *file.Folder
wantErr bool
}{
{
"valid",
getPath(folderIdxWithFiles),
makeFolderWithID(folderIdxWithFiles),
false,
},
{
"invalid",
"invalid path",
nil,
false,
},
}
qb := db.Folder
for _, tt := range tests {
runWithRollbackTxn(t, tt.name, func(t *testing.T, ctx context.Context) {
got, err := qb.FindByPath(ctx, tt.path)
if (err != nil) != tt.wantErr {
t.Errorf("FolderStore.FindByPath() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FolderStore.FindByPath() = %v, want %v", got, tt.want)
}
})
}
}