Skip to content

Commit

Permalink
Merge branch 'main' into basicOperatorMap
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdubbelboer authored Jun 15, 2024
2 parents 1b2fd1b + 6348ca9 commit 6269295
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 14 deletions.
56 changes: 53 additions & 3 deletions fuzz/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fuzz

import (
"encoding/json"
"strings"
"testing"

Expand Down Expand Up @@ -33,20 +34,69 @@ func FuzzConverter(f *testing.F) {
`{"name": {}}`,
`{"$or": []}`,
`{"status": {"$in": []}}`,
`{"$or": [{}, {}]}`,
`{"\"bla = 1 --": 1}`,
}
for _, tc := range tcs {
f.Add(tc)
f.Add(tc, true)
f.Add(tc, false)
}

f.Fuzz(func(t *testing.T, in string) {
c := filter.NewConverter(filter.WithArrayDriver(pq.Array))
f.Fuzz(func(t *testing.T, in string, jsonb bool) {
options := []filter.Option{
filter.WithArrayDriver(pq.Array),
}
if jsonb {
options = append(options, filter.WithNestedJSONB("meta"))
}
c := filter.NewConverter(options...)
conditions, _, err := c.Convert([]byte(in), 1)
if err == nil && conditions != "" {
j, err := pg_query.ParseToJSON("SELECT * FROM test WHERE 1 AND " + conditions)
if err != nil {
t.Fatalf("%q %q %v", in, conditions, err)
}

t.Log(j)

var q struct {
Stmts []struct {
Stmt struct {
SelectStmt struct {
FromClause []struct {
RangeVar struct {
Relname string `json:"relname"`
} `json:"RangeVar"`
} `json:"fromClause"`

WhereClause struct {
BoolExpr struct {
Boolop string `json:"boolop"`
Args []any `json:"args"`
} `json:"BoolExpr"`
} `json:"whereClause"`
} `json:"SelectStmt"`
} `json:"stmt"`
} `json:"stmts"`
}
if err := json.Unmarshal([]byte(j), &q); err != nil {
t.Fatal(err)
}
if len(q.Stmts) != 1 {
t.Fatal(conditions, "len(q.Stmts) != 1")
}
if len(q.Stmts[0].Stmt.SelectStmt.FromClause) != 1 {
t.Fatal(conditions, "len(q.Stmts[0].Stmt.SelectStmt.FromClause) != 1")
}
if q.Stmts[0].Stmt.SelectStmt.FromClause[0].RangeVar.Relname != "test" {
t.Fatal(conditions, "q.Stmts[0].Stmt.SelectStmt.FromClause[0].RangeVar.Relname != test")
}
if q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Boolop != "AND_EXPR" {
t.Fatal(conditions, "q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Boolop != AND_EXPR")
}
if len(q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Args) != 2 {
t.Fatal(conditions, "len(q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Args) != 2")
}
if strings.Contains(j, "CommentStmt") {
t.Fatal(conditions, "CommentStmt found")
}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/poki/mongodb-filter-to-postgres/fuzz

go 1.22.2
go 1.18

replace github.com/poki/mongodb-filter-to-postgres v0.0.0 => ../

Expand Down
2 changes: 0 additions & 2 deletions fuzz/testdata/fuzz/FuzzConverter/3e813b4e3bbd8eca

This file was deleted.

2 changes: 0 additions & 2 deletions fuzz/testdata/fuzz/FuzzConverter/439ca8de24f74c60

This file was deleted.

2 changes: 0 additions & 2 deletions fuzz/testdata/fuzz/FuzzConverter/cbbad75732661f48

This file was deleted.

2 changes: 0 additions & 2 deletions fuzz/testdata/fuzz/FuzzConverter/f7947d6c886ffd04

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/poki/mongodb-filter-to-postgres

go 1.22.2
go 1.18
2 changes: 1 addition & 1 deletion integration/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/poki/mongodb-filter-to-postgres/fuzz

go 1.22.2
go 1.21

replace github.com/poki/mongodb-filter-to-postgres v0.0.0 => ../

Expand Down

0 comments on commit 6269295

Please sign in to comment.