From 950ebeb2817e475a4ed524e3f5160a5beb124995 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Tue, 25 May 2021 10:37:40 +0300 Subject: [PATCH 1/3] close connection on graph object destruction only when the connection was created by the constructor (by passing host and port) --- redisgraph/graph.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/redisgraph/graph.py b/redisgraph/graph.py index 7653645..11ccf15 100644 --- a/redisgraph/graph.py +++ b/redisgraph/graph.py @@ -10,12 +10,21 @@ class Graph: Graph, collection of nodes and edges. """ - def __init__(self, name, redis_con): + def __init__(self, name, redis_con=None, host=None, port=None, password=None): """ Create a new graph. """ self.name = name # Graph key - self.redis_con = redis_con + if redis_con is not None: + self.close_con = False + self.redis_con = redis_con + elif host is not None and port is not None: + self.close_con = True + self.redis_con = redis.Redis(host, port, password=password) + else: + raise RuntimeError( + "The constructor must have redis_con argument or host and port." + ) self.nodes = {} self.edges = [] self._labels = [] # List of node labels. @@ -23,6 +32,10 @@ def __init__(self, name, redis_con): self._relationshipTypes = [] # List of relation types. self.version = 0 # Graph version + def __del__(self): + if self.close_con: # Close the connection only if it was + self.redis_con.close() # created by the constructor. + def _clear_schema(self): self._labels = [] self._properties = [] From feb08610a2e5ea48d77d205565e44c332e287a3c Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Tue, 25 May 2021 10:37:40 +0300 Subject: [PATCH 2/3] close connection on graph object destruction only when the connection was created by the constructor (by passing host and port) --- redisgraph/graph.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/redisgraph/graph.py b/redisgraph/graph.py index 7653645..c8fa89e 100644 --- a/redisgraph/graph.py +++ b/redisgraph/graph.py @@ -10,12 +10,21 @@ class Graph: Graph, collection of nodes and edges. """ - def __init__(self, name, redis_con): + def __init__(self, name, redis_con=None, host=None, port=None, password=None): """ Create a new graph. """ self.name = name # Graph key - self.redis_con = redis_con + if redis_con is not None: + self.close_con = False + self.redis_con = redis_con + elif host is not None and port is not None: + self.close_con = True + self.redis_con = redis.Redis(host, port, password=password) + else: + raise RuntimeError( + "The constructor must have redis_con argument or host and port." + ) self.nodes = {} self.edges = [] self._labels = [] # List of node labels. @@ -23,6 +32,10 @@ def __init__(self, name, redis_con): self._relationshipTypes = [] # List of relation types. self.version = 0 # Graph version + def __del__(self): + if hasattr(self, 'close_con') and self.close_con: # Close the connection only if it was + self.redis_con.close() # created by the constructor. + def _clear_schema(self): self._labels = [] self._properties = [] From 552b2c94a87c02dd7ddf0e5f4126108017c279f1 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Thu, 27 May 2021 12:58:39 +0300 Subject: [PATCH 3/3] Add test and change the default host and port to host='localhost', port=6379 --- redisgraph/graph.py | 15 +++------------ tests/functional/test_all.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/redisgraph/graph.py b/redisgraph/graph.py index c8fa89e..0be1cc4 100644 --- a/redisgraph/graph.py +++ b/redisgraph/graph.py @@ -10,21 +10,16 @@ class Graph: Graph, collection of nodes and edges. """ - def __init__(self, name, redis_con=None, host=None, port=None, password=None): + def __init__(self, name, redis_con=None, host='localhost', port=6379, password=None): """ Create a new graph. """ self.name = name # Graph key if redis_con is not None: - self.close_con = False self.redis_con = redis_con - elif host is not None and port is not None: - self.close_con = True - self.redis_con = redis.Redis(host, port, password=password) else: - raise RuntimeError( - "The constructor must have redis_con argument or host and port." - ) + self.redis_con = redis.Redis(host, port, password=password) + self.nodes = {} self.edges = [] self._labels = [] # List of node labels. @@ -32,10 +27,6 @@ def __init__(self, name, redis_con=None, host=None, port=None, password=None): self._relationshipTypes = [] # List of relation types. self.version = 0 # Graph version - def __del__(self): - if hasattr(self, 'close_con') and self.close_con: # Close the connection only if it was - self.redis_con.close() # created by the constructor. - def _clear_schema(self): self._labels = [] self._properties = [] diff --git a/tests/functional/test_all.py b/tests/functional/test_all.py index 865ea9c..13655c6 100644 --- a/tests/functional/test_all.py +++ b/tests/functional/test_all.py @@ -46,6 +46,19 @@ def test_graph_creation(self): # All done, remove graph. redis_graph.delete() + def test_graph_connection(self): + connections = len(self.r.client_list()) + redis_graph = Graph('social', self.r) + del redis_graph + self.assertTrue(self.r.ping()) + self.assertTrue(len(self.r.client_list()) == connections) + + new_graph = Graph('social-new') + self.assertTrue(new_graph.redis_con.ping()) + self.assertTrue(len(self.r.client_list()) == connections + 1) + del new_graph + self.assertTrue(len(self.r.client_list()) == connections) + def test_array_functions(self): redis_graph = Graph('social', self.r)