-
Notifications
You must be signed in to change notification settings - Fork 27
/
codegen_test.go
53 lines (49 loc) · 1.38 KB
/
codegen_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package composite
import (
"github.com/jschaf/pggen"
"github.com/jschaf/pggen/internal/pgtest"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
)
func TestGenerate_Go_Example_Composite(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: "composite",
Language: pggen.LangGo,
InlineParamCount: 2,
TypeOverrides: map[string]string{
"_bool": "[]bool",
"bool": "bool",
"int8": "int",
"int4": "int",
"text": "string",
"citext": "github.com/jackc/pgtype.Text",
},
})
if err != nil {
t.Fatalf("Generate(): %s", err)
}
wantQueryFile := "query.sql.go"
gotQueryFile := filepath.Join(tmpDir, "query.sql.go")
assert.FileExists(t, gotQueryFile,
"Generate() should emit query.sql.go")
wantQueries, err := os.ReadFile(wantQueryFile)
if err != nil {
t.Fatalf("read wanted query.go.sql: %s", err)
}
gotQueries, err := os.ReadFile(gotQueryFile)
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",
gotQueryFile, wantQueryFile)
}