-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test example for composite types in query params
Relates to #29.
- Loading branch information
Showing
11 changed files
with
288 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package complex_params | ||
|
||
import ( | ||
"github.com/jschaf/pggen" | ||
"github.com/jschaf/pggen/internal/pgtest" | ||
"github.com/stretchr/testify/assert" | ||
"io/ioutil" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestGenerate_Go_Example_ComplexParams(t *testing.T) { | ||
conn, cleanupFunc := pgtest.NewPostgresSchema(t, []string{"schema.sql"}) | ||
defer cleanupFunc() | ||
|
||
tmpDir := t.TempDir() | ||
err := pggen.Generate( | ||
pggen.GenerateOptions{ | ||
ConnString: conn.Config().ConnString(), | ||
QueryFiles: []string{"query.sql"}, | ||
OutputDir: tmpDir, | ||
GoPackage: "complex_params", | ||
Language: pggen.LangGo, | ||
TypeOverrides: map[string]string{ | ||
"int4": "int", | ||
"text": "string", | ||
}, | ||
}) | ||
if err != nil { | ||
t.Fatalf("Generate() example/complex_params: %s", err) | ||
} | ||
|
||
wantQueriesFile := "query.sql.go" | ||
gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") | ||
assert.FileExists(t, gotQueriesFile, "Generate() should emit query.sql.go") | ||
wantQueries, err := ioutil.ReadFile(wantQueriesFile) | ||
if err != nil { | ||
t.Fatalf("read wanted query.go.sql: %s", err) | ||
} | ||
gotQueries, err := ioutil.ReadFile(gotQueriesFile) | ||
if err != nil { | ||
t.Fatalf("read generated query.go.sql: %s", err) | ||
} | ||
assert.Equalf(t, string(wantQueries), string(gotQueries), | ||
"Got file %s; does not match contents of %s", | ||
gotQueriesFile, wantQueriesFile) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- name: ParamNested1 :one | ||
SELECT pggen.arg('dimensions')::dimensions; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package complex_params | ||
|
||
import ( | ||
"context" | ||
"github.com/jackc/pgx/v4" | ||
"github.com/jschaf/pggen/internal/pgtest" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestNewQuerier_ParamNested1(t *testing.T) { | ||
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"}) | ||
defer cleanup() | ||
|
||
q := NewQuerier(conn) | ||
ctx := context.Background() | ||
|
||
wantDim := Dimensions{Width: 77, Height: 77} | ||
|
||
t.Run("ParamNested1", func(t *testing.T) { | ||
row, err := q.ParamNested1(ctx, wantDim) | ||
require.NoError(t, err) | ||
assert.Equal(t, wantDim, row) | ||
}) | ||
|
||
t.Run("ArrayNested2Batch", func(t *testing.T) { | ||
batch := &pgx.Batch{} | ||
q.ParamNested1Batch(batch, wantDim) | ||
results := conn.SendBatch(ctx, batch) | ||
row, err := q.ParamNested1Scan(results) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
assert.Equal(t, wantDim, row) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
CREATE TYPE dimensions AS ( | ||
width int4, | ||
height int4 | ||
); | ||
|
||
CREATE TYPE product_image_type AS ( | ||
source text, | ||
dimensions dimensions | ||
); | ||
|
||
CREATE TYPE product_image_set_type AS ( | ||
name text, | ||
orig_image product_image_type, | ||
images product_image_type[] | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters