From b8e4a2e7c0fa7ed80888457d6b64a2293914126f Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Tue, 14 Jun 2022 19:44:25 +0800 Subject: [PATCH] test: add driver and account test --- drivers/local/driver.go | 3 ++- go.mod | 4 ++- go.sum | 8 ++++++ internal/operations/account_test.go | 41 +++++++++++++++++++++++++++++ internal/operations/driver.go | 4 +++ internal/operations/driver_test.go | 17 ++++++++++++ internal/store/store.go | 6 +++++ 7 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 internal/operations/account_test.go create mode 100644 internal/operations/driver_test.go diff --git a/drivers/local/driver.go b/drivers/local/driver.go index aeb2142a..b764e27f 100644 --- a/drivers/local/driver.go +++ b/drivers/local/driver.go @@ -2,6 +2,7 @@ package local import ( "context" + "github.com/alist-org/alist/v3/internal/driver" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/pkg/utils" @@ -20,7 +21,7 @@ func (d Driver) Config() driver.Config { func (d *Driver) Init(ctx context.Context, account model.Account) error { d.Account = account addition := d.Account.Addition - err := utils.Json.UnmarshalFromString(addition, d.Addition) + err := utils.Json.UnmarshalFromString(addition, &d.Addition) if err != nil { return errors.Wrap(err, "error while unmarshal addition") } diff --git a/go.mod b/go.mod index 530be4c8..02a125e6 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.8.1 - gorm.io/gorm v1.23.5 + gorm.io/gorm v1.23.6 ) require ( @@ -25,6 +25,7 @@ require ( github.com/leodido/go-urn v1.2.1 // indirect github.com/lestrrat-go/strftime v1.0.6 // indirect github.com/mattn/go-isatty v0.0.14 // indirect + github.com/mattn/go-sqlite3 v1.14.13 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.1 // indirect @@ -35,4 +36,5 @@ require ( golang.org/x/text v0.3.7 // indirect google.golang.org/protobuf v1.28.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gorm.io/driver/sqlite v1.3.4 // indirect ) diff --git a/go.sum b/go.sum index b145b358..35ab047e 100644 --- a/go.sum +++ b/go.sum @@ -53,6 +53,9 @@ github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205Ah github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/mattn/go-sqlite3 v1.14.13 h1:1tj15ngiFfcZzii7yd82foL+ks+ouQcj8j/TPq3fk1I= +github.com/mattn/go-sqlite3 v1.14.13/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -114,5 +117,10 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/sqlite v1.3.4 h1:NnFOPVfzi4CPsJPH4wXr6rMkPb4ElHEqKMvrsx9c9Fk= +gorm.io/driver/sqlite v1.3.4/go.mod h1:B+8GyC9K7VgzJAcrcXMRPdnMcck+8FgJynEehEPM16U= +gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= gorm.io/gorm v1.23.5 h1:TnlF26wScKSvknUC/Rn8t0NLLM22fypYBlvj1+aH6dM= gorm.io/gorm v1.23.5/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= +gorm.io/gorm v1.23.6 h1:KFLdNgri4ExFFGTRGGFWON2P1ZN28+9SJRN8voOoYe0= +gorm.io/gorm v1.23.6/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= diff --git a/internal/operations/account_test.go b/internal/operations/account_test.go new file mode 100644 index 00000000..622dc2c0 --- /dev/null +++ b/internal/operations/account_test.go @@ -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) + } + } + } +} diff --git a/internal/operations/driver.go b/internal/operations/driver.go index 93c11101..84bdca7a 100644 --- a/internal/operations/driver.go +++ b/internal/operations/driver.go @@ -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) diff --git a/internal/operations/driver_test.go b/internal/operations/driver_test.go new file mode 100644 index 00000000..e9f088b5 --- /dev/null +++ b/internal/operations/driver_test.go @@ -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") + } +} diff --git a/internal/store/store.go b/internal/store/store.go index bd43c0f6..d049ac75 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -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{}) +}