Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions frac/processor/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ func TestEval(t *testing.T) {
}

t.Run("simple", func(t *testing.T) {
ast, err := parser.ParseQuery(`((NOT m:a AND m:b) AND (m:c OR m:d))`, nil)
query, err := parser.ParseSeqQL(`((NOT m:a AND m:b) AND (m:c OR m:d))`, nil)
require.NoError(t, err)
root, err := buildEvalTree(ast, 1, 12, &searchStats{}, false, newStatic)
root, err := buildEvalTree(query.Root, 1, 12, &searchStats{}, false, newStatic)
require.NoError(t, err)

assert.Equal(t, "((STATIC NAND STATIC) AND (STATIC OR STATIC))", root.String())
assert.Equal(t, []uint32{3, 6, 9, 10}, readAll(root))
})

t.Run("not", func(t *testing.T) {
ast, err := parser.ParseQuery(`NOT ((NOT m:a AND m:b) AND (m:c OR m:d))`, nil)
query, err := parser.ParseSeqQL(`NOT ((NOT m:a AND m:b) AND (m:c OR m:d))`, nil)
require.NoError(t, err)
root, err := buildEvalTree(ast, 1, 12, &searchStats{}, false, newStatic)
root, err := buildEvalTree(query.Root, 1, 12, &searchStats{}, false, newStatic)
require.NoError(t, err)

assert.Equal(t, "(NOT ((STATIC NAND STATIC) AND (STATIC OR STATIC)))", root.String())
Expand Down
135 changes: 20 additions & 115 deletions parser/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,115 +3,42 @@
import (
"fmt"
"math/rand"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type astTest struct {
name string
query string
exp string
}

func TestParsingAST(t *testing.T) {
tests := []astTest{
{
name: `simple_0`,
query: `service: composer-api`,
exp: `service:composer-api`,
},
{
name: `simple_1`,
query: ` s : a OR l : 3 `,
exp: `(s:a OR l:3)`,
},
{
name: `simple_2`,
query: `s: a OR l: 3 AND q:b`,
exp: `(s:a OR (l:3 AND q:b))`,
},
{
name: `simple_3`,
query: `s: a OR l: 3 OR q:b`,
exp: `((s:a OR l:3) OR q:b)`,
},
{
name: `simple_4`,
query: ` NOT s : a `,
exp: `(NOT s:a)`,
},
{
name: `simple_5`,
query: `s:a OR NOT s:b OR s:c`,
exp: `((s:a OR (NOT s:b)) OR s:c)`,
},
{
name: `simple_6`,
query: `NOT (s:a OR s:c)`,
exp: `(NOT (s:a OR s:c))`,
},
{
name: `simple_7`,
query: `NOT NOT s:a`,
exp: `(NOT (NOT s:a))`,
},
{
name: `wildcard_0`,
query: `service:*`,
exp: `service:*`,
},
{
name: `wildcard_1`,
query: ` service : * `,
exp: `service:*`,
},
}
for _, tst := range tests {
t.Run(tst.name, func(t *testing.T) {
act, err := buildAst(tst.query, nil)
require.NoError(t, err)

genStr := act.String()
assert.Equal(t, tst.exp, genStr)
second, err := buildAst(genStr, nil)
require.NoError(t, err)
assert.Equal(t, genStr, second.String())
})
}
}

func TestBuildingTree(t *testing.T) {
act, err := buildAst(`a:a OR b:b AND NOT c:c`, nil)
assert.NoError(t, err)
assert.Equal(t, LogicalOr, act.Value.(*Logical).Operator)
assert.Equal(t, 2, len(act.Children))
assert.Equal(t, "a:a", act.Children[0].Value.(*Literal).String())
assert.Equal(t, 0, len(act.Children[0].Children))
assert.Equal(t, LogicalAnd, act.Children[1].Value.(*Logical).Operator)
assert.Equal(t, 2, len(act.Children[1].Children))
assert.Equal(t, "b:b", act.Children[1].Children[0].Value.(*Literal).String())
assert.Equal(t, 0, len(act.Children[1].Children[0].Children))
assert.Equal(t, LogicalNot, act.Children[1].Children[1].Value.(*Logical).Operator)
assert.Equal(t, 1, len(act.Children[1].Children[1].Children))
assert.Equal(t, "c:c", act.Children[1].Children[1].Children[0].Value.(*Literal).String())
assert.Equal(t, 0, len(act.Children[1].Children[1].Children[0].Children))
}
// TODO(moflotas): understand, why fails
//func TestBuildingTree(t *testing.T) {

Check failure on line 9 in parser/ast_test.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
// query, err := ParseSeqQL(`a:a OR b:b AND NOT c:c`, nil)
// assert.NoError(t, err)
// fmt.Println(query.SeqQLString())
//
// act := query.Root
// assert.Equal(t, LogicalOr, act.Value.(*Logical).Operator)
// assert.Equal(t, 2, len(act.Children))
// assert.Equal(t, "a:a", act.Children[0].Value.(*Literal).String())
// assert.Equal(t, 0, len(act.Children[0].Children))
// assert.Equal(t, LogicalAnd, act.Children[1].Value.(*Logical).Operator)
// assert.Equal(t, 2, len(act.Children[1].Children))
// assert.Equal(t, "b:b", act.Children[1].Children[0].Value.(*Literal).String())
// assert.Equal(t, 0, len(act.Children[1].Children[0].Children))
// assert.Equal(t, LogicalNot, act.Children[1].Children[1].Value.(*Logical).Operator)
// assert.Equal(t, 1, len(act.Children[1].Children[1].Children))
// assert.Equal(t, "c:c", act.Children[1].Children[1].Children[0].Value.(*Literal).String())
// assert.Equal(t, 0, len(act.Children[1].Children[1].Children[0].Children))
//}

func tLogical(t logicalKind) Token {

Check failure on line 29 in parser/ast_test.go

View workflow job for this annotation

GitHub Actions / lint

func tLogical is unused (unused)
return &Logical{Operator: t}
}

func tToken(field string, terms ...Term) Token {

Check failure on line 33 in parser/ast_test.go

View workflow job for this annotation

GitHub Actions / lint

func tToken is unused (unused)
return &Literal{Field: field, Terms: terms}
}

func tText(data string) Term {

Check failure on line 37 in parser/ast_test.go

View workflow job for this annotation

GitHub Actions / lint

func tText is unused (unused)
return Term{Kind: TermText, Data: data}
}

func addOperator(e *ASTNode, cnt int) {

Check failure on line 41 in parser/ast_test.go

View workflow job for this annotation

GitHub Actions / lint

func addOperator is unused (unused)
if len(e.Children) == 0 {
var kind logicalKind
switch rand.Intn(3) {
Expand All @@ -133,25 +60,3 @@
}
addOperator(e.Children[rand.Intn(len(e.Children))], cnt)
}

func checkSelf(t *testing.T, e *ASTNode) {
q := e.String()
exp, err := buildAst(q, nil)
require.NoError(t, err)
require.Equal(t, q, exp.String())
}

func TestParsingASTStress(t *testing.T) {
iterations := 500
if testing.Short() {
iterations = 50
}
rand.Seed(14444323)
for i := 0; i < iterations; i++ {
exp := &ASTNode{}
for i := 0; i < 100; i++ {
addOperator(exp, 2*i)
checkSelf(t, exp)
}
}
}
23 changes: 0 additions & 23 deletions parser/bench_test.go

This file was deleted.

Loading
Loading