From 4fccede3dee2e1ad14d19032b0742b6523b2a302 Mon Sep 17 00:00:00 2001 From: Mat Moore Date: Thu, 7 Nov 2024 15:53:32 +0000 Subject: [PATCH] feat: add classes to generate table CSVs --- home/service/details_csv.py | 69 ++++++++++ .../data_platform_catalogue/entities.py | 2 +- tests/home/service/test_details_csv.py | 122 ++++++++++++++++++ 3 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 home/service/details_csv.py create mode 100644 tests/home/service/test_details_csv.py diff --git a/home/service/details_csv.py b/home/service/details_csv.py new file mode 100644 index 00000000..69b8a2ff --- /dev/null +++ b/home/service/details_csv.py @@ -0,0 +1,69 @@ +from home.service.details import ( + DashboardDetailsService, + DatabaseDetailsService, + DatasetDetailsService, +) + + +class DatasetDetailsCsvFormatter: + def __init__(self, details_service: DatasetDetailsService): + self.details_service = details_service + + def data(self): + return [ + ( + column.name, + column.display_name, + column.is_primary_key, + column.type, + column.nullable, + column.description, + ) + for column in self.details_service.table_metadata.column_details + ] + + def headers(self): + return [ + "name", + "display_name", + "is_primary_key", + "type", + "nullable", + "description", + ] + + +class DatabaseDetailsCsvFormatter: + def __init__(self, details_service: DatabaseDetailsService): + self.details_service = details_service + + def data(self): + return [ + ( + table.entity_ref.urn, + table.entity_ref.display_name, + table.description, + ) + for table in self.details_service.entities_in_database + ] + + def headers(self): + return [ + "urn", + "display_name", + "description", + ] + + +class DashboardDetailsCsvFormatter: + def __init__(self, details_service: DashboardDetailsService): + self.details_service = details_service + + def data(self): + return [ + (chart.entity_ref.urn, chart.entity_ref.display_name, chart.description) + for chart in self.details_service.children + ] + + def headers(self): + return ["urn", "display_name", "description"] diff --git a/lib/datahub-client/data_platform_catalogue/entities.py b/lib/datahub-client/data_platform_catalogue/entities.py index 6f187776..002a6ca8 100644 --- a/lib/datahub-client/data_platform_catalogue/entities.py +++ b/lib/datahub-client/data_platform_catalogue/entities.py @@ -265,7 +265,7 @@ class EntitySummary(BaseModel): ) description: str = Field(description="A description of the entity") entity_type: str = Field( - description="indicates the tpye of entity that is summarised" + description="indicates the type of entity that is summarised" ) tags: list[TagRef] = Field(description="Any tags associated with the entity") diff --git a/tests/home/service/test_details_csv.py b/tests/home/service/test_details_csv.py new file mode 100644 index 00000000..bf079ee5 --- /dev/null +++ b/tests/home/service/test_details_csv.py @@ -0,0 +1,122 @@ +from unittest.mock import MagicMock + +from data_platform_catalogue.entities import Column, EntityRef, EntitySummary + +from home.service.details import ( + DashboardDetailsService, + DatabaseDetailsService, + DatasetDetailsService, +) +from home.service.details_csv import ( + DashboardDetailsCsvFormatter, + DatabaseDetailsCsvFormatter, + DatasetDetailsCsvFormatter, +) + + +def test_dataset_details_csv_formatter(example_table): + details_service = MagicMock(spec=DatasetDetailsService) + columns = [ + Column( + name="foo", + display_name="Foo", + type="string", + description="an example", + nullable=False, + is_primary_key=True, + ), + Column( + name="bar", + display_name="Bar", + type="integer", + description="another example", + nullable=True, + is_primary_key=False, + ), + ] + details_service.table_metadata = example_table + example_table.column_details = columns + csv_formatter = DatasetDetailsCsvFormatter(details_service) + + assert csv_formatter.headers() == [ + "name", + "display_name", + "is_primary_key", + "type", + "nullable", + "description", + ] + assert csv_formatter.data() == [ + ( + "foo", + "Foo", + True, + "string", + False, + "an example", + ), + ( + "bar", + "Bar", + False, + "integer", + True, + "another example", + ), + ] + + +def test_database_details_csv_formatter(example_database): + tables = [ + EntitySummary( + entity_ref=EntityRef(display_name="foo", urn="urn:foo"), + description="an example", + entity_type="Table", + tags=[], + ), + EntitySummary( + entity_ref=EntityRef(display_name="bar", urn="urn:bar"), + description="another example", + entity_type="Table", + tags=[], + ), + ] + + details_service = MagicMock(spec=DatabaseDetailsService) + details_service.entities_in_database = tables + + csv_formatter = DatabaseDetailsCsvFormatter(details_service) + + assert csv_formatter.headers() == ["urn", "display_name", "description"] + assert csv_formatter.data() == [ + ("urn:foo", "foo", "an example"), + ("urn:bar", "bar", "another example"), + ] + + +def test_dashboard_details_csv_formatter(example_dashboard): + charts = [ + EntitySummary( + entity_ref=EntityRef(display_name="foo", urn="urn:foo"), + description="an example", + entity_type="Chart", + tags=[], + ), + EntitySummary( + entity_ref=EntityRef(display_name="bar", urn="urn:bar"), + description="another example", + entity_type="Chart", + tags=[], + ), + ] + + details_service = MagicMock(spec=DashboardDetailsService) + details_service.children = charts + + csv_formatter = DashboardDetailsCsvFormatter(details_service) + + assert csv_formatter.headers() == ["urn", "display_name", "description"] + assert csv_formatter.data() == [ + ("urn:foo", "foo", "an example"), + ("urn:bar", "bar", "another example"), + ]