forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_test.go
105 lines (99 loc) · 2.43 KB
/
version_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
package gorm
import (
"errors"
"reflect"
"testing"
sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/jinzhu/gorm"
)
func TestVersionFromPath(t *testing.T) {
v, err := MaxVersionFrom("testdata/fake_migrations")
if err != nil {
t.Errorf("Unexpected error %q", err.Error())
}
expected := &singleVersion{
target: 3,
}
if !reflect.DeepEqual(v, expected) {
t.Errorf("Expected %+v but got %+v", expected, v)
}
}
func TestVersionInDB(t *testing.T) {
for _, tc := range []struct {
name string
hasV int64
hasDirty bool
checkV MigrationVersionValidator
expErr error
}{
{
name: "version exact correct",
hasV: 3,
checkV: VersionExactly(3),
hasDirty: false,
expErr: nil,
},
{
name: "version exactly wrong",
hasV: 5,
checkV: VersionExactly(3),
hasDirty: false,
expErr: errors.New("Database at version 5, not equal to requirement of 3"),
},
{
name: "version range correct",
hasV: 3,
checkV: VersionRange(1, 4),
hasDirty: false,
expErr: nil,
},
{
name: "version too low",
hasV: 3,
checkV: VersionRange(1, 2),
hasDirty: false,
expErr: errors.New("Database at version 3, higher than requirement of 2"),
},
{
name: "version too high",
hasV: 3,
checkV: VersionRange(4, 5),
hasDirty: false,
expErr: errors.New("Database at version 3, lower than requirement of 4"),
},
{
name: "version dirty",
hasV: 3,
checkV: VersionRange(1, 5),
hasDirty: true,
expErr: errors.New("Database at version 3, but is dirty"),
},
} {
t.Run(tc.name, func(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock - %s", err)
}
vrows := sqlmock.NewRows([]string{"version", "dirty"})
vrows.AddRow(tc.hasV, tc.hasDirty)
mock.ExpectQuery(`SELECT \* FROM schema_migrations`).WillReturnRows(vrows)
gdb, err := gorm.Open("postgres", db)
if err != nil {
t.Fatalf("failed to open gorm db - %s", err)
}
err = VerifyMigrationVersion(gdb, tc.checkV)
if tc.expErr != nil {
if !reflect.DeepEqual(err, tc.expErr) {
t.Errorf("Was supposed to return error (%s) but returned (%s)", tc.expErr, err)
}
} else {
if err != nil {
t.Fatalf("failed to verify mocked version - %s", err)
}
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("failed to query properly - %s", err)
}
})
}
}