Skip to content

Commit a611fdd

Browse files
mingen-panswilly22
authored andcommitted
Edge can be str() after returning from QueryResult (#59)
* id should be int not float as indicated by the above comment * edge can be converted to string even the src_node and dest_node are assigned with id * edge can be converted to string even the src_node and dest_node are assigned with id * __str__ of Edge will include str(src_node) and str(dest_node)
1 parent 3e84f57 commit a611fdd

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

Diff for: redisgraph/edge.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from redisgraph import Node
2+
13
from .util import *
24

35
class Edge(object):
@@ -26,7 +28,10 @@ def toString(self):
2628

2729
def __str__(self):
2830
# Source node.
29-
res = '(' + self.src_node.alias + ')'
31+
if isinstance(self.src_node, Node):
32+
res = str(self.src_node)
33+
else:
34+
res = '()'
3035

3136
# Edge
3237
res += "-["
@@ -38,7 +43,10 @@ def __str__(self):
3843
res += ']->'
3944

4045
# Dest node.
41-
res += '(' + self.dest_node.alias + ')'
46+
if isinstance(self.dest_node, Node):
47+
res += str(self.dest_node)
48+
else:
49+
res += '()'
4250

4351
return res
4452

Diff for: test.py

+38-1
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,42 @@ def test_index_response(self):
126126

127127
redis_graph.delete()
128128

129+
def test_stringify_query_result(self):
130+
redis_graph = Graph('stringify', self.r)
131+
132+
john = Node(alias='a', label='person',
133+
properties={'name': 'John Doe', 'age': 33, 'gender': 'male', 'status': 'single'})
134+
redis_graph.add_node(john)
135+
japan = Node(alias='b', label='country', properties={'name': 'Japan'})
136+
137+
redis_graph.add_node(japan)
138+
edge = Edge(john, 'visited', japan, properties={'purpose': 'pleasure'})
139+
redis_graph.add_edge(edge)
140+
141+
self.assertEqual(str(john),
142+
"""(a:person{name:"John Doe",age:33,gender:"male",status:"single"})""")
143+
self.assertEqual(str(edge),
144+
"""(a:person{name:"John Doe",age:33,gender:"male",status:"single"})""" +
145+
"""-[:visited{purpose:"pleasure"}]->""" +
146+
"""(b:country{name:"Japan"})""")
147+
self.assertEqual(str(japan), """(b:country{name:"Japan"})""")
148+
149+
redis_graph.commit()
150+
151+
query = """MATCH (p:person)-[v:visited {purpose:"pleasure"}]->(c:country)
152+
RETURN p, v, c"""
153+
154+
result = redis_graph.query(query)
155+
person = result.result_set[0][0]
156+
visit = result.result_set[0][1]
157+
country = result.result_set[0][2]
158+
159+
self.assertEqual(str(person), """(:person{name:"John Doe",age:33,gender:"male",status:"single"})""")
160+
self.assertEqual(str(visit), """()-[:visited{purpose:"pleasure"}]->()""")
161+
self.assertEqual(str(country), """(:country{name:"Japan"})""")
162+
163+
redis_graph.delete()
164+
165+
129166
if __name__ == '__main__':
130-
unittest.main()
167+
unittest.main()

0 commit comments

Comments
 (0)