Skip to content

feat(actions): add DomainNameResolver for human-friendly domain names #14355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions datahub-actions/src/datahub_actions/utils/name_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
CorpUserInfoClass,
DashboardInfoClass,
DatasetPropertiesClass,
DomainPropertiesClass,
GlossaryTermInfoClass,
SubTypesClass,
TagPropertiesClass,
Expand Down Expand Up @@ -238,6 +239,21 @@ def get_entity_name(
return super().get_entity_name(entity_urn, datahub_graph)


class DomainNameResolver(DefaultNameResolver):
def get_entity_name(
self, entity_urn: Urn, datahub_graph: Optional[DataHubGraph]
) -> str:
if datahub_graph:
domain_properties: Optional[DomainPropertiesClass] = (
datahub_graph.get_aspect(str(entity_urn), DomainPropertiesClass)
)
if domain_properties and domain_properties.name:
return domain_properties.name

# fall-through to default
return super().get_entity_name(entity_urn, datahub_graph)


class NameResolverRegistry:
def __init__(self):
self.registry = {
Expand All @@ -250,6 +266,7 @@ def __init__(self):
"container": ContainerNameResolver(),
"dataJob": DataJobNameResolver(),
"dataFlow": DataFlowNameResolver(),
"domain": DomainNameResolver(),
}
self.default_resolver = DefaultNameResolver()

Expand Down
95 changes: 95 additions & 0 deletions datahub-actions/tests/unit/utils/test_name_resolver.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests look extremely verbose - I was expecting something more like ~50 lines of code for the tests

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Copyright 2021 Acryl Data, Inc.
#
# 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.

from unittest.mock import Mock

from datahub.metadata.schema_classes import DomainPropertiesClass
from datahub.utilities.urns.urn import Urn
from datahub_actions.utils.name_resolver import (
DomainNameResolver,
_name_resolver_registry,
get_entity_name_from_urn,
get_entity_qualifier_from_urn,
)


def test_domain_name_resolver_without_graph():
"""Test DomainNameResolver fallback behavior when DataHubGraph is None"""
resolver = DomainNameResolver()
domain_urn = Urn.from_string("urn:li:domain:marketing-domain")

entity_name = resolver.get_entity_name(domain_urn, None)

# Should fallback to the domain ID from the URN
assert entity_name == "marketing-domain"


def test_domain_name_resolver_with_graph_no_properties():
"""Test DomainNameResolver when DataHubGraph is available but no properties found"""
resolver = DomainNameResolver()
domain_urn = Urn.from_string("urn:li:domain:marketing-domain")

# Mock DataHubGraph that returns None for domain properties
mock_graph = Mock()
mock_graph.get_aspect.return_value = None

entity_name = resolver.get_entity_name(domain_urn, mock_graph)

# Should call get_aspect with correct parameters
mock_graph.get_aspect.assert_called_once_with(
str(domain_urn), DomainPropertiesClass
)

# Should fallback to the domain ID from the URN
assert entity_name == "marketing-domain"

def test_domain_name_resolver_with_graph_and_properties():
"""Test DomainNameResolver when DataHubGraph returns properties with name"""
resolver = DomainNameResolver()
domain_urn = Urn.from_string("urn:li:domain:marketing-domain")

# Mock DataHubGraph that returns domain properties with a friendly name
mock_graph = Mock()
mock_properties = DomainPropertiesClass(name="Marketing Domain")
mock_graph.get_aspect.return_value = mock_properties

entity_name = resolver.get_entity_name(domain_urn, mock_graph)

# Should call get_aspect with correct parameters
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these comments + docstrings are a bit overzealous. in general, comments should add new information and not just reexplain individual lines of code

mock_graph.get_aspect.assert_called_once_with(
str(domain_urn), DomainPropertiesClass
)

# Should return the friendly name from properties
assert entity_name == "Marketing Domain"
Comment on lines +38 to +75
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these two tests look extremely similar - can you reduce the code duplication?



def test_domain_name_resolver_with_graph_and_empty_string_name():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is also not necessary imo

"""Test DomainNameResolver when domain properties has empty string name"""
resolver = DomainNameResolver()
domain_urn = Urn.from_string("urn:li:domain:marketing-domain")

# Mock DataHubGraph that returns domain properties with empty string name
mock_graph = Mock()
mock_properties = DomainPropertiesClass(
name=""
) # Empty string should be treated as falsy
mock_graph.get_aspect.return_value = mock_properties

entity_name = resolver.get_entity_name(domain_urn, mock_graph)

# Should fallback to the domain ID from the URN
assert entity_name == "marketing-domain"


Loading