-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsakuin_test.go
242 lines (204 loc) · 5.17 KB
/
sakuin_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
package sakuin
import (
"bytes"
"context"
"crypto/rand"
"strings"
"testing"
pb "github.com/z5labs/sakuin/proto"
)
func TestGetObject(t *testing.T) {
objStore := NewInMemoryObjectStore()
testObjectID := "testObject"
testObjectContent := []byte("test content")
err := objStore.Put(context.Background(), testObjectID, testObjectContent)
if err != nil {
t.Error(err)
return
}
s := New(Config{
ObjectStore: objStore,
})
t.Run("should fail if ID doesn't exist", func(subT *testing.T) {
_, err := s.GetObject(context.Background(), &pb.GetObjectRequest{
Id: "",
})
if _, ok := err.(ObjectDoesNotExistErr); err == nil || !ok {
subT.Log("expected error since object with given id doesn't exist")
subT.Fail()
return
}
})
t.Run("should succeed if object exists", func(subT *testing.T) {
resp, err := s.GetObject(context.Background(), &pb.GetObjectRequest{
Id: testObjectID,
})
if err != nil {
subT.Error(err)
return
}
if !bytes.Equal(testObjectContent, resp.Content) {
subT.Logf("expected object content to match\n\texpected: %s\n\tactual: %s", testObjectContent, resp.Content)
subT.Fail()
return
}
})
}
func TestUpdateObject(t *testing.T) {
t.Run("should fail if ID doesn't exist", func(subT *testing.T) {
s := New(Config{
ObjectStore: NewInMemoryObjectStore(),
})
_, err := s.UpdateObject(context.Background(), &pb.UpdateObjectRequest{
Id: "objectDoesNotExistID",
})
if _, ok := err.(ObjectDoesNotExistErr); err == nil || !ok {
subT.Log("expected error since object with given id doesn't exist")
subT.Fail()
return
}
})
t.Run("should succeed if object exists", func(subT *testing.T) {
objStore := NewInMemoryObjectStore()
testObjectID := "testObject"
testObjectContent := []byte("test content")
err := objStore.Put(context.Background(), testObjectID, testObjectContent)
if err != nil {
t.Error(err)
return
}
s := New(Config{
ObjectStore: objStore,
})
resp, err := s.GetObject(context.Background(), &pb.GetObjectRequest{
Id: testObjectID,
})
if err != nil {
subT.Error(err)
return
}
if !bytes.Equal(testObjectContent, resp.Content) {
subT.Logf("expected object content to match\n\texpected: %s\n\tactual: %s", testObjectContent, resp.Content)
subT.Fail()
return
}
})
}
func TestGetMetadata(t *testing.T) {
docStore := NewInMemoryDocumentStore()
testGoodDocID := "testGoodDoc"
testGoodDoc := map[string]interface{}{
"name": "test",
"description": "test description",
}
err := docStore.Upsert(context.Background(), testGoodDocID, testGoodDoc)
if err != nil {
t.Error(err)
return
}
s := New(Config{
DocumentStore: docStore,
})
t.Run("should fail if ID doesn't exist", func(subT *testing.T) {
_, err := s.GetMetadata(context.Background(), &pb.GetMetadataRequest{
Id: "",
})
if _, ok := err.(DocumentDoesNotExistErr); err == nil || !ok {
subT.Log("expected error since document with given id doesn't exist")
subT.Fail()
return
}
})
t.Run("should succeed if doc exists", func(subT *testing.T) {
resp, err := s.GetMetadata(context.Background(), &pb.GetMetadataRequest{
Id: testGoodDocID,
})
if err != nil {
subT.Error(err)
return
}
metadata, err := unmarshalAnyToJSON(resp.Metadata)
if err != nil {
subT.Error(err)
return
}
if testGoodDoc["name"] != metadata["name"] {
subT.Logf("expected name to match\n\texpected: %s\n\tactual: %s", testGoodDoc["name"], metadata["name"])
subT.Fail()
return
}
if testGoodDoc["description"] != metadata["description"] {
subT.Logf("expected description to match\n\texpected: %s\n\tactual: %s", testGoodDoc["description"], metadata["description"])
subT.Fail()
return
}
})
}
func TestIndex(t *testing.T) {
objStore := NewInMemoryObjectStore()
docStore := NewInMemoryDocumentStore()
t.Run("should succeed", func(subT *testing.T) {
s := New(Config{
ObjectStore: objStore,
DocumentStore: docStore,
RandSrc: rand.Reader,
})
metadata, err := marshalJSONToAny(map[string]interface{}{
"name": "test",
"description": "test",
})
if err != nil {
subT.Error(err)
return
}
resp, err := s.Index(context.Background(), &pb.IndexRequest{
Metadata: metadata,
Object: []byte("test object content"),
})
if err != nil {
subT.Error(err)
return
}
if resp.Id == "" {
subT.Fail()
return
}
if objStore.NumOfObects() != 1 {
subT.Fail()
return
}
if docStore.NumOfDocs() != 1 {
subT.Fail()
return
}
})
t.Run("should succeed even if uuid already exists in db", func(subT *testing.T) {
same := "0123456789ABCDEF"
different := "FEDBCA9876543210"
s := New(Config{
ObjectStore: objStore,
DocumentStore: docStore,
RandSrc: strings.NewReader(same + same + different),
})
metadata, err := marshalJSONToAny(map[string]interface{}{
"name": "test",
"description": "test",
})
if err != nil {
subT.Error(err)
return
}
resp, err := s.Index(context.Background(), &pb.IndexRequest{
Metadata: metadata,
Object: []byte("test object content"),
})
if err != nil {
subT.Error(err)
return
}
if resp.Id == "" {
subT.Fail()
return
}
})
}