Skip to content

Commit aa9c89d

Browse files
committed
test cache sync
1 parent 1d3b241 commit aa9c89d

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

redisgraph/graph.py

+7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ def __init__(self, name, redis_con):
1919
self._properties = [] # List of properties.
2020
self._relationshipTypes = [] # List of relation types.
2121

22+
def _clear_schema(self):
23+
self._labels = []
24+
self._properties = []
25+
self._relationshipTypes = []
26+
2227
def _refresh_schema(self):
28+
self._clear_schema()
2329
self._refresh_labels()
2430
self._refresh_relations()
2531
self._refresh_attributes()
@@ -167,6 +173,7 @@ def delete(self):
167173
"""
168174
Deletes graph.
169175
"""
176+
self._clear_schema()
170177
return self.redis_con.execute_command("GRAPH.DELETE", self.name)
171178

172179
def merge(self, pattern):

test.py

+70
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,75 @@ def test_query_timeout(self):
228228
# Expecting an error.
229229
pass
230230

231+
def test_cache_sync(self):
232+
# This test verifies that client internal graph schema cache stays
233+
# in sync with the graph schema
234+
#
235+
# Client B will try to get Client A out of sync by:
236+
# 1. deleting the graph
237+
# 2. reconstructing the graph in a different order, this will casuse
238+
# a differance in the current mapping between string IDs and the
239+
# mapping Client A is aware of
240+
#
241+
# Client A should pick up on the changes by comparing graph versions
242+
# and resyncing its cache.
243+
244+
A = Graph('cache-sync', self.r)
245+
B = Graph('cache-sync', self.r)
246+
247+
# Build order:
248+
# 1. introduce label 'L' and 'K'
249+
# 2. introduce attribute 'x' and 'q'
250+
# 3. introduce relationship-type 'R' and 'S'
251+
252+
A.query("CREATE (:L)")
253+
B.query("CREATE (:K)")
254+
A.query("MATCH (n) SET n.x = 1")
255+
B.query("MATCH (n) SET n.q = 1")
256+
A.query("MATCH (n) CREATE (n)-[:R]->()")
257+
B.query("MATCH (n) CREATE (n)-[:S]->()")
258+
259+
# Cause client A to populate its cache
260+
A.query("MATCH (n)-[e]->() RETURN n, e")
261+
262+
assert(len(A._labels) == 2)
263+
assert(len(A._properties) == 2)
264+
assert(len(A._relationshipTypes) == 2)
265+
assert(A._labels[0] == 'L')
266+
assert(A._labels[1] == 'K')
267+
assert(A._properties[0] == 'x')
268+
assert(A._properties[1] == 'q')
269+
assert(A._relationshipTypes[0] == 'R')
270+
assert(A._relationshipTypes[1] == 'S')
271+
272+
# Have client B reconstruct the graph in a different order.
273+
B.delete()
274+
275+
# Build order:
276+
# 1. introduce relationship-type 'R'
277+
# 2. introduce label 'L'
278+
# 3. introduce attribute 'x'
279+
B.query("CREATE ()-[:S]->()")
280+
B.query("CREATE ()-[:R]->()")
281+
B.query("CREATE (:K)")
282+
B.query("CREATE (:L)")
283+
B.query("MATCH (n) SET n.q = 1")
284+
B.query("MATCH (n) SET n.x = 1")
285+
286+
# A's internal cached mapping is now out of sync
287+
# issue a query and make sure A's cache is synced.
288+
A.query("MATCH (n)-[e]->() RETURN n, e")
289+
290+
assert(len(A._labels) == 2)
291+
assert(len(A._properties) == 2)
292+
assert(len(A._relationshipTypes) == 2)
293+
assert(A._labels[0] == 'K')
294+
assert(A._labels[1] == 'L')
295+
assert(A._properties[0] == 'q')
296+
assert(A._properties[1] == 'x')
297+
assert(A._relationshipTypes[0] == 'S')
298+
assert(A._relationshipTypes[1] == 'R')
299+
231300
if __name__ == '__main__':
232301
unittest.main()
302+

0 commit comments

Comments
 (0)