+ test
This commit is contained in:
parent
6a3034e6d4
commit
6cd7d19290
8 changed files with 117 additions and 10 deletions
|
|
@ -27,8 +27,8 @@ env:
|
|||
|
||||
# 数据库配置
|
||||
- XIANG_DB_DEBUG=true
|
||||
- XIANG_DB_PRIMARY="root:123456@tcp(db-server:3308)/xiang?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
- XIANG_DB_SECONDARY="root:123456@tcp(db-server:3308)/xiang?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
- XIANG_DB_PRIMARY="root:123456@tcp(db-server:3308)/xun?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
- XIANG_DB_SECONDARY="root:123456@tcp(db-server:3308)/xun?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
- XIANG_DB_AESKEY="ZLX=T&f6refeCh-ro*r@"
|
||||
|
||||
# JWT配置
|
||||
|
|
@ -88,7 +88,7 @@ script:
|
|||
- make fmt-check
|
||||
- make misspell-check
|
||||
- make plugin
|
||||
# - make migrate
|
||||
- make migrate
|
||||
- make test
|
||||
|
||||
after_success:
|
||||
|
|
|
|||
6
Makefile
6
Makefile
|
|
@ -149,6 +149,6 @@ clean:
|
|||
rm -rf dist
|
||||
|
||||
|
||||
# .PHONY: migrate
|
||||
# migrate:
|
||||
# $(GO) test -tags $(TESTTAGS) -run TestModelMigrate$
|
||||
.PHONY: migrate
|
||||
migrate:
|
||||
$(GO) test -tags $(TESTTAGS) -run TestCommandMigrate$
|
||||
4
go.mod
4
go.mod
|
|
@ -5,10 +5,12 @@ go 1.16
|
|||
require (
|
||||
github.com/caarlos0/env/v6 v6.7.1 // indirect
|
||||
github.com/joho/godotenv v1.3.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/stretchr/testify v1.7.0
|
||||
github.com/yaoapp/gou v0.0.0-20210906083912-fc06d8ba7e7a // indirect
|
||||
github.com/yaoapp/kun v0.6.1
|
||||
golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect
|
||||
github.com/yaoapp/xun v0.5.2 // indirect
|
||||
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34 // indirect
|
||||
)
|
||||
|
||||
// go env -w GOPRIVATE=github.com/yaoapp/*
|
||||
|
|
|
|||
5
go.sum
5
go.sum
|
|
@ -112,6 +112,8 @@ github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcME
|
|||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-isatty v0.0.13 h1:qdl+GuBjcsKKDco5BsxPJlId98mSWNKqYA+Co0SC1yA=
|
||||
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
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.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
|
||||
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
|
||||
|
|
@ -206,10 +208,13 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210903071746-97244b99971b h1:3Dq0eVHn0uaQJmPO+/aYPI/fRMqdrVDbu7MQcku54gg=
|
||||
golang.org/x/sys v0.0.0-20210903071746-97244b99971b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34 h1:GkvMjFtXUmahfDtashnc1mnrCtuBVcwse5QV2lUk/tI=
|
||||
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
|
|
|
|||
16
init_test.go
16
init_test.go
|
|
@ -1,19 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/yaoapp/gou"
|
||||
"github.com/yaoapp/xun/capsule"
|
||||
)
|
||||
|
||||
var cfg Config
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
|
||||
cfg = NewConfig()
|
||||
|
||||
// 数据库连接
|
||||
capsule.AddConn("primary", "mysql", cfg.Database.Primary[0]).SetAsGlobal()
|
||||
|
||||
// 加密密钥
|
||||
gou.LoadCrypt(fmt.Sprintf(`{"key":"%s"}`, cfg.Database.AESKey), "AES")
|
||||
gou.LoadCrypt(`{}`, "PASSWORD")
|
||||
|
||||
// 加载数据
|
||||
Load(cfg)
|
||||
|
||||
// Run test suites
|
||||
exitVal := m.Run()
|
||||
|
||||
// we can do clean up code here
|
||||
gou.KillPlugins()
|
||||
|
||||
os.Exit(exitVal)
|
||||
}
|
||||
|
|
|
|||
3
load.go
3
load.go
|
|
@ -20,10 +20,9 @@ type Script struct {
|
|||
}
|
||||
|
||||
// Load 根据配置加载 API, FLow, Model, Plugin
|
||||
func Load(cfg Config) error {
|
||||
func Load(cfg Config) {
|
||||
LoadEngine(cfg.Path)
|
||||
LoadApp(cfg.RootAPI, cfg.RootFLow, cfg.RootModel, cfg.RootPlugin)
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadEngine 加载引擎的 API, Flow, Model 配置
|
||||
|
|
|
|||
37
xiang.go
37
xiang.go
|
|
@ -2,9 +2,46 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/yaoapp/gou"
|
||||
"github.com/yaoapp/kun/utils"
|
||||
"github.com/yaoapp/xun/capsule"
|
||||
)
|
||||
|
||||
// 主程序
|
||||
func main() {
|
||||
fmt.Printf("象传应用引擎 %s %s\n", VERSION, DOMAIN)
|
||||
cfg := NewConfig()
|
||||
|
||||
// 加载脚本
|
||||
capsule.AddConn("primary", "mysql", cfg.Database.Primary[0]).SetAsGlobal()
|
||||
|
||||
// 加密密钥
|
||||
gou.LoadCrypt(fmt.Sprintf(`{"key":"%s"}`, cfg.Database.AESKey), "AES")
|
||||
gou.LoadCrypt(`{}`, "PASSWORD")
|
||||
|
||||
// 加载模型
|
||||
Load(cfg)
|
||||
|
||||
// 启动服务
|
||||
for _, api := range gou.APIs {
|
||||
for _, p := range api.HTTP.Paths {
|
||||
utils.Dump(api.Name + ":" + p.Path)
|
||||
}
|
||||
|
||||
}
|
||||
gou.ServeHTTP(gou.Server{
|
||||
Host: cfg.Service.Host,
|
||||
Port: cfg.Service.Port,
|
||||
Allows: cfg.Service.Allow,
|
||||
Root: "/api",
|
||||
})
|
||||
}
|
||||
|
||||
// Migrate 数据迁移
|
||||
func Migrate() {
|
||||
for name, mod := range gou.Models {
|
||||
utils.Dump(name)
|
||||
mod.Migrate(true)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,61 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/yaoapp/kun/any"
|
||||
"github.com/yaoapp/kun/maps"
|
||||
"github.com/yaoapp/kun/utils"
|
||||
)
|
||||
|
||||
func TestCommand(t *testing.T) {
|
||||
|
||||
go main()
|
||||
|
||||
// 发送请求
|
||||
request := func() (maps.MapStr, error) {
|
||||
time.Sleep(time.Microsecond * 1000)
|
||||
url := fmt.Sprintf("http://%s:%d/api/user/info/1?select=id,name", "127.0.0.1", cfg.Service.Port)
|
||||
utils.Dump(url)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
res := maps.MakeMapStr()
|
||||
err = jsoniter.Unmarshal(body, &res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// 等待服务启动
|
||||
times := 0
|
||||
for times < 20 { // 2秒超时
|
||||
times++
|
||||
res, err := request()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
assert.Equal(t, 1, any.Of(res.Get("id")).CInt())
|
||||
assert.Equal(t, "管理员", res.Get("name"))
|
||||
return
|
||||
}
|
||||
|
||||
assert.True(t, false)
|
||||
}
|
||||
|
||||
func TestCommandMigrate(t *testing.T) {
|
||||
assert.NotPanics(t, func() {
|
||||
main()
|
||||
Migrate()
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue