57 lines
985 B
Go
57 lines
985 B
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))
|
||
|
})
|
||
|
}
|