Skip to content

Commit

Permalink
Add pgcrypto example
Browse files Browse the repository at this point in the history
  • Loading branch information
jschaf committed Feb 13, 2021
1 parent e5703e2 commit 1564c8d
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ Examples embedded in the repo:
- [./example/enums] - Postgres and Go enums.
- [./example/erp] - A few tables with mildly complex queries.
- [./example/syntax] - A smoke test of interesting SQL syntax.
- [./example/pgcrypto] - pgcrypto Postgres extension.
[./example/integration_test.go]: ./example/integration_test.go
[./example/author]: ./example/author
Expand Down
7 changes: 7 additions & 0 deletions example/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ func TestExamples(t *testing.T) {
"--go-type", "my_int=int",
},
},
{
name: "example/pgcrypto",
args: []string{
"--schema-glob", "example/pgcrypto/schema.sql",
"--query-glob", "example/pgcrypto/query.sql",
},
},
}
if *update {
// update only disables the assertions. Running the tests causes pggen
Expand Down
43 changes: 43 additions & 0 deletions example/pgcrypto/codegen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package pgcrypto

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_Pgcrypto(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: "pgcrypto",
Language: pggen.LangGo,
})
if err != nil {
t.Fatalf("Generate() example/syntax: %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)
}
7 changes: 7 additions & 0 deletions example/pgcrypto/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- name: CreateUser :exec
INSERT INTO "user" (email, pass)
VALUES (pggen.arg('email'), crypt(pggen.arg('password'), gen_salt('bf')));

-- name: FindUser :one
SELECT email, pass from "user"
where email = pggen.arg('email');
128 changes: 128 additions & 0 deletions example/pgcrypto/query.sql.go

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

31 changes: 31 additions & 0 deletions example/pgcrypto/query.sql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pgcrypto

import (
"context"
"github.com/jschaf/pggen/internal/pgtest"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)

func TestQuerier(t *testing.T) {
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()

q := NewQuerier(conn)
ctx := context.Background()

_, err := q.CreateUser(ctx, "foo", "hunter2")
if err != nil {
t.Fatal(err)
}

row, err := q.FindUser(ctx, "foo")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "foo", row.Email, "email should match")
if !strings.HasPrefix(row.Pass, "$2a$") {
t.Fatalf("expected hashed password to have prefix $2a$; got %s", row.Pass)
}
}
6 changes: 6 additions & 0 deletions example/pgcrypto/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE EXTENSION IF NOT EXISTS pgcrypto;

CREATE TABLE "user" (
email TEXT PRIMARY KEY,
pass TEXT NOT NULL
);

0 comments on commit 1564c8d

Please sign in to comment.