-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_details.py
284 lines (248 loc) · 9.31 KB
/
test_details.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import pytest
from data_platform_catalogue.entities import (
AccessInformation,
Chart,
CustomEntityProperties,
Dashboard,
Database,
DomainRef,
EntityRef,
EntitySummary,
FurtherInformation,
Governance,
OwnerRef,
RelationshipType,
)
from home.service.details import (
ChartDetailsService,
DashboardDetailsService,
DatabaseDetailsService,
DatasetDetailsService,
_parse_parent,
is_access_requirements_a_url,
)
from tests.conftest import (
generate_chart_metadata,
generate_dashboard_metadata,
generate_database_metadata,
generate_table_metadata,
)
@pytest.mark.parametrize(
"input, expected_output",
[
(
{
RelationshipType.PARENT: [
EntitySummary(
entity_ref=EntityRef(urn="urn:li:db", display_name="db"),
description="test",
entity_type="database",
tags=[],
)
]
},
EntityRef(urn="urn:li:db", display_name="db"),
),
({}, None),
(
{
RelationshipType.DATA_LINEAGE: [
EntitySummary(
entity_ref=EntityRef(urn="urn:li:db", display_name="db"),
description="test",
entity_type="database",
tags=[],
)
]
},
None,
),
],
)
def test_parse_parent(input, expected_output):
result = _parse_parent(input)
assert result == expected_output
@pytest.mark.parametrize(
"input, expected_output",
[
("122", False),
("https://test.gov.uk", True),
("https://test.gov.uk/data/#readme", True),
("http://test.co.uk", True),
("ftp.example.com/how-to-access.txt", False),
("Just some instructions", False),
("", False),
(123, False),
(None, False),
(["https://test.gov.uk"], False),
],
)
def test_is_access_requirements_a_url(input, expected_output):
result = is_access_requirements_a_url(input)
assert result == expected_output
class TestDatasetDetailsService:
def test_get_context_contains_table_metadata(self, dataset_with_parent):
service = DatasetDetailsService(dataset_with_parent["urn"])
context = service.context
assert context["entity"] == dataset_with_parent["table_metadata"]
def test_get_context_contains_parent(self, mock_catalogue):
parent = {
RelationshipType.PARENT: [
EntitySummary(
entity_ref=EntityRef(
urn="urn:li:container:parent", display_name="parent"
),
description="",
tags=[],
entity_type="DATABASE",
)
],
}
mock_table = generate_table_metadata(relations=parent)
mock_catalogue.get_table_details.return_value = mock_table
service = DatasetDetailsService("urn:li:datsset:test")
context = service.context
assert context["parent_entity"] == EntityRef(
urn="urn:li:container:parent", display_name="parent"
)
def test_get_context_contains_slack(self, mock_catalogue):
custom_properties = CustomEntityProperties(
further_information=FurtherInformation(
dc_slack_channel_name="test",
dc_slack_channel_url="https://test_url.com",
)
)
mock_table = generate_table_metadata(custom_properties=custom_properties)
mock_catalogue.get_table_details.return_value = mock_table
service = DatasetDetailsService("urn:li:datsset:test")
context = service.context
assert context["entity"].custom_properties == custom_properties
assert (
context[
"entity"
].custom_properties.further_information.dc_slack_channel_name
== "test"
)
class TestDatabaseDetailsService:
def test_get_context_database(self, mock_catalogue, example_database):
"""
Tests that the context contains the database metadata returned by the
mock catalogue.
"""
mock_database_name = "example_database"
service = DatabaseDetailsService(mock_database_name)
context = service.context
assert context["entity"] == example_database
def test_get_context_contains_slack(self, mock_catalogue):
"""
Tests that the context contains the slack channel name and URL
from the custom properties of the database
"""
custom_properties = CustomEntityProperties(
further_information=FurtherInformation(
dc_slack_channel_name="test",
dc_slack_channel_url="https://test_url.com",
)
)
mock_database_name = "urn:li:container:fake"
mock_database_metadata = generate_database_metadata(
name=mock_database_name, custom_properties=custom_properties
)
mock_catalogue.get_database_details.return_value = mock_database_metadata
service = DatabaseDetailsService(mock_database_name)
context = service.context
assert context["entity"].custom_properties == custom_properties
assert (
context[
"entity"
].custom_properties.further_information.dc_slack_channel_name
== "test"
)
def test_database_entities_in_context(self, example_database: Database):
service = DatabaseDetailsService("example_database")
context = service.context
expected = example_database.relationships[RelationshipType.CHILD]
assert context["tables"] == expected
class TestChartDetailsService:
def test_get_context(self, mock_catalogue):
chart_metadata = Chart(
urn="urn:li:chart:(justice-data,test)",
name="test",
display_name="test",
fully_qualified_name="test",
description="test",
external_url="https://www.test.com",
domain=DomainRef(urn="LAA", display_name="LAA"),
governance=Governance(
data_owner=OwnerRef(
display_name="", email="Contact email for the user", urn=""
),
data_stewards=[
OwnerRef(
display_name="", email="Contact email for the user", urn=""
)
],
),
platform=EntityRef(urn="", display_name="justice-data"),
)
mock_catalogue.get_chart_details.return_value = chart_metadata
context = ChartDetailsService("urn").context
expected = {
"entity": chart_metadata,
"entity_type": "Chart",
"platform_name": "Justice Data",
"parent_entity": None,
"parent_type": "dashboard",
"h1_value": "test",
"is_access_requirements_a_url": False,
}
assert context == expected
def test_get_context_contains_parent(self, mock_catalogue):
parent = {
RelationshipType.PARENT: [
EntitySummary(
entity_ref=EntityRef(
urn="urn:li:container:parent", display_name="parent"
),
description="",
tags=[],
entity_type="DASHBOARD",
)
],
}
mock_chart = generate_chart_metadata(relations=parent)
mock_catalogue.get_chart_details.return_value = mock_chart
service = ChartDetailsService("urn:li:chart:test")
context = service.context
assert context["parent_entity"] == EntityRef(
urn="urn:li:container:parent", display_name="parent"
)
class TestDashboardDetailsService:
def test_get_context_dashboard(self, mock_catalogue, example_dashboard: Dashboard):
"""
Tests that the context contains the dashboard metadata returned by the
mock catalogue.
"""
mock_dashboard_name = "example_dashboard"
service = DashboardDetailsService(mock_dashboard_name)
context = service.context
assert context["entity"] == example_dashboard
def test_chart_entities_in_context(self, example_dashboard: Dashboard):
service = DashboardDetailsService("example_dashboard")
context = service.context
expected = example_dashboard.relationships[RelationshipType.CHILD]
assert context["charts"] == expected
def test_custom_properties_in_context(self, mock_catalogue):
custom_properties = CustomEntityProperties(
access_information=AccessInformation(
dc_access_requirements="This is a test there's nothing to access"
)
)
mock_dashboard_name = "urn:li:dashboard:fake"
mock_dashboard_metadata = generate_dashboard_metadata(
name=mock_dashboard_name, custom_properties=custom_properties
)
mock_catalogue.get_dashboard_details.return_value = mock_dashboard_metadata
service = DashboardDetailsService(mock_dashboard_name)
context = service.context
assert context["entity"].custom_properties == custom_properties