Skip to content

Commit eb62d44

Browse files
authoredSep 5, 2019
Merge pull request #13 from RedisGraph/array_support
added array support
2 parents 0d66f2d + a325fb3 commit eb62d44

File tree

6 files changed

+333
-210
lines changed

6 files changed

+333
-210
lines changed
 

‎client_test.go

+93-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package redisgraph
22

33
import (
4-
"testing"
54
"os"
6-
"github.com/stretchr/testify/assert"
5+
"testing"
6+
77
"github.com/gomodule/redigo/redis"
8+
"github.com/stretchr/testify/assert"
89
)
910

1011
var graph Graph
@@ -24,7 +25,7 @@ func createGraph() {
2425
john.SetProperty("age", 33)
2526
john.SetProperty("gender", "male")
2627
john.SetProperty("status", "single")
27-
28+
2829
japan.SetProperty("name", "Japan")
2930
japan.SetProperty("population", 126800000)
3031

@@ -51,10 +52,10 @@ func shutdown() {
5152
}
5253

5354
func TestMain(m *testing.M) {
54-
setup()
55-
code := m.Run()
56-
shutdown()
57-
os.Exit(code)
55+
setup()
56+
code := m.Run()
57+
shutdown()
58+
os.Exit(code)
5859
}
5960

6061
func TestMatchQuery(t *testing.T) {
@@ -63,7 +64,7 @@ func TestMatchQuery(t *testing.T) {
6364
if err != nil {
6465
t.Error(err)
6566
}
66-
67+
6768
assert.Equal(t, len(res.results), 1, "expecting 1 result record")
6869

6970
s, ok := (res.results[0][0]).(*Node)
@@ -83,7 +84,7 @@ func TestMatchQuery(t *testing.T) {
8384
assert.Equal(t, s.GetProperty("age"), 33, "Unexpected property value.")
8485
assert.Equal(t, s.GetProperty("gender"), "male", "Unexpected property value.")
8586
assert.Equal(t, s.GetProperty("status"), "single", "Unexpected property value.")
86-
87+
8788
assert.Equal(t, e.GetProperty("year"), 2017, "Unexpected property value.")
8889

8990
assert.Equal(t, d.GetProperty("name"), "Japan", "Unexpected property value.")
@@ -96,9 +97,9 @@ func TestCreateQuery(t *testing.T) {
9697
if err != nil {
9798
t.Error(err)
9899
}
99-
100+
100101
assert.True(t, res.Empty(), "Expecting empty result-set")
101-
102+
102103
// Validate statistics.
103104
assert.Equal(t, res.NodesCreated(), 1, "Expecting a single node to be created.")
104105
assert.Equal(t, res.PropertiesSet(), 1, "Expecting a songle property to be added.")
@@ -108,8 +109,88 @@ func TestCreateQuery(t *testing.T) {
108109
if err != nil {
109110
t.Error(err)
110111
}
111-
112+
112113
assert.False(t, res.Empty(), "Expecting resultset to include a single node.")
113114
w := (res.results[0][0]).(*Node)
114115
assert.Equal(t, w.Label, "WorkPlace", "Unexpected node label.")
115116
}
117+
118+
func TestArray(t *testing.T) {
119+
120+
graph.Flush()
121+
graph.Query("MATCH (n) DELETE n")
122+
123+
q := "CREATE (:person{name:'a',age:32,array:[0,1,2]})"
124+
res, err := graph.Query(q)
125+
if err != nil {
126+
t.Error(err)
127+
}
128+
129+
q = "CREATE (:person{name:'b',age:30,array:[3,4,5]})"
130+
res, err = graph.Query(q)
131+
if err != nil {
132+
t.Error(err)
133+
}
134+
135+
q = "WITH [0,1,2] as x return x"
136+
res, err = graph.Query(q)
137+
if err != nil {
138+
t.Error(err)
139+
}
140+
141+
assert.Equal(t, len(res.results), 1, "expecting 1 result record")
142+
143+
assert.Equal(t, []interface{}{0, 1, 2}, res.results[0][0])
144+
145+
q = "unwind([0,1,2]) as x return x"
146+
res, err = graph.Query(q)
147+
if err != nil {
148+
t.Error(err)
149+
}
150+
assert.Equal(t, len(res.results), 3, "expecting 3 result record")
151+
152+
for i := 0; i < 3; i++ {
153+
assert.Equal(t, i, res.results[i][0])
154+
}
155+
156+
q = "MATCH(n) return collect(n) as x"
157+
res, err = graph.Query(q)
158+
if err != nil {
159+
t.Error(err)
160+
}
161+
162+
a := NodeNew("person", "", nil)
163+
b := NodeNew("person", "", nil)
164+
165+
a.SetProperty("name", "a")
166+
a.SetProperty("age", 32)
167+
a.SetProperty("array", []interface{}{0, 1, 2})
168+
169+
b.SetProperty("name", "b")
170+
b.SetProperty("age", 30)
171+
b.SetProperty("array", []interface{}{3, 4, 5})
172+
173+
assert.Equal(t, 1, len(res.results), "expecting 1 results record")
174+
175+
arr := res.results[0][0].([]interface{})
176+
177+
assert.Equal(t, 2, len(arr))
178+
179+
resA := arr[0].(*Node)
180+
resB := arr[1].(*Node)
181+
// the order of values in the array returned by collect operation is not defined
182+
// check for the node that contains the name "a" and set it to be resA
183+
if resA.GetProperty("name") != "a" {
184+
resA = arr[1].(*Node)
185+
resB = arr[0].(*Node)
186+
}
187+
188+
assert.Equal(t, a.GetProperty("name"), resA.GetProperty("name"), "Unexpected property value.")
189+
assert.Equal(t, a.GetProperty("age"), resA.GetProperty("age"), "Unexpected property value.")
190+
assert.Equal(t, a.GetProperty("array"), resA.GetProperty("array"), "Unexpected property value.")
191+
192+
assert.Equal(t, b.GetProperty("name"), resB.GetProperty("name"), "Unexpected property value.")
193+
assert.Equal(t, b.GetProperty("age"), resB.GetProperty("age"), "Unexpected property value.")
194+
assert.Equal(t, b.GetProperty("array"), resB.GetProperty("array"), "Unexpected property value.")
195+
196+
}

‎edge.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,50 @@ import (
77

88
// Edge represents an edge connecting two nodes in the graph.
99
type Edge struct {
10-
ID uint64
10+
ID uint64
1111
Relation string
1212
Source *Node
13-
Destination *Node
13+
Destination *Node
1414
Properties map[string]interface{}
15-
srcNodeID uint64
16-
destNodeID uint64
17-
graph *Graph
15+
srcNodeID uint64
16+
destNodeID uint64
17+
graph *Graph
1818
}
1919

2020
func EdgeNew(relation string, srcNode *Node, destNode *Node, properties map[string]interface{}) *Edge {
21-
p := properties
21+
p := properties
2222
if p == nil {
2323
p = make(map[string]interface{})
2424
}
2525

26-
return &Edge{
27-
Relation: relation,
28-
Source: srcNode,
29-
Destination: destNode,
30-
Properties: p,
31-
graph: nil,
32-
}
26+
return &Edge{
27+
Relation: relation,
28+
Source: srcNode,
29+
Destination: destNode,
30+
Properties: p,
31+
graph: nil,
32+
}
3333
}
3434

3535
func (e *Edge) SetProperty(key string, value interface{}) {
3636
e.Properties[key] = value
3737
}
3838

3939
func (e *Edge) GetProperty(key string) interface{} {
40-
v,_ := e.Properties[key]
40+
v, _ := e.Properties[key]
4141
return v
4242
}
4343

4444
func (e Edge) SourceNodeID() uint64 {
45-
if(e.Source != nil) {
45+
if e.Source != nil {
4646
return e.Source.ID
4747
} else {
4848
return e.srcNodeID
4949
}
5050
}
5151

5252
func (e Edge) DestNodeID() uint64 {
53-
if(e.Source != nil) {
53+
if e.Source != nil {
5454
return e.Destination.ID
5555
} else {
5656
return e.destNodeID
@@ -64,7 +64,7 @@ func (e Edge) String() string {
6464

6565
p := make([]string, 0, len(e.Properties))
6666
for k, v := range e.Properties {
67-
p = append(p, fmt.Sprintf("%s:%v", k, QuoteString(v)))
67+
p = append(p, fmt.Sprintf("%s:%v", k, ToString(v)))
6868
}
6969

7070
s := fmt.Sprintf("{%s}", strings.Join(p, ","))
@@ -83,7 +83,7 @@ func (e Edge) Encode() string {
8383
if len(e.Properties) > 0 {
8484
p := make([]string, 0, len(e.Properties))
8585
for k, v := range e.Properties {
86-
p = append(p, fmt.Sprintf("%s:%v", k, QuoteString(v)))
86+
p = append(p, fmt.Sprintf("%s:%v", k, ToString(v)))
8787
}
8888

8989
s = append(s, "{")

‎graph.go

+40-40
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@ import (
1010

1111
// Graph represents a graph, which is a collection of nodes and edges.
1212
type Graph struct {
13-
Id string
14-
Nodes map[string]*Node
15-
Edges []*Edge
16-
Conn redis.Conn
17-
labels []string // List of node labels.
18-
relationshipTypes []string // List of relation types.
19-
properties []string // List of properties.
20-
mutex sync.Mutex // Lock, used for updating internal state.
13+
Id string
14+
Nodes map[string]*Node
15+
Edges []*Edge
16+
Conn redis.Conn
17+
labels []string // List of node labels.
18+
relationshipTypes []string // List of relation types.
19+
properties []string // List of properties.
20+
mutex sync.Mutex // Lock, used for updating internal state.
2121
}
2222

2323
// New creates a new graph.
2424
func GraphNew(Id string, conn redis.Conn) Graph {
25-
g := Graph {
26-
Id: Id,
27-
Nodes: make(map[string]*Node, 0),
28-
Edges: make([]*Edge, 0),
29-
Conn: conn,
30-
labels: make([]string, 0),
25+
g := Graph{
26+
Id: Id,
27+
Nodes: make(map[string]*Node, 0),
28+
Edges: make([]*Edge, 0),
29+
Conn: conn,
30+
labels: make([]string, 0),
3131
relationshipTypes: make([]string, 0),
32-
properties: make([]string, 0),
32+
properties: make([]string, 0),
3333
}
3434
return g
3535
}
@@ -179,53 +179,53 @@ func (g *Graph) getProperty(propIdx int) string {
179179

180180
// CallProcedure invokes procedure.
181181
func (g *Graph) CallProcedure(procedure string, yield []string, args ...interface{}) (*QueryResult, error) {
182-
q := fmt.Sprintf("CALL %s(", procedure)
183-
184-
tmp := make([]string, 0, len(args))
182+
q := fmt.Sprintf("CALL %s(", procedure)
183+
184+
tmp := make([]string, 0, len(args))
185185
for arg := range args {
186-
tmp = append(tmp, QuoteString(arg).(string))
186+
tmp = append(tmp, ToString(arg).(string))
187187
}
188188
q += fmt.Sprintf("%s)", strings.Join(tmp, ","))
189189

190190
if yield != nil && len(yield) > 0 {
191-
q += fmt.Sprintf(" YIELD %s", strings.Join(yield, ","))
191+
q += fmt.Sprintf(" YIELD %s", strings.Join(yield, ","))
192192
}
193193

194-
return g.Query(q)
194+
return g.Query(q)
195195
}
196196

197197
// Labels, retrieves all node labels.
198198
func (g *Graph) Labels() []string {
199-
qr,_ := g.CallProcedure("db.labels", nil)
200-
201-
l := make([]string, len(qr.results))
199+
qr, _ := g.CallProcedure("db.labels", nil)
200+
201+
l := make([]string, len(qr.results))
202202

203-
for idx, r := range(qr.results) {
204-
l[idx] = r[0].(string)
205-
}
206-
return l
203+
for idx, r := range qr.results {
204+
l[idx] = r[0].(string)
205+
}
206+
return l
207207
}
208208

209209
// RelationshipTypes, retrieves all edge relationship types.
210210
func (g *Graph) RelationshipTypes() []string {
211-
qr,_ := g.CallProcedure("db.relationshipTypes", nil)
211+
qr, _ := g.CallProcedure("db.relationshipTypes", nil)
212212

213-
rt := make([]string, len(qr.results))
213+
rt := make([]string, len(qr.results))
214214

215-
for idx, r := range(qr.results) {
216-
rt[idx] = r[0].(string)
217-
}
218-
return rt
215+
for idx, r := range qr.results {
216+
rt[idx] = r[0].(string)
217+
}
218+
return rt
219219
}
220220

221221
// PropertyKeys, retrieves all properties names.
222222
func (g *Graph) PropertyKeys() []string {
223-
qr,_ := g.CallProcedure("db.propertyKeys", nil)
223+
qr, _ := g.CallProcedure("db.propertyKeys", nil)
224224

225-
p := make([]string, len(qr.results))
225+
p := make([]string, len(qr.results))
226226

227-
for idx, r := range(qr.results) {
228-
p[idx] = r[0].(string)
229-
}
230-
return p
227+
for idx, r := range qr.results {
228+
p[idx] = r[0].(string)
229+
}
230+
return p
231231
}

0 commit comments

Comments
 (0)
Please sign in to comment.