This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gostore_test.go
301 lines (242 loc) · 7.28 KB
/
gostore_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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"os"
"path/filepath"
"strings"
"testing"
yaml "gopkg.in/yaml.v2"
"github.com/pirmd/clapp"
"github.com/pirmd/verify"
"github.com/pirmd/gostore/store"
)
const (
testdataPath = "./testdata"
)
type testGostore struct {
*Gostore
*verify.TestFolder
}
func (gs *testGostore) Close() {
gs.Gostore.Close()
gs.TestFolder.Clean()
}
func newTestGostore(tb testing.TB, cfg *Config) *testGostore {
tstPathName := strings.Replace(tb.Name(), string(os.PathSeparator), "_", -1)
tstDir, err := verify.NewTestFolder(tstPathName)
if err != nil {
tb.Fatalf("Failed to create test folder: %v", err)
}
store.UseFrozenTimeStamps()
cfg.UI.Auto = true
cfg.Store.Path = tstDir.Root
gs, err := openGostore(cfg)
if err != nil {
tb.Fatalf("cannot generate gostore from config: %s", err)
}
gs.log = verify.NewLogger(tb)
return &testGostore{gs, tstDir}
}
func TestGostoreWithDefaultConfig(t *testing.T) {
cfg := newConfig()
httpmock := verify.StartMockHTTPResponse()
defer httpmock.Stop()
for _, style := range cfg.UI.Styles() {
t.Run(style+"Fmt", func(t *testing.T) {
cfg.UI.OutputFormat = style
gs := newTestGostore(t, cfg)
defer gs.Close()
testImport(t, gs)
testList(t, gs)
testListAll(t, gs)
testSearch(t, gs)
testDelete(t, gs)
})
}
}
func TestGostoreWithConfigExample(t *testing.T) {
cfg := newConfig()
appCfg := &clapp.Config{
Unmarshaller: yaml.Unmarshal,
Files: []*clapp.ConfigFile{{Name: "config.example.yaml"}},
Var: cfg,
}
if err := appCfg.Load(); err != nil {
t.Fatalf("Fail to read 'config.example.yaml': %v", err)
}
httpmock := verify.StartMockHTTPResponse()
defer httpmock.Stop()
for _, style := range cfg.UI.Styles() {
t.Run(style+"Fmt", func(t *testing.T) {
cfg.UI.OutputFormat = style
gs := newTestGostore(t, cfg)
defer gs.Close()
testImport(t, gs)
testList(t, gs)
testListAll(t, gs)
testSearch(t, gs)
testDelete(t, gs)
})
}
}
func testImport(t *testing.T, gs *testGostore) {
testCases, err := filepath.Glob(filepath.Join(testdataPath, "*.epub"))
if err != nil {
t.Fatalf("cannot read test data in %s:%v", testdataPath, err)
}
t.Run("ImportEpubs", func(t *testing.T) {
stdout, err := verify.StartMockStdout()
if err != nil {
t.Fatalf("Fail to mock stdout: %v", err)
}
defer stdout.Stop()
if err := gs.Import(testCases); err != nil {
t.Errorf("Fail to import epub '%s': %v", testCases, err)
}
if failure := verify.MatchStdoutGolden(t.Name(), stdout); failure != nil {
t.Fatalf("Import output is not as expected.\n%v", failure)
}
if gs.store.IsDirty() {
t.Fatalf("Collection is inconsistent")
}
})
t.Run("ImportTwiceEpubs", func(t *testing.T) {
stdout, err := verify.StartMockStdout()
if err != nil {
t.Fatalf("Fail to mock stdout: %v", err)
}
defer stdout.Stop()
if err := gs.Import(testCases); err == nil {
t.Errorf("Importing '%s' a second time worked but should not", testCases)
}
if failure := verify.EqualStdoutString(stdout, ""); failure != nil {
t.Errorf("Importing an existing record should not print anything:\n%v", failure)
}
})
}
func testList(t *testing.T, gs *testGostore) {
allepubs, err := gs.ListWithExt(".epub")
if err != nil {
t.Fatalf("Fail to list epub: %v", err)
}
t.Run("ListEpubs", func(t *testing.T) {
stdout, err := verify.StartMockStdout()
if err != nil {
t.Fatalf("Fail to mock stdout: %v", err)
}
defer stdout.Stop()
if err := gs.Gostore.ListGlob(allepubs, []string{}); err != nil {
t.Errorf("List failed: %v", err)
}
if failure := verify.MatchStdoutGolden(t.Name(), stdout); failure != nil {
t.Errorf("List output is not as expected:\n%v", failure)
}
})
t.Run("ListEpubsSorted", func(t *testing.T) {
stdout, err := verify.StartMockStdout()
if err != nil {
t.Fatalf("Fail to mock stdout: %v", err)
}
defer stdout.Stop()
if err := gs.ListGlob([]string{"*.epub"}, []string{"PublishedDate"}); err != nil {
t.Fatalf("ListAll: fail to list epubs from collection: %v", err)
}
if failure := verify.MatchStdoutGolden(t.Name(), stdout); failure != nil {
t.Errorf("ListAll output is not as expected:\n%v", failure)
}
})
}
func testListAll(t *testing.T, gs *testGostore) {
t.Run("ListAll", func(t *testing.T) {
stdout, err := verify.StartMockStdout()
if err != nil {
t.Fatalf("Fail to mock stdout: %v", err)
}
defer stdout.Stop()
if err := gs.ListAll([]string{}); err != nil {
t.Fatalf("ListAll: fail to list epubs from collection: %v", err)
}
if failure := verify.MatchStdoutGolden(t.Name(), stdout); failure != nil {
t.Errorf("ListAll output is not as expected:\n%v", failure)
}
})
t.Run("ListAllSorted", func(t *testing.T) {
stdout, err := verify.StartMockStdout()
if err != nil {
t.Fatalf("Fail to mock stdout: %v", err)
}
defer stdout.Stop()
if err := gs.ListAll([]string{"PublishedDate"}); err != nil {
t.Fatalf("ListAll: fail to list epubs from collection: %v", err)
}
if failure := verify.MatchStdoutGolden(t.Name(), stdout); failure != nil {
t.Errorf("ListAll output is not as expected:\n%v", failure)
}
})
}
func testSearch(t *testing.T, gs *testGostore) {
t.Run("SearchAll", func(t *testing.T) {
stdout, err := verify.StartMockStdout()
if err != nil {
t.Fatalf("Fail to mock stdout: %v", err)
}
defer stdout.Stop()
if err := gs.ListQuery("*", []string{}); err != nil {
t.Fatalf("SearchAll: fail to list epubs from collection: %v", err)
}
if failure := verify.MatchStdoutGolden(t.Name(), stdout); failure != nil {
t.Errorf("SearchAll output is not as expected:\n%s", failure)
}
})
t.Run("Search", func(t *testing.T) {
stdout, err := verify.StartMockStdout()
if err != nil {
t.Fatalf("Fail to mock stdout: %v", err)
}
defer stdout.Stop()
if err := gs.ListQuery("Adventures", []string{}); err != nil {
t.Fatalf("Fail to search the collection: %v", err)
}
if failure := verify.MatchStdoutGolden(t.Name(), stdout); failure != nil {
t.Errorf("Search output is not as expected:\n%v", failure)
}
})
t.Run("SearchSort", func(t *testing.T) {
stdout, err := verify.StartMockStdout()
if err != nil {
t.Fatalf("Fail to mock stdout: %v", err)
}
defer stdout.Stop()
if err := gs.ListQuery("*", []string{"PublishedDate"}); err != nil {
t.Fatalf("Fail to search the collection: %v", err)
}
if failure := verify.MatchStdoutGolden(t.Name(), stdout); failure != nil {
t.Errorf("Search output is not as expected:\n%v", failure)
}
})
//TODO(pirmd): add additional search pattern using date and book series number
}
func testDelete(t *testing.T, gs *testGostore) {
allepubs, err := gs.ListWithExt(".epub")
if err != nil {
t.Fatalf("Fail to list epub: %v", err)
}
if len(allepubs) < 1 {
t.Fatalf("Delete failed: found no epubs to delete")
}
t.Run("DeleteEpubs", func(t *testing.T) {
if err := gs.Delete(allepubs[0:1]); err != nil {
t.Fatalf("Delete failed: %v", err)
}
stdout, err := verify.StartMockStdout()
if err != nil {
t.Fatalf("Fail to mock stdout: %v", err)
}
defer stdout.Stop()
if err := gs.ListAll([]string{}); err != nil {
t.Fatalf("ListAll after delete failed: cannot list epubs from collection: %v", err)
}
if failure := verify.MatchStdoutGolden(t.Name(), stdout); failure != nil {
t.Errorf("Delete output is not as expected:\n%v", failure)
}
})
}