|
| 1 | +package filter_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + pg_query "github.com/pganalyze/pg_query_go/v5" |
| 8 | + "github.com/poki/mongodb-filter-to-postgres/filter" |
| 9 | +) |
| 10 | + |
| 11 | +func FuzzConverter(f *testing.F) { |
| 12 | + tcs := []string{ |
| 13 | + `{"name": "John"}`, |
| 14 | + `{"age": 30, "name": "John"}`, |
| 15 | + `{"players": {"$gt": 0}}`, |
| 16 | + `{"age": {"$gte": 18}, "name": "John"}`, |
| 17 | + `{"created_at": {"$gte": "2020-01-01T00:00:00Z"}, "name": "John", "role": "admin"}`, |
| 18 | + `{"b": 1, "c": 2, "a": 3}`, |
| 19 | + `{"status": {"$in": ["NEW", "OPEN"]}}`, |
| 20 | + `{"status": {"$in": [{"hacker": 1}, "OPEN"]}}`, |
| 21 | + `{"status": {"$in": "text"}}`, |
| 22 | + `{"status": {"$in": ["guest", null]}}`, |
| 23 | + `{"$or": [{"name": "John"}, {"name": "Doe"}]}`, |
| 24 | + `{"$or": [{"org": "poki", "admin": true}, {"age": {"$gte": 18}}]}`, |
| 25 | + `{"$or": [{"$or": [{"name": "John"}, {"name": "Doe"}]}, {"name": "Jane"}]}`, |
| 26 | + `{"foo": { "$or": [ "bar", "baz" ] }}`, |
| 27 | + `{"$and": [{"name": "John"}, {"version": 3}]}`, |
| 28 | + `{"$and": [{"name": "John", "version": 3}]}`, |
| 29 | + `{"name": {"$regex": "John"}}`, |
| 30 | + `{"$or": [{"name": {"$regex": "John"}}, {"name": {"$regex": "Jane"}}]}`, |
| 31 | + `{"name": {}}`, |
| 32 | + `{"$or": []}`, |
| 33 | + `{"status": {"$in": []}}`, |
| 34 | + } |
| 35 | + for _, tc := range tcs { |
| 36 | + f.Add(tc) |
| 37 | + } |
| 38 | + |
| 39 | + f.Fuzz(func(t *testing.T, in string) { |
| 40 | + c := filter.NewConverter() |
| 41 | + where, _, err := c.Convert([]byte(in)) |
| 42 | + if err == nil && where != "" { |
| 43 | + j, err := pg_query.ParseToJSON("SELECT * FROM test WHERE 1 AND " + where) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("%q %q %v", in, where, err) |
| 46 | + } |
| 47 | + |
| 48 | + if strings.Contains(j, "CommentStmt") { |
| 49 | + t.Fatal(where, "CommentStmt found") |
| 50 | + } |
| 51 | + } |
| 52 | + }) |
| 53 | +} |
0 commit comments