Skip to content

Commit

Permalink
Add some different types of columns and data to our test setup
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 committed Jun 8, 2024
1 parent 0b1842c commit bbadf91
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
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.22.2

Expand Down
10 changes: 6 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 @@ -301,12 +301,14 @@ func TestIntegration_BasicOperators(t *testing.T) {

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 +333,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 Down Expand Up @@ -368,7 +370,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 bbadf91

Please sign in to comment.