2023-07-20 14:55:00 +08:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.fsdpf.net/go/condition"
|
2024-05-08 22:32:51 +08:00
|
|
|
"git.fsdpf.net/go/req"
|
2023-07-20 14:55:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type TestTable struct {
|
|
|
|
Null any
|
|
|
|
Age int64 `db:"age"`
|
|
|
|
Name string `db:"name"`
|
|
|
|
Map map[string]any `db:"map"`
|
|
|
|
Bool bool `db:"bool"`
|
|
|
|
Struct *struct{ Name string } `db:"json"`
|
|
|
|
Array []any `db:"array"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEngine(t *testing.T) {
|
|
|
|
engine := Engine[any]{
|
|
|
|
code: "TestTable",
|
2024-05-08 22:32:51 +08:00
|
|
|
g: req.NewGlobalParam(`{"age": 30}`, nil),
|
2023-07-20 14:55:00 +08:00
|
|
|
}
|
|
|
|
|
2024-05-09 10:08:28 +08:00
|
|
|
cond1 := condition.New(condition.Describe("条件1"))
|
2023-07-20 14:55:00 +08:00
|
|
|
|
2024-05-09 10:21:15 +08:00
|
|
|
cond1.SetExpr(condition.NewExpr("TestTable", "age", condition.Token("age", condition.PARAM)))
|
2023-07-20 14:55:00 +08:00
|
|
|
|
2024-05-08 22:32:51 +08:00
|
|
|
engine.Case(cond1, func(data any, g req.GlobalParams) error {
|
2023-07-20 14:55:00 +08:00
|
|
|
t.Log("cond1", data)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2024-05-08 22:32:51 +08:00
|
|
|
engine.Default(func(data any, g req.GlobalParams) error {
|
2023-07-20 14:55:00 +08:00
|
|
|
t.Log("default", data)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
engine.Execute(map[string]any{
|
|
|
|
"null": nil,
|
|
|
|
"age": 30,
|
|
|
|
"name": "张三",
|
|
|
|
"map": map[string]any{
|
|
|
|
"field": 1,
|
|
|
|
},
|
|
|
|
"bool": false,
|
|
|
|
"struct": &struct{ Name string }{"李四"},
|
|
|
|
"array": []any{1, "a", false},
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Log("execute error", engine.Execute(TestTable{nil, 30, "张三", map[string]any{"field": 1}, false, &struct{ Name string }{"李四"}, []any{1, "a", false}}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRelationEngine(t *testing.T) {
|
|
|
|
engine := Engine[any]{
|
2023-07-24 13:39:12 +08:00
|
|
|
code: "TestTable",
|
2024-05-08 22:32:51 +08:00
|
|
|
g: req.NewGlobalParam(`{"age": 30}`, nil),
|
2023-07-24 13:39:12 +08:00
|
|
|
opts: engineOptions{
|
|
|
|
debug: false,
|
|
|
|
relations: []string{"TestTableA"},
|
|
|
|
},
|
2023-07-20 14:55:00 +08:00
|
|
|
}
|
|
|
|
|
2024-05-09 10:08:28 +08:00
|
|
|
cond1 := condition.New(condition.Describe("条件1")).
|
2024-05-09 10:21:15 +08:00
|
|
|
SetExpr(condition.NewExpr("TestTable", "age", condition.Token("age", condition.PARAM)))
|
2023-07-20 14:55:00 +08:00
|
|
|
|
2024-05-08 22:32:51 +08:00
|
|
|
engine.Case(cond1, func(data any, g req.GlobalParams) error {
|
2023-07-20 14:55:00 +08:00
|
|
|
t.Log("cond1", data)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2024-05-09 10:08:28 +08:00
|
|
|
cond2 := condition.New(condition.Type(condition.OR), condition.Describe("条件2")).
|
2024-05-09 10:21:15 +08:00
|
|
|
SetExpr(condition.NewExpr("TestTableA", "age", condition.Token("age", condition.PARAM))).
|
|
|
|
SetExpr(condition.NewExpr("TestTable", "age", condition.Token("age", condition.PARAM)))
|
2023-07-20 14:55:00 +08:00
|
|
|
|
2024-05-08 22:32:51 +08:00
|
|
|
engine.Case(cond2, func(data any, g req.GlobalParams) error {
|
2023-07-20 14:55:00 +08:00
|
|
|
t.Log("cond2", data)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2024-05-08 22:32:51 +08:00
|
|
|
engine.Default(func(data any, g req.GlobalParams) error {
|
2023-07-20 14:55:00 +08:00
|
|
|
t.Log("default", data)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
engine.Execute(map[string]any{
|
|
|
|
"null": nil,
|
|
|
|
"age": 31,
|
|
|
|
"name": "张三",
|
|
|
|
"map": map[string]any{
|
|
|
|
"field": 1,
|
|
|
|
},
|
|
|
|
"bool": false,
|
|
|
|
"struct": &struct{ Name string }{"李四"},
|
|
|
|
"array": []any{1, "a", false},
|
|
|
|
"TestTableA": map[string]any{
|
|
|
|
"age": 30,
|
|
|
|
"TestTableA": map[string]any{
|
|
|
|
"age": 22,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// t.Log("execute error", engine.Execute(TestTable{nil, 30, "张三", map[string]any{"field": 1}, false, &struct{ Name string }{"李四"}, []any{1, "a", false}}))
|
|
|
|
}
|