@@ -2,54 +2,115 @@ package redisgraph
22
33import (
44 "testing"
5-
5+ "os"
6+ "time"
7+ "github.com/stretchr/testify/assert"
68 "github.com/gomodule/redigo/redis"
79)
810
9- func TestGraphCreation (t * testing.T ) {
10- // Setup.
11- conn , _ := redis .Dial ("tcp" , "0.0.0.0:6379" )
12- defer conn .Close ()
11+ var graph Graph
1312
13+ func createGraph () {
14+ conn , _ := redis .Dial ("tcp" , "0.0.0.0:6379" )
1415 conn .Do ("FLUSHALL" )
15- rg : = GraphNew ("social" , conn )
16+ graph = GraphNew ("social" , conn )
1617
1718 // Create 2 nodes connect via a single edge.
18- japan := NodeNew (0 , "country " , "j" , nil )
19- john := NodeNew (0 , "person " , "p" , nil )
20- edge := EdgeNew (0 , "visited " , john , japan , nil )
19+ japan := NodeNew ("Country " , "j" , nil )
20+ john := NodeNew ("Person " , "p" , nil )
21+ edge := EdgeNew ("Visited " , john , japan , nil )
2122
2223 // Set node properties.
2324 john .SetProperty ("name" , "John Doe" )
2425 john .SetProperty ("age" , 33 )
2526 john .SetProperty ("gender" , "male" )
2627 john .SetProperty ("status" , "single" )
28+
29+ japan .SetProperty ("name" , "Japan" )
30+ japan .SetProperty ("population" , 126800000 )
31+
32+ edge .SetProperty ("year" , 2017 )
2733
2834 // Introduce entities to graph.
29- rg .AddNode (john )
30- rg .AddNode (japan )
31- rg .AddEdge (edge )
35+ graph .AddNode (john )
36+ graph .AddNode (japan )
37+ graph .AddEdge (edge )
3238
3339 // Flush graph to DB.
34- resp , err := rg .Commit ()
40+ _ , err := graph .Commit ()
41+ if err != nil {
42+ panic (err )
43+ }
44+ }
45+
46+ func setup () {
47+ createGraph ()
48+ }
49+
50+ func shutdown () {
51+ graph .Conn .Close ()
52+ }
53+
54+ func TestMain (m * testing.M ) {
55+ setup ()
56+ code := m .Run ()
57+ shutdown ()
58+ os .Exit (code )
59+ }
60+
61+ func TestMatchQuery (t * testing.T ) {
62+ q := "MATCH (s)-[e]->(d) RETURN s,e,d"
63+ res , err := graph .Query (q )
3564 if err != nil {
3665 t .Error (err )
3766 }
67+
68+ assert .Equal (t , len (res .results ), 1 , "expecting 1 result record" )
69+
70+ s , ok := (res .results [0 ][0 ]).(* Node )
71+ assert .True (t , ok , "First column should contain nodes." )
72+ e , ok := (res .results [0 ][1 ]).(* Edge )
73+ assert .True (t , ok , "Second column should contain edges." )
74+ d , ok := (res .results [0 ][2 ]).(* Node )
75+ assert .True (t , ok , "Third column should contain nodes." )
76+
77+ assert .Equal (t , s .Label , "Person" , "Node should be of type 'Person'" )
78+ assert .Equal (t , e .Relation , "Visited" , "Edge should be of relation type 'Visited'" )
79+ assert .Equal (t , d .Label , "Country" , "Node should be of type 'Country'" )
80+
81+ assert .Equal (t , len (s .Properties ), 4 , "Person node should have 4 properties" )
82+
83+ assert .Equal (t , s .GetProperty ("name" ), "John Doe" , "Unexpected property value." )
84+ assert .Equal (t , s .GetProperty ("age" ), 33 , "Unexpected property value." )
85+ assert .Equal (t , s .GetProperty ("gender" ), "male" , "Unexpected property value." )
86+ assert .Equal (t , s .GetProperty ("status" ), "single" , "Unexpected property value." )
87+
88+ assert .Equal (t , e .GetProperty ("year" ), 2017 , "Unexpected property value." )
89+
90+ assert .Equal (t , d .GetProperty ("name" ), "Japan" , "Unexpected property value." )
91+ assert .Equal (t , d .GetProperty ("population" ), 126800000 , "Unexpected property value." )
92+ }
3893
39- // Validate response.
40- if (resp .results != nil ) {
41- t .FailNow ()
94+ func TestCreateQuery (t * testing.T ) {
95+ q := "CREATE (w:WorkPlace {name:'RedisLabs'})"
96+ res , err := graph .Query (q )
97+ if err != nil {
98+ t .Error (err )
4299 }
43- if (resp .statistics ["Labels added" ] != 2 ) {
44- t .FailNow ()
100+
101+ assert .True (t , res .Empty (), "Expecting empty result-set" )
102+
103+ // Validate statistics.
104+ assert .Equal (t , res .NodesCreated (), 1 , "Expecting a single node to be created." )
105+ assert .Equal (t , res .PropertiesSet (), 1 , "Expecting a songle property to be added." )
106+
107+ q = "MATCH (w:WorkPlace) RETURN w"
108+ res , err = graph .Query (q )
109+ if err != nil {
110+ t .Error (err )
45111 }
46- if (resp .statistics ["Nodes created" ] != 2 ) {
47- t .FailNow ()
48- }
49- if (resp .statistics ["Properties set" ] != 4 ) {
50- t .FailNow ()
51- }
52- if (resp .statistics ["Relationships created" ] != 1 ) {
53- t .FailNow ()
54- }
112+
113+ assert .False (t , res .Empty (), "Expecting resultset to include a single node." )
114+ w := (res .results [0 ][0 ]).(* Node )
115+ assert .Equal (t , w .Label , "WorkPlace" , "Unexpected node label." )
55116}
0 commit comments