test: add `GetAccountVirtualFilesByPath` test

refactor/fs
Noah Hsu 2022-06-14 22:23:33 +08:00
parent 097b516dc5
commit 5780d9d834
2 changed files with 45 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/operations"
"github.com/alist-org/alist/v3/internal/store"
"github.com/alist-org/alist/v3/pkg/utils"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
@ -39,3 +40,34 @@ func TestCreateAccount(t *testing.T) {
}
}
}
func TestGetAccountVirtualFilesByPath(t *testing.T) {
Setup(t)
virtualFiles := operations.GetAccountVirtualFilesByPath("/a")
var names []string
for _, virtualFile := range virtualFiles {
names = append(names, virtualFile.GetName())
}
var expectedNames = []string{"b", "c", "d"}
if utils.SliceEqual(names, expectedNames) {
t.Logf("passed")
} else {
t.Errorf("expected: %+v, got: %+v", expectedNames, names)
}
}
func Setup(t *testing.T) {
var accounts = []model.Account{
{Driver: "Local", VirtualPath: "/a/b", Index: 0, Addition: "{}"},
{Driver: "Local", VirtualPath: "/a/c", Index: 1, Addition: "{}"},
{Driver: "Local", VirtualPath: "/a/d", Index: 2, Addition: "{}"},
{Driver: "Local", VirtualPath: "/a/d/e", Index: 3, Addition: "{}"},
{Driver: "Local", VirtualPath: "/a/d/e.balance", Index: 4, Addition: "{}"},
}
for _, account := range accounts {
err := operations.CreateAccount(context.Background(), account)
if err != nil {
t.Fatalf("failed to create account: %+v", err)
}
}
}

13
pkg/utils/slice.go Normal file
View File

@ -0,0 +1,13 @@
package utils
func SliceEqual[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}