Skip to content

Commit

Permalink
Add test example for composite types in query params
Browse files Browse the repository at this point in the history
Relates to #29.
  • Loading branch information
jschaf committed Apr 21, 2021
1 parent 7603c38 commit b25f3a5
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 5 deletions.
10 changes: 10 additions & 0 deletions example/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ func TestExamples(t *testing.T) {
"--query-glob", "example/author/query.sql",
},
},
{
name: "example/complex_params",
args: []string{
"--schema-glob", "example/complex_params/schema.sql",
"--query-glob", "example/complex_params/query.sql",
"--go-type", "int8=int",
"--go-type", "int4=int",
"--go-type", "text=string",
},
},
{
name: "example/composite",
args: []string{
Expand Down
47 changes: 47 additions & 0 deletions example/complex_params/codegen_test.go
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)
}
2 changes: 2 additions & 0 deletions example/complex_params/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: ParamNested1 :one
SELECT pggen.arg('dimensions')::dimensions;
172 changes: 172 additions & 0 deletions example/complex_params/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions example/complex_params/query.sql_test.go
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)
})
}
15 changes: 15 additions & 0 deletions example/complex_params/schema.sql
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[]
);
2 changes: 1 addition & 1 deletion internal/codegen/golang/declarer.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ var ignoredOIDDeclarer = NewConstantDeclarer("const::ignoredOID", ignoredOIDDecl

const textEncoderDecl = `// textEncoder wraps a pgtype.ValueTranscoder and sets the preferred encoding
// format to text instead binary (the default). pggen must use the text format
// because the Postgres binary format requires the type OID but pggen doesn't
// because the Postgres binary format requires the type OID but pggen doesn't
// necessarily know the OIDs of the types, hence ignoredOID.
type textEncoder struct {
pgtype.ValueTranscoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const ignoredOID = 0

// textEncoder wraps a pgtype.ValueTranscoder and sets the preferred encoding
// format to text instead binary (the default). pggen must use the text format
// because the Postgres binary format requires the type OID but pggen doesn't
// because the Postgres binary format requires the type OID but pggen doesn't
// necessarily know the OIDs of the types, hence ignoredOID.
type textEncoder struct {
pgtype.ValueTranscoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const ignoredOID = 0

// textEncoder wraps a pgtype.ValueTranscoder and sets the preferred encoding
// format to text instead binary (the default). pggen must use the text format
// because the Postgres binary format requires the type OID but pggen doesn't
// because the Postgres binary format requires the type OID but pggen doesn't
// necessarily know the OIDs of the types, hence ignoredOID.
type textEncoder struct {
pgtype.ValueTranscoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const ignoredOID = 0

// textEncoder wraps a pgtype.ValueTranscoder and sets the preferred encoding
// format to text instead binary (the default). pggen must use the text format
// because the Postgres binary format requires the type OID but pggen doesn't
// because the Postgres binary format requires the type OID but pggen doesn't
// necessarily know the OIDs of the types, hence ignoredOID.
type textEncoder struct {
pgtype.ValueTranscoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const ignoredOID = 0

// textEncoder wraps a pgtype.ValueTranscoder and sets the preferred encoding
// format to text instead binary (the default). pggen must use the text format
// because the Postgres binary format requires the type OID but pggen doesn't
// because the Postgres binary format requires the type OID but pggen doesn't
// necessarily know the OIDs of the types, hence ignoredOID.
type textEncoder struct {
pgtype.ValueTranscoder
Expand Down

0 comments on commit b25f3a5

Please sign in to comment.