test: add driver and account test

This commit is contained in:
Noah Hsu
2022-06-14 19:44:25 +08:00
parent 0d4542a3f1
commit b8e4a2e7c0
7 changed files with 81 additions and 2 deletions

View File

@ -0,0 +1,41 @@
package operations_test
import (
"context"
"testing"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/operations"
"github.com/alist-org/alist/v3/internal/store"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func init() {
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
store.Init(db)
}
func TestCreateAccount(t *testing.T) {
var accounts = []struct {
account model.Account
iserr bool
}{
{account: model.Account{Driver: "Local", VirtualPath: "/local", Addition: "{}"}, iserr: false},
{account: model.Account{Driver: "Local", VirtualPath: "/local", Addition: "{}"}, iserr: true},
{account: model.Account{Driver: "None", VirtualPath: "/none", Addition: "{}"}, iserr: true},
}
for _, account := range accounts {
err := operations.CreateAccount(context.Background(), account.account)
if err != nil {
if !account.iserr {
t.Errorf("failed to create account: %+v", err)
} else {
t.Logf("expect failed to create account: %+v", err)
}
}
}
}

View File

@ -28,6 +28,10 @@ func GetDriverNew(name string) (New, error) {
return n, nil
}
func GetDriverItemsMap() map[string]driver.Items {
return driverItemsMap
}
func registerDriverItems(config driver.Config, addition driver.Additional) {
tAddition := reflect.TypeOf(addition)
mainItems := getMainItems(config)

View File

@ -0,0 +1,17 @@
package operations_test
import (
"testing"
_ "github.com/alist-org/alist/v3/drivers"
"github.com/alist-org/alist/v3/internal/operations"
)
func TestDriverItemsMap(t *testing.T) {
itemsMap := operations.GetDriverItemsMap()
if len(itemsMap) != 0 {
t.Logf("driverItemsMap: %v", itemsMap)
} else {
t.Errorf("expected driverItemsMap not empty, but got empty")
}
}

View File

@ -1,7 +1,13 @@
package store
import (
"github.com/alist-org/alist/v3/internal/model"
"gorm.io/gorm"
)
var db gorm.DB
func Init(dbgorm *gorm.DB) {
db = *dbgorm
db.AutoMigrate(&model.Account{})
}