Skip to content

Commit 92eba8d

Browse files
close connection on graph destruction (#130)
* close connection on graph object destruction only when the connection was created by the constructor (by passing host and port) * close connection on graph object destruction only when the connection was created by the constructor (by passing host and port) * Add test and change the default host and port to host='localhost', port=6379 Co-authored-by: Guy Korland <[email protected]>
1 parent eff17ac commit 92eba8d

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

redisgraph/graph.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ class Graph:
1010
Graph, collection of nodes and edges.
1111
"""
1212

13-
def __init__(self, name, redis_con):
13+
def __init__(self, name, redis_con=None, host='localhost', port=6379, password=None):
1414
"""
1515
Create a new graph.
1616
"""
1717
self.name = name # Graph key
18-
self.redis_con = redis_con
18+
if redis_con is not None:
19+
self.redis_con = redis_con
20+
else:
21+
self.redis_con = redis.Redis(host, port, password=password)
22+
1923
self.nodes = {}
2024
self.edges = []
2125
self._labels = [] # List of node labels.

tests/functional/test_all.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ def test_graph_creation(self):
4646
# All done, remove graph.
4747
redis_graph.delete()
4848

49+
def test_graph_connection(self):
50+
connections = len(self.r.client_list())
51+
redis_graph = Graph('social', self.r)
52+
del redis_graph
53+
self.assertTrue(self.r.ping())
54+
self.assertTrue(len(self.r.client_list()) == connections)
55+
56+
new_graph = Graph('social-new')
57+
self.assertTrue(new_graph.redis_con.ping())
58+
self.assertTrue(len(self.r.client_list()) == connections + 1)
59+
del new_graph
60+
self.assertTrue(len(self.r.client_list()) == connections)
61+
4962
def test_array_functions(self):
5063
redis_graph = Graph('social', self.r)
5164

0 commit comments

Comments
 (0)