|
1 |
| -from typing import List |
| 1 | +import re |
| 2 | +import unittest |
| 3 | +from typing import List, Optional |
| 4 | + |
| 5 | +import pytest |
2 | 6 |
|
3 | 7 | import datahub.emitter.mce_builder as builder
|
4 | 8 | import datahub.metadata.schema_classes as models
|
5 | 9 | from datahub.emitter.mce_builder import make_dataset_urn, make_schema_field_urn
|
6 | 10 | from datahub.emitter.mcp import MetadataChangeProposalWrapper
|
7 |
| -from datahub.ingestion.api.incremental_lineage_helper import auto_incremental_lineage |
| 11 | +from datahub.ingestion.api.incremental_lineage_helper import ( |
| 12 | + auto_incremental_lineage, |
| 13 | + convert_dashboard_info_to_patch, |
| 14 | +) |
8 | 15 | from datahub.ingestion.api.source_helpers import auto_workunit
|
| 16 | +from datahub.ingestion.api.workunit import MetadataWorkUnit |
9 | 17 | from datahub.ingestion.sink.file import write_metadata_file
|
10 | 18 | from tests.test_helpers import mce_helpers
|
11 | 19 |
|
@@ -183,3 +191,129 @@ def test_incremental_lineage_pass_through(tmp_path, pytestconfig):
|
183 | 191 | mce_helpers.check_golden_file(
|
184 | 192 | pytestconfig=pytestconfig, output_path=test_file, golden_path=golden_file
|
185 | 193 | )
|
| 194 | + |
| 195 | + |
| 196 | +class TestConvertDashboardInfoToPatch(unittest.TestCase): |
| 197 | + def setUp(self): |
| 198 | + self.urn = builder.make_dashboard_urn( |
| 199 | + platform="platform", name="test", platform_instance="instance" |
| 200 | + ) |
| 201 | + self.system_metadata = models.SystemMetadataClass() |
| 202 | + self.aspect = models.DashboardInfoClass( |
| 203 | + title="Test Dashboard", |
| 204 | + description="This is a test dashboard", |
| 205 | + lastModified=models.ChangeAuditStampsClass(), |
| 206 | + ) |
| 207 | + |
| 208 | + def _validate_dashboard_info_patch( |
| 209 | + self, result: Optional[MetadataWorkUnit], expected_aspect_value: bytes |
| 210 | + ) -> None: |
| 211 | + assert ( |
| 212 | + result |
| 213 | + and result.metadata |
| 214 | + and isinstance(result.metadata, models.MetadataChangeProposalClass) |
| 215 | + ) |
| 216 | + mcp: models.MetadataChangeProposalClass = result.metadata |
| 217 | + assert ( |
| 218 | + mcp.entityUrn == self.urn |
| 219 | + and mcp.entityType == "dashboard" |
| 220 | + and mcp.aspectName == "dashboardInfo" |
| 221 | + and mcp.changeType == "PATCH" |
| 222 | + ) |
| 223 | + assert mcp.aspect and isinstance(mcp.aspect, models.GenericAspectClass) |
| 224 | + aspect: models.GenericAspectClass = mcp.aspect |
| 225 | + assert aspect.value == expected_aspect_value |
| 226 | + |
| 227 | + def test_convert_dashboard_info_to_patch_with_no_additional_values(self): |
| 228 | + result = convert_dashboard_info_to_patch( |
| 229 | + self.urn, self.aspect, self.system_metadata |
| 230 | + ) |
| 231 | + self._validate_dashboard_info_patch( |
| 232 | + result=result, |
| 233 | + expected_aspect_value=b'[{"op": "add", "path": "/title", "value": "Test Dashboard"}, {"op": "add", "path": "/description", "value": "This is a test dashboard"}, {"op": "add", "path": "/lastModified", "value": {"created": {"time": 0, "actor": "urn:li:corpuser:unknown"}, "lastModified": {"time": 0, "actor": "urn:li:corpuser:unknown"}}}]', |
| 234 | + ) |
| 235 | + |
| 236 | + def test_convert_dashboard_info_to_patch_with_custom_properties(self): |
| 237 | + self.aspect.customProperties = { |
| 238 | + "key1": "value1", |
| 239 | + "key2": "value2", |
| 240 | + } |
| 241 | + result = convert_dashboard_info_to_patch( |
| 242 | + self.urn, self.aspect, self.system_metadata |
| 243 | + ) |
| 244 | + self._validate_dashboard_info_patch( |
| 245 | + result=result, |
| 246 | + expected_aspect_value=b'[{"op": "add", "path": "/customProperties/key1", "value": "value1"}, {"op": "add", "path": "/customProperties/key2", "value": "value2"}, {"op": "add", "path": "/title", "value": "Test Dashboard"}, {"op": "add", "path": "/description", "value": "This is a test dashboard"}, {"op": "add", "path": "/lastModified", "value": {"created": {"time": 0, "actor": "urn:li:corpuser:unknown"}, "lastModified": {"time": 0, "actor": "urn:li:corpuser:unknown"}}}]', |
| 247 | + ) |
| 248 | + |
| 249 | + def test_convert_dashboard_info_to_patch_with_chart_edges(self): |
| 250 | + self.aspect.chartEdges = [ |
| 251 | + models.EdgeClass( |
| 252 | + destinationUrn=builder.make_chart_urn( |
| 253 | + platform="platform", |
| 254 | + name="target-test", |
| 255 | + platform_instance="instance", |
| 256 | + ), |
| 257 | + ) |
| 258 | + ] |
| 259 | + result = convert_dashboard_info_to_patch( |
| 260 | + self.urn, self.aspect, self.system_metadata |
| 261 | + ) |
| 262 | + self._validate_dashboard_info_patch( |
| 263 | + result, |
| 264 | + b'[{"op": "add", "path": "/chartEdges/urn:li:chart:(platform,instance.target-test)", "value": {"destinationUrn": "urn:li:chart:(platform,instance.target-test)"}}, {"op": "add", "path": "/title", "value": "Test Dashboard"}, {"op": "add", "path": "/description", "value": "This is a test dashboard"}, {"op": "add", "path": "/lastModified", "value": {"created": {"time": 0, "actor": "urn:li:corpuser:unknown"}, "lastModified": {"time": 0, "actor": "urn:li:corpuser:unknown"}}}]', |
| 265 | + ) |
| 266 | + |
| 267 | + def test_convert_dashboard_info_to_patch_with_chart_edges_no_chart(self): |
| 268 | + self.aspect.chartEdges = [ |
| 269 | + models.EdgeClass( |
| 270 | + destinationUrn=builder.make_dashboard_urn( |
| 271 | + platform="platform", |
| 272 | + name="target-test", |
| 273 | + platform_instance="instance", |
| 274 | + ), |
| 275 | + ) |
| 276 | + ] |
| 277 | + with pytest.raises( |
| 278 | + ValueError, |
| 279 | + match=re.escape( |
| 280 | + "add_chart_edge: urn:li:dashboard:(platform,instance.target-test) is not of type chart" |
| 281 | + ), |
| 282 | + ): |
| 283 | + convert_dashboard_info_to_patch(self.urn, self.aspect, self.system_metadata) |
| 284 | + |
| 285 | + def test_convert_dashboard_info_to_patch_with_dashboards(self): |
| 286 | + self.aspect.dashboards = [ |
| 287 | + models.EdgeClass( |
| 288 | + destinationUrn=builder.make_dashboard_urn( |
| 289 | + platform="platform", |
| 290 | + name="target-test", |
| 291 | + platform_instance="instance", |
| 292 | + ), |
| 293 | + ) |
| 294 | + ] |
| 295 | + result = convert_dashboard_info_to_patch( |
| 296 | + self.urn, self.aspect, self.system_metadata |
| 297 | + ) |
| 298 | + self._validate_dashboard_info_patch( |
| 299 | + result, |
| 300 | + b'[{"op": "add", "path": "/title", "value": "Test Dashboard"}, {"op": "add", "path": "/description", "value": "This is a test dashboard"}, {"op": "add", "path": "/dashboards/urn:li:dashboard:(platform,instance.target-test)", "value": {"destinationUrn": "urn:li:dashboard:(platform,instance.target-test)"}}, {"op": "add", "path": "/lastModified", "value": {"created": {"time": 0, "actor": "urn:li:corpuser:unknown"}, "lastModified": {"time": 0, "actor": "urn:li:corpuser:unknown"}}}]', |
| 301 | + ) |
| 302 | + |
| 303 | + def test_convert_dashboard_info_to_patch_with_dashboards_no_dashboard(self): |
| 304 | + self.aspect.dashboards = [ |
| 305 | + models.EdgeClass( |
| 306 | + destinationUrn=builder.make_chart_urn( |
| 307 | + platform="platform", |
| 308 | + name="target-test", |
| 309 | + platform_instance="instance", |
| 310 | + ), |
| 311 | + ) |
| 312 | + ] |
| 313 | + with pytest.raises( |
| 314 | + ValueError, |
| 315 | + match=re.escape( |
| 316 | + "add_dashboard: urn:li:chart:(platform,instance.target-test) is not of type dashboard" |
| 317 | + ), |
| 318 | + ): |
| 319 | + convert_dashboard_info_to_patch(self.urn, self.aspect, self.system_metadata) |
0 commit comments