-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository_test.go
116 lines (106 loc) · 2.74 KB
/
repository_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
package badger
import (
"fmt"
"testing"
"time"
"github.com/dgraph-io/badger/v4"
vocab "github.com/go-ap/activitypub"
)
func initBadgerForTesting(t *testing.T) (*repo, error) {
tempDir, err := Path(Config{Path: t.TempDir()})
if err != nil {
return nil, fmt.Errorf("invalid path for initializing boltdb %s: %s", tempDir, err)
}
c := badger.DefaultOptions(tempDir)
r := &repo{
path: tempDir,
logFn: t.Logf,
errFn: t.Errorf,
}
r.d, err = badger.Open(c)
defer r.d.Close()
if err != nil {
return nil, fmt.Errorf("failed to open boltdb database at path %s: %s", tempDir, err)
}
t.Logf("Initialized test db at %s", r.path)
return r, nil
}
func orderedCollection(iri vocab.IRI) vocab.CollectionInterface {
col := vocab.OrderedCollectionNew(iri)
col.Published = time.Now().UTC().Truncate(time.Second)
return col
}
func Test_repo_AddTo(t *testing.T) {
type args struct {
col vocab.IRI
it vocab.ItemCollection
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "inbox One IRI",
args: args{
col: vocab.IRI("http://example.com/inbox"),
it: vocab.ItemCollection{vocab.IRI("http://example.com/1")},
},
wantErr: false,
},
{
name: "replies One IRI",
args: args{
col: vocab.IRI("http://example.com/replies"),
it: vocab.ItemCollection{vocab.IRI("http://example.com/1")},
},
wantErr: false,
},
{
name: "replies multiple IRI",
args: args{
col: vocab.IRI("http://example.com/replies"),
it: vocab.ItemCollection{vocab.IRI("http://example.com/1"), vocab.IRI("http://example.com/2")},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r, err := initBadgerForTesting(t)
if err != nil {
t.Errorf("Unable to initialize boltdb: %s", err)
}
if _, err = r.Create(orderedCollection(tt.args.col)); err != nil {
t.Errorf("unable to create collection %s: %s", tt.args.it, err)
}
for _, it := range tt.args.it {
mock := vocab.Object{ID: it.GetLink()}
if _, err = r.Save(mock); err != nil {
t.Errorf("unable to save %s: %s", tt.args.it, err)
}
if err := r.AddTo(tt.args.col, it); (err != nil) != tt.wantErr {
t.Errorf("AddTo() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr {
return
}
}
res, err := r.Load(tt.args.col.GetLink())
if err != nil {
t.Errorf("unable to load %s: %s", tt.args.col, err)
}
for _, expected := range tt.args.it {
err := vocab.OnCollectionIntf(res, func(col vocab.CollectionInterface) error {
if col.Contains(expected) {
return nil
}
return fmt.Errorf("unable to find expected item in loaded collection: %s", expected.GetLink())
})
if err != nil {
t.Errorf("%s", err)
}
}
})
}
}