|
| 1 | +# pylint:disable=redefined-outer-name |
| 2 | + |
| 3 | +import mock |
| 4 | +import pytest |
| 5 | +from click.testing import CliRunner |
| 6 | +from databricks_cli.unity_catalog.utils import mc_pretty_format |
| 7 | + |
| 8 | +from databricks_cli.unity_catalog import lineage_cli |
| 9 | +from tests.utils import provide_conf |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture() |
| 13 | +def lineage_api_mock(): |
| 14 | + with mock.patch('databricks_cli.unity_catalog.lineage_cli.UnityCatalogApi') as uc_api_mock: |
| 15 | + _lineage_api_mock = mock.MagicMock() |
| 16 | + uc_api_mock.return_value = _lineage_api_mock |
| 17 | + yield _lineage_api_mock |
| 18 | + |
| 19 | + |
| 20 | +RUN_PAGE_URL = '/lineage-tracking/column-lineage/get' |
| 21 | +TABLE_NAME = 'main.lineage.source' |
| 22 | +COLUMN_NAME = 'price' |
| 23 | + |
| 24 | +EXPECTED_OUTPUT = '''digraph "lineage graph of main.lineage.source" { |
| 25 | +\t"main.lineage.source" -> "main.lineage.target1","main.lineage.target2","main.lineage.target3"; |
| 26 | +\t"main.lineage.target1" -> "main.lineage.target3"; |
| 27 | +\t"main.lineage.target3" -> "main.lineage.target4"; |
| 28 | +}''' |
| 29 | + |
| 30 | + |
| 31 | +@provide_conf |
| 32 | +def test_get_table_lineage(lineage_api_mock): |
| 33 | + """ |
| 34 | + +--------------------------+ |
| 35 | + | | |
| 36 | + | v |
| 37 | + | +--->target1------->target3------>target4 |
| 38 | + | | |
| 39 | + source-| |
| 40 | + | |
| 41 | + +--->target2 |
| 42 | + """ |
| 43 | + with mock.patch('databricks_cli.unity_catalog.lineage_cli.click.echo') as echo_mock: |
| 44 | + # lineage_api_mock.list_lineages_by_table_recursive.return_value = { |
| 45 | + # 'source': ['target1', 'target2', 'target3'], |
| 46 | + # 'target1': ['target3'], |
| 47 | + # 'target3': ['target4'] |
| 48 | + # } |
| 49 | + lineage_api_mock.list_lineages_by_table.side_effect = [ |
| 50 | + { |
| 51 | + 'downstream_tables': |
| 52 | + [ |
| 53 | + {'catalog_name': 'main', 'schema_name': 'lineage', 'name': 'target1'}, |
| 54 | + {'catalog_name': 'main', 'schema_name': 'lineage', 'name': 'target2'}, |
| 55 | + {'catalog_name': 'main', 'schema_name': 'lineage', 'name': 'target3'} |
| 56 | + ], |
| 57 | + 'upstream_tables': [] |
| 58 | + }, # source downstream |
| 59 | + |
| 60 | + { |
| 61 | + 'downstream_tables': |
| 62 | + [ |
| 63 | + {'catalog_name': 'main', 'schema_name': 'lineage', 'name': 'target3'} |
| 64 | + ], |
| 65 | + 'upstream_tables': [] |
| 66 | + }, # target1 downstream |
| 67 | + |
| 68 | + {}, # target2 downstream |
| 69 | + |
| 70 | + { |
| 71 | + 'downstream_tables': |
| 72 | + [ |
| 73 | + {'catalog_name': 'main', 'schema_name': 'lineage', 'name': 'target4'} |
| 74 | + ], |
| 75 | + 'upstream_tables': [] |
| 76 | + }, # target3 downstream |
| 77 | + |
| 78 | + {}, # target4 downstream |
| 79 | + |
| 80 | + { |
| 81 | + 'downstream_tables': |
| 82 | + [ |
| 83 | + {'catalog_name': 'main', 'schema_name': 'lineage', 'name': 'target1'}, |
| 84 | + {'catalog_name': 'main', 'schema_name': 'lineage', 'name': 'target2'}, |
| 85 | + {'catalog_name': 'main', 'schema_name': 'lineage', 'name': 'target3'} |
| 86 | + ], |
| 87 | + 'upstream_tables': [] |
| 88 | + }, # 1st time downstream |
| 89 | + ] |
| 90 | + runner = CliRunner() |
| 91 | + runner.invoke( |
| 92 | + cli=lineage_cli.list_table_lineages_cli, |
| 93 | + args=['--table-name', TABLE_NAME, '--level', 3] |
| 94 | + ) |
| 95 | + assert echo_mock.call_args[0][0] == EXPECTED_OUTPUT |
| 96 | + |
| 97 | + |
| 98 | +EMPTY_LINEAGE_OUTPUT = '''digraph "lineage graph of main.lineage.source" { |
| 99 | +\t |
| 100 | +}''' |
| 101 | + |
| 102 | + |
| 103 | +@provide_conf |
| 104 | +def test_get_table_lineage_with_empty_result(lineage_api_mock): |
| 105 | + with mock.patch('databricks_cli.unity_catalog.lineage_cli.click.echo') as echo_mock: |
| 106 | + lineage_api_mock.list_lineages_by_table_recursive.return_value = {} |
| 107 | + runner = CliRunner() |
| 108 | + runner.invoke(cli=lineage_cli.list_table_lineages_cli, args=['--table-name', TABLE_NAME]) |
| 109 | + assert echo_mock.call_args[0][0] == EMPTY_LINEAGE_OUTPUT |
| 110 | + |
| 111 | + |
| 112 | +COLUMN_LINEAGE_OUTPUT = '''{ |
| 113 | + "downstream_cols": [ |
| 114 | + { |
| 115 | + "workspace_id": 6051921418418893, |
| 116 | + "table_type": "TABLE", |
| 117 | + "catalog_name": "main", |
| 118 | + "table_name": "dinner_price", |
| 119 | + "schema_name": "lineage", |
| 120 | + "name": "full_menu" |
| 121 | + } |
| 122 | + ], |
| 123 | + "upstream_cols": [ |
| 124 | + { |
| 125 | + "workspace_id": 6051921418418893, |
| 126 | + "table_type": "TABLE", |
| 127 | + "catalog_name": "main", |
| 128 | + "table_name": "menu", |
| 129 | + "schema_name": "lineage", |
| 130 | + "name": "app" |
| 131 | + }, |
| 132 | + { |
| 133 | + "workspace_id": 6051921418418893, |
| 134 | + "table_type": "TABLE", |
| 135 | + "catalog_name": "main", |
| 136 | + "table_name": "menu", |
| 137 | + "schema_name": "lineage", |
| 138 | + "name": "desert" |
| 139 | + } |
| 140 | + ] |
| 141 | + }''' |
| 142 | + |
| 143 | + |
| 144 | +@provide_conf |
| 145 | +def test_get_column_lineage(lineage_api_mock): |
| 146 | + with mock.patch('databricks_cli.unity_catalog.lineage_cli.click.echo') as echo_mock: |
| 147 | + lineage_api_mock.list_lineages_by_column.return_value = COLUMN_LINEAGE_OUTPUT |
| 148 | + runner = CliRunner() |
| 149 | + runner.invoke( |
| 150 | + cli=lineage_cli.list_column_lineages_cli, |
| 151 | + args=['--table-name', TABLE_NAME, '--column-name', COLUMN_NAME] |
| 152 | + ) |
| 153 | + assert sorted(echo_mock.call_args[0][0]) == sorted(mc_pretty_format(COLUMN_LINEAGE_OUTPUT)) |
0 commit comments