condition/engine/engine_test.go

124 lines
2.8 KiB
Go
Raw Normal View History

2023-07-20 14:55:00 +08:00
package engine
import (
"testing"
"git.fsdpf.net/go/condition"
"git.fsdpf.net/go/condition/contracts"
"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",
g: req.NewGlobalParam(`{"age": 30}`, nil),
2023-07-20 14:55:00 +08:00
}
cond1 := condition.New(contracts.AND, "条件1")
2023-07-20 14:55:00 +08:00
2023-07-20 15:04:07 +08:00
cond1.SetExpr(condition.NewConditionExpr(
2023-07-20 14:55:00 +08:00
"TestTable", "age", "age",
contracts.EQ, contracts.PARAM,
2023-07-20 14:55:00 +08:00
false, "", "",
))
engine.Case(cond1, func(data any, g req.GlobalParams) error {
2023-07-20 14:55:00 +08:00
t.Log("cond1", data)
return nil
})
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",
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
}
cond1 := condition.New(contracts.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.EQ, contracts.PARAM,
2023-07-20 14:55:00 +08:00
false, "", "",
))
engine.Case(cond1, func(data any, g req.GlobalParams) error {
2023-07-20 14:55:00 +08:00
t.Log("cond1", data)
return nil
})
cond2 := condition.New(contracts.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.EQ, contracts.PARAM,
2023-07-20 14:55:00 +08:00
false, "", "",
)).
2023-07-20 15:04:07 +08:00
SetExpr(condition.NewConditionExpr(
2023-07-20 14:55:00 +08:00
"TestTable", "age", "age",
contracts.EQ, contracts.PARAM,
2023-07-20 14:55:00 +08:00
false, "", "",
))
engine.Case(cond2, func(data any, g req.GlobalParams) error {
2023-07-20 14:55:00 +08:00
t.Log("cond2", data)
return nil
})
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}}))
}