-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase_test.go
402 lines (352 loc) · 10.8 KB
/
database_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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package rrh
import (
"fmt"
"os"
"testing"
)
func openDatabase() *Database {
os.Setenv(DatabasePath, "testdata/database.json")
os.Setenv(ConfigPath, "testdata/config.json")
var config = OpenConfig()
var db, _ = Open(config)
return db
}
func TestRelationStringer(t *testing.T) {
var testdata = []struct {
relation Relation
result string
}{
{Relation{RepositoryID: "hoge", GroupName: "g1"}, "g1/hoge"},
}
for _, td := range testdata {
if td.relation.String() != td.result {
t.Errorf("%v: stringer result did not match, wont: %s, got: %s", td, td.result, td.relation.String())
}
}
}
func TestOpenBrokenJson(t *testing.T) {
os.Setenv(ConfigPath, "testdata/config.json")
os.Setenv(DatabasePath, "testdata/broken.json")
var config = OpenConfig()
var _, err = Open(config)
if err == nil {
t.Error("it can read broken json!?")
}
}
func TestAutoCreateGroup(t *testing.T) {
var db = openDatabase()
var group, err = db.AutoCreateGroup("newgroup", "desc", true)
if err != nil {
t.Errorf("auto create group failed: %s", err.Error())
}
if !db.HasGroup("newgroup") && group.Description != "desc" && group.OmitList {
t.Errorf("auto created group did not match, wont: %v, got: %v", Group{"newgroup", "desc", true}, group)
}
var group2, err2 = db.AutoCreateGroup("no-group", "description1", false)
if err2 != nil {
t.Errorf("auto create group failed: %s", err.Error())
}
if !db.HasGroup("no-group") && group.Description != "" && group.OmitList {
t.Errorf("existing group did not match, wont: %v, got: %v", Group{"group1", "desc1", true}, group2)
}
db.Config.Update(AutoCreateGroup, "false")
var _, err3 = db.AutoCreateGroup("failgroup", "desc", true)
if err3 == nil {
t.Errorf("auto create group should fail: %s", err3.Error())
}
}
func TestOpenNonExistFile(t *testing.T) {
os.Setenv(ConfigPath, "testdata/config.json")
os.Setenv(DatabasePath, "testdata/not-exist-file.json")
var config = OpenConfig()
var db, _ = Open(config)
if len(db.Repositories) != 0 {
t.Error("null db have no repository entries")
}
if len(db.Groups) != 0 {
t.Error("null db have no group entries")
}
}
func TestOpenNullDatabase(t *testing.T) {
os.Setenv(ConfigPath, "testdata/config.json")
os.Setenv(DatabasePath, "testdata/nulldb.json")
var config = OpenConfig()
var db, _ = Open(config)
if len(db.Repositories) != 0 {
t.Error("null db have no repository entries")
}
if len(db.Groups) != 0 {
t.Error("null db have no group entries")
}
if len(db.Relations) != 0 {
t.Error("null db have no group entries")
}
}
func TestStore(t *testing.T) {
var dbFile = Rollback("testdata/test_db.json", "testdata/config.json", func(config *Config, db *Database) {
db.CreateGroup("group1", "desc1", false)
db.CreateGroup("group2", "desc2", false)
db.CreateRepository("repo1", "path1", "desc1", []*Remote{})
db.CreateRepository("repo2", "path2", "desc2", []*Remote{})
db.Relate("group1", "repo1")
db.StoreAndClose()
var db2, _ = Open(config)
if !db2.HasGroup("group1") {
t.Error("group1 not found!")
}
if !db2.HasGroup("group2") {
t.Error("group2 not found!")
}
if !db2.HasRepository("repo1") {
t.Error("repo1 not found!")
}
if !db2.HasRepository("repo2") {
t.Error("repo2 not found!")
}
if !db2.HasRelation("group1", "repo1") {
t.Error("group1 does not relate with repo1")
}
})
defer os.Remove(dbFile)
}
func TestPrune(t *testing.T) {
var db = openDatabase()
db.CreateGroup("group1", "desc1", false)
db.CreateGroup("group2", "desc2", false)
db.CreateRepository("repo1", "path1", "desc1", []*Remote{})
db.CreateRepository("repo2", "path2", "desc2", []*Remote{})
db.Relate("group1", "repo1")
db.Prune()
if db.HasGroup("group2") {
t.Error("group2 was not pruned")
}
if db.HasRepository("repo2") {
t.Error("repo2 was not pruned")
}
if !db.HasRelation("group1", "repo1") {
t.Error("relation is removed?")
}
}
func TestDeleteGroup(t *testing.T) {
var db = openDatabase()
db.CreateGroup("group1", "desc1", false)
db.CreateGroup("group2", "desc2", false)
db.CreateRepository("repo1", "path1", "desc1", []*Remote{})
db.CreateRepository("repo2", "path2", "desc2", []*Remote{})
db.Relate("group1", "repo1")
if err := db.DeleteGroup("unknown"); err == nil {
t.Error("uknown: group found!")
}
if err := db.DeleteGroup("group2"); err != nil {
t.Error(err.Error())
}
if err := db.DeleteGroup("group1"); err == nil {
t.Error("group1 has one relation.")
}
if err := db.ForceDeleteGroup("group1"); err != nil {
t.Error(err.Error())
}
if err := db.ForceDeleteGroup("unknown"); err == nil {
t.Error("uknown: group found!")
}
}
func TestDeleteRepository(t *testing.T) {
var db = openDatabase()
db.CreateRepository("repo1", "path1", "desc1", []*Remote{})
db.CreateRepository("repo2", "path2", "desc2", []*Remote{})
if err := db.DeleteRepository("unknown"); err == nil {
t.Error("unknown: repository found!")
}
if err := db.DeleteRepository("rrh"); err != nil {
t.Error(err.Error())
}
}
func TestUnrelate(t *testing.T) {
var db = openDatabase()
db.CreateRepository("somerepo", "unknown", "desc", []*Remote{})
db.CreateGroup("group2", "desc2", false)
db.Relate("group2", "somerepo")
db.Relate("no-group", "somerepo")
db.Unrelate("group2", "Rrh")
db.Unrelate("no-group", "rrh")
if !db.HasRelation("group2", "somerepo") {
t.Error("group2 does not relate with somerepo")
}
}
func TestCreateRepository(t *testing.T) {
var db = openDatabase()
// rrh is already registered repository, therefore, the CreateRepository will fail.
var r1, err1 = db.CreateRepository("rrh", "unknown", "desc", []*Remote{})
if r1 != nil && err1 == nil {
t.Error(err1.Error())
}
var r2, err2 = db.CreateRepository("somerepo", "unknown", "desc", []*Remote{{"name1", "url1"}, {"name2", "url2"}})
if r2 == nil && err2 != nil {
t.Error("somerepo: cannot create repository")
}
if len(r2.Remotes) != 2 {
t.Error("remotes were not match.")
}
var wd, _ = os.Getwd()
assert(t, r2.ID, "somerepo")
assert(t, r2.Path, fmt.Sprintf("%s/unknown", wd))
assert(t, r2.Remotes[0].Name, "name1")
assert(t, r2.Remotes[0].URL, "url1")
assert(t, r2.Remotes[1].Name, "name2")
assert(t, r2.Remotes[1].URL, "url2")
}
func assert(t *testing.T, got, wont string) {
if wont != got {
t.Errorf("wont %s, got: %s", wont, got)
}
}
func TestCreateGroupRelateAndUnrelate(t *testing.T) {
var db = openDatabase()
var g1, err1 = db.CreateGroup("newGroup1", "desc1", false)
if err1 != nil {
t.Error(err1.Error())
}
if g1.Name != "newGroup1" {
t.Error("the name of created group is different")
}
if g1.Description != "desc1" {
t.Error("the description of created group is different")
}
var g2, err2 = db.CreateGroup("newGroup1", "desc2", false)
if err2 == nil || g2 != nil {
t.Error("cannot create same name group")
}
if err := db.Relate("no-group", "rrh"); err != nil {
t.Error("existing relation was never error")
}
if err := db.Relate("newGroup1", "rrh"); err != nil {
t.Error(err.Error())
}
if !db.HasRelation("newGroup1", "rrh") {
t.Error("created relation was not found!")
}
db.Unrelate("no-group", "rrh")
if db.HasRelation("no-group", "rrh") {
t.Error("deleted relation was not found!")
}
if err := db.Relate("unknown", "rrh"); err != nil {
t.Error(err.Error())
}
}
func TestUpdateGroup(t *testing.T) {
var db = openDatabase()
db.UpdateGroup("no-group", &Group{"updated-group", "description", false})
var group = db.FindGroup("updated-group")
if group.Name != "updated-group" {
t.Error("Update is failed (group name was not updated)")
}
if group.Description != "description" {
t.Error("Update is failed (description was not updated)")
}
if db.UpdateGroup("unknown", &Group{"never used", "never used2", false}) {
t.Error("unknown group is successfully updated.")
}
}
func TestFindFunction(t *testing.T) {
var db = openDatabase()
var group1 = db.FindGroup("no-group")
if group1 == nil {
t.Error("no-group: not found")
}
var group2 = db.FindGroup("unknown")
if group2 != nil {
t.Error("unknown: found!")
}
var repo1 = db.FindRepository("rrh")
var repo2 = db.FindRepository("unknown")
if repo1 == nil {
t.Error("rrh: not found!")
}
if repo2 != nil {
t.Error("rrh: found!")
}
}
func TestHasGroup(t *testing.T) {
var db = openDatabase()
if !db.HasGroup("no-group") {
t.Error("no-group: group not found")
}
if db.HasGroup("unknown") {
t.Error("unknown: exist not existing group")
}
if !db.HasRepository("rrh") {
t.Error("rrh: repository not found")
}
if db.HasRepository("unknown") {
t.Error("unknown: found!")
}
if !db.HasRelation("no-group", "rrh") {
t.Error("rrh: no relation with no-group")
}
if db.HasRelation("unknown", "rrh") {
t.Error("rrh: unknown relation found!")
}
if db.HasRelation("no-group", "unknown") {
t.Error("unknown relation in no-group found!")
}
}
func TestFindRelations(t *testing.T) {
var db = openDatabase()
var repos = db.FindRelationsOfGroup("no-group")
if len(repos) != 2 || repos[0] != "rrh" {
t.Errorf("find relations: wont: [\"rrh\"], got: %v", repos)
}
var groups = db.FindRelationsOfRepository("rrh")
if len(groups) != 1 || groups[0] != "no-group" {
t.Errorf("find relations: wont: [\"no-group\"], got: %v", groups)
}
}
func TestUpdateRepository(t *testing.T) {
var dbFile = Rollback("testdata/test_db.json", "testdata/config.json", func(config *Config, db *Database) {
if !db.UpdateRepository("repo1", Repository{ID: "newRepo1", Path: "newPath1", Description: "desc1"}) {
t.Errorf("Update failed")
}
if !db.HasRepository("newRepo1") || !db.HasRelation("group1", "newRepo1") {
t.Errorf("update repository failed.")
}
if db.HasRepository("repo1") || db.HasRelation("group1", "repo1") {
t.Errorf("old information are remained.")
}
if db.UpdateRepository("unknownrepo", Repository{}) {
t.Errorf("missing repository updation succeeded.")
}
})
defer os.Remove(dbFile)
}
func TestCounting(t *testing.T) {
var db = openDatabase()
if count := db.BelongingCount("rrh"); count != 1 {
t.Errorf("belonging count of %s: wont: 1, got: %d", "rrh", count)
}
if count := db.ContainsCount("no-group"); count != 2 {
t.Errorf("%s contains: wont: 2, got: %d", "no-group", count)
}
}
func TestFindTargets(t *testing.T) {
var testcases = []struct {
groupNames []string
size int
rels []Relation
}{
{[]string{"group1", "group3"}, 2, []Relation{{"repo1", "group1"}, {"repo2", "group3"}}},
}
for _, tc := range testcases {
var dbFile = Rollback("testdata/test_db.json", "testdata/config.json", func(config *Config, db *Database) {
var rels = FindTargets(db, tc.groupNames)
if len(rels) != tc.size {
t.Errorf("the length of result did not match, wont: %d, got: %d", tc.size, len(rels))
}
for i := range rels {
if rels[i].GroupName != tc.rels[i].GroupName && rels[i].RepositoryID != tc.rels[i].RepositoryID {
t.Errorf("result did not match, wont: %s, got: %s", tc.rels[i].String(), rels[i].String())
}
}
})
defer os.Remove(dbFile)
}
}