130 lines
3.1 KiB
Go
130 lines
3.1 KiB
Go
package engine
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.kingecg.top/kingecg/gomog/pkg/types"
|
|
)
|
|
|
|
func TestMatchFilter(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
doc map[string]interface{}
|
|
filter types.Filter
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "empty filter",
|
|
doc: map[string]interface{}{"name": "Alice"},
|
|
filter: nil,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "simple equality",
|
|
doc: map[string]interface{}{"name": "Alice", "age": 25},
|
|
filter: types.Filter{"name": "Alice"},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "simple equality no match",
|
|
doc: map[string]interface{}{"name": "Alice", "age": 25},
|
|
filter: types.Filter{"name": "Bob"},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "greater than",
|
|
doc: map[string]interface{}{"age": 30},
|
|
filter: types.Filter{"age": types.Filter{"$gt": 25}},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "less than",
|
|
doc: map[string]interface{}{"age": 20},
|
|
filter: types.Filter{"age": types.Filter{"$lt": 25}},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "in array",
|
|
doc: map[string]interface{}{"status": "active"},
|
|
filter: types.Filter{"status": types.Filter{"$in": []interface{}{"active", "pending"}}},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "exists",
|
|
doc: map[string]interface{}{"name": "Alice"},
|
|
filter: types.Filter{"name": types.Filter{"$exists": true}},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "not exists",
|
|
doc: map[string]interface{}{"name": "Alice"},
|
|
filter: types.Filter{"email": types.Filter{"$exists": false}},
|
|
expected: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := MatchFilter(tt.doc, tt.filter)
|
|
if result != tt.expected {
|
|
t.Errorf("MatchFilter() = %v, want %v", result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyUpdate(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
data map[string]interface{}
|
|
update types.Update
|
|
expected map[string]interface{}
|
|
}{
|
|
{
|
|
name: "set field",
|
|
data: map[string]interface{}{"name": "Alice", "age": 25},
|
|
update: types.Update{
|
|
Set: map[string]interface{}{"age": 26},
|
|
},
|
|
expected: map[string]interface{}{"name": "Alice", "age": 26},
|
|
},
|
|
{
|
|
name: "unset field",
|
|
data: map[string]interface{}{"name": "Alice", "email": "alice@example.com"},
|
|
update: types.Update{
|
|
Unset: map[string]interface{}{"email": 1},
|
|
},
|
|
expected: map[string]interface{}{"name": "Alice"},
|
|
},
|
|
{
|
|
name: "increment field",
|
|
data: map[string]interface{}{"count": 5},
|
|
update: types.Update{
|
|
Inc: map[string]interface{}{"count": 3},
|
|
},
|
|
expected: map[string]interface{}{"count": 8},
|
|
},
|
|
{
|
|
name: "multiply field",
|
|
data: map[string]interface{}{"price": 100},
|
|
update: types.Update{
|
|
Mul: map[string]interface{}{"price": 0.9},
|
|
},
|
|
expected: map[string]interface{}{"price": 90},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := applyUpdate(tt.data, tt.update, false)
|
|
|
|
// 简单比较(实际应该深度比较)
|
|
for k, v := range tt.expected {
|
|
if result[k] != v {
|
|
t.Errorf("applyUpdate()[%s] = %v, want %v", k, result[k], v)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|