gomog/internal/engine/aggregate_batch2_test.go

155 lines
3.6 KiB
Go

package engine
import (
"testing"
)
// TestSwitchExpr 测试 $switch 条件表达式
func TestSwitchExpr(t *testing.T) {
engine := &AggregationEngine{}
tests := []struct {
name string
data map[string]interface{}
spec map[string]interface{}
expected interface{}
}{
{
name: "switch with matching first branch",
data: map[string]interface{}{"score": float64(95)},
spec: map[string]interface{}{
"branches": []interface{}{
map[string]interface{}{
"case": map[string]interface{}{
"$gte": []interface{}{"$score", float64(90)},
},
"then": "A",
},
map[string]interface{}{
"case": map[string]interface{}{
"$gte": []interface{}{"$score", float64(80)},
},
"then": "B",
},
},
"default": "F",
},
expected: "A",
},
{
name: "switch with matching second branch",
data: map[string]interface{}{"score": float64(85)},
spec: map[string]interface{}{
"branches": []interface{}{
map[string]interface{}{
"case": map[string]interface{}{
"$gte": []interface{}{"$score", float64(90)},
},
"then": "A",
},
map[string]interface{}{
"case": map[string]interface{}{
"$gte": []interface{}{"$score", float64(80)},
},
"then": "B",
},
},
"default": "F",
},
expected: "B",
},
{
name: "switch with no matching branches uses default",
data: map[string]interface{}{"score": float64(70)},
spec: map[string]interface{}{
"branches": []interface{}{
map[string]interface{}{
"case": map[string]interface{}{
"$gte": []interface{}{"$score", float64(90)},
},
"then": "A",
},
map[string]interface{}{
"case": map[string]interface{}{
"$gte": []interface{}{"$score", float64(80)},
},
"then": "B",
},
},
"default": "F",
},
expected: "F",
},
{
name: "switch with arithmetic in case",
data: map[string]interface{}{"price": float64(100), "tax": float64(20)},
spec: map[string]interface{}{
"branches": []interface{}{
map[string]interface{}{
"case": map[string]interface{}{
"$gt": []interface{}{
map[string]interface{}{
"$add": []interface{}{"$price", "$tax"},
},
float64(100),
},
},
"then": "expensive",
},
},
"default": "cheap",
},
expected: "expensive",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := engine.switchExpr(tt.spec, tt.data)
if result != tt.expected {
t.Errorf("switchExpr() = %v, want %v", result, tt.expected)
}
})
}
}
// TestEvaluateExpressionWithSwitch 测试 $switch 在聚合表达式中的评估
func TestEvaluateExpressionWithSwitch(t *testing.T) {
engine := &AggregationEngine{}
tests := []struct {
name string
data map[string]interface{}
expression interface{}
expected interface{}
}{
{
name: "nested switch in expression",
data: map[string]interface{}{"x": float64(1)},
expression: map[string]interface{}{
"$switch": map[string]interface{}{
"branches": []interface{}{
map[string]interface{}{
"case": map[string]interface{}{
"$eq": []interface{}{"$x", float64(1)},
},
"then": "one",
},
},
"default": "other",
},
},
expected: "one",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := engine.evaluateExpression(tt.data, tt.expression)
if result != tt.expected {
t.Errorf("evaluateExpression() = %v, want %v", result, tt.expected)
}
})
}
}