|
1 | 1 | package fuzz
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "strings"
|
5 | 6 | "testing"
|
6 | 7 |
|
@@ -35,18 +36,65 @@ func FuzzConverter(f *testing.F) {
|
35 | 36 | `{"status": {"$in": []}}`,
|
36 | 37 | }
|
37 | 38 | for _, tc := range tcs {
|
38 |
| - f.Add(tc) |
| 39 | + f.Add(tc, true) |
| 40 | + f.Add(tc, false) |
39 | 41 | }
|
40 | 42 |
|
41 |
| - f.Fuzz(func(t *testing.T, in string) { |
42 |
| - c := filter.NewConverter(filter.WithArrayDriver(pq.Array)) |
| 43 | + f.Fuzz(func(t *testing.T, in string, jsonb bool) { |
| 44 | + options := []filter.Option{ |
| 45 | + filter.WithArrayDriver(pq.Array), |
| 46 | + } |
| 47 | + if jsonb { |
| 48 | + options = append(options, filter.WithNestedJSONB("meta")) |
| 49 | + } |
| 50 | + c := filter.NewConverter(options...) |
43 | 51 | conditions, _, err := c.Convert([]byte(in), 1)
|
44 | 52 | if err == nil && conditions != "" {
|
45 | 53 | j, err := pg_query.ParseToJSON("SELECT * FROM test WHERE 1 AND " + conditions)
|
46 | 54 | if err != nil {
|
47 | 55 | t.Fatalf("%q %q %v", in, conditions, err)
|
48 | 56 | }
|
49 | 57 |
|
| 58 | + t.Log(j) |
| 59 | + |
| 60 | + var q struct { |
| 61 | + Stmts []struct { |
| 62 | + Stmt struct { |
| 63 | + SelectStmt struct { |
| 64 | + FromClause []struct { |
| 65 | + RangeVar struct { |
| 66 | + Relname string `json:"relname"` |
| 67 | + } `json:"RangeVar"` |
| 68 | + } `json:"fromClause"` |
| 69 | + |
| 70 | + WhereClause struct { |
| 71 | + BoolExpr struct { |
| 72 | + Boolop string `json:"boolop"` |
| 73 | + Args []any `json:"args"` |
| 74 | + } `json:"BoolExpr"` |
| 75 | + } `json:"whereClause"` |
| 76 | + } `json:"SelectStmt"` |
| 77 | + } `json:"stmt"` |
| 78 | + } `json:"stmts"` |
| 79 | + } |
| 80 | + if err := json.Unmarshal([]byte(j), &q); err != nil { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | + if len(q.Stmts) != 1 { |
| 84 | + t.Fatal(conditions, "len(q.Stmts) != 1") |
| 85 | + } |
| 86 | + if len(q.Stmts[0].Stmt.SelectStmt.FromClause) != 1 { |
| 87 | + t.Fatal(conditions, "len(q.Stmts[0].Stmt.SelectStmt.FromClause) != 1") |
| 88 | + } |
| 89 | + if q.Stmts[0].Stmt.SelectStmt.FromClause[0].RangeVar.Relname != "test" { |
| 90 | + t.Fatal(conditions, "q.Stmts[0].Stmt.SelectStmt.FromClause[0].RangeVar.Relname != test") |
| 91 | + } |
| 92 | + if q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Boolop != "AND_EXPR" { |
| 93 | + t.Fatal(conditions, "q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Boolop != AND_EXPR") |
| 94 | + } |
| 95 | + if len(q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Args) != 2 { |
| 96 | + t.Fatal(conditions, "len(q.Stmts[0].Stmt.SelectStmt.WhereClause.BoolExpr.Args) != 2") |
| 97 | + } |
50 | 98 | if strings.Contains(j, "CommentStmt") {
|
51 | 99 | t.Fatal(conditions, "CommentStmt found")
|
52 | 100 | }
|
|
0 commit comments