You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.7 KiB
Go
87 lines
2.7 KiB
Go
package matcher
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/noahlann/nnet/internal/codec"
|
|
"github.com/noahlann/nnet/internal/request"
|
|
"github.com/noahlann/nnet/internal/response"
|
|
ctxpkg "github.com/noahlann/nnet/pkg/context"
|
|
routerpkg "github.com/noahlann/nnet/pkg/router"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// helper to build context with given body map
|
|
func buildCtxWithBody(t *testing.T, body map[string]interface{}) (ctx ctxpkg.Context, raw []byte) {
|
|
t.Helper()
|
|
conn := &mockConnection{id: "test-conn"}
|
|
raw, _ = json.Marshal(body)
|
|
req := request.New(raw, nil)
|
|
if setter := request.AsRequestSetter(req); setter != nil {
|
|
setter.SetData(body)
|
|
}
|
|
codecRegistry := codec.NewRegistry()
|
|
resp := response.New(conn, nil, codecRegistry, "json")
|
|
ctx = ctxpkg.New(nil, conn, req, resp)
|
|
return
|
|
}
|
|
|
|
func TestFrameDataMatcher_CompareNumericOps(t *testing.T) {
|
|
body := map[string]interface{}{
|
|
"n": float64(10),
|
|
}
|
|
ctx, raw := buildCtxWithBody(t, body)
|
|
input := routerpkg.MatchInput{
|
|
Raw: raw,
|
|
DataBytes: raw,
|
|
Data: body,
|
|
}
|
|
|
|
assert.True(t, NewFrameDataMatcher("n", ">", float64(5)).Match(input, ctx))
|
|
assert.True(t, NewFrameDataMatcher("n", ">=", float64(10)).Match(input, ctx))
|
|
assert.True(t, NewFrameDataMatcher("n", "<", float64(11)).Match(input, ctx))
|
|
assert.True(t, NewFrameDataMatcher("n", "<=", float64(10)).Match(input, ctx))
|
|
assert.True(t, NewFrameDataMatcher("n", "==", float64(10)).Match(input, ctx))
|
|
assert.True(t, NewFrameDataMatcher("n", "!=", float64(9)).Match(input, ctx))
|
|
|
|
assert.False(t, NewFrameDataMatcher("n", ">", float64(10)).Match(input, ctx))
|
|
assert.False(t, NewFrameDataMatcher("n", "<", float64(10)).Match(input, ctx))
|
|
}
|
|
|
|
func TestFrameDataMatcher_InOperator(t *testing.T) {
|
|
body := map[string]interface{}{
|
|
"user": map[string]interface{}{
|
|
"role": "admin",
|
|
},
|
|
}
|
|
ctx, raw := buildCtxWithBody(t, body)
|
|
input := routerpkg.MatchInput{
|
|
Raw: raw,
|
|
DataBytes: raw,
|
|
Data: body,
|
|
}
|
|
|
|
assert.True(t, NewFrameDataMatcher("user.role", "in", []interface{}{"user", "admin"}).Match(input, ctx))
|
|
assert.False(t, NewFrameDataMatcher("user.role", "in", []interface{}{"guest", "editor"}).Match(input, ctx))
|
|
}
|
|
|
|
func TestFrameDataMatcher_ContainsOperator(t *testing.T) {
|
|
body := map[string]interface{}{
|
|
"s": "hello world",
|
|
"a": []interface{}{"a", "b", "c"},
|
|
}
|
|
ctx, raw := buildCtxWithBody(t, body)
|
|
input := routerpkg.MatchInput{
|
|
Raw: raw,
|
|
DataBytes: raw,
|
|
Data: body,
|
|
}
|
|
|
|
assert.True(t, NewFrameDataMatcher("s", "contains", "world").Match(input, ctx))
|
|
assert.False(t, NewFrameDataMatcher("s", "contains", "zzz").Match(input, ctx))
|
|
|
|
assert.True(t, NewFrameDataMatcher("a", "contains", "b").Match(input, ctx))
|
|
assert.False(t, NewFrameDataMatcher("a", "contains", "x").Match(input, ctx))
|
|
}
|