-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_test.go
164 lines (146 loc) · 3.84 KB
/
module_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
package migres
import (
"errors"
"testing"
"github.com/annybs/go-version"
)
func TestModule_Upgrade(t *testing.T) {
type TestCase struct {
Input Module
Up bool
From string
To string
Expected []string
Err error
}
testOutput := []string{}
testMigration := func(v string, ok bool) Migration {
f := func() error {
if ok {
testOutput = append(testOutput, v)
return nil
}
return errors.New("test")
}
return Func(f, f)
}
testCases := []TestCase{
{
Input: Module{
"1.0.0": testMigration("1.0.0", true),
"2.0.0": testMigration("2.0.0", true),
"3.0.0": testMigration("3.0.0", true),
},
Up: true,
From: "0",
To: "3",
Expected: []string{"1.0.0", "2.0.0", "3.0.0"},
},
{
Input: Module{
"1.0.0": testMigration("1.0.0", true),
"2.0.0": testMigration("2.0.0", true),
"3.0.0": testMigration("3.0.0", true),
},
Up: true,
From: "1",
To: "3",
Expected: []string{"2.0.0", "3.0.0"},
},
{
Input: Module{
"1.0.0": testMigration("1.0.0", true),
"2.0.0": testMigration("2.0.0", true),
"3.0.0": testMigration("3.0.0", true),
},
From: "3",
To: "0",
Expected: []string{"3.0.0", "2.0.0", "1.0.0"},
},
{
Input: Module{
"1.0.0": testMigration("1.0.0", true),
"2.0.0": testMigration("2.0.0", true),
"3.0.0": testMigration("3.0.0", true),
},
From: "3",
To: "2",
Expected: []string{"3.0.0"},
},
{
Input: Module{
"1.0.0": testMigration("1.0.0", true),
"2.0.0": testMigration("2.0.0", false),
"3.0.0": testMigration("3.0.0", true),
},
Up: true,
From: "0",
To: "3",
Err: failMigration(errors.New("test"), version.MustParse("2.0.0"), version.MustParse("1.0.0")),
},
{
Input: Module{
"1.0.0": testMigration("1.0.0", true),
"2.0.0": testMigration("2.0.0", false),
"3.0.0": testMigration("3.0.0", true),
},
From: "4",
To: "1",
Err: failMigration(errors.New("test"), version.MustParse("2.0.0"), version.MustParse("3.0.0")),
},
{
Input: Module{
"1.0.0": testMigration("1.0.0", true),
"2.0.0": testMigration("2.0.0", true),
"3.0.0": testMigration("3.0.0", false),
},
From: "4",
To: "1",
Err: failMigration(errors.New("test"), version.MustParse("3.0.0"), nil),
},
}
for i, testCase := range testCases {
testOutput = []string{}
var err error
if testCase.Up {
err = testCase.Input.Upgrade(testCase.From, testCase.To)
} else {
err = testCase.Input.Downgrade(testCase.From, testCase.To)
}
if err != nil {
if testCase.Err == nil {
t.Errorf("test %d failed (expected nil error, got error %v)", i, err)
} else if !errors.Is(err, testCase.Err) {
t.Errorf("test %d failed (expected error %v, got error %v)", i, testCase.Err, err)
} else {
a := err.(*Error)
e := testCase.Err.(*Error)
if !a.Version.Equal(e.Version) {
t.Errorf("test %d failed (expected error.Version %s, got error.Version %s)", i, e.Version, a.Version)
} else if !a.LastVersion.Equal(e.LastVersion) {
t.Errorf("test %d failed (expected error.LastVersion %s, got error.LastVersion %s)", i, e.LastVersion, a.LastVersion)
} else {
t.Logf("test %d passed (expected error %v, got error %v)", i, testCase.Err, err)
}
}
continue
} else if testCase.Err != nil {
t.Errorf("test %d failed (expected error %v, got nil error)", i, testCase.Err)
continue
}
if len(testOutput) != len(testCase.Expected) {
t.Errorf("test %d failed (expected %v, got %v)", i, testCase.Expected, testOutput)
continue
}
ok := true
for j, v := range testOutput {
if v != testCase.Expected[j] {
t.Errorf("test %d failed (expected version %q at index %d, got version %q)", i, testCase.Expected[j], j, v)
ok = false
}
}
if ok {
t.Logf("test %d passed (expected %v, got %v)", i, testCase.Expected, testOutput)
}
}
}