diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 5f910a3f..14051b1d 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -62,7 +62,7 @@ jobs: mongo: ["6.0"] steps: - name: Setup Cache - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: | ~/.cache/go-build @@ -185,6 +185,6 @@ jobs: make test - name: Codecov Report - uses: codecov/codecov-action@v2 + uses: codecov/codecov-action@v3 with: token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos diff --git a/Makefile b/Makefile index 66d29d45..d4eeef09 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ COMMIT := $(shell git log | head -n 1 | awk '{print substr($$2, 0, 12)}') NOW := $(shell date +"%FT%T%z") # ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) -TESTFOLDER := $(shell $(GO) list ./... | grep -E 'api' | grep -vE 'examples|tests*|config|widgets') +TESTFOLDER := $(shell $(GO) list ./... | grep -E 'api|model|flow' | grep -vE 'examples|tests*|config|widgets') TESTTAGS ?= "" # TESTWIDGETS := $(shell $(GO) list ./widgets/...) diff --git a/api/api_test.go b/api/api_test.go index c5c76808..ed91aeef 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -10,7 +10,7 @@ import ( ) func TestLoad(t *testing.T) { - test.Prepare(t) + test.Prepare(t, config.Conf) defer test.Clean() Load(config.Conf) diff --git a/flow/flow.go b/flow/flow.go index b938d875..9b5e07cf 100644 --- a/flow/flow.go +++ b/flow/flow.go @@ -10,7 +10,10 @@ import ( // Load 加载业务逻辑编排 func Load(cfg config.Config) error { exts := []string{"*.flow.yao", "*.flow.json", "*.flow.jsonc"} - return application.App.Walk("apis", func(root, file string, isdir bool) error { + return application.App.Walk("flows", func(root, file string, isdir bool) error { + if isdir { + return nil + } _, err := flow.Load(file, share.ID(root, file)) return err }, exts...) diff --git a/flow/flow_test.go b/flow/flow_test.go index f9edb93a..6922d7a7 100644 --- a/flow/flow_test.go +++ b/flow/flow_test.go @@ -6,17 +6,30 @@ import ( "github.com/stretchr/testify/assert" "github.com/yaoapp/gou/flow" "github.com/yaoapp/yao/config" + "github.com/yaoapp/yao/test" ) func TestLoad(t *testing.T) { + test.Prepare(t, config.Conf) + defer test.Clean() + Load(config.Conf) check(t) } func check(t *testing.T) { - keys := []string{} - for key := range flow.Flows { - keys = append(keys, key) + ids := map[string]bool{} + for id := range flow.Flows { + ids[id] = true } - assert.Equal(t, 26, len(keys)) + assert.True(t, ids["tests.basic"]) + assert.True(t, ids["tests.session"]) + + // wskeys := []string{} + // for key := range websocket.Upgraders { + // wskeys = append(wskeys, key) + // } + + // assert.Equal(t, 5, len(keys)) + // assert.Equal(t, 1, len(wskeys)) } diff --git a/model/model.go b/model/model.go index f0e481b8..4a8d2569 100644 --- a/model/model.go +++ b/model/model.go @@ -1,6 +1,8 @@ package model import ( + "fmt" + "github.com/yaoapp/gou/application" "github.com/yaoapp/gou/model" "github.com/yaoapp/yao/config" @@ -9,6 +11,10 @@ import ( // Load 加载数据模型 func Load(cfg config.Config) error { + + model.WithCrypt([]byte(fmt.Sprintf(`{"key":"%s"}`, cfg.DB.AESKey)), "AES") + model.WithCrypt([]byte(`{}`), "PASSWORD") + exts := []string{"*.mod.yao", "*.mod.json", "*.mod.jsonc"} return application.App.Walk("models", func(root, file string, isdir bool) error { if isdir { diff --git a/model/model_test.go b/model/model_test.go index 1f30ddc4..8fa84445 100644 --- a/model/model_test.go +++ b/model/model_test.go @@ -11,7 +11,7 @@ import ( ) func TestLoad(t *testing.T) { - test.Prepare(t) + test.Prepare(t, config.Conf) defer test.Clean() Load(config.Conf) diff --git a/test/test.go b/test/test.go index d6b6f360..27c9d066 100644 --- a/test/test.go +++ b/test/test.go @@ -1,14 +1,22 @@ package test import ( + "fmt" "os" "testing" "github.com/yaoapp/gou/application" + "github.com/yaoapp/gou/model" + "github.com/yaoapp/gou/query" + "github.com/yaoapp/gou/query/gou" + "github.com/yaoapp/kun/exception" + "github.com/yaoapp/xun/capsule" + "github.com/yaoapp/yao/config" + "github.com/yaoapp/yao/share" ) // Prepare test environment -func Prepare(t *testing.T) { +func Prepare(t *testing.T, cfg config.Config) { root := os.Getenv("YAO_TEST_APPLICATION") var app application.Application var err error @@ -28,7 +36,76 @@ func Prepare(t *testing.T) { t.Fatal(err) } application.Load(app) + + dbconnect(t, cfg) + load(t, cfg) } // Clean the test environment -func Clean() {} +func Clean() { + dbclose() +} + +func dbclose() { + if capsule.Global != nil { + capsule.Global.Connections.Range(func(key, value any) bool { + if conn, ok := value.(*capsule.Connection); ok { + conn.Close() + } + return true + }) + } +} + +func dbconnect(t *testing.T, cfg config.Config) { + + // connect db + switch cfg.DB.Driver { + case "sqlite3": + capsule.AddConn("primary", "sqlite3", cfg.DB.Primary[0]).SetAsGlobal() + break + default: + capsule.AddConn("primary", "mysql", cfg.DB.Primary[0]).SetAsGlobal() + break + } + +} + +func load(t *testing.T, cfg config.Config) { + loadModel(t, cfg) + loadQuery(t, cfg) +} + +func loadModel(t *testing.T, cfg config.Config) { + model.WithCrypt([]byte(fmt.Sprintf(`{"key":"%s"}`, cfg.DB.AESKey)), "AES") + model.WithCrypt([]byte(`{}`), "PASSWORD") + + exts := []string{"*.mod.yao", "*.mod.json", "*.mod.jsonc"} + err := application.App.Walk("models", func(root, file string, isdir bool) error { + if isdir { + return nil + } + _, err := model.Load(file, share.ID(root, file)) + return err + }, exts...) + + if err != nil { + t.Fatal(err) + } +} + +func loadQuery(t *testing.T, cfg config.Config) { + + // query engine + query.Register("query-test", &gou.Query{ + Query: capsule.Query(), + GetTableName: func(s string) string { + if mod, has := model.Models[s]; has { + return mod.MetaData.Table.Name + } + exception.New("[query] %s not found", 404, s).Throw() + return s + }, + AESKey: cfg.DB.AESKey, + }) +}