-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathimport_test.go
273 lines (197 loc) · 12.3 KB
/
import_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package commands
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/activecm/rita-legacy/config"
)
func TestParseFlags(t *testing.T) {
type cfg = config.RollingStaticCfg // including the definition here for reference:
// DefaultChunks int `yaml:"DefaultChunks" default:"12"`
// Rolling bool
// CurrentChunk int
// TotalChunks int
type tc struct {
msg string
dbExists bool
dbIsRolling bool
dbCurrChunk int
dbTotalChunks int
userIsRolling bool
userCurrChunk int
userTotalChunks int
cfgDefaultChunks int
deleteOldData bool
expected cfg
err bool
}
// this is the sentinel value that signifies that a user did not supply
// a command line value for --chunk or --numchunks
const blank int = -1
// these are used to help make the test table below (a little) more readable
const exists bool = true
const rolling bool = true
const delete bool = true
const returnsError bool = true
const default12 int = 12
const default24 int = 24
testCases := []tc{
// new database scenarios
{"rita import (default 12)",
!exists, !rolling, 0, 0, !rolling, blank, blank, default12, !delete, cfg{12, !rolling, 0, 1}, !returnsError},
{"rita import --rolling (default 12)",
!exists, !rolling, 0, 0, rolling, blank, blank, default12, !delete, cfg{12, rolling, 0, 12}, !returnsError},
{"rita import --rolling --chunk 0 --numchunks 24 (default 12)",
!exists, !rolling, 0, 0, rolling, 0, 24, default12, !delete, cfg{12, rolling, 0, 24}, !returnsError},
{"rita import --numchunks 24 (default 12)",
!exists, !rolling, 0, 0, !rolling, blank, 24, default12, !delete, cfg{12, rolling, 0, 24}, !returnsError},
{"rita import --chunk 5 (default 12)",
!exists, !rolling, 0, 0, !rolling, 5, blank, default12, !delete, cfg{12, rolling, 5, 12}, !returnsError},
{"rita import --chunk 12 (default 12)",
!exists, !rolling, 0, 0, !rolling, 12, blank, default12, !delete, cfg{12, rolling, 12, 12}, returnsError},
{"rita import --chunk 12 (default 24)",
!exists, !rolling, 0, 0, !rolling, 12, blank, default24, !delete, cfg{24, rolling, 12, 24}, !returnsError},
{"rita import --chunk 12 --numchunks 24 (default 12)",
!exists, !rolling, 0, 0, !rolling, 12, 24, default12, !delete, cfg{12, rolling, 12, 24}, !returnsError},
{"rita import --chunk -2 (default 12)", // error reason: chunk number must be positive
!exists, !rolling, 0, 0, !rolling, -2, blank, default12, !delete, cfg{}, returnsError},
{"rita import --numchunks -2 (default 12)", // error reason: numchunks must be positive
!exists, !rolling, 0, 0, !rolling, blank, -2, default12, !delete, cfg{}, returnsError},
{"rita import --delete (default 12)",
!exists, !rolling, 0, 0, !rolling, blank, blank, default12, delete, cfg{12, !rolling, 0, 1}, !returnsError},
{"rita import --delete --rolling (default 12)",
!exists, !rolling, 0, 0, rolling, blank, blank, default12, delete, cfg{12, rolling, 0, 12}, !returnsError},
{"rita import --delete --rolling --chunk 0 --numchunks 24 (default 12)",
!exists, !rolling, 0, 0, rolling, 0, 24, default12, delete, cfg{12, rolling, 0, 24}, !returnsError},
{"rita import --delete --chunk 5 (default 12)",
!exists, !rolling, 0, 0, !rolling, 5, blank, default12, delete, cfg{12, rolling, 5, 12}, !returnsError},
// existing database scenarios
// non-rolling, current chunk 0, total chunks 1
{"rita import", // error reason: cannot import into existing non-rolling db
exists, !rolling, 0, 1, !rolling, blank, blank, default12, !delete, cfg{}, returnsError},
{"rita import --rolling",
exists, !rolling, 0, 1, rolling, blank, blank, default12, !delete, cfg{12, rolling, 1, 12}, !returnsError},
{"rita import --rolling --chunk 0 --numchunks 24",
exists, !rolling, 0, 1, rolling, 0, 24, default12, !delete, cfg{12, rolling, 0, 24}, !returnsError},
{"rita import --numchunks 24",
exists, !rolling, 0, 1, !rolling, blank, 24, default12, !delete, cfg{12, rolling, 1, 24}, !returnsError},
{"rita import --chunk 5 (default 12)",
exists, !rolling, 0, 1, !rolling, 5, blank, default12, !delete, cfg{12, rolling, 5, 12}, !returnsError},
{"rita import --chunk 12 (default 12)",
exists, !rolling, 0, 1, !rolling, 12, blank, default12, !delete, cfg{12, rolling, 12, 12}, returnsError},
{"rita import --chunk 12 (default 24)",
exists, !rolling, 0, 1, !rolling, 12, blank, default24, !delete, cfg{24, rolling, 12, 24}, !returnsError},
{"rita import --chunk 12 --numchunks 24",
exists, !rolling, 0, 1, !rolling, 12, 24, default12, !delete, cfg{12, rolling, 12, 24}, !returnsError},
{"rita import --chunk -2", // error reason: chunk number must be positive
exists, !rolling, 0, 1, !rolling, -2, blank, default12, !delete, cfg{}, returnsError},
{"rita import --numchunks -2", // error reason: numchunks must be positive
exists, !rolling, 0, 1, !rolling, blank, -2, default12, !delete, cfg{}, returnsError},
{"rita import --delete (default 12)",
exists, !rolling, 0, 1, !rolling, blank, blank, default12, delete, cfg{12, !rolling, 0, 1}, !returnsError},
{"rita import --delete --rolling (default 12)",
exists, !rolling, 0, 1, rolling, blank, blank, default12, delete, cfg{12, rolling, 0, 12}, !returnsError},
{"rita import --delete --chunk 5 (default 12)",
exists, !rolling, 0, 1, !rolling, 5, blank, default12, delete, cfg{12, rolling, 5, 12}, !returnsError},
{"rita import --delete --rolling --chunk 0 --numchunks 24 (default 12)",
exists, !rolling, 0, 1, rolling, 0, 24, default12, delete, cfg{12, rolling, 0, 24}, !returnsError},
// rolling, current chunk 1, total chunks 12
{"rita import",
exists, rolling, 1, 12, !rolling, blank, blank, default12, !delete, cfg{12, rolling, 2, 12}, !returnsError},
{"rita import --rolling",
exists, rolling, 1, 12, rolling, blank, blank, default12, !delete, cfg{12, rolling, 2, 12}, !returnsError},
{"rita import --rolling --chunk 0 --numchunks 24",
exists, rolling, 1, 12, rolling, 0, 24, default12, !delete, cfg{12, rolling, 0, 24}, !returnsError},
{"rita import --numchunks 24",
exists, rolling, 1, 12, !rolling, blank, 24, default12, !delete, cfg{12, rolling, 2, 24}, !returnsError},
{"rita import --chunk 5 (default 12)",
exists, rolling, 1, 12, !rolling, 5, blank, default12, !delete, cfg{12, rolling, 5, 12}, !returnsError},
{"rita import --chunk 12 (default 12)", // error reason: chunk must be less than db numchunks
exists, rolling, 1, 12, !rolling, 12, blank, default12, !delete, cfg{}, returnsError},
{"rita import --chunk 12 (default 24)", // error reason: chunk must be less than db numchunks
exists, rolling, 1, 12, !rolling, 12, blank, default24, !delete, cfg{}, returnsError},
{"rita import --chunk 12 --numchunks 24",
exists, rolling, 1, 12, !rolling, 12, 24, default12, !delete, cfg{12, rolling, 12, 24}, !returnsError},
{"rita import --chunk -2", // error reason: chunk number must be positive
exists, rolling, 1, 12, !rolling, -2, blank, default12, !delete, cfg{}, returnsError},
{"rita import --numchunks -2", // error reason: numchunks must be positive
exists, rolling, 1, 12, !rolling, blank, -2, default12, !delete, cfg{}, returnsError},
{"rita import --delete (default 12)",
exists, rolling, 1, 12, !rolling, blank, blank, default12, delete, cfg{12, rolling, 1, 12}, !returnsError},
{"rita import --delete --rolling (default 12)",
exists, rolling, 1, 12, !rolling, blank, blank, default12, delete, cfg{12, rolling, 1, 12}, !returnsError},
{"rita import --delete --chunk 5 (default 12)",
exists, rolling, 1, 12, !rolling, 5, blank, default12, delete, cfg{12, rolling, 5, 12}, !returnsError},
{"rita import --delete --rolling --chunk 0 --numchunks 24 (default 12)",
exists, rolling, 1, 12, rolling, 0, 24, default12, delete, cfg{12, rolling, 0, 24}, !returnsError},
// rolling, current chunk 11, total chunks 12
{"rita import",
exists, rolling, 11, 12, !rolling, blank, blank, default12, !delete, cfg{12, rolling, 0, 12}, !returnsError},
{"rita import --rolling",
exists, rolling, 11, 12, rolling, blank, blank, default12, !delete, cfg{12, rolling, 0, 12}, !returnsError},
{"rita import --rolling --chunk 0 --numchunks 24",
exists, rolling, 11, 12, rolling, 0, 24, default12, !delete, cfg{12, rolling, 0, 24}, !returnsError},
{"rita import --numchunks 24",
exists, rolling, 11, 12, !rolling, blank, 24, default12, !delete, cfg{12, rolling, 12, 24}, !returnsError},
{"rita import --chunk 5 (default 12)",
exists, rolling, 11, 12, !rolling, 5, blank, default12, !delete, cfg{12, rolling, 5, 12}, !returnsError},
{"rita import --chunk 12 (default 12)", // error reason: chunk must be less than db numchunks
exists, rolling, 11, 12, !rolling, 12, blank, default12, !delete, cfg{}, returnsError},
{"rita import --chunk 12 (default 24)", // error reason: chunk must be less than db numchunks
exists, rolling, 11, 12, !rolling, 12, blank, default24, !delete, cfg{}, returnsError},
{"rita import --chunk 12 --numchunks 24",
exists, rolling, 11, 12, !rolling, 12, 24, default12, !delete, cfg{12, rolling, 12, 24}, !returnsError},
{"rita import --delete (default 12)",
exists, rolling, 11, 12, !rolling, blank, blank, default12, delete, cfg{12, rolling, 11, 12}, !returnsError},
{"rita import --delete --rolling (default 12)",
exists, rolling, 11, 12, !rolling, blank, blank, default12, delete, cfg{12, rolling, 11, 12}, !returnsError},
{"rita import --delete --chunk 5 (default 12)",
exists, rolling, 11, 12, !rolling, 5, blank, default12, delete, cfg{12, rolling, 5, 12}, !returnsError},
{"rita import --delete --rolling --chunk 0 --numchunks 24 (default 12)",
exists, rolling, 11, 12, rolling, 0, 24, default12, delete, cfg{12, rolling, 0, 24}, !returnsError},
// rolling, current chunk 11, total chunks 24
{"rita import",
exists, rolling, 11, 24, !rolling, blank, blank, default12, !delete, cfg{12, rolling, 12, 24}, !returnsError},
{"rita import --rolling",
exists, rolling, 11, 24, rolling, blank, blank, default12, !delete, cfg{12, rolling, 12, 24}, !returnsError},
{"rita import --rolling --chunk 0 --numchunks 24",
exists, rolling, 11, 24, rolling, 0, 24, default12, !delete, cfg{12, rolling, 0, 24}, !returnsError},
{"rita import --numchunks 12", // error reason: cannot reduce the number of chunks
exists, rolling, 11, 24, !rolling, blank, 12, default12, !delete, cfg{}, returnsError},
{"rita import --chunk 12 --numchunks 12", // error reason: cannot reduce the number of chunks
exists, rolling, 11, 24, !rolling, 12, 12, default12, !delete, cfg{}, returnsError},
{"rita import --chunk 13 (default 12)",
exists, rolling, 11, 24, !rolling, 13, blank, default12, !delete, cfg{12, rolling, 13, 24}, !returnsError},
{"rita import --delete (default 12)",
exists, rolling, 11, 24, !rolling, blank, blank, default12, delete, cfg{12, rolling, 11, 24}, !returnsError},
{"rita import --delete --rolling (default 12)",
exists, rolling, 11, 12, !rolling, blank, blank, default12, delete, cfg{12, rolling, 11, 12}, !returnsError},
{"rita import --delete --chunk 5 (default 12)",
exists, rolling, 11, 24, !rolling, 5, blank, default12, !delete, cfg{12, rolling, 5, 24}, !returnsError},
{"rita import --delete --rolling --chunk 0 --numchunks 24 (default 12)",
exists, rolling, 11, 24, rolling, 0, 24, default12, !delete, cfg{12, rolling, 0, 24}, !returnsError},
}
// runner for the test table above
for _, testCase := range testCases {
actual, err := parseFlags(
testCase.dbExists, testCase.dbIsRolling, testCase.dbCurrChunk, testCase.dbTotalChunks,
testCase.userIsRolling, testCase.userCurrChunk, testCase.userTotalChunks, testCase.cfgDefaultChunks,
testCase.deleteOldData,
)
// Construct a message that will help pinpoint which test failed
var dbMsg string
if testCase.dbExists && testCase.dbIsRolling {
dbMsg = fmt.Sprintf("rolling, currChunk=%d, totalChunks=%d", testCase.dbCurrChunk, testCase.dbTotalChunks)
} else if testCase.dbExists && !testCase.dbIsRolling {
dbMsg = fmt.Sprintf("non-rolling, currChunk=%d, totalChunks=%d", testCase.dbCurrChunk, testCase.dbTotalChunks)
} else {
dbMsg = "new"
}
if testCase.err {
assert.Errorf(t, err, "db: <%s> cmd: <%s>", dbMsg, testCase.msg)
} else {
assert.NoErrorf(t, err, "db: <%s> cmd: <%s>", dbMsg, testCase.msg)
assert.Equalf(t, testCase.expected, actual, "db: <%s> cmd: <%s>", dbMsg, testCase.msg)
}
}
}