Teddy 205850be79
[MINOR] fix antlr parser definition for entity link (#15758)
* fix: update antlr regex for entity fqn

* fix: update antlr rule to allow single character

* style: ran python linting

* fix: updated antlr token for NAME_OR_FQN
2024-04-03 08:34:43 +00:00

148 lines
4.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Copyright 2021 Collate
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
test entity link utils
"""
import pytest
from antlr4.error.Errors import ParseCancellationException
from metadata.utils.entity_link import get_decoded_column, get_table_or_column_fqn
@pytest.mark.parametrize(
"entity_link,expected",
[
(
"<#E::table::rds.dev.dbt_jaffle.column_w_space::columns::first+name>",
"first name",
),
(
"<#E::table::rds.dev.dbt_jaffle.column_w_space::columns::last_name>",
"last_name",
),
(
"<#E::table::rds.dev.dbt_jaffle.column_w_space::columns::随机的>",
"随机的",
),
],
)
def test_get_decoded_column(entity_link, expected):
"""test get_decoded_column return expected values"""
assert get_decoded_column(entity_link) == expected
@pytest.mark.parametrize(
"entity_link,fqn",
[
pytest.param(
"<#E::table::rds.dev.dbt_jaffle.customers::columns::number_of_orders>",
"rds.dev.dbt_jaffle.customers.number_of_orders",
id="valid_entity_link1",
),
pytest.param(
"<#E::table::rds.dev.dbt_jaffle.customers::columns::a>",
"rds.dev.dbt_jaffle.customers.a",
id="valid_entity_link2",
),
pytest.param(
"<#E::table::rds.dev.dbt_jaffle.customers::columns::阿比西]/>",
"rds.dev.dbt_jaffle.customers.阿比西]/",
id="valid_entity_link3",
),
pytest.param(
"<#E::table::rds.dev.dbt_jaffle.customers::columns::阿>",
"rds.dev.dbt_jaffle.customers.阿",
id="valid_entity_link4",
),
pytest.param(
"<#E::table::rds.dev.dbt_jaffle.customers::columns::14532>",
"rds.dev.dbt_jaffle.customers.14532",
id="valid_entity_link5",
),
pytest.param(
"<#E::table::rds.dev.dbt_jaffle.customers::columns::1>",
"rds.dev.dbt_jaffle.customers.1",
id="valid_entity_link6",
),
pytest.param(
"<#E::table::rds.dev.dbt_jaffle.customers::columns::абц%>",
"rds.dev.dbt_jaffle.customers.абц%",
id="valid_entity_link7",
),
pytest.param(
"<#E::table::rds.dev.dbt_jaffle.customers::columns::б>",
"rds.dev.dbt_jaffle.customers.б",
id="valid_entity_link8",
),
pytest.param(
"<#E::table::rds.dev.dbt_jaffle.customers>",
"rds.dev.dbt_jaffle.customers",
id="valid_entity_link9",
),
pytest.param(
"<#E::dashboard::rds.dev.dbt_jaffle>.customers>",
"rds.dev.dbt_jaffle>.customers",
id="valid_entity_link10",
),
],
)
def test_valid_get_table_or_column_fqn(entity_link, fqn):
"""test get_table_or_column_fqn return expected values
Args:
entity_link (str): pytest param input
fqn (str): pytest param input
"""
assert get_table_or_column_fqn(entity_link) == fqn
@pytest.mark.parametrize(
"entity_link,error",
[
pytest.param(
"<#E::table::rds.dev.dbt_jaffle.customers::foo::number_of_orders>",
ParseCancellationException,
id="invalid_entity_link1",
),
pytest.param(
"<#E::table::rds.dev.dbt_jaffle.customers::columns::grea:>hdfwsd>",
ParseCancellationException,
id="invalid_entity_link2",
),
pytest.param(
"<#E::table::rds.dev.:dbt_jaffle.customers::columns::阿>",
ParseCancellationException,
id="invalid_entity_link3",
),
pytest.param(
"<#E::table::rds.dev.dbt_jaffle.customers::columns>",
ValueError,
id="invalid_entity_link4",
),
pytest.param(
"<#E::table::rds.dev.dbt_jaffle.customers::columns::>",
ParseCancellationException,
id="invalid_entity_link5",
),
],
)
def test_invalid_get_table_or_column_fqn(entity_link, error):
"""test get_table_or_column_fqn return expected values
Args:
entity_link (str): pytest param input
fqn (str): pytest param input
"""
with pytest.raises(error):
get_table_or_column_fqn(entity_link)