-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add classes to generate table CSVs
- Loading branch information
Showing
3 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
] |