Skip to content

Commit a827073

Browse files
Add some different types of columns and data to our test setup
These can then be used to test all new operators.
1 parent 0b1842c commit a827073

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

filter/converter_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ func TestConverter_Convert(t *testing.T) {
208208
nil,
209209
fmt.Errorf("empty objects not allowed"),
210210
},
211+
{
212+
"sql injection",
213+
nil,
214+
`{"\"bla = 1 --": 1}`,
215+
``,
216+
nil,
217+
fmt.Errorf("invalid column name: \"bla = 1 --"),
218+
},
211219
}
212220

213221
for _, tt := range tests {

integration/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/poki/mongodb-filter-to-postgres/fuzz
1+
module github.com/poki/mongodb-filter-to-postgres/integration
22

33
go 1.22.2
44

integration/postgres_test.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ func TestIntegration_BasicOperators(t *testing.T) {
282282
{
283283
`unknown column`,
284284
`{"foobar": "admin"}`,
285+
[]int{},
285286
nil,
286-
errors.New("pq: column \"foobar\" does not exist"),
287287
},
288288
{
289289
`invalid value`,
@@ -297,16 +297,24 @@ func TestIntegration_BasicOperators(t *testing.T) {
297297
[]int{},
298298
nil,
299299
},
300+
{
301+
"$gt with jsonb column",
302+
`{"guild_id": { "$gt": 40 }}`,
303+
[]int{7, 8, 9, 10},
304+
nil,
305+
},
300306
}
301307

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

316+
t.Log(conditions, values)
317+
310318
rows, err := db.Query(`
311319
SELECT id
312320
FROM players
@@ -331,7 +339,7 @@ func TestIntegration_BasicOperators(t *testing.T) {
331339
}
332340

333341
if !reflect.DeepEqual(players, tt.expectedPlayers) {
334-
t.Fatalf("%q expected %v, got %v (conditions used: %q)", tt.input, tt.expectedPlayers, players, conditions)
342+
t.Fatalf("expected %v, got %v", tt.expectedPlayers, players)
335343
}
336344
})
337345
}
@@ -368,7 +376,7 @@ func TestIntegration_NestedJSONB(t *testing.T) {
368376
{
369377
"jsonb regex",
370378
`{"pet": {"$regex": "^.{3}$"}}`,
371-
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
379+
[]int{1, 2, 3, 4, 5, 6, 7, 8},
372380
},
373381
{
374382
"excemption column",

integration/setup_test.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,27 @@ func createPlayersTable(t *testing.T, db *sql.DB) {
114114
"name" text,
115115
"metadata" jsonb,
116116
"level" int,
117-
"class" text
117+
"class" text,
118+
"mount" text,
119+
"items" text[],
120+
"parents" int[]
118121
);
119122
`); err != nil {
120123
t.Fatal(err)
121124
}
122125
if _, err := db.Exec(`
123-
INSERT INTO players ("id", "name", "metadata", "level", "class")
124-
VALUES
125-
(1, 'Alice', '{"guild_id": 20, "pet": "dog"}', 10, 'warrior'),
126-
(2, 'Bob', '{"guild_id": 20, "pet": "cat"}', 20, 'mage'),
127-
(3, 'Charlie', '{"guild_id": 30, "pet": "dog"}', 30, 'rogue'),
128-
(4, 'David', '{"guild_id": 30, "pet": "cat"}', 40, 'warrior'),
129-
(5, 'Eve', '{"guild_id": 40, "pet": "dog"}', 50, 'mage'),
130-
(6, 'Frank', '{"guild_id": 40, "pet": "cat"}', 60, 'rogue'),
131-
(7, 'Grace', '{"guild_id": 50, "pet": "dog"}', 70, 'warrior'),
132-
(8, 'Hank', '{"guild_id": 50, "pet": "cat"}', 80, 'mage'),
133-
(9, 'Ivy', '{"guild_id": 60, "pet": "dog"}', 90, 'rogue'),
134-
(10, 'Jack', '{"guild_id": 60, "pet": "cat"}', 100, 'warrior')
126+
INSERT INTO players
127+
("id", "name", "metadata", "level", "class", "mount", "items", "parents") VALUES
128+
(1, 'Alice', '{"guild_id": 20, "pet": "dog" }', 10, 'warrior', 'horse', '{}', '{40, 60}'),
129+
(2, 'Bob', '{"guild_id": 20, "pet": "cat", "keys": [1, 3] }', 20, 'mage', 'horse', '{}', '{20, 30}'),
130+
(3, 'Charlie', '{"guild_id": 30, "pet": "dog", "keys": [4, 6] }', 30, 'rogue', NULL, '{}', '{30, 50}'),
131+
(4, 'David', '{"guild_id": 30, "pet": "cat" }', 40, 'warrior', NULL, '{}', '{}'),
132+
(5, 'Eve', '{"guild_id": 40, "pet": "dog", "hats": ["helmet"]}', 50, 'mage', 'griffon', '{"staff", "cloak"}', '{}'),
133+
(6, 'Frank', '{"guild_id": 40, "pet": "cat", "hats": ["cap"] }', 60, 'rogue', 'griffon', '{"dagger"}', '{}'),
134+
(7, 'Grace', '{"guild_id": 50, "pet": "dog" }', 70, 'warrior', 'dragon', '{"sword"}', '{}'),
135+
(8, 'Hank', '{"guild_id": 50, "pet": "cat" }', 80, 'mage', 'dragon', '{}', '{}'),
136+
(9, 'Ivy', '{"guild_id": 60 }', 90, 'rogue', 'phoenix', '{}', '{}'),
137+
(10, 'Jack', '{"guild_id": 60, "pet": null }', 100, 'warrior', 'phoenix', '{}', '{}');
135138
`); err != nil {
136139
t.Fatal(err)
137140
}

0 commit comments

Comments
 (0)