-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsparql_test.go
122 lines (106 loc) · 3.94 KB
/
sparql_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
package sparql
import (
"bytes"
"testing"
"github.com/knakk/rdf"
)
const testResults = `
{
"head": {
"link": [],
"vars": [ "x", "hpage", "name", "mbox", "age", "friend", "score", "z", "updated" ]
},
"results": {
"bindings": [
{
"x" : { "type": "bnode", "value": "r1" },
"hpage" : { "type": "uri", "value": "http://work.example.org/alice/" },
"name" : { "type": "literal", "value": "Alice" } ,
"friend" : { "type": "bnode", "value": "r2" },
"age": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#integer", "value": "17" },
"score": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#float", "value": "0.2" },
"z": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#boolean", "value": "true" },
"updated": {
"type": "typed-literal",
"datatype": "http://www.w3.org/2001/XMLSchema#dateTime",
"value": "2014-07-21T04:00:40+02:00"
}
},
{
"x" : { "type": "bnode", "value": "r2" },
"hpage" : { "type": "uri", "value": "http://work.example.org/bob/" },
"name" : { "type": "literal", "value": "Bob", "xml:lang": "en" },
"mbox" : { "type": "uri", "value": "mailto:[email protected]" },
"friend" : { "type": "bnode", "value": "r1" },
"age": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#integer", "value": "43" },
"score": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#float", "value": "11.93" },
"z": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#boolean", "value": "false" }
}
]
}
}`
func TestParseJSON(t *testing.T) {
b := bytes.NewBufferString(testResults)
r, err := ParseJSON(b)
if err != nil {
t.Fatal(err)
}
if len(r.Results.Bindings) != 2 {
t.Errorf("Got %d solutions, want 2", len(r.Results.Bindings))
}
if len(r.Head.Vars) != 9 {
t.Errorf("Got %d vars in head, want 9", len(r.Head.Vars))
}
}
func TestBindings(t *testing.T) {
b := bytes.NewBufferString(testResults)
r, err := ParseJSON(b)
if err != nil {
t.Fatal(err)
}
bi := r.Bindings()
if len(bi) != 9 {
t.Errorf("Got %d bound variables, want 9", len(bi))
}
if len(bi["x"]) != 2 {
t.Errorf("Got %d bindings for x; want 2", len(bi["x"]))
}
if len(bi["updated"]) != 1 {
t.Errorf("Got %d bindings for updated; want 1", len(bi["updated"]))
}
}
func TestSolutions(t *testing.T) {
b := bytes.NewBufferString(testResults)
r, err := ParseJSON(b)
if err != nil {
t.Fatal(err)
}
s := r.Solutions()
blankR1, _ := rdf.NewBlank("r1")
iriAlice, _ := rdf.NewIRI("http://work.example.org/alice/")
litAlice, _ := rdf.NewLiteral("Alice")
litBob, _ := rdf.NewLangLiteral("Bob", "en")
xsdInteger, _ := rdf.NewIRI("http://www.w3.org/2001/XMLSchema#integer")
xsdFloat, _ := rdf.NewIRI("http://www.w3.org/2001/XMLSchema#float")
xsdBoolean, _ := rdf.NewIRI("http://www.w3.org/2001/XMLSchema#boolean")
xsdDateTime, _ := rdf.NewIRI("http://www.w3.org/2001/XMLSchema#dateTime")
var tests = []struct {
got rdf.Term
want rdf.Term
}{
{s[0]["x"], blankR1},
{s[0]["hpage"], iriAlice},
{s[0]["name"], litAlice},
{s[1]["name"], litBob},
{s[0]["age"], rdf.NewTypedLiteral("17", xsdInteger)},
{s[0]["score"], rdf.NewTypedLiteral("0.2", xsdFloat)},
{s[0]["z"], rdf.NewTypedLiteral("true", xsdBoolean)},
{s[1]["z"], rdf.NewTypedLiteral("false", xsdBoolean)},
{s[0]["updated"], rdf.NewTypedLiteral("2014-07-21T04:00:40+02:00", xsdDateTime)},
}
for _, tt := range tests {
if !rdf.TermsEqual(tt.got, tt.want) {
t.Errorf("Got \"%v\", want \"%v\"", tt.got, tt.want)
}
}
}