2023-07-20 14:55:00 +08:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.fsdpf.net/go/condition"
|
|
|
|
"git.fsdpf.net/go/contracts"
|
|
|
|
"git.fsdpf.net/go/contracts/support"
|
|
|
|
)
|
|
|
|
|
|
|
|
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",
|
|
|
|
g: support.NewGlobalParam(`{"age": 30}`, nil),
|
|
|
|
}
|
|
|
|
|
|
|
|
cond1 := condition.New(contracts.ConditionType_AND, "条件1")
|
|
|
|
|
2023-07-20 15:04:07 +08:00
|
|
|
cond1.SetExpr(condition.NewConditionExpr(
|
2023-07-20 14:55:00 +08:00
|
|
|
"TestTable", "age", "age",
|
|
|
|
contracts.ConditionOperator_EQ, contracts.ConditionTokenType_PARAM,
|
|
|
|
false, "", "",
|
|
|
|
))
|
|
|
|
|
|
|
|
engine.Case(cond1, func(data any, g contracts.GlobalParams) error {
|
|
|
|
t.Log("cond1", data)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
engine.Default(func(data any, g contracts.GlobalParams) error {
|
|
|
|
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]{
|
|
|
|
code: "TestTable",
|
|
|
|
g: support.NewGlobalParam(`{"age": 30}`, nil),
|
|
|
|
relations: []string{"TestTableA"},
|
|
|
|
}
|
|
|
|
|
|
|
|
cond1 := condition.New(contracts.ConditionType_AND, "条件1").
|
2023-07-20 15:04:07 +08:00
|
|
|
SetExpr(condition.NewConditionExpr(
|
2023-07-20 14:55:00 +08:00
|
|
|
"TestTable", "age", "age",
|
|
|
|
contracts.ConditionOperator_EQ, contracts.ConditionTokenType_PARAM,
|
|
|
|
false, "", "",
|
|
|
|
))
|
|
|
|
|
|
|
|
engine.Case(cond1, func(data any, g contracts.GlobalParams) error {
|
|
|
|
t.Log("cond1", data)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
cond2 := condition.New(contracts.ConditionType_OR, "条件2").
|
2023-07-20 15:04:07 +08:00
|
|
|
SetExpr(condition.NewConditionExpr(
|
2023-07-20 14:55:00 +08:00
|
|
|
"TestTableA", "age", "age",
|
|
|
|
contracts.ConditionOperator_EQ, contracts.ConditionTokenType_PARAM,
|
|
|
|
false, "", "",
|
|
|
|
)).
|
2023-07-20 15:04:07 +08:00
|
|
|
SetExpr(condition.NewConditionExpr(
|
2023-07-20 14:55:00 +08:00
|
|
|
"TestTable", "age", "age",
|
|
|
|
contracts.ConditionOperator_EQ, contracts.ConditionTokenType_PARAM,
|
|
|
|
false, "", "",
|
|
|
|
))
|
|
|
|
|
|
|
|
engine.Case(cond2, func(data any, g contracts.GlobalParams) error {
|
|
|
|
t.Log("cond2", data)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
engine.Default(func(data any, g contracts.GlobalParams) error {
|
|
|
|
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}}))
|
|
|
|
}
|