Skip to content

Commit

Permalink
Add test for Walk function.
Browse files Browse the repository at this point in the history
  • Loading branch information
akrennmair committed Jan 6, 2024
1 parent ac5e801 commit 1e19fe3
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions internal/queryparser/walk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package queryparser_test

import (
"fmt"
"testing"

"github.com/akrennmair/updog/internal/queryparser"
updogv1 "github.com/akrennmair/updog/proto/updog/v1"
"github.com/stretchr/testify/require"
)

func TestWalk(t *testing.T) {
q, err := queryparser.ParseQuery(`foo = "bar" | ( bar = "baz" & ^ baz = "quux" )`)
require.NoError(t, err)

var types []string

queryparser.Walk(q, func(e *updogv1.Query_Expression) bool {
types = append(types, fmt.Sprintf("%T", e.Value))
return true
})

expectedTypes := []string{
"*updogv1.Query_Expression_Or_",
"*updogv1.Query_Expression_Eq",
"*updogv1.Query_Expression_And_",
"*updogv1.Query_Expression_Eq",
"*updogv1.Query_Expression_Not_",
"*updogv1.Query_Expression_Eq",
}

require.Equal(t, expectedTypes, types)
}

func TestReplacePlaceholders(t *testing.T) {
q, err := queryparser.ParseQuery(`foo = $1 | ( bar = $2 & ^ baz = $3 )`)
require.NoError(t, err)

q2 := queryparser.ReplacePlaceholders(q, []string{"1", "2", "3"})

require.Equal(t, `foo = "1" | ( bar = "2" & ^ baz = "3" )`, queryparser.QueryToString(q2))
}

0 comments on commit 1e19fe3

Please sign in to comment.