-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline_test.go
More file actions
148 lines (132 loc) · 4.23 KB
/
pipeline_test.go
File metadata and controls
148 lines (132 loc) · 4.23 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package sqlproc
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/DATA-DOG/go-sqlmock"
)
func TestRun_GenerateOnly(t *testing.T) {
t.Parallel()
dir := t.TempDir()
sqlFile := writeTestFile(t, dir, "ping.sql", sampleProcedureSQL())
outDir := filepath.Join(dir, "generated")
result, err := Run(context.Background(), PipelineOptions{
SQLInputs: []string{sqlFile},
OutputDir: outDir,
SkipMigrate: true,
PackageName: "autogen",
SkipGenerate: false,
})
if err != nil {
t.Fatalf("Run returned error: %v", err)
}
if len(result.GeneratedFiles) != 3 {
t.Fatalf("expected 3 generated files, got %d", len(result.GeneratedFiles))
}
for _, file := range result.GeneratedFiles {
if _, err := os.Stat(file); err != nil {
t.Fatalf("expected generated file %s to exist: %v", file, err)
}
}
}
func TestRun_MissingDB(t *testing.T) {
t.Parallel()
dir := t.TempDir()
sqlFile := writeTestFile(t, dir, "ping.sql", sampleProcedureSQL())
_, err := Run(context.Background(), PipelineOptions{
SQLInputs: []string{sqlFile},
})
if err == nil {
t.Fatal("expected error when DB is missing and migrations are enabled")
}
}
func TestRun_WithDBAndMigrations(t *testing.T) {
t.Parallel()
dir := t.TempDir()
sqlFile := writeTestFile(t, dir, "ping.sql", sampleProcedureSQL())
migrationFile := writeTestFile(t, dir, "001_init.sql", "CREATE TABLE foo(id INT PRIMARY KEY);")
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock: %v", err)
}
defer db.Close()
mock.ExpectExec(`CREATE TABLE IF NOT EXISTS sqlproc_schema_migrations`).
WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectQuery(`SELECT version FROM sqlproc_schema_migrations`).
WillReturnRows(sqlmock.NewRows([]string{"version"}))
mock.ExpectBegin()
mock.ExpectExec(`CREATE TABLE foo`).WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectExec(`INSERT INTO sqlproc_schema_migrations`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
mock.ExpectExec(`CREATE OR REPLACE FUNCTION ping_proc`).WillReturnResult(sqlmock.NewResult(0, 0))
_, err = Run(context.Background(), PipelineOptions{
SQLInputs: []string{sqlFile},
MigrationInputs: []string{migrationFile},
DB: db,
SkipGenerate: true,
})
if err != nil {
t.Fatalf("Run returned error: %v", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet expectations: %v", err)
}
}
func TestRun_SchemaModelsOnly(t *testing.T) {
t.Parallel()
dir := t.TempDir()
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock: %v", err)
}
defer db.Close()
queryRegex := "SELECT table_schema, table_name, column_name, data_type, udt_name, is_nullable"
rows := sqlmock.NewRows([]string{"table_schema", "table_name", "column_name", "data_type", "udt_name", "is_nullable"}).
AddRow("public", "users", "id", "integer", "int4", "NO").
AddRow("public", "users", "email", "text", "text", "YES")
mock.ExpectQuery(queryRegex).WithArgs("public").WillReturnRows(rows)
result, err := Run(context.Background(), PipelineOptions{
SkipMigrate: true,
DB: db,
SchemaModels: &SchemaModelOptions{Schemas: []string{"public"}, OutputDir: dir, PackageName: "models"},
})
if err != nil {
t.Fatalf("Run returned error: %v", err)
}
if len(result.SchemaFiles) != 1 {
t.Fatalf("expected 1 schema model file, got %d", len(result.SchemaFiles))
}
if _, err := os.Stat(result.SchemaFiles[0]); err != nil {
t.Fatalf("expected schema file to exist: %v", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet expectations: %v", err)
}
}
func TestRun_SchemaModelsRequireDB(t *testing.T) {
t.Parallel()
_, err := Run(context.Background(), PipelineOptions{
SchemaModels: &SchemaModelOptions{Schemas: []string{"public"}, OutputDir: "./tmp"},
})
if err == nil {
t.Fatal("expected error when schema models requested without DB")
}
}
func writeTestFile(t *testing.T, dir, name, contents string) string {
t.Helper()
path := filepath.Join(dir, name)
if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
t.Fatalf("write file %s: %v", path, err)
}
return path
}
func sampleProcedureSQL() string {
return `-- name: Ping :exec
CREATE OR REPLACE FUNCTION ping_proc()
RETURNS void AS $$
BEGIN
PERFORM 1;
END;
$$ LANGUAGE plpgsql;`
}