-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtypes_mapping_test.go
226 lines (213 loc) · 12.7 KB
/
types_mapping_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
package neoql
import (
"gopkg.in/neoql.v1/types"
"gopkg.in/packstream.v1"
"reflect"
"testing"
)
func TestRecordToType(t *testing.T) {
if v, err := recordToType(42); err != nil {
t.Error(err)
} else if v != 42 {
t.Errorf("invalid value, expected %v got %v.", 42, v)
}
m := map[string]interface{}{"test1": 1, "test2": 2}
if v, err := recordToType(m); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(m, v) {
t.Errorf("invalid value, expected %v got %v.", m, v)
}
l := []interface{}{"test1", "test2"}
if v, err := recordToType(l); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(l, v) {
t.Errorf("invalid value, expected %v got %v.", m, v)
}
if _, err := recordToType(*packstream.NewStructure(0)); err == nil {
t.Error("error should not be nil when passing invalid structure")
}
if _, err := recordToType(*packstream.NewStructure("N"[0])); err == nil {
t.Error("error should not be nil when passing invalid structure")
}
}
func TestStructRecordToType(t *testing.T) {
if _, err := recordToType(*packstream.NewStructure(0)); err == nil {
t.Error("error should not be nil when passing invalid structure")
}
if v, err := recordToType(*packstream.NewStructure("N"[0], int64(42), []interface{}{"label"}, map[string]interface{}{"prop": "value"})); err != nil {
t.Error(err)
} else if _, ok := v.(*types.Node); !ok {
t.Error("returned value should be a node.")
}
}
func TestHydrateNode(t *testing.T) {
node := new(types.Node)
if err := hydrateNode(node, packstream.NewStructure("N"[0], int64(42), []interface{}{"label"}, map[string]interface{}{"prop": "value"})); err != nil {
t.Error(err)
} else if node.ID != 42 {
t.Errorf("node has invalid ID, expected %v, got %v", 42, node.ID)
} else if node.Label != "label" {
t.Errorf("node has invalid label, expected %v, got %v", "label", node.Label)
} else if prop, ok := node.Properties["prop"]; !ok {
t.Error("node should have property prop.")
} else if prop != "value" {
t.Errorf("node prop has invalid value, expected %v, got %v.", "value", prop)
}
if err := hydrateNode(node, packstream.NewStructure("N"[0], int64(42), []interface{}{"label"})); err == nil {
t.Error("error should not be nil when structure does not have enough fields.")
}
if err := hydrateNode(node, packstream.NewStructure("N"[0], "string", []interface{}{"label"}, map[string]interface{}{"prop": "value"})); err == nil {
t.Error("error should not be nil when structure first field is not an int64.")
}
if err := hydrateNode(node, packstream.NewStructure("N"[0], int64(42), "label", map[string]interface{}{"prop": "value"})); err == nil {
t.Error("error should not be nil when structure second field is not a list.")
}
if err := hydrateNode(node, packstream.NewStructure("N"[0], int64(42), []interface{}{42}, map[string]interface{}{"prop": "value"})); err == nil {
t.Error("error should not be nil when structure second field is not a list of string.")
}
if err := hydrateNode(node, packstream.NewStructure("N"[0], int64(42), []interface{}{"label"}, 42)); err == nil {
t.Error("error should not be nil when structure third field is not a map.")
}
}
func TestHydrateRelationship(t *testing.T) {
rs := new(types.Relationship)
if err := hydrateRelationship(rs, packstream.NewStructure("R"[0], int64(1), int64(2), int64(3), "label", map[string]interface{}{"prop": "value"})); err != nil {
t.Error(err)
} else if rs.ID != 1 {
t.Errorf("rs has invalid ID, expected %v, got %v", 1, rs.ID)
} else if rs.FromID != 2 {
t.Errorf("rs has invalid StartID, expected %v, got %v", 3, rs.FromID)
} else if rs.ToID != 3 {
t.Errorf("rs has invalid EndID, expected %v, got %v", 3, rs.ToID)
} else if rs.Type != "label" {
t.Errorf("rs has invalid type, expected %v, got %v", "label", rs.Type)
} else if prop, ok := rs.Properties["prop"]; !ok {
t.Error("rs should have property prop.")
} else if prop != "value" {
t.Errorf("rs prop has invalid value, expected %v, got %v.", "value", prop)
}
if err := hydrateRelationship(rs, packstream.NewStructure("R"[0], int64(2), int64(3), "label", map[string]interface{}{"prop": "value"})); err == nil {
t.Error("error should not be nil when structure does not have enough fields.")
}
if err := hydrateRelationship(rs, packstream.NewStructure("R"[0], "hello", int64(2), int64(3), "label", map[string]interface{}{"prop": "value"})); err == nil {
t.Error("error should not be nil when structure field 1 is not an int64.")
}
if err := hydrateRelationship(rs, packstream.NewStructure("R"[0], int64(1), "hello", int64(3), "label", map[string]interface{}{"prop": "value"})); err == nil {
t.Error("error should not be nil when structure field 2 is not an int64.")
}
if err := hydrateRelationship(rs, packstream.NewStructure("R"[0], int64(1), int64(2), "hello", "label", map[string]interface{}{"prop": "value"})); err == nil {
t.Error("error should not be nil when structure field 3 is not an int64.")
}
if err := hydrateRelationship(rs, packstream.NewStructure("R"[0], int64(1), int64(2), int64(3), 42, map[string]interface{}{"prop": "value"})); err == nil {
t.Error("error should not be nil when structure field 4 is not a string.")
}
if err := hydrateRelationship(rs, packstream.NewStructure("R"[0], int64(1), int64(2), int64(3), "label", 42)); err == nil {
t.Error("error should not be nil when structure field 5 is not a map.")
}
}
func TestHydrateUnboundRelationship(t *testing.T) {
rs := new(types.UnboundRelationship)
if err := hydrateUnboundRelationship(rs, packstream.NewStructure("r"[0], int64(42), "label", map[string]interface{}{"prop": "value"})); err != nil {
t.Error(err)
} else if rs.ID != 42 {
t.Errorf("rs has invalid ID, expected %v, got %v", 42, rs.ID)
} else if rs.Type != "label" {
t.Errorf("rs has invalid label, expected %v, got %v", "label", rs.Type)
} else if prop, ok := rs.Properties["prop"]; !ok {
t.Error("rs should have property prop.")
} else if prop != "value" {
t.Errorf("rs prop has invalid value, expected %v, got %v.", "value", prop)
}
if err := hydrateUnboundRelationship(rs, packstream.NewStructure("r"[0], int64(42), "label")); err == nil {
t.Error("error should not be nil when structure does not have enough fields.")
}
if err := hydrateUnboundRelationship(rs, packstream.NewStructure("r"[0], "string", "label", map[string]interface{}{"prop": "value"})); err == nil {
t.Error("error should not be nil when structure first field is not an int64.")
}
if err := hydrateUnboundRelationship(rs, packstream.NewStructure("r"[0], int64(42), 42, map[string]interface{}{"prop": "value"})); err == nil {
t.Error("error should not be nil when structure second field is not a string.")
}
if err := hydrateUnboundRelationship(rs, packstream.NewStructure("r"[0], int64(42), []interface{}{"label"}, 42)); err == nil {
t.Error("error should not be nil when structure third field is not a map.")
}
}
func TestHydratePath(t *testing.T) {
p := new(types.Path)
n1 := *packstream.NewStructure("N"[0], int64(1), []interface{}{"label"}, map[string]interface{}{"prop": "value"})
n2 := *packstream.NewStructure("N"[0], int64(2), []interface{}{"label"}, map[string]interface{}{"prop": "value"})
n3 := *packstream.NewStructure("N"[0], int64(3), []interface{}{"label"}, map[string]interface{}{"prop": "value"})
urs := *packstream.NewStructure("r"[0], int64(101), "label", map[string]interface{}{"prop": "value"})
urs2 := *packstream.NewStructure("r"[0], int64(102), "label", map[string]interface{}{"prop": "value"})
if err := hydratePath(p, packstream.NewStructure("P"[0], []interface{}{n1, n2, n3}, []interface{}{urs, urs2}, []interface{}{int64(1), int64(1), int64(2), int64(2)})); err != nil {
t.Error(err)
} else if len(p.Nodes) != 3 {
t.Errorf("expected %v nodes, got %v.", 3, len(p.Nodes))
} else if p.Nodes[0].ID != 1 {
t.Errorf("invalid node 0, expected ID %v got %v.", 1, p.Nodes[0].ID)
} else if p.Nodes[1].ID != 2 {
t.Errorf("invalid node 1, expected ID %v got %v.", 2, p.Nodes[1].ID)
} else if p.Nodes[2].ID != 3 {
t.Errorf("invalid node 2, expected ID %v got %v.", 3, p.Nodes[2].ID)
} else if len(p.Relationships) != 2 {
t.Errorf("expected %v relationships, got %v.", 2, len(p.Relationships))
} else if p.Relationships[0].FromID != 1 {
t.Errorf("invalid relationship 0, expected StartID %v got %v.", 1, p.Relationships[0].FromID)
} else if p.Relationships[0].From != p.Nodes[0] {
t.Errorf("invalid relationship 0, expected Start %v got %v.", p.Nodes[0], p.Relationships[0].From)
} else if p.Relationships[0].ToID != 2 {
t.Errorf("invalid relationship 0, expected EndID %v got %v.", 2, p.Relationships[0].ToID)
} else if p.Relationships[0].End != p.Nodes[1] {
t.Errorf("invalid relationship 0, expected End %v got %v.", p.Nodes[1], p.Relationships[0].End)
} else if p.Relationships[1].FromID != 2 {
t.Errorf("invalid relationship 1, expected StartID %v got %v.", 2, p.Relationships[1].FromID)
} else if p.Relationships[1].From != p.Nodes[1] {
t.Errorf("invalid relationship 1, expected Start %v got %v.", p.Nodes[1], p.Relationships[1].From)
} else if p.Relationships[1].ToID != 3 {
t.Errorf("invalid relationship 1, expected EndID %v got %v.", 3, p.Relationships[1].ToID)
} else if p.Relationships[1].End != p.Nodes[2] {
t.Errorf("invalid relationship 1, expected End %v got %v.", p.Nodes[2], p.Relationships[1].End)
}
// Failures
if err := hydratePath(p, packstream.NewStructure("P"[0], []interface{}{n1, n2, n3}, []interface{}{urs, urs2})); err == nil {
t.Error("error should not be nil when structure does not have enough fields.")
}
if err := hydratePath(p, packstream.NewStructure("P"[0], "string", []interface{}{urs, urs2}, []interface{}{int64(1), int64(1), int64(2), int64(2)})); err == nil {
t.Error("error should not be nil when structure field 1 is not a list.")
}
if err := hydratePath(p, packstream.NewStructure("P"[0], []interface{}{"string"}, []interface{}{urs, urs2}, []interface{}{int64(1), int64(1), int64(2), int64(2)})); err == nil {
t.Error("error should not be nil when structure field 1 is not a list of structures.")
}
if err := hydratePath(p, packstream.NewStructure("P"[0], []interface{}{urs}, []interface{}{urs, urs2}, []interface{}{int64(1), int64(1), int64(2), int64(2)})); err == nil {
t.Error("error should not be nil when structure field 1 is not a list of nodes.")
}
if err := hydratePath(p, packstream.NewStructure("P"[0], []interface{}{n1, n2, n3}, "string", []interface{}{int64(1), int64(1), int64(2), int64(2)})); err == nil {
t.Error("error should not be nil when structure field 2 is not a list.")
}
if err := hydratePath(p, packstream.NewStructure("P"[0], []interface{}{n1, n2, n3}, []interface{}{"string"}, []interface{}{int64(1), int64(1), int64(2), int64(2)})); err == nil {
t.Error("error should not be nil when structure field 2 is not a list of structures.")
}
if err := hydratePath(p, packstream.NewStructure("P"[0], []interface{}{n1, n2, n3}, []interface{}{n1}, []interface{}{int64(1), int64(1), int64(2), int64(2)})); err == nil {
t.Error("error should not be nil when structure field 2 is not a list of relationships.")
}
if err := hydratePath(p, packstream.NewStructure("P"[0], []interface{}{n1, n2, n3}, []interface{}{urs, urs2}, "string")); err == nil {
t.Error("error should not be nil when structure field 3 is not a list.")
}
if err := hydratePath(p, packstream.NewStructure("P"[0], []interface{}{n1, n2, n3}, []interface{}{urs, urs2}, []interface{}{"string"})); err == nil {
t.Error("error should not be nil when structure field 3 is not a list of integers.")
}
if err := hydratePath(p, packstream.NewStructure("P"[0], []interface{}{n1, n2, n3}, []interface{}{urs, urs2}, []interface{}{int64(1)})); err == nil {
t.Error("error should not be nil when structure field 3 is not a list length is not divisible by 2.")
}
if err := hydratePath(p, packstream.NewStructure("P"[0], []interface{}{n1, n2, n3}, []interface{}{urs, urs2}, []interface{}{int64(42), int64(1), int64(2), int64(2)})); err == nil {
t.Error("error should not be nil when structure field 3 is not a valid sequence.")
}
if err := hydratePath(p, packstream.NewStructure("P"[0], []interface{}{n1, n2, n3}, []interface{}{urs, urs2}, []interface{}{int64(1), int64(42), int64(2), int64(2)})); err == nil {
t.Error("error should not be nil when structure field 3 is not a valid sequence.")
}
if err := hydratePath(p, packstream.NewStructure("P"[0], []interface{}{n1, n2, n3}, []interface{}{urs, urs2}, []interface{}{int64(1), int64(1), int64(42), int64(2)})); err == nil {
t.Error("error should not be nil when structure field 3 is not a valid sequence.")
}
if err := hydratePath(p, packstream.NewStructure("P"[0], []interface{}{n1, n2, n3}, []interface{}{urs, urs2}, []interface{}{int64(1), int64(1), int64(2), int64(42)})); err == nil {
t.Error("error should not be nil when structure field 3 is not a valid sequence.")
}
}