34 lines
1.2 KiB
Go
34 lines
1.2 KiB
Go
package run
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestMatchCondition(t *testing.T) {
|
|
answers := map[string]interface{}{"weight": float64(6), "urgent": true, "symptom": "持续呕吐"}
|
|
tests := []struct {
|
|
name string
|
|
rule string
|
|
want bool
|
|
}{
|
|
{name: "default", rule: `{}`, want: true},
|
|
{name: "equals", rule: `{"field":"urgent","operator":"equals","value":true}`, want: true},
|
|
{name: "greater", rule: `{"field":"weight","operator":"greater_than","value":5}`, want: true},
|
|
{name: "contains", rule: `{"field":"symptom","operator":"contains","value":"呕吐"}`, want: true},
|
|
{name: "all", rule: `{"all":[{"field":"urgent","operator":"equals","value":true},{"field":"weight","operator":"greater_than","value":5}]}`, want: true},
|
|
{name: "any false", rule: `{"any":[{"field":"urgent","operator":"equals","value":false},{"field":"weight","operator":"less_than","value":3}]}`, want: false},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
got, err := matchCondition(json.RawMessage(test.rule), answers)
|
|
if err != nil {
|
|
t.Fatalf("matchCondition returned error: %v", err)
|
|
}
|
|
if got != test.want {
|
|
t.Fatalf("matchCondition() = %v, want %v", got, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|