-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository_test.go
174 lines (161 loc) · 3.85 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
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
package boltdb
import (
"fmt"
"os"
"testing"
vocab "github.com/go-ap/activitypub"
)
func TestNew(t *testing.T) {
dir := os.TempDir()
conf := Config{
Path: dir,
LogFn: func(s string, p ...interface{}) { t.Logf(s, p...) },
ErrFn: func(s string, p ...interface{}) { t.Errorf(s, p...) },
}
repo, _ := New(conf)
if repo == nil {
t.Errorf("Nil result from opening boltdb %s", repo.path)
}
if repo.d != nil {
t.Errorf("Non nil boltdb from New")
}
if repo.errFn == nil {
t.Errorf("Nil error log function, expected %T[%p]", t.Errorf, t.Errorf)
}
if repo.logFn == nil {
t.Errorf("Nil log function, expected %T[%p]", t.Logf, t.Logf)
}
}
func TestRepo_Open(t *testing.T) {
dir := os.TempDir()
conf := Config{Path: dir}
path, _ := Path(conf)
err := Bootstrap(conf)
if err != nil {
t.Errorf("Unable to bootstrap boltdb %s: %s", path, err)
}
defer os.Remove(path)
repo, err := New(conf)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
err = repo.Open()
if err != nil {
t.Errorf("Unable to open boltdb %s: %s", path, err)
}
if repo.d == nil {
t.Errorf("Nil %T for path %s", repo.d, path)
}
}
func TestRepo_Close(t *testing.T) {
dir := os.TempDir()
conf := Config{
Path: dir,
}
path, _ := Path(conf)
err := Bootstrap(conf)
if err != nil {
t.Errorf("Unable to bootstrap boltdb %s: %s", path, err)
}
defer os.Remove(path)
repo, err := New(conf)
if err != nil {
t.Errorf("Error initializing db: %s", err)
}
err = repo.Open()
if err != nil {
t.Errorf("Unable to open boltdb %s: %s", path, err)
}
err = repo.close()
if err != nil {
t.Errorf("Unable to close boltdb %s: %s", path, err)
}
os.Remove(path)
}
func TestRepo_Load(t *testing.T) {
t.Skipf("TODO")
}
func initBoltDBForTesting(t *testing.T) (*repo, error) {
c := Config{Path: t.TempDir()}
Bootstrap(c)
r, err := New(c)
if err != nil {
return nil, fmt.Errorf("invalid path for initializing boltdb %s: %s", c.Path, err)
}
t.Logf("Initialized test db at %s", r.path)
return r, nil
}
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 := initBoltDBForTesting(t)
if err != nil {
t.Errorf("Unable to initialize boltdb: %s", err)
}
for _, it := range tt.args.it {
toCheck := vocab.Object{ID: it.GetLink()}
if _, err = r.Save(toCheck); 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)
}
if !res.IsCollection() {
t.Errorf("Received response is not a collection: %v", res)
}
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)
}
}
})
}
}