-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrator_test.go
214 lines (186 loc) · 6.27 KB
/
migrator_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package migrator_test
import (
"context"
"errors"
"math"
"testing"
migrator "github.com/cybertec-postgresql/pgx-migrator"
pgx "github.com/jackc/pgx/v5"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
)
var migrations = []interface{}{
&migrator.Migration{
Name: "Using tx, encapsulate two queries",
Func: func(ctx context.Context, tx pgx.Tx) error {
if _, err := tx.Exec(ctx, "CREATE TABLE foo (id INT PRIMARY KEY)"); err != nil {
return err
}
if _, err := tx.Exec(ctx, "INSERT INTO foo (id) VALUES (1)"); err != nil {
return err
}
return nil
},
},
&migrator.MigrationNoTx{
Name: "Using db, execute one query",
Func: func(ctx context.Context, db migrator.PgxIface) error {
if _, err := db.Exec(ctx, "INSERT INTO foo (id) VALUES (2)"); err != nil {
return err
}
return nil
},
},
}
func TestMigratorConstructor(t *testing.T) {
_, err := migrator.New() //migrator.Migrations(migrations...)
assert.Error(t, err, "Should throw error when migrations are empty")
_, err = migrator.New(migrator.Migrations(struct{ Foo string }{Foo: "bar"}))
assert.Error(t, err, "Should throw error for unknown migration type")
}
func TestTableExists(t *testing.T) {
mock, err := pgxmock.NewPool()
assert.NoError(t, err)
defer mock.Close()
m, err := migrator.New(migrator.Migrations(migrations...))
assert.NoError(t, err)
assert.NotNil(t, m)
sqlresults := []struct {
testname string
tableexists bool
appliedcount int
needupgrade bool
tableerr error
counterr error
}{
{
testname: "table exists and no migrations applied",
tableexists: true,
appliedcount: 0,
needupgrade: true,
tableerr: nil,
counterr: nil,
},
{
testname: "table exists and a lot of migrations applied",
tableexists: true,
appliedcount: math.MaxInt32,
needupgrade: false,
tableerr: nil,
counterr: nil,
},
{
testname: "error occurred during count query",
tableexists: true,
appliedcount: 0,
needupgrade: false,
tableerr: nil,
counterr: errors.New("internal error"),
},
{
testname: "error occurred during table exists query",
tableexists: false,
appliedcount: 0,
needupgrade: true,
tableerr: errors.New("internal error"),
counterr: nil,
},
}
var expectederr error
for _, res := range sqlresults {
if q := mock.ExpectQuery("SELECT to_regclass").WithArgs(pgxmock.AnyArg()); res.tableerr != nil {
q.WillReturnError(res.tableerr)
expectederr = res.tableerr
} else {
q.WillReturnRows(pgxmock.NewRows([]string{"to_regclass"}).AddRow(res.tableexists))
}
if q := mock.ExpectQuery("SELECT count"); res.counterr != nil {
q.WillReturnError(res.counterr)
expectederr = res.counterr
} else {
q.WillReturnRows(pgxmock.NewRows([]string{"count"}).AddRow(res.appliedcount))
}
need, err := m.NeedUpgrade(context.Background(), mock)
assert.Equal(t, expectederr, err, "NeedUpgrade test failed: ", res.testname)
assert.Equal(t, res.needupgrade, need, "NeedUpgrade incorrect return: ", res.testname)
}
}
func TestMigrateExists(t *testing.T) {
mock, err := pgxmock.NewPool()
assert.NoError(t, err)
defer mock.Close()
m, err := migrator.New(migrator.Migrations(migrations...))
assert.NoError(t, err)
assert.NotNil(t, m)
expectederr := errors.New("internal error")
mock.ExpectExec("CREATE TABLE").WillReturnResult(pgxmock.NewResult("DDL", 0))
mock.ExpectQuery("SELECT count").WillReturnError(expectederr)
err = m.Migrate(context.Background(), mock)
assert.Equal(t, expectederr, err, "Migrate test failed: ", err)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestMigrateNoTxError(t *testing.T) {
mock, err := pgxmock.NewPool()
assert.NoError(t, err)
defer mock.Close()
m, err := migrator.New(migrator.Migrations(&migrator.MigrationNoTx{Func: func(context.Context, migrator.PgxIface) error { return nil }}))
assert.NoError(t, err)
assert.NotNil(t, m)
expectederr := errors.New("internal error")
mock.ExpectExec("CREATE TABLE").WillReturnResult(pgxmock.NewResult("DDL", 0))
mock.ExpectQuery("SELECT count").WillReturnRows(pgxmock.NewRows([]string{"count"}).AddRow(0))
mock.ExpectExec("INSERT").WillReturnError(expectederr)
err = m.Migrate(context.Background(), mock)
for errors.Unwrap(err) != nil {
err = errors.Unwrap(err)
}
assert.Equal(t, expectederr, err, "MigrateNoTxError test failed: ", err)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestMigrateTxError(t *testing.T) {
mock, err := pgxmock.NewPool()
assert.NoError(t, err)
defer mock.Close()
m, err := migrator.New(migrator.Migrations(&migrator.Migration{Func: func(context.Context, pgx.Tx) error { return nil }}))
assert.NoError(t, err)
assert.NotNil(t, m)
expectederr := errors.New("create table error")
mock.ExpectExec("CREATE TABLE").WillReturnError(expectederr)
err = m.Migrate(context.Background(), mock)
assert.Equal(t, expectederr, err, "MigrateTxError test failed: ", err)
expectederr = errors.New("internal tx error")
mock.ExpectExec("CREATE TABLE").WillReturnResult(pgxmock.NewResult("DDL", 0))
mock.ExpectQuery("SELECT count").WillReturnRows(pgxmock.NewRows([]string{"count"}).AddRow(0))
mock.ExpectBegin().WillReturnError(expectederr)
err = m.Migrate(context.Background(), mock)
for errors.Unwrap(err) != nil {
err = errors.Unwrap(err)
}
assert.Equal(t, expectederr, err, "MigrateTxError test failed: ", err)
expectederr = errors.New("internal tx error")
mock.ExpectExec("CREATE TABLE").WillReturnResult(pgxmock.NewResult("DDL", 0))
mock.ExpectQuery("SELECT count").WillReturnRows(pgxmock.NewRows([]string{"count"}).AddRow(0))
mock.ExpectBegin()
mock.ExpectExec("INSERT").WillReturnError(expectederr)
err = m.Migrate(context.Background(), mock)
for errors.Unwrap(err) != nil {
err = errors.Unwrap(err)
}
assert.Equal(t, expectederr, err, "MigrateTxError test failed: ", err)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestMigratorOptions(t *testing.T) {
O := migrator.TableName("foo")
m := &migrator.Migrator{}
O(m)
assert.Equal(t, "foo", m.TableName)
f := func(string) {}
O = migrator.SetNotice(f)
O(m)
}