-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgomigration_test.go
More file actions
175 lines (140 loc) · 4.3 KB
/
Copy pathgomigration_test.go
File metadata and controls
175 lines (140 loc) · 4.3 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
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
package gomigration
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type mockDriver struct {
mock.Mock
Driver
}
func (m *mockDriver) SetMigrationTableName(name string) {
m.Called(name)
}
func (m *mockDriver) CreateMigrationsTable(ctx context.Context) error {
args := m.Called(ctx)
return args.Error(0)
}
func (m *mockDriver) GetExecutedMigrations(ctx context.Context, includeRollbacked bool) ([]ExecutedMigration, error) {
args := m.Called(ctx, includeRollbacked)
return args.Get(0).([]ExecutedMigration), args.Error(1)
}
func (m *mockDriver) ApplyMigrations(ctx context.Context, migrations []Migration, before, after func(*Migration), onError func(*Migration, error)) error {
args := m.Called(ctx, migrations)
return args.Error(0)
}
func (m *mockDriver) UnapplyMigrations(ctx context.Context, migrations []Migration, before, after func(*Migration), onError func(*Migration, error)) error {
args := m.Called(ctx, migrations)
return args.Error(0)
}
func (m *mockDriver) CleanDatabase(ctx context.Context) error {
args := m.Called(ctx)
return args.Error(0)
}
func TestGoMigration_New_ErrorNilConfig(t *testing.T) {
q, err := New(nil)
assert.Nil(t, q)
assert.Equal(t, ErrConfigNotProvided, err)
}
func TestGoMigration_New_ErrorNilDriver(t *testing.T) {
cfg := &Config{}
q, err := New(cfg)
assert.Nil(t, q)
assert.Equal(t, ErrDriverNotProvided, err)
}
func TestGoMigration_Register_Duplicate(t *testing.T) {
q := &GoMigration{migrations: make(map[string]Migration)}
migration := dummyMigration{name: "001_create_users"}
err := q.Register(migration)
assert.NoError(t, err)
err = q.Register(migration)
assert.Error(t, err)
assert.Contains(t, err.Error(), "registered more than once")
}
func TestGoMigration_Migrate_NoMigrations(t *testing.T) {
ctx := context.TODO()
driver := new(mockDriver)
driver.On("CreateMigrationsTable", ctx).Return(nil)
driver.On("GetExecutedMigrations", ctx, false).Return([]ExecutedMigration{}, nil)
q := &GoMigration{
driver: driver,
migrations: map[string]Migration{},
}
err := q.Migrate(ctx)
assert.NoError(t, err)
driver.AssertExpectations(t)
}
func TestGoMigration_Fresh_Success(t *testing.T) {
ctx := context.TODO()
driver := new(mockDriver)
driver.On("CleanDatabase", ctx).Return(nil)
driver.On("CreateMigrationsTable", ctx).Return(nil)
driver.On("GetExecutedMigrations", ctx, false).Return([]ExecutedMigration{}, nil)
q := &GoMigration{
driver: driver,
migrations: map[string]Migration{},
}
err := q.Fresh(ctx)
assert.NoError(t, err)
driver.AssertExpectations(t)
}
func TestGoMigration_Reset_NoExecuted(t *testing.T) {
ctx := context.TODO()
driver := new(mockDriver)
driver.On("GetExecutedMigrations", ctx, true).Return([]ExecutedMigration{}, nil)
q := &GoMigration{
driver: driver,
}
err := q.Reset(ctx)
assert.NoError(t, err)
driver.AssertExpectations(t)
}
func TestGoMigration_Clean_Error(t *testing.T) {
ctx := context.TODO()
driver := new(mockDriver)
driver.On("CleanDatabase", ctx).Return(errors.New("clean error"))
q := &GoMigration{
driver: driver,
}
err := q.Clean(ctx)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to clean database")
driver.AssertExpectations(t)
}
func TestGoMigration_List(t *testing.T) {
ctx := context.TODO()
driver := new(mockDriver)
driver.On("CreateMigrationsTable", ctx).Return(nil)
driver.On("GetExecutedMigrations", ctx, false).Return([]ExecutedMigration{{Name: "001_create_users", ExecutedAt: time.Now()}}, nil)
migration := dummyMigration{name: "001_create_users"}
q := &GoMigration{
driver: driver,
migrations: map[string]Migration{"001_create_users": migration},
}
list, err := q.List(ctx)
assert.NoError(t, err)
assert.Len(t, list, 1)
assert.True(t, list[0].IsExecuted)
driver.AssertExpectations(t)
}
func TestSetMigrationFilesDir(t *testing.T) {
q := &GoMigration{}
q.SetMigrationFilesDir("migrations")
assert.Equal(t, "migrations", q.migrationFilesDir)
}
// dummyMigration is a simple implementation of the Migration interface for testing.
type dummyMigration struct {
name string
}
func (d dummyMigration) Name() string {
return d.name
}
func (d dummyMigration) UpScript() string {
return "CREATE TABLE dummy (id INT);"
}
func (d dummyMigration) DownScript() string {
return "DROP TABLE dummy;"
}