leaf/filter_test.go
2024-01-12 12:20:28 +03:00

70 lines
1.8 KiB
Go

package leaf
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFilter(t *testing.T) {
t.Run("merge two filter documents", func(t *testing.T) {
doc1 := Filter(
Gt("field1", 1),
Lt("field2", 10),
)
doc2 := Filter(
Ne("field1", 2),
Eq("field3", "test"),
)
expected := FilterDocument{
"field1": {"$gt": 1, "$ne": 2},
"field2": {"$lt": 10},
"field3": {"$eq": "test"},
}
assert.Equal(t, expected, Filter(doc1, doc2))
})
t.Run("merge three filter documents", func(t *testing.T) {
doc1 := Filter(
Gt("field1", 1),
Lt("field2", 10),
)
doc2 := Filter(
Ne("field1", 2),
Eq("field3", "test"),
)
doc3 := Filter(
Gt("field1", 5),
Ne("field2", 5),
Eq("field4", "hello"),
)
expected := FilterDocument{
"field1": {"$gt": 5, "$ne": 2},
"field2": {"$lt": 10, "$ne": 5},
"field3": {"$eq": "test"},
"field4": {"$eq": "hello"},
}
assert.Equal(t, expected, Filter(doc1, doc2, doc3))
})
}
func TestOperators(t *testing.T) {
assert.Equal(t, Eq("field", 1), FilterDocument{"field": {"$eq": 1}})
assert.Equal(t, Ne("field", 1), FilterDocument{"field": {"$ne": 1}})
assert.Equal(t, Gt("field", 1), FilterDocument{"field": {"$gt": 1}})
assert.Equal(t, Gte("field", 1), FilterDocument{"field": {"$gte": 1}})
assert.Equal(t, Lt("field", 1), FilterDocument{"field": {"$lt": 1}})
assert.Equal(t, Lte("field", 1), FilterDocument{"field": {"$lte": 1}})
assert.Equal(t, In("field", []int{1, 2, 3}), FilterDocument{"field": {"$in": []int{1, 2, 3}}})
assert.Equal(t, Nin("field", []int{1, 2, 3}), FilterDocument{"field": {"$nin": []int{1, 2, 3}}})
assert.Equal(t, Exists("field", true), FilterDocument{"field": {"$exists": true}})
assert.Equal(t, Regex("field", "test"), FilterDocument{"field": {"$regex": "test"}})
}