Skip to content

Commit

Permalink
Add some different types of columns and data to our test setup (#12)
Browse files Browse the repository at this point in the history
These can then be used to test all new operators.
  • Loading branch information
erikdubbelboer authored Jun 15, 2024
1 parent bd01a75 commit a7572af
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
8 changes: 8 additions & 0 deletions filter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ func TestConverter_Convert(t *testing.T) {
nil,
fmt.Errorf("empty objects not allowed"),
},
{
"sql injection",
nil,
`{"\"bla = 1 --": 1}`,
``,
nil,
fmt.Errorf("invalid column name: \"bla = 1 --"),
},
}

for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion integration/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/poki/mongodb-filter-to-postgres/fuzz
module github.com/poki/mongodb-filter-to-postgres/integration

go 1.21

Expand Down
16 changes: 12 additions & 4 deletions integration/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ func TestIntegration_BasicOperators(t *testing.T) {
{
`unknown column`,
`{"foobar": "admin"}`,
[]int{},
nil,
errors.New("pq: column \"foobar\" does not exist"),
},
{
`invalid value`,
Expand All @@ -297,16 +297,24 @@ func TestIntegration_BasicOperators(t *testing.T) {
[]int{},
nil,
},
{
"$gt with jsonb column",
`{"guild_id": { "$gt": 40 }}`,
[]int{7, 8, 9, 10},
nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := filter.NewConverter(filter.WithArrayDriver(pq.Array))
c := filter.NewConverter(filter.WithArrayDriver(pq.Array), filter.WithNestedJSONB("metadata", "name", "level", "class", "mount", "items", "parents"))
conditions, values, err := c.Convert([]byte(tt.input), 1)
if err != nil {
t.Fatal(err)
}

t.Log(conditions, values)

rows, err := db.Query(`
SELECT id
FROM players
Expand All @@ -331,7 +339,7 @@ func TestIntegration_BasicOperators(t *testing.T) {
}

if !reflect.DeepEqual(players, tt.expectedPlayers) {
t.Fatalf("%q expected %v, got %v (conditions used: %q)", tt.input, tt.expectedPlayers, players, conditions)
t.Fatalf("expected %v, got %v", tt.expectedPlayers, players)
}
})
}
Expand All @@ -355,7 +363,7 @@ func TestIntegration_NestedJSONB(t *testing.T) {
{
"jsonb regex",
`{"pet": {"$regex": "^.{3}$"}}`,
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
[]int{1, 2, 3, 4, 5, 6, 7, 8},
},
{
"excemption column",
Expand Down
29 changes: 16 additions & 13 deletions integration/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,27 @@ func createPlayersTable(t *testing.T, db *sql.DB) {
"name" text,
"metadata" jsonb,
"level" int,
"class" text
"class" text,
"mount" text,
"items" text[],
"parents" int[]
);
`); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`
INSERT INTO players ("id", "name", "metadata", "level", "class")
VALUES
(1, 'Alice', '{"guild_id": 20, "pet": "dog"}', 10, 'warrior'),
(2, 'Bob', '{"guild_id": 20, "pet": "cat"}', 20, 'mage'),
(3, 'Charlie', '{"guild_id": 30, "pet": "dog"}', 30, 'rogue'),
(4, 'David', '{"guild_id": 30, "pet": "cat"}', 40, 'warrior'),
(5, 'Eve', '{"guild_id": 40, "pet": "dog"}', 50, 'mage'),
(6, 'Frank', '{"guild_id": 40, "pet": "cat"}', 60, 'rogue'),
(7, 'Grace', '{"guild_id": 50, "pet": "dog"}', 70, 'warrior'),
(8, 'Hank', '{"guild_id": 50, "pet": "cat"}', 80, 'mage'),
(9, 'Ivy', '{"guild_id": 60, "pet": "dog"}', 90, 'rogue'),
(10, 'Jack', '{"guild_id": 60, "pet": "cat"}', 100, 'warrior')
INSERT INTO players
("id", "name", "metadata", "level", "class", "mount", "items", "parents") VALUES
(1, 'Alice', '{"guild_id": 20, "pet": "dog" }', 10, 'warrior', 'horse', '{}', '{40, 60}'),
(2, 'Bob', '{"guild_id": 20, "pet": "cat", "keys": [1, 3] }', 20, 'mage', 'horse', '{}', '{20, 30}'),
(3, 'Charlie', '{"guild_id": 30, "pet": "dog", "keys": [4, 6] }', 30, 'rogue', NULL, '{}', '{30, 50}'),
(4, 'David', '{"guild_id": 30, "pet": "cat" }', 40, 'warrior', NULL, '{}', '{}'),
(5, 'Eve', '{"guild_id": 40, "pet": "dog", "hats": ["helmet"]}', 50, 'mage', 'griffon', '{"staff", "cloak"}', '{}'),
(6, 'Frank', '{"guild_id": 40, "pet": "cat", "hats": ["cap"] }', 60, 'rogue', 'griffon', '{"dagger"}', '{}'),
(7, 'Grace', '{"guild_id": 50, "pet": "dog" }', 70, 'warrior', 'dragon', '{"sword"}', '{}'),
(8, 'Hank', '{"guild_id": 50, "pet": "cat" }', 80, 'mage', 'dragon', '{}', '{}'),
(9, 'Ivy', '{"guild_id": 60 }', 90, 'rogue', 'phoenix', '{}', '{}'),
(10, 'Jack', '{"guild_id": 60, "pet": null }', 100, 'warrior', 'phoenix', '{}', '{}');
`); err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit a7572af

Please sign in to comment.