rql/parser_test.go
2024-05-12 07:04:35 +03:00

88 lines
1.8 KiB
Go

package rql
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParse(t *testing.T) {
one := "1"
john := "John"
cases := []struct {
name string
query string
predicates []*Predicate
errMessage string
}{
{
name: "Valid Query",
query: `id.eq="1"`,
predicates: []*Predicate{{Key: "id", Operator: "eq", Values: Values{Single: &one}}},
errMessage: "",
},
{
name: "Multiple Predicates",
query: `id.eq="1" name.eq="John"`,
predicates: []*Predicate{{Key: "id", Operator: "eq", Values: Values{Single: &one}}, {Key: "name", Operator: "eq", Values: Values{Single: &john}}},
errMessage: "",
},
{
name: "List Values",
query: `id.in=["1", "2", "3"]`,
predicates: []*Predicate{
{
Key: "id",
Operator: "in",
Values: Values{
List: []string{"1", "2", "3"},
},
},
},
errMessage: "",
},
{
name: "Empty Query",
query: "",
predicates: nil,
errMessage: "",
},
{
name: "Whitespace in Query",
query: ` id . eq = "1" `,
predicates: []*Predicate{{Key: "id", Operator: "eq", Values: Values{Single: &one}}},
errMessage: "",
},
{
name: "Missing Key",
query: `.eq="1"`,
predicates: nil,
errMessage: `unexpected token "."`,
},
{
name: "Invalid Operator",
query: `id.invalid="1"`,
predicates: nil,
errMessage: `unexpected token "invalid"`,
},
{
name: "Invalid Query",
query: "invalid_query",
predicates: nil,
errMessage: `unexpected token "<EOF>"`,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
result, err := Parse(tc.query)
assert.Equal(t, tc.predicates, result.Predicates)
if tc.errMessage != "" {
assert.ErrorContains(t, err, tc.errMessage)
}
})
}
}