-
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.
- Loading branch information
Showing
7 changed files
with
223 additions
and
0 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
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,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) | ||
} |
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,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'); |
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,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) | ||
} | ||
} |
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,6 @@ | ||
CREATE EXTENSION IF NOT EXISTS pgcrypto; | ||
|
||
CREATE TABLE "user" ( | ||
email TEXT PRIMARY KEY, | ||
pass TEXT NOT NULL | ||
); |