-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_query_evaluator.py
398 lines (373 loc) · 16.8 KB
/
test_query_evaluator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import unittest
from query_evaluator import QueryEvaluator
from graph_structure import GraphStructure
from predicates import Predicates
from project import Project
class TestQueryEvaluator(unittest.TestCase):
def test_add_node(self):
"""
Tests L{QueryEvaluator.add_node} method of L{QueryEvaluator}.
"""
gs = GraphStructure()
q = QueryEvaluator(gs)
attrs1 = {'Label' : 'Person', 'Name' : 'You'}
attrs2 = {'Label' : 'Person', 'Name' : 'Me'}
attrs3 = {'Label' : 'Person', 'Name' : 'She', 'Age' : 23}
# Test first node
node1 = q.add_node(attrs1)
self.assertEqual(node1[0], 1)
self.assertEqual(node1[1], attrs1)
# Test second node
node2 = q.add_node(attrs2)
self.assertEqual(node2[0], 2)
self.assertEqual(node2[1], attrs2)
# Test third node
node3 = q.add_node(attrs3)
self.assertEqual(node3[0], 3)
self.assertEqual(node3[1], attrs3)
def test_add_relationship(self):
"""
Tests L{QueryEvaluator.add_relationship} method for L{QueryEvaluator}.
"""
gs = GraphStructure()
q = QueryEvaluator(gs)
attrs1 = {'Label' : 'Person', 'Name' : 'Alice'}
attrs2 = {'Label' : 'Person', 'Name' : 'Bob'}
attrs3 = {'Label' : 'Person', 'Name' : 'John'}
node1 = q.add_node(attrs1)
node2 = q.add_node(attrs2)
node3 = q.add_node(attrs3)
edge_attrs1 = {'rel_type' : 'friend'}
edge_attrs2 = {'rel_type' : 'cousin'}
# Test first edge
edge1 = q.add_relationship(node1, node2, edge_attrs1)
self.assertEqual(edge1, (node1[0], node2[0], edge_attrs1))
# Test second edge
edge2 = q.add_relationship(node2, node3, edge_attrs2)
self.assertEqual(edge2, (node2[0], node3[0], edge_attrs2))
def test_match_node(self):
"""
Tests L{QueryEvaluator.match_node} method for L{QueryEvaluator}.
"""
gs = GraphStructure()
q = QueryEvaluator(gs)
attrs1 = {'Label' : 'Person', 'Name' : 'Alice'}
attrs2 = {'Label' : 'Person', 'Name' : 'Bob'}
attrs3 = {'Label' : 'Person', 'Name' : 'John'}
node1 = q.add_node(attrs1)
node2 = q.add_node(attrs2)
node3 = q.add_node(attrs3)
# Test matching all node
match_lst1 = q.match_node({'Label' : 'Person'})
self.assertEqual(match_lst1, [node1, node2, node3])
# Test matching single node
match_lst2 = q.match_node({'Name' : 'Alice'})
self.assertEqual(match_lst2, [node1])
def test_match_rel(self):
"""
Tests L{QueryEvaluator.match_rel} method for L{QueryEvaluator}.
"""
gs = GraphStructure()
q = QueryEvaluator(gs)
attrs1 = {'Label' : 'Person', 'Name' : 'Alice'}
attrs2 = {'Label' : 'Person', 'Name' : 'Bob'}
attrs3 = {'Label' : 'Person', 'Name' : 'John'}
node1 = q.add_node(attrs1)
node2 = q.add_node(attrs2)
node3 = q.add_node(attrs3)
edge_attrs1 = {'rel_type' : 'friend'}
edge_attrs2 = {'rel_type' : 'friend', 'fam' : 'cousin'}
edge1 = q.add_relationship(node1, node2, edge_attrs1)
edge2 = q.add_relationship(node2, node3, edge_attrs2)
# Test matching relationship with multiple results
match_lst1 = q.match_rel(edge_attrs1)
result1 = [(node1[0], node2[0], edge_attrs1),
(node2[0], node3[0], edge_attrs2)]
self.assertEqual(sorted(match_lst1), sorted(result1))
# Test matching relationship with single results
match_lst2 = q.match_rel(edge_attrs2)
result2 = [(node2[0], node3[0], edge_attrs2)]
self.assertEqual(match_lst2, result2)
def test_match_node_rel(self):
"""
Tests L{QueryEvaluator.match_node_rel} method for L{QueryEvaluator}.
"""
gs = GraphStructure()
q = QueryEvaluator(gs)
attrs1 = {'Label' : 'Person', 'Name' : 'Alice'}
attrs2 = {'Label' : 'Person', 'Name' : 'Bob'}
attrs3 = {'Label' : 'Person', 'Name' : 'John'}
node1 = q.add_node(attrs1)
node2 = q.add_node(attrs2)
node3 = q.add_node(attrs3)
edge_attrs1 = {'rel_type' : 'friend'}
edge_attrs2 = {'rel_type' : 'friend', 'fam' : 'cousin'}
edge1 = q.add_relationship(node1, node2, edge_attrs1)
edge2 = q.add_relationship(node2, node3, edge_attrs2)
edge3 = q.add_relationship(node2, node1, edge_attrs1)
# Test matching with no results
match_lst1 = q.match_node_rel(node1[1], edge_attrs2)
result1 = []
self.assertEqual(match_lst1, result1)
# Test matching with single result
match_lst2 = q.match_node_rel(node2[1], edge_attrs2)
result2 = [(node2[0], node3[0], edge_attrs2)]
self.assertEqual(match_lst2, result2)
# Test matching with multiple result
match_lst3 = q.match_node_rel(node2[1], {'rel_type' : 'friend'})
result3 = [(node2[0], node1[0], edge_attrs1),
(node2[0], node3[0], edge_attrs2)]
self.assertEqual(match_lst3, result3)
def test_match_find_rel(self):
"""
Tests L{QueryEvaluator.match_find_rel} method for L{QueryEvaluator}.
"""
gs = GraphStructure()
q = QueryEvaluator(gs)
attrs1 = {'Label' : 'Person', 'Name' : 'Alice'}
attrs2 = {'Label' : 'Person', 'Name' : 'Bob'}
attrs3 = {'Label' : 'Person', 'Name' : 'John'}
node1 = q.add_node(attrs1)
node2 = q.add_node(attrs2)
node3 = q.add_node(attrs3)
edge_attrs1 = {'rel_type' : 'friend'}
edge_attrs2 = {'rel_type' : 'friend', 'fam' : 'cousin'}
edge1 = q.add_relationship(node1, node2, edge_attrs1)
edge2 = q.add_relationship(node2, node3, edge_attrs2)
# Test matching with no results
match_lst1 = q.match_find_rel(node1[1], node3[1])
result1 = []
self.assertEqual(match_lst1, result1)
# Test matching with result
match_lst2 = q.match_find_rel(node1[1], node2[1])
result2 = [(node1[0], node2[0], edge_attrs1)]
self.assertEqual(match_lst2, result2)
def test_match_node_node_rel(self):
"""
Tests L{QueryEvaluator.match_node_node_rel} method for L{QueryEvaluator}.
"""
gs = GraphStructure()
q = QueryEvaluator(gs)
attrs1 = {'Label' : 'Person', 'Name' : 'Alice'}
attrs2 = {'Label' : 'Person', 'Name' : 'Bob'}
attrs3 = {'Label' : 'Person', 'Name' : 'John'}
node1 = q.add_node(attrs1)
node2 = q.add_node(attrs2)
node3 = q.add_node(attrs3)
edge_attrs1 = {'rel_type' : 'friend'}
edge_attrs2 = {'rel_type' : 'friend', 'fam' : 'cousin'}
edge1 = q.add_relationship(node1, node2, edge_attrs1)
edge2 = q.add_relationship(node2, node3, edge_attrs2)
# Test matching with no results
match_lst1 = q.match_node_node_rel(node1[1], node2[1], edge_attrs2)
result1 = []
self.assertEqual(match_lst1, result1)
# Test matching with single result
match_lst2 = q.match_node_node_rel(node1[1], node2[1], edge_attrs1)
result2 = [(node1[0], node2[0], edge_attrs1)]
self.assertEqual(match_lst2, result2)
# Test matching with multiple results
match_lst3 = q.match_node_node_rel({'Label' : 'Person'},
{'Label' : 'Person'}, {'rel_type' : 'friend'})
result3 = [(node1[0], node2[0], edge_attrs1),
(node2[0], node3[0], edge_attrs2)]
self.assertEqual(match_lst3, result3)
def test_delete_node(self):
"""
Tests L{QueryEvaluator.delete_node} method for L{QueryEvaluator}.
"""
gs = GraphStructure()
q = QueryEvaluator(gs)
attrs1 = {'Label' : 'Person', 'Name' : 'Alice'}
attrs2 = {'Label' : 'Person', 'Name' : 'Bob'}
attrs3 = {'Label' : 'Person', 'Name' : 'John'}
node1 = q.add_node(attrs1)
node2 = q.add_node(attrs2)
node3 = q.add_node(attrs3)
edge_attrs1 = {'rel_type' : 'friend'}
edge_attrs2 = {'rel_type' : 'friend', 'fam' : 'cousin'}
edge1 = q.add_relationship(node1, node2, edge_attrs1)
edge2 = q.add_relationship(node2, node3, edge_attrs2)
# Tests deleting no nodes
q.delete_node({'Label' : 'Person', 'Name' : 'Fred'})
match_lst1 = q.match({}, {}, {})
result1 = [(node1[0], node2[0], edge_attrs1),
(node2[0], node3[0], edge_attrs2)]
self.assertEqual(match_lst1, result1)
# Tests deleting nodes
q.delete_node({'Label' : 'Person', 'Name' : 'Bob'})
match_lst2 = q.match({}, {}, {})
result2 = []
self.assertEqual(match_lst2, result2)
def test_delete_rel(self):
"""
Tests L{QueryEvaluator.delete_rel} method for L{QueryEvaluator}.
"""
gs = GraphStructure()
q = QueryEvaluator(gs)
attrs1 = {'Label' : 'Person', 'Name' : 'Alice'}
attrs2 = {'Label' : 'Person', 'Name' : 'Bob'}
attrs3 = {'Label' : 'Person', 'Name' : 'John'}
node1 = q.add_node(attrs1)
node2 = q.add_node(attrs2)
node3 = q.add_node(attrs3)
edge_attrs1 = {'rel_type' : 'friend'}
edge_attrs2 = {'rel_type' : 'friend', 'fam' : 'cousin'}
edge1 = q.add_relationship(node1, node2, edge_attrs1)
edge2 = q.add_relationship(node2, node3, edge_attrs2)
# Tests deleting no relationship
q.delete_rel({'rel_type' : 'friend', 'fam' : 'dad'})
match_lst1 = q.match({}, {}, {})
result1 = [(node1[0], node2[0], edge_attrs1),
(node2[0], node3[0], edge_attrs2)]
self.assertEqual(match_lst1, result1)
# Tests deleting relationship
q.delete_rel(edge_attrs1)
match_lst2 = q.match({}, {}, {})
result2 = []
self.assertEqual(match_lst2, result2)
def test_modify_node(self):
"""
Tests L{QueryEvaluator.modify_node} method for L{QueryEvaluator}.
"""
gs = GraphStructure()
q = QueryEvaluator(gs)
attrs1 = {'Label' : 'Person', 'Name' : 'Alice'}
attrs2 = {'Label' : 'Person', 'Name' : 'Bob'}
attrs3 = {'Label' : 'Person', 'Name' : 'John'}
node1 = q.add_node(attrs1)
node2 = q.add_node(attrs2)
node3 = q.add_node(attrs3)
edge_attrs1 = {'rel_type' : 'friend'}
edge_attrs2 = {'rel_type' : 'friend', 'fam' : 'cousin'}
edge1 = q.add_relationship(node1, node2, edge_attrs1)
edge2 = q.add_relationship(node2, node3, edge_attrs2)
# Tests adding attributes to node
q.modify_node(attrs1, {'Age' : '10'}, True)
match_lst1 = q.match_node(attrs1)
result1 = [(node1[0], {'Label' : 'Person', 'Name' : 'Alice', 'Age' : '10'})]
self.assertEqual(match_lst1, result1)
# Tests removing attributes to node
q.modify_node(attrs1, {'Age' : '10'}, False)
match_lst2 = q.match_node(attrs1)
result2 = [(node1[0], attrs1)]
self.assertEqual(match_lst2, result2)
def test_modify_rel(self):
"""
Test L{QueryEvaluator.modify_rel} method for L{QueryEvaluator}.
"""
gs = GraphStructure()
q = QueryEvaluator(gs)
attrs1 = {'Label' : 'Person', 'Name' : 'Alice'}
attrs2 = {'Label' : 'Person', 'Name' : 'Bob'}
attrs3 = {'Label' : 'Person', 'Name' : 'John'}
node1 = q.add_node(attrs1)
node2 = q.add_node(attrs2)
node3 = q.add_node(attrs3)
edge_attrs1 = {'rel_type' : 'friend'}
edge_attrs2 = {'rel_type' : 'friend', 'fam' : 'cousin'}
edge1 = q.add_relationship(node1, node2, edge_attrs1)
edge2 = q.add_relationship(node2, node3, edge_attrs2)
# Tests adding attributes to relationship
q.modify_rel(edge_attrs2, {'like' : 'no'}, True)
match_lst1 = q.match_rel(edge_attrs2)
result1 = [(node2[0], node3[0], {'rel_type' : 'friend',
'fam' : 'cousin',
'like' : 'no'})]
self.assertEqual(sorted(match_lst1), sorted(result1))
# Tests deleting attributes to relationship
q.modify_rel(edge_attrs2, {'like' : 'no'}, False)
match_lst2 = q.match_rel(edge_attrs2)
result2 = [(node2[0], node3[0], edge_attrs2)]
self.assertEqual(sorted(match_lst2), sorted(result2))
def test_multi_match(self):
"""
Test L{QueryEvaluator.multi_match} method for L{QueryEvaluator}.
"""
gs = GraphStructure()
q = QueryEvaluator(gs)
attrs1 = {'Label' : 'Person', 'Name' : 'Alice'}
attrs2 = {'Label' : 'Person', 'Name' : 'Bob'}
attrs3 = {'Label' : 'Person', 'Name' : 'John'}
attrs4 = {'Label' : 'Person', 'Name' : 'Rick'}
attrs5 = {'Label' : 'Person', 'Name' : 'Cat'}
node1 = q.add_node(attrs1)
node2 = q.add_node(attrs2)
node3 = q.add_node(attrs3)
node4 = q.add_node(attrs4)
node5 = q.add_node(attrs5)
edge_attrs1 = {'rel_type' : 'friend'}
edge_attrs2 = {'rel_type' : 'friend', 'fam' : 'cousin'}
edge_attrs3 = {'rel_type' : 'dad'}
edge_attrs4 = {'rel_type' : 'mom'}
edge1 = q.add_relationship(node1, node2, edge_attrs1)
edge2 = q.add_relationship(node2, node3, edge_attrs2)
edge3 = q.add_relationship(node3, node4, edge_attrs3)
edge4 = q.add_relationship(node4, node5, edge_attrs4)
# Test no match for multiple length query
result1 = q.multi_match([attrs1, attrs2, attrs3], [edge_attrs3, edge_attrs1])
self.assertEqual(result1, None)
# Test single match for multiple length query
result2 = q.multi_match([attrs1, attrs2, attrs3],
[edge_attrs1, edge_attrs2])
self.assertEqual(result2, [(1, 3)])
# Tests many matches for multiple length query
result3 = q.multi_match([{'Label' : 'Person'}, {'Label' : 'Person'}],
[edge_attrs1])
self.assertEqual(result3, [edge1, edge2])
# Test all matches for multiple length query
result4 = q.multi_match([{}, {}], [{}])
self.assertEqual(result4, [edge1, edge2, edge3, edge4])
def test_match_node_predicate(self):
"""
Tests applying a predicate to a simple match node query for L{QueryEvaluator}.
"""
gs = GraphStructure()
q = QueryEvaluator(gs)
pred = Predicates(gs)
attrs1 = {'Label' : 'Person', 'Name' : 'Alice', 'Salary' : '500'}
attrs2 = {'Label' : 'Person', 'Name' : 'Bob', 'Salary' : '1000'}
attrs3 = {'Label' : 'Person', 'Name' : 'John', 'Salary' : '20000'}
attrs4 = {'Label' : 'Person', 'Name' : 'Donnie', 'Salary' : '100000000'}
node1 = q.add_node(attrs1)
node2 = q.add_node(attrs2)
node3 = q.add_node(attrs3)
node4 = q.add_node(attrs4)
# Test matching all nodes and then filtering with a predicate
match_lst1 = q.match_node({'Label' : 'Person'})
# The predicate is salary > 2000
filtered_lst1 = pred.filter(match_lst1, 'Salary', '2000', '>')
self.assertEqual(filtered_lst1, [node3, node4])
# Test matching a single node and then filtering with a predicate
match_lst2 = q.match_node({'Name' : 'Alice'})
filtered_lst2 = pred.filter(match_lst2, 'Salary', '500', '=')
self.assertEqual(filtered_lst2, [node1])
def test_match_node_project_node(self):
"""
Tests applying a project to a simple match node query for L{QueryEvaluator}.
"""
gs = GraphStructure()
q = QueryEvaluator(gs)
proj = Project()
attrs1 = {'Label' : 'Person', 'Name' : 'Alice', 'Salary' : '500'}
attrs2 = {'Label' : 'Person', 'Name' : 'Bob', 'Salary' : '1000'}
attrs3 = {'Label' : 'Person', 'Name' : 'John', 'Salary' : '20000'}
attrs4 = {'Label' : 'Person', 'Name' : 'Donnie', 'Salary' : '100000000'}
node1 = q.add_node(attrs1)
node2 = q.add_node(attrs2)
node3 = q.add_node(attrs3)
node4 = q.add_node(attrs4)
# Test matching all nodes and then projecting to a single attribute.
match_lst1 = q.match_node({'Label' : 'Person'})
# Project on salary field.
filtered_lst1 = proj.project(match_lst1, ['Salary'])
self.assertEqual(filtered_lst1, [{'Salary' : '500'},
{'Salary' : '1000'},
{'Salary' : '20000'},
{'Salary' : '100000000'}])
# Test matching a single node and then projecting to a single attribute.
match_lst2 = q.match_node({'Name' : 'Alice'})
filtered_lst2 = proj.project(match_lst2, ['Salary'])
self.assertEqual(filtered_lst2, [{'Salary' : '500'}])
if __name__ == '__main__':
unittest.main()