/
test_neo4j_proxy.py
599 lines (503 loc) · 29.4 KB
/
test_neo4j_proxy.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import copy
import textwrap
import unittest
from typing import Any, Dict # noqa: F401
from mock import patch, MagicMock
from neo4j.v1 import GraphDatabase
from metadata_service import create_app
from metadata_service.entity.popular_table import PopularTable
from metadata_service.entity.table_detail import (Application, Column, Table, Tag,
Watermark, Source, Statistics, User)
from metadata_service.entity.tag_detail import TagDetail
from metadata_service.proxy.neo4j_proxy import Neo4jProxy
from metadata_service.util import UserResourceRel
class TestNeo4jProxy(unittest.TestCase):
def setUp(self) -> None:
self.app = create_app(config_module_class='metadata_service.config.LocalConfig')
self.app_context = self.app.app_context()
self.app_context.push()
table_entry = {'db': {'name': 'hive'},
'clstr': {
'name': 'gold'},
'schema': {
'name': 'foo_schema'},
'tbl': {
'name': 'foo_table'},
'tbl_dscrpt': {
'description': 'foo description'}
}
col1 = copy.deepcopy(table_entry) # type: Dict[Any, Any]
col1['col'] = {'name': 'bar_id_1',
'type': 'varchar',
'sort_order': 0}
col1['col_dscrpt'] = {'description': 'bar col description'}
col1['col_stats'] = [{'stat_name': 'avg', 'start_epoch': 1, 'end_epoch': 1, 'stat_val': '1'}]
col2 = copy.deepcopy(table_entry) # type: Dict[Any, Any]
col2['col'] = {'name': 'bar_id_2',
'type': 'bigint',
'sort_order': 1}
col2['col_dscrpt'] = {'description': 'bar col2 description'}
col2['col_stats'] = [{'stat_name': 'avg', 'start_epoch': 2, 'end_epoch': 2, 'stat_val': '2'}]
table_level_results = MagicMock()
table_level_results.single.return_value = {
'wmk_records': [
{
'key': 'hive://gold.test_schema/test_table/high_watermark/',
'partition_key': 'ds',
'partition_value': 'fake_value',
'create_time': 'fake_time',
},
{
'key': 'hive://gold.test_schema/test_table/low_watermark/',
'partition_key': 'ds',
'partition_value': 'fake_value',
'create_time': 'fake_time',
}
],
'application': {
'application_url': 'airflow_host/admin/airflow/tree?dag_id=test_table',
'description': 'DAG generating a table',
'name': 'Airflow',
'id': 'dag/task_id'
},
'last_updated_timestamp': 1,
'owner_records': [
{
'key': 'tester@lyft.com',
'email': 'tester@lyft.com'
}
],
'tag_records': [
{
'key': 'test',
'tag_type': 'default'
}
],
'src': {
'source': '/source_file_loc',
'key': 'some key',
'source_type': 'github'
}
}
table_writer = {
'application_url': 'airflow_host/admin/airflow/tree?dag_id=test_table',
'description': 'DAG generating a table',
'name': 'Airflow',
'id': 'dag/task_id'
}
last_updated_timestamp = '01'
self.col_usage_return_value = [
col1,
col2
]
self.table_level_return_value = table_level_results
self.table_writer = table_writer
self.last_updated_timestamp = last_updated_timestamp
def tearDown(self) -> None:
pass
def test_get_table(self) -> None:
with patch.object(GraphDatabase, 'driver'), patch.object(Neo4jProxy, '_execute_cypher_query') as mock_execute:
mock_execute.side_effect = [self.col_usage_return_value, [], self.table_level_return_value]
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
table = neo4j_proxy.get_table(table_uri='dummy_uri')
expected = Table(database='hive', cluster='gold', schema='foo_schema', name='foo_table',
tags=[Tag(tag_name='test', tag_type='default')],
table_readers=[], description='foo description',
watermarks=[Watermark(watermark_type='high_watermark',
partition_key='ds',
partition_value='fake_value',
create_time='fake_time'),
Watermark(watermark_type='low_watermark',
partition_key='ds',
partition_value='fake_value',
create_time='fake_time')],
columns=[Column(name='bar_id_1', description='bar col description', col_type='varchar',
sort_order=0, stats=[Statistics(start_epoch=1,
end_epoch=1,
stat_type='avg',
stat_val='1')]),
Column(name='bar_id_2', description='bar col2 description', col_type='bigint',
sort_order=1, stats=[Statistics(start_epoch=2,
end_epoch=2,
stat_type='avg',
stat_val='2')])],
owners=[User(email='tester@lyft.com')],
table_writer=Application(application_url=self.table_writer['application_url'],
description=self.table_writer['description'],
name=self.table_writer['name'],
id=self.table_writer['id']),
last_updated_timestamp=1,
source=Source(source='/source_file_loc',
source_type='github'),
is_view=False)
self.assertEqual(str(expected), str(table))
def test_get_table_view_only(self) -> None:
col_usage_return_value = copy.deepcopy(self.col_usage_return_value)
for col in col_usage_return_value:
col['tbl']['is_view'] = True
with patch.object(GraphDatabase, 'driver'), patch.object(Neo4jProxy, '_execute_cypher_query') as mock_execute:
mock_execute.side_effect = [col_usage_return_value, [], self.table_level_return_value]
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
table = neo4j_proxy.get_table(table_uri='dummy_uri')
expected = Table(database='hive', cluster='gold', schema='foo_schema', name='foo_table',
tags=[Tag(tag_name='test', tag_type='default')],
table_readers=[], description='foo description',
watermarks=[Watermark(watermark_type='high_watermark',
partition_key='ds',
partition_value='fake_value',
create_time='fake_time'),
Watermark(watermark_type='low_watermark',
partition_key='ds',
partition_value='fake_value',
create_time='fake_time')],
columns=[Column(name='bar_id_1', description='bar col description', col_type='varchar',
sort_order=0, stats=[Statistics(start_epoch=1,
end_epoch=1,
stat_type='avg',
stat_val='1')]),
Column(name='bar_id_2', description='bar col2 description', col_type='bigint',
sort_order=1, stats=[Statistics(start_epoch=2,
end_epoch=2,
stat_type='avg',
stat_val='2')])],
owners=[User(email='tester@lyft.com')],
table_writer=Application(application_url=self.table_writer['application_url'],
description=self.table_writer['description'],
name=self.table_writer['name'],
id=self.table_writer['id']),
last_updated_timestamp=1,
source=Source(source='/source_file_loc',
source_type='github'),
is_view=True)
self.assertEqual(str(expected), str(table))
def test_get_table_with_valid_description(self) -> None:
"""
Test description is returned for table
:return:
"""
with patch.object(GraphDatabase, 'driver'), patch.object(Neo4jProxy, '_execute_cypher_query') as mock_execute:
mock_execute.return_value.single.return_value = dict(description='sample description')
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
table_description = neo4j_proxy.get_table_description(table_uri='test_table')
table_description_query = textwrap.dedent("""
MATCH (tbl:Table {key: $tbl_key})-[:DESCRIPTION]->(d:Description)
RETURN d.description AS description;
""")
mock_execute.assert_called_with(statement=table_description_query,
param_dict={'tbl_key': 'test_table'})
self.assertEquals(table_description, 'sample description')
def test_get_table_with_no_description(self) -> None:
"""
Test None is returned for table with no description
:return:
"""
with patch.object(GraphDatabase, 'driver'), patch.object(Neo4jProxy, '_execute_cypher_query') as mock_execute:
mock_execute.return_value.single.return_value = None
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
table_description = neo4j_proxy.get_table_description(table_uri='test_table')
table_description_query = textwrap.dedent("""
MATCH (tbl:Table {key: $tbl_key})-[:DESCRIPTION]->(d:Description)
RETURN d.description AS description;
""")
mock_execute.assert_called_with(statement=table_description_query,
param_dict={'tbl_key': 'test_table'})
self.assertIsNone(table_description)
def test_put_table_description(self) -> None:
"""
Test updating table description
:return:
"""
with patch.object(GraphDatabase, 'driver') as mock_driver:
mock_session = MagicMock()
mock_driver.return_value.session.return_value = mock_session
mock_transaction = MagicMock()
mock_session.begin_transaction.return_value = mock_transaction
mock_run = MagicMock()
mock_transaction.run = mock_run
mock_commit = MagicMock()
mock_transaction.commit = mock_commit
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
neo4j_proxy.put_table_description(table_uri='test_table',
description='test_description')
self.assertEquals(mock_run.call_count, 2)
self.assertEquals(mock_commit.call_count, 1)
def test_get_column_with_valid_description(self) -> None:
"""
Test description is returned for column
:return:
"""
with patch.object(GraphDatabase, 'driver'), patch.object(Neo4jProxy, '_execute_cypher_query') as mock_execute:
mock_execute.return_value.single.return_value = dict(description='sample description')
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
col_description = neo4j_proxy.get_column_description(table_uri='test_table',
column_name='test_column')
column_description_query = textwrap.dedent("""
MATCH (tbl:Table {key: $tbl_key})-[:COLUMN]->(c:Column {name: $column_name})-[:DESCRIPTION]->(d:Description)
RETURN d.description AS description;
""")
mock_execute.assert_called_with(statement=column_description_query,
param_dict={'tbl_key': 'test_table',
'column_name': 'test_column'})
self.assertEquals(col_description, 'sample description')
def test_get_column_with_no_description(self) -> None:
"""
Test None is returned for column with no description
:return:
"""
with patch.object(GraphDatabase, 'driver'), patch.object(Neo4jProxy, '_execute_cypher_query') as mock_execute:
mock_execute.return_value.single.return_value = None
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
col_description = neo4j_proxy.get_column_description(table_uri='test_table',
column_name='test_column')
column_description_query = textwrap.dedent("""
MATCH (tbl:Table {key: $tbl_key})-[:COLUMN]->(c:Column {name: $column_name})-[:DESCRIPTION]->(d:Description)
RETURN d.description AS description;
""")
mock_execute.assert_called_with(statement=column_description_query,
param_dict={'tbl_key': 'test_table',
'column_name': 'test_column'})
self.assertIsNone(col_description)
def test_put_column_description(self) -> None:
"""
Test updating column description
:return:
"""
with patch.object(GraphDatabase, 'driver') as mock_driver:
mock_session = MagicMock()
mock_driver.return_value.session.return_value = mock_session
mock_transaction = MagicMock()
mock_session.begin_transaction.return_value = mock_transaction
mock_run = MagicMock()
mock_transaction.run = mock_run
mock_commit = MagicMock()
mock_transaction.commit = mock_commit
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
neo4j_proxy.put_column_description(table_uri='test_table',
column_name='test_column',
description='test_description')
self.assertEquals(mock_run.call_count, 2)
self.assertEquals(mock_commit.call_count, 1)
def test_add_owner(self) -> None:
with patch.object(GraphDatabase, 'driver') as mock_driver:
mock_session = MagicMock()
mock_driver.return_value.session.return_value = mock_session
mock_transaction = MagicMock()
mock_session.begin_transaction.return_value = mock_transaction
mock_run = MagicMock()
mock_transaction.run = mock_run
mock_commit = MagicMock()
mock_transaction.commit = mock_commit
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
neo4j_proxy.add_owner(table_uri='dummy_uri',
owner='tester')
# we call neo4j twice in add_owner call
self.assertEquals(mock_run.call_count, 2)
self.assertEquals(mock_commit.call_count, 1)
def test_delete_owner(self) -> None:
with patch.object(GraphDatabase, 'driver') as mock_driver:
mock_session = MagicMock()
mock_driver.return_value.session.return_value = mock_session
mock_transaction = MagicMock()
mock_session.begin_transaction.return_value = mock_transaction
mock_run = MagicMock()
mock_transaction.run = mock_run
mock_commit = MagicMock()
mock_transaction.commit = mock_commit
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
neo4j_proxy.delete_owner(table_uri='dummy_uri',
owner='tester')
# we only call neo4j once in delete_owner call
self.assertEquals(mock_run.call_count, 1)
self.assertEquals(mock_commit.call_count, 1)
def test_add_tag(self) -> None:
with patch.object(GraphDatabase, 'driver') as mock_driver:
mock_session = MagicMock()
mock_driver.return_value.session.return_value = mock_session
mock_transaction = MagicMock()
mock_session.begin_transaction.return_value = mock_transaction
mock_run = MagicMock()
mock_transaction.run = mock_run
mock_commit = MagicMock()
mock_transaction.commit = mock_commit
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
neo4j_proxy.add_tag(table_uri='dummy_uri',
tag='hive')
# we call neo4j twice in add_tag call
self.assertEquals(mock_run.call_count, 3)
self.assertEquals(mock_commit.call_count, 1)
def test_delete_tag(self) -> None:
with patch.object(GraphDatabase, 'driver') as mock_driver:
mock_session = MagicMock()
mock_driver.return_value.session.return_value = mock_session
mock_transaction = MagicMock()
mock_session.begin_transaction.return_value = mock_transaction
mock_run = MagicMock()
mock_transaction.run = mock_run
mock_commit = MagicMock()
mock_transaction.commit = mock_commit
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
neo4j_proxy.delete_tag(table_uri='dummy_uri',
tag='hive')
# we only call neo4j once in delete_tag call
self.assertEquals(mock_run.call_count, 1)
self.assertEquals(mock_commit.call_count, 1)
def test_get_tags(self) -> None:
with patch.object(GraphDatabase, 'driver'), patch.object(Neo4jProxy, '_execute_cypher_query') as mock_execute:
mock_execute.return_value = [
{'tag_name': {'key': 'tag1'}, 'tag_count': 2},
{'tag_name': {'key': 'tag2'}, 'tag_count': 1}
]
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
actual = neo4j_proxy.get_tags()
expected = [
TagDetail(tag_name='tag1', tag_count=2),
TagDetail(tag_name='tag2', tag_count=1),
]
self.assertEqual(actual.__repr__(), expected.__repr__())
def test_get_neo4j_latest_updated_ts(self) -> None:
with patch.object(GraphDatabase, 'driver'), patch.object(Neo4jProxy, '_execute_cypher_query') as mock_execute:
mock_execute.return_value.single.return_value = {
'ts': {
'latest_timestmap': '1000'
}
}
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
neo4j_last_updated_ts = neo4j_proxy.get_latest_updated_ts()
self.assertEquals(neo4j_last_updated_ts, '1000')
mock_execute.return_value.single.return_value = {
'ts': {
}
}
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
neo4j_last_updated_ts = neo4j_proxy.get_latest_updated_ts()
self.assertIs(neo4j_last_updated_ts, None)
def test_get_popular_tables(self) -> None:
# Test cache hit
with patch.object(GraphDatabase, 'driver'), patch.object(Neo4jProxy, '_execute_cypher_query') as mock_execute:
mock_execute.return_value = [{'table_key': 'foo'}, {'table_key': 'bar'}]
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
self.assertEqual(neo4j_proxy._get_popular_tables_uris(2), ['foo', 'bar'])
self.assertEqual(neo4j_proxy._get_popular_tables_uris(2), ['foo', 'bar'])
self.assertEqual(neo4j_proxy._get_popular_tables_uris(2), ['foo', 'bar'])
self.assertEquals(mock_execute.call_count, 1)
with patch.object(GraphDatabase, 'driver'), patch.object(Neo4jProxy, '_execute_cypher_query') as mock_execute:
mock_execute.return_value = [
{'database_name': 'db', 'cluster_name': 'clstr', 'schema_name': 'sch', 'table_name': 'foo',
'table_description': 'test description'},
{'database_name': 'db', 'cluster_name': 'clstr', 'schema_name': 'sch', 'table_name': 'bar'}
]
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
actual = neo4j_proxy.get_popular_tables(num_entries=2)
expected = [
PopularTable(database='db', cluster='clstr', schema='sch', name='foo', description='test description'),
PopularTable(database='db', cluster='clstr', schema='sch', name='bar'),
]
self.assertEqual(actual.__repr__(), expected.__repr__())
def test_get_users(self) -> None:
with patch.object(GraphDatabase, 'driver'), patch.object(Neo4jProxy, '_execute_cypher_query') as mock_execute:
mock_execute.return_value.single.return_value = {
'user_record': {
'employee_type': 'teamMember',
'full_name': 'test_full_name',
'is_active': 'True',
'github_username': 'test-github',
'slack_id': 'test_id',
'last_name': 'test_last_name',
'first_name': 'test_first_name',
'team_name': 'test_team',
'email': 'test_email',
},
'manager_record': {
'full_name': 'test_manager_fullname'
}
}
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
neo4j_user = neo4j_proxy.get_user_detail(user_id='test_email')
self.assertEquals(neo4j_user.email, 'test_email')
def test_get_resources_by_user_relation(self) -> None:
with patch.object(GraphDatabase, 'driver'), \
patch.object(Neo4jProxy, '_execute_cypher_query') as mock_execute, \
patch.object(Neo4jProxy, 'get_table') as mock_get_table:
mock_execute.return_value.single.return_value = {
'table_records': [
{
'key': 'table_uri',
}
]
}
mock_get_table.return_value = Table(database='hive',
cluster='gold',
schema='foo_schema',
name='foo_table',
tags=[Tag(tag_name='test', tag_type='default')],
table_readers=[], description='foo description',
watermarks=[Watermark(watermark_type='high_watermark',
partition_key='ds',
partition_value='fake_value',
create_time='fake_time'),
Watermark(watermark_type='low_watermark',
partition_key='ds',
partition_value='fake_value',
create_time='fake_time')],
columns=[Column(name='bar_id_1',
description='bar col description',
col_type='varchar',
sort_order=0, stats=[Statistics(start_epoch=1,
end_epoch=1,
stat_type='avg',
stat_val='1')]),
Column(name='bar_id_2',
description='bar col2 description',
col_type='bigint',
sort_order=1, stats=[Statistics(start_epoch=2,
end_epoch=2,
stat_type='avg',
stat_val='2')])],
owners=[User(email='tester@lyft.com')],
table_writer=Application(
application_url=self.table_writer['application_url'],
description=self.table_writer['description'],
name=self.table_writer['name'],
id=self.table_writer['id']),
last_updated_timestamp=1,
source=Source(source='/source_file_loc',
source_type='github'))
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
result = neo4j_proxy.get_table_by_user_relation(user_email='test_user',
relation_type=UserResourceRel.follow)
self.assertEqual(len(result['table']), 1)
self.assertEqual(result['table'][0].name, 'foo_table')
def test_add_resource_relation_by_user(self) -> None:
with patch.object(GraphDatabase, 'driver') as mock_driver:
mock_session = MagicMock()
mock_driver.return_value.session.return_value = mock_session
mock_transaction = MagicMock()
mock_session.begin_transaction.return_value = mock_transaction
mock_run = MagicMock()
mock_transaction.run = mock_run
mock_commit = MagicMock()
mock_transaction.commit = mock_commit
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
neo4j_proxy.add_table_relation_by_user(table_uri='dummy_uri',
user_email='tester',
relation_type=UserResourceRel.follow)
self.assertEquals(mock_run.call_count, 2)
self.assertEquals(mock_commit.call_count, 1)
def test_delete_resource_relation_by_user(self) -> None:
with patch.object(GraphDatabase, 'driver') as mock_driver:
mock_session = MagicMock()
mock_driver.return_value.session.return_value = mock_session
mock_transaction = MagicMock()
mock_session.begin_transaction.return_value = mock_transaction
mock_run = MagicMock()
mock_transaction.run = mock_run
mock_commit = MagicMock()
mock_transaction.commit = mock_commit
neo4j_proxy = Neo4jProxy(host='DOES_NOT_MATTER', port=0000)
neo4j_proxy.delete_table_relation_by_user(table_uri='dummy_uri',
user_email='tester',
relation_type=UserResourceRel.follow)
self.assertEquals(mock_run.call_count, 1)
self.assertEquals(mock_commit.call_count, 1)
if __name__ == '__main__':
unittest.main()