Skip to content

Edge can be str() after returning from QueryResult #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions redisgraph/edge.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from redisgraph import Node

from .util import *

class Edge(object):
Expand Down Expand Up @@ -26,7 +28,10 @@ def toString(self):

def __str__(self):
# Source node.
res = '(' + self.src_node.alias + ')'
if isinstance(self.src_node, Node):
res = str(self.src_node)
else:
res = '()'

# Edge
res += "-["
Expand All @@ -38,7 +43,10 @@ def __str__(self):
res += ']->'

# Dest node.
res += '(' + self.dest_node.alias + ')'
if isinstance(self.dest_node, Node):
res += str(self.dest_node)
else:
res += '()'

return res

Expand Down
39 changes: 38 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,42 @@ def test_index_response(self):

redis_graph.delete()

def test_stringify_query_result(self):
redis_graph = Graph('stringify', self.r)

john = Node(alias='a', label='person',
properties={'name': 'John Doe', 'age': 33, 'gender': 'male', 'status': 'single'})
redis_graph.add_node(john)
japan = Node(alias='b', label='country', properties={'name': 'Japan'})

redis_graph.add_node(japan)
edge = Edge(john, 'visited', japan, properties={'purpose': 'pleasure'})
redis_graph.add_edge(edge)

self.assertEqual(str(john),
"""(a:person{name:"John Doe",age:33,gender:"male",status:"single"})""")
self.assertEqual(str(edge),
"""(a:person{name:"John Doe",age:33,gender:"male",status:"single"})""" +
"""-[:visited{purpose:"pleasure"}]->""" +
"""(b:country{name:"Japan"})""")
self.assertEqual(str(japan), """(b:country{name:"Japan"})""")

redis_graph.commit()

query = """MATCH (p:person)-[v:visited {purpose:"pleasure"}]->(c:country)
RETURN p, v, c"""

result = redis_graph.query(query)
person = result.result_set[0][0]
visit = result.result_set[0][1]
country = result.result_set[0][2]

self.assertEqual(str(person), """(:person{name:"John Doe",age:33,gender:"male",status:"single"})""")
self.assertEqual(str(visit), """()-[:visited{purpose:"pleasure"}]->()""")
self.assertEqual(str(country), """(:country{name:"Japan"})""")

redis_graph.delete()


if __name__ == '__main__':
unittest.main()
unittest.main()