-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata_test.go
343 lines (310 loc) · 9.37 KB
/
metadata_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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/blevesearch/bleve"
"github.com/knakk/kbp/rdf"
"github.com/knakk/kbp/rdf/memory"
"github.com/knakk/mormor/entity"
)
func mustDecode(s string) *memory.Graph {
g, err := memory.NewFromNTriples(bytes.NewBufferString(s))
if err != nil {
panic("mustDecode: " + err.Error())
}
return g
}
func mustEncode(g *memory.Graph) string {
var b bytes.Buffer
if err := g.EncodeNTriples(&b); err != nil {
panic(err)
}
return b.String()
}
func testWantGraph(t *testing.T, method string, url string, body string, wantGraph string) {
req, err := http.NewRequest(method, url, bytes.NewBufferString(body))
if err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("got %v; want 200 OK", resp.Status)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
got := mustDecode(string(b))
want := mustDecode(wantGraph)
if !got.Eq(want) {
t.Fatalf("got:\n%v\nwant:\n%v", mustEncode(got), mustEncode(want))
}
}
func testWantStatus(t *testing.T, method, url, body string, status int) *http.Response {
req, err := http.NewRequest(method, url, bytes.NewBufferString(body))
if err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != status {
t.Fatalf("got %v; want %v", resp.Status, status)
}
return resp
}
func newTestSearchService() *searchService {
index, err := bleve.NewMemOnly(bleve.NewIndexMapping())
if err != nil {
panic(err)
}
return &searchService{
Index: index,
}
}
func testWantSearchResultsToContain(t *testing.T, s *searchService, idx entity.Type, q string, want ...doc) {
timeout := time.After(1 * time.Second)
tick := time.NewTicker(10 * time.Millisecond).C
// Keep trying to query every tick until timeout:
for {
select {
case <-timeout:
t.Fatalf("docs not found in index after 1 second: %+v", want)
case <-tick:
res, err := s.query(idx, q)
if err != nil {
t.Fatal(err)
}
foundN := 0
for _, wantDoc := range want {
for _, gotDoc := range res.Hits {
if wantDoc == gotDoc {
foundN++
}
}
}
if foundN == len(want) {
return
}
}
}
}
// Verify that resources can be created, fetched, updated and deleted (TODO),
// and that the resources are indexed reflecting any changes.
func TestResourceLifecycle(t *testing.T) {
m := &metadataService{
triplestore: memory.NewGraph(),
searchService: newTestSearchService(),
indexingQueue: make(chan rdf.NamedNode),
}
go m.processIndexingQueue()
srv := httptest.NewServer(m)
// Create two resources and find their URIs in Location header
resp := testWantStatus(t, "POST", srv.URL+"/resource/person",
`<> <hasName> "Name" .
<> <hasBirthYear> "1988"^^<http://www.w3.org/2001/XMLSchema#integer> .`,
http.StatusCreated)
personuri := resp.Header.Get("Location")
if personuri == "" {
t.Fatal("URI of created resource not found in Location header")
}
resp = testWantStatus(t, "POST", srv.URL+"/resource/work",
`<> <hasMainTitle> "title" .
<> <hasContribution> _:c .
_:c <hasAgent> <`+personuri+`> .
_:c <hasRole> <author> .`,
http.StatusCreated)
bookuri := resp.Header.Get("Location")
if bookuri == "" {
t.Fatal("URI of created resource not found in Location header")
}
rpl := strings.NewReplacer("bookuri", bookuri, "personuri", personuri)
// Fetch single resource
testWantGraph(t, "GET", srv.URL+"/resource/"+personuri, "",
rpl.Replace(`<personuri> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <Person> .
<personuri> <hasName> "Name" .
<personuri> <hasBirthYear> "1988"^^<http://www.w3.org/2001/XMLSchema#integer> .`))
// Fetch multiple resouces
testWantGraph(t, "GET", srv.URL+"/resource/"+personuri+"+"+bookuri, "",
rpl.Replace(`<personuri> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <Person> .
<personuri> <hasName> "Name" .
<personuri> <hasBirthYear> "1988"^^<http://www.w3.org/2001/XMLSchema#integer> .
<bookuri> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <Work> .
<bookuri> <hasMainTitle> "title" .
<bookuri> <hasContribution> _:c .
_:c <hasAgent> <personuri> .
_:c <hasRole> <author> .`))
// Find resources in index
testWantSearchResultsToContain(t, m.searchService, entity.TypePerson, "Name",
doc{Title: "Name (1988-)", ID: personuri, Type: "Person"})
testWantSearchResultsToContain(t, m.searchService, entity.TypeWork, "Name",
doc{Title: "Name: title", ID: bookuri, Type: "Work"})
// Update resource
testWantStatus(t, "PATCH", srv.URL+"/resource/"+personuri,
rpl.Replace(`- <personuri> <hasBirthYear> "1988"^^<http://www.w3.org/2001/XMLSchema#integer> .
+ <personuri> <hasBirthYear> "1888"^^<http://www.w3.org/2001/XMLSchema#integer> .
+ <personuri> <hasDeathYear> "1958"^^<http://www.w3.org/2001/XMLSchema#integer> .`),
http.StatusOK)
// Verify it's been updated and indexed
testWantGraph(t, "GET", srv.URL+"/resource/"+personuri, "",
rpl.Replace(`<personuri> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <Person> .
<personuri> <hasName> "Name" .
<personuri> <hasBirthYear> "1888"^^<http://www.w3.org/2001/XMLSchema#integer> .
<personuri> <hasDeathYear> "1958"^^<http://www.w3.org/2001/XMLSchema#integer> .`))
testWantSearchResultsToContain(t, m.searchService, entity.TypePerson, "Name",
doc{Title: "Name (1888-1958)", ID: personuri, Type: "Person"})
// Update bnode resource
testWantStatus(t, "PATCH", srv.URL+"/resource/"+bookuri,
rpl.Replace(`- ?c <hasRole> <author> .
+ ?c <hasRole> <editor> .
<bookuri> <hasContribution> ?c .
?c <hasAgent> <personuri> .
?c <hasRole> <author> .`),
http.StatusOK)
// Verify it's been updated
testWantGraph(t, "GET", srv.URL+"/resource/"+bookuri, "",
rpl.Replace(`<bookuri> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <Work> .
<bookuri> <hasMainTitle> "title" .
<bookuri> <hasContribution> _:c .
_:c <hasAgent> <personuri> .
_:c <hasRole> <editor> .`))
// Verify you cannot update resources not "in focus"
testWantStatus(t, "PATCH", srv.URL+"/resource/book1",
rpl.Replace(`- ?c <hasRole> <author> .
+ ?c <hasRole> <editor> .
<book123> <hasContribution> ?c .
?c <hasAgent> <personuri> .
?c <hasRole> <author> .`),
http.StatusBadRequest)
}
// Verify that resources are indexed, kept up to date on changes, and can be retrieved.
func TestIndexingAndSearchingResources(t *testing.T) {
t.Skip()
m := &metadataService{
triplestore: memory.NewGraph(),
searchService: newTestSearchService(),
indexingQueue: make(chan rdf.NamedNode),
}
go m.processIndexingQueue()
srv := httptest.NewServer(m)
// Create resource and find it
resp := testWantStatus(t, "POST", srv.URL+"/resource/person",
`<> <hasName> "Kari Olsen" .`,
http.StatusCreated)
kariID := resp.Header.Get("Location")
if kariID == "" {
t.Fatal("URI of created resource not found in Location header")
}
testWantSearchResultsToContain(t, m.searchService, entity.TypePerson, "Olsen",
doc{Title: "Kari Olsen", ID: kariID})
// Create another resource and find both
resp = testWantStatus(t, "POST", srv.URL+"/resource/person",
`<> <hasName> "Knut Olsen" .`,
http.StatusCreated)
knutID := resp.Header.Get("Location")
if knutID == "" {
t.Fatal("URI of created resource not found in Location header")
}
testWantSearchResultsToContain(t, m.searchService, entity.TypePerson, "Olsen",
doc{Title: "Kari Olsen", ID: kariID},
doc{Title: "Knut Olsen", ID: knutID})
// Update resource and verify the indexed version get's uptdated too
testWantStatus(t, "PATCH", srv.URL+"/resource/"+kariID,
fmt.Sprintf(`
- <%s> <hasName> "Kari Olsen" .
+ <%s> <hasName> "Kari Knutsdatter Olsen" .`, kariID, kariID),
http.StatusOK)
}
func TestOutOfBoundsQ(t *testing.T) {
tests := []struct {
query string
resources []string
want bool
}{
{ // 1
`- <book> ?p ?o .
<book> ?p ?o .`,
[]string{"book"},
false,
},
{ // 2
`- <book> ?p ?o .
<book> ?p ?o .`,
[]string{"books", "book1", "book2"},
true,
},
{ // 3
`- ?book ?p ?o .
<person> <wrote> ?book .
?book ?p ?o .`,
[]string{"person"},
false,
},
{ // 4
`+ ?book <title> "xyz" .
<person> <wrote> ?book .
?book ?p ?o .`,
[]string{"book", "person1"},
true,
},
{ // 5
`- ?s ?p ?o .
?s ?p ?o .`,
[]string{"book/123", "person/456"},
true,
},
{ // 6
`- ?work ?p ?o .
<person> <created> ?book .
?book <isPublicationOf> ?work .
?work ?p ?o .`,
[]string{"person"},
true,
},
{ // 7
`- ?work ?p ?o .
<person> <created> ?book .
?book <isPublicationOf> <work> .
?work ?p ?o .`,
[]string{"person", "work"},
true,
},
{ // 8
`- ?work <title> "x" .
+ ?work <title> "y" .
<person> <created> ?work .
?work <title> "x" .`,
[]string{"person"},
false,
},
{ // 9
`- ?work <title> "x" .
+ ?work <title> "y" .
<person> <created> ?work .
?work <title> "x" .`,
[]string{"work"},
true,
},
}
for i, test := range tests {
del, ins, where, err := rdf.ParseUpdateQuery(test.query)
if err != nil {
t.Fatal(err)
}
if got := outOfBoundsQuery(test.resources, del, ins, where); got != test.want {
t.Errorf("outOfBoundsQuery #%d: got %v; want %v", i+1, got, test.want)
}
}
}