forked from lmika/oaipmh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecordsearch_test.go
136 lines (110 loc) · 4.21 KB
/
recordsearch_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
package main
import "testing"
// Test parsing of a search predicate
func TestParseXpath(t *testing.T) {
_, err := ParseRecordMatchExpr(`xp("/a/b/c")`)
if err != nil {
t.Error(err)
}
}
// Test the search predicate.
func TestXPMatch(t *testing.T) {
expr := `xp("/a/b/c")`
rs, err := ParseRecordMatchExpr(expr)
if err != nil {
t.Error(err)
}
rec1 := &RecordResult{}
rec1.Content = "<gmd:a xmlns:gmd=\"something\"><val>Some value</val><b><d>Something</d><c>I've got C</c></b></gmd:a>"
rec2 := &RecordResult{}
rec2.Content = "<gmd:a xmlns:gmd=\"something\"><val>Some value</val><b><d>No C Here</d></b></gmd:a>"
if r, v, _ := rs.SearchRecord(rec1) ; !(r && (v=="I've got C")) {
t.Error("rec1 must match")
}
if r, _, _ := rs.SearchRecord(rec2) ; !(!r) {
t.Error("rec2 must not match")
}
}
// Test the start with predicate
func TestStartWith(t *testing.T) {
expr := `startsWith(xp("/a/val"), "Some")`
rs, err := ParseRecordMatchExpr(expr)
if err != nil {
t.Error(err)
}
rec1 := &RecordResult{}
rec1.Content = "<gmd:a xmlns:gmd=\"something\"><val>Some value</val><b><d>Something</d><c>I've got C</c></b></gmd:a>"
rec2 := &RecordResult{}
rec2.Content = "<gmd:a xmlns:gmd=\"something\"><val>Another value</val><b><d>No C Here</d></b></gmd:a>"
if r, v, _ := rs.SearchRecord(rec1) ; !(r && (v=="Some value")) {
t.Error("rec1 must match")
}
if r, v, _ := rs.SearchRecord(rec2) ; !(!r && (v=="")) {
t.Error("rec2 must not match")
}
}
// Test the contains predicate
func TestContains(t *testing.T) {
expr := `contains(xp("/a/val"), "value")`
rs, err := ParseRecordMatchExpr(expr)
if err != nil {
t.Error(err)
}
rec1 := &RecordResult{}
rec1.Content = "<gmd:a xmlns:gmd=\"something\"><val>Some value</val><b><d>Something</d><c>I've got C</c></b></gmd:a>"
rec2 := &RecordResult{}
rec2.Content = "<gmd:a xmlns:gmd=\"something\"><val>Another value</val><b><d>No C Here</d></b></gmd:a>"
if r, v, _ := rs.SearchRecord(rec1) ; !(r && (v=="Some value")) {
t.Error("rec1 must match")
}
if r, v, _ := rs.SearchRecord(rec2) ; !(r && (v=="Another value")) {
t.Error("rec2 must not match")
}
}
// Test the urn predicate
func TestUrnPredicate(t *testing.T) {
expr := `urn`
rs, err := ParseRecordMatchExpr(expr)
if err != nil {
t.Error(err)
}
rec1 := &RecordResult{}
rec1.Header.Identifier = "abc123"
rec1.Content = "<gmd:a xmlns:gmd=\"something\"><val>Some value</val><b><d>Something</d><c>I've got C</c></b></gmd:a>"
rec2 := &RecordResult{}
rec2.Header.Identifier = "foobar"
rec2.Content = "<gmd:a xmlns:gmd=\"something\"><val>Another value</val><b><d>No C Here</d></b></gmd:a>"
if r, v, _ := rs.SearchRecord(rec1) ; !(r && (v=="abc123")) {
t.Error("rec1 must match")
}
if r, v, _ := rs.SearchRecord(rec2) ; !(r && (v=="foobar")) {
t.Error("rec2 must not match")
}
}
// Test the nonEmpty predicate
func TestNonEmptyValues(t *testing.T) {
assertSearchExpr(t, `"This is not empty"`, "<xml></xml>", true, "This is not empty")
assertSearchExpr(t, `"")`, "<xml></xml>", false, "")
assertSearchExpr(t, `xp("/xml/value")`, "<xml>This has<value>a value</value> set</xml>", true, "a value")
assertSearchExpr(t, `xp("/xml/missing")`, "<xml>This has<value>a value</value> set</xml>", false, "")
assertSearchExpr(t, `xp("/xml/value")`, "<xml>This has<value attr=\"some attribute\" /> set</xml>", false, "")
assertSearchExpr(t, `xp("/xml/value/@attr")`, "<xml>This has<value attr=\"some attribute\" /> set</xml>", true, "some attribute")
}
func assertSearchExpr(t *testing.T, expr string, xml string, expectedVal bool, expectedValue string) {
rs, err := ParseRecordMatchExpr(expr)
if err != nil {
t.Error(err)
return
}
rec1 := &RecordResult{}
rec1.Content = xml
r, v, _ := rs.SearchRecord(rec1)
if r != expectedVal {
t.Error("Result of expression must be %v but was %v", expectedVal, r)
return
}
if v != expectedValue {
t.Error("Value of expression must be %v but was %v", expectedValue, v)
return
}
}