-
Notifications
You must be signed in to change notification settings - Fork 11
/
struct_test.go
180 lines (158 loc) · 4.78 KB
/
struct_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
package triplestore
import (
"net"
"testing"
"time"
)
type TestStruct struct {
Name string `predicate:"name"`
Age int `predicate:"age"`
Size int64 `predicate:"size"`
Male bool `predicate:"male"`
Birth time.Time `predicate:"birth"`
Surnames []string `predicate:"surnames"`
Counts []int `predicate:"counts"`
// special cases that should be ignored
NoTag string
Unsupported complex64 `predicate:"complex"`
Pointer *string `predicate:"ptr"`
Slice []complex64 `predicate:"complexes"`
PtrSlice []*string `predicate:"strptr"`
unexported string
}
type MainStruct struct {
Name string `predicate:"name"`
Age int `predicate:"age"`
E Embedded `predicate:"embedded" bnode:""`
}
type OtherStruct struct {
Name string `predicate:"name"`
Age int `predicate:"age"`
E Embedded `predicate:"embedded" bnode:"dimension"`
}
type Embedded struct {
Size int64 `predicate:"size"`
Male bool `predicate:"male"`
}
func TestEmbeddedStructToTriple(t *testing.T) {
t.Run("name bnode", func(t *testing.T) {
e := Embedded{Size: 186, Male: true}
s := OtherStruct{Name: "donald", Age: 32, E: e}
tris := TriplesFromStruct("me", s)
src := NewSource()
src.Add(tris...)
snap := src.Snapshot()
if got, want := snap.Count(), 5; got != want {
t.Fatalf("got %d, want %d", got, want)
}
all := snap.WithSubjPred("me", "embedded")
if got, want := len(all), 1; got != want {
t.Fatalf("got %d, want %d", got, want)
}
all = snap.WithPredObj("size", IntegerLiteral(186))
if got, want := len(all), 1; got != want {
t.Fatalf("got %d, want %d", got, want)
}
all = snap.WithPredObj("male", BooleanLiteral(true))
if got, want := len(all), 1; got != want {
t.Fatalf("got %d, want %d", got, want)
}
if tri := SubjPred("me", "embedded").Bnode("dimension"); !snap.Contains(tri) {
t.Fatalf("snap should contains %v", tri)
}
if tri := BnodePred("dimension", "male").BooleanLiteral(true); !snap.Contains(tri) {
t.Fatalf("snap should contains %v", tri)
}
if tri := BnodePred("dimension", "size").IntegerLiteral(186); !snap.Contains(tri) {
t.Fatalf("snap should contains %v", tri)
}
})
t.Run("random bnode", func(t *testing.T) {
e := Embedded{Size: 186, Male: true}
s := MainStruct{Name: "donald", Age: 32, E: e}
tris := TriplesFromStruct("me", s)
src := NewSource()
src.Add(tris...)
snap := src.Snapshot()
if got, want := snap.Count(), 5; got != want {
t.Fatalf("got %d, want %d", got, want)
}
all := snap.WithSubjPred("me", "embedded")
if got, want := len(all), 1; got != want {
t.Fatalf("got %d, want %d", got, want)
}
all = snap.WithPredObj("size", IntegerLiteral(186))
if got, want := len(all), 1; got != want {
t.Fatalf("got %d, want %d", got, want)
}
all = snap.WithPredObj("male", BooleanLiteral(true))
if got, want := len(all), 1; got != want {
t.Fatalf("got %d, want %d", got, want)
}
})
}
func TestSimpleStructToTriple(t *testing.T) {
now := time.Now()
s := TestStruct{
Name: "donald", Age: 32, Size: 186,
Male: true, Birth: now,
Surnames: []string{"one", "two", "three"},
Counts: []int{1, 2, 3},
}
exp := []Triple{
SubjPred("me", "name").StringLiteral("donald"),
SubjPred("me", "age").IntegerLiteral(32),
SubjPred("me", "size").IntegerLiteral(186),
SubjPred("me", "male").BooleanLiteral(true),
SubjPred("me", "birth").DateTimeLiteral(now),
SubjPred("me", "surnames").StringLiteral("one"),
SubjPred("me", "surnames").StringLiteral("two"),
SubjPred("me", "surnames").StringLiteral("three"),
SubjPred("me", "counts").IntegerLiteral(1),
SubjPred("me", "counts").IntegerLiteral(2),
SubjPred("me", "counts").IntegerLiteral(3),
}
tris := TriplesFromStruct("me", s)
if got, want := Triples(tris), Triples(exp); !got.Equal(want) {
t.Fatalf("got %s\n\n want %s", got, want)
}
tris = TriplesFromStruct("me", &s)
if got, want := Triples(tris), Triples(exp); !got.Equal(want) {
t.Fatalf("got %s\n\n want %s", got, want)
}
}
func TestReturnEmptyTriplesOnNonStructElem(t *testing.T) {
var ptr *string
var strPtr *stringer
var ipnet *net.IPNet
tcases := []struct {
Val interface{} `predicate:"anything"`
}{
{true}, {"any"}, {ptr}, {strPtr}, {ipnet},
}
for i, tc := range tcases {
tris := TriplesFromStruct("", tc.Val)
if len(tris) != 0 {
t.Fatalf("case %d: expected no triples", i+1)
}
}
}
func TestReturnEmptyTriplesOnVoidPointers(t *testing.T) {
type anyStruct struct {
Val interface{} `predicate:"anything"`
}
var ptr *string
var strPtr *stringer
var ipnet *net.IPNet
tcases := []struct {
st anyStruct
}{
{anyStruct{ptr}}, {anyStruct{strPtr}}, {anyStruct{ipnet}},
}
for i, tc := range tcases {
tris := TriplesFromStruct("", tc.st)
if len(tris) != 0 {
t.Fatalf("case %d: expected no triples", i+1)
}
}
}