Skip to content

Commit

Permalink
feat: add classes to generate table CSVs
Browse files Browse the repository at this point in the history
  • Loading branch information
MatMoore committed Nov 8, 2024
1 parent 1302c2e commit 4fccede
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 1 deletion.
69 changes: 69 additions & 0 deletions home/service/details_csv.py
Original file line number Diff line number Diff line change
@@ -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"]
2 changes: 1 addition & 1 deletion lib/datahub-client/data_platform_catalogue/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
122 changes: 122 additions & 0 deletions tests/home/service/test_details_csv.py
Original file line number Diff line number Diff line change
@@ -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"),
]

0 comments on commit 4fccede

Please sign in to comment.