-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgodfish_test.go
171 lines (149 loc) · 4.5 KB
/
godfish_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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package godfish_test
import (
"database/sql"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/rafaelespinoza/godfish"
"github.com/rafaelespinoza/godfish/internal"
"github.com/rafaelespinoza/godfish/internal/stub"
"github.com/rafaelespinoza/godfish/internal/test"
)
const dsnKey = "DB_DSN"
func TestCreateMigrationFiles(t *testing.T) {
t.Run("err", func(t *testing.T) {
err := godfish.CreateMigrationFiles("err_test", true, t.TempDir(), "bad", "bad2")
if err == nil {
t.Fatal(err)
}
})
t.Run("ok", func(t *testing.T) {
testdir := t.TempDir()
err := godfish.CreateMigrationFiles("err_test", true, testdir, "", "")
if err != nil {
t.Fatal(err)
}
entries, err := os.ReadDir(testdir)
if err != nil {
t.Fatal(err)
}
if len(entries) != 2 {
t.Fatalf("wrong number of entries, got %d, expected %d", len(entries), 2)
}
for i, direction := range []string{"forward", "reverse"} {
got := entries[i].Name()
if !strings.HasPrefix(got, direction) {
t.Errorf("expected filename, %q, to have prefix %q", got, direction)
}
if !strings.HasSuffix(got, "err_test.sql") {
t.Errorf("expected filename, %q, to have suffix %q", got, "err_test.sql")
}
}
})
}
func TestMigrate(t *testing.T) {
t.Run("missing DB_DSN", func(t *testing.T) {
t.Setenv(dsnKey, "")
err := godfish.Migrate(stub.NewDriver(), t.TempDir(), false, "")
if err == nil {
t.Fatalf("expected an error, got %v", err)
}
got := err.Error()
if !strings.Contains(got, dsnKey) {
t.Errorf("expected error message %q to mention %q", got, dsnKey)
}
})
}
func TestApplyMigration(t *testing.T) {
t.Run("missing DB_DSN", func(t *testing.T) {
t.Setenv(dsnKey, "")
err := godfish.ApplyMigration(stub.NewDriver(), t.TempDir(), false, "")
if err == nil {
t.Fatalf("expected an error, got %v", err)
}
got := err.Error()
if !strings.Contains(got, dsnKey) {
t.Errorf("expected error message %q to mention %q", got, dsnKey)
}
})
}
func TestInfo(t *testing.T) {
t.Run("missing DB_DSN", func(t *testing.T) {
t.Setenv(dsnKey, "")
err := godfish.Info(stub.NewDriver(), t.TempDir(), false, "", os.Stderr, "")
if err == nil {
t.Fatalf("expected an error, got %v", err)
}
got := err.Error()
if !strings.Contains(got, dsnKey) {
t.Errorf("expected error message %q to mention %q", got, dsnKey)
}
})
t.Run("unknown format does not error out", func(t *testing.T) {
t.Setenv(dsnKey, "test")
err := godfish.Info(stub.NewDriver(), t.TempDir(), false, "", os.Stderr, "tea_ess_vee")
if err != nil {
t.Fatalf("unexpected error, %v", err)
}
})
}
func TestInit(t *testing.T) {
var err error
testOutputDir := t.TempDir()
pathToFile := filepath.Clean(filepath.Join(testOutputDir, "config.json"))
// setup: file should not exist at first
if _, err = os.Stat(pathToFile); !os.IsNotExist(err) {
t.Fatalf("setup error; file at %q should not exist", pathToFile)
}
// test 1: file created with this shape
if err = godfish.Init(pathToFile); err != nil {
t.Fatalf("something else is wrong with setup; %v", err)
}
var conf internal.Config
if data, err := os.ReadFile(pathToFile); err != nil {
t.Fatal(err)
} else if err = json.Unmarshal(data, &conf); err != nil {
t.Fatal(err)
}
conf.PathToFiles = testOutputDir + "/bar"
// test2: write data and make sure it's not overwritten after calling Init
if data, err := json.MarshalIndent(conf, "", "\t"); err != nil {
t.Fatal(err)
} else {
err = os.WriteFile(pathToFile, append(data, byte('\n')), os.FileMode(0640))
if err != nil {
t.Fatal(err)
}
}
if err := godfish.Init(pathToFile); err != nil {
t.Fatal(err)
}
var conf2 internal.Config
if data, err := os.ReadFile(pathToFile); err != nil {
t.Fatal(err)
} else if err = json.Unmarshal(data, &conf2); err != nil {
t.Fatal(err)
}
if conf2.PathToFiles != testOutputDir+"/bar" {
t.Errorf(
"expected conf.PathToFiles to be %q, got %q",
"foo", conf2.PathToFiles,
)
}
}
func TestAppliedVersions(t *testing.T) {
// Regression test on the API. It's supposed to wrap this type from the
// standard library for the most common cases.
var thing interface{} = new(sql.Rows)
if _, ok := thing.(godfish.AppliedVersions); !ok {
t.Fatalf("expected %T to implement godfish.AppliedVersions", thing)
}
}
func TestDriver(t *testing.T) {
// These tests also run in the stub package. They are duplicated here to
// make test coverage tool consider the tests in godfish.go as covered.
t.Setenv("DB_DSN", "stub_dsn")
test.RunDriverTests(t, stub.NewDriver(), test.DefaultQueries)
}