forked from datahub-project/datahub
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ingest/gc): Adding test and more checks to gc source (datahub-pro…
- Loading branch information
Showing
2 changed files
with
156 additions
and
23 deletions.
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
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,109 @@ | ||
import unittest | ||
from datetime import datetime, timezone | ||
from unittest.mock import MagicMock, patch | ||
|
||
from datahub.ingestion.api.common import PipelineContext | ||
from datahub.ingestion.source.gc.dataprocess_cleanup import ( | ||
DataJobEntity, | ||
DataProcessCleanup, | ||
DataProcessCleanupConfig, | ||
DataProcessCleanupReport, | ||
) | ||
|
||
|
||
class TestDataProcessCleanup(unittest.TestCase): | ||
def setUp(self): | ||
self.ctx = PipelineContext(run_id="test_run") | ||
self.ctx.graph = MagicMock() | ||
self.config = DataProcessCleanupConfig() | ||
self.report = DataProcessCleanupReport() | ||
self.cleanup = DataProcessCleanup( | ||
self.ctx, self.config, self.report, dry_run=True | ||
) | ||
|
||
@patch( | ||
"datahub.ingestion.source.gc.dataprocess_cleanup.DataProcessCleanup.fetch_dpis" | ||
) | ||
def test_delete_dpi_from_datajobs(self, mock_fetch_dpis): | ||
job = DataJobEntity( | ||
urn="urn:li:dataJob:1", | ||
flow_urn="urn:li:dataFlow:1", | ||
lastIngested=int(datetime.now(timezone.utc).timestamp()), | ||
jobId="job1", | ||
dataPlatformInstance="urn:li:dataPlatformInstance:1", | ||
total_runs=10, | ||
) | ||
mock_fetch_dpis.return_value = [ | ||
{ | ||
"urn": f"urn:li:dataprocessInstance:{i}", | ||
"created": { | ||
"time": int(datetime.now(timezone.utc).timestamp() + i) * 1000 | ||
}, | ||
} | ||
for i in range(10) | ||
] | ||
self.cleanup.delete_dpi_from_datajobs(job) | ||
self.assertEqual(5, self.report.num_aspects_removed) | ||
|
||
@patch( | ||
"datahub.ingestion.source.gc.dataprocess_cleanup.DataProcessCleanup.fetch_dpis" | ||
) | ||
def test_delete_dpi_from_datajobs_without_dpis(self, mock_fetch_dpis): | ||
job = DataJobEntity( | ||
urn="urn:li:dataJob:1", | ||
flow_urn="urn:li:dataFlow:1", | ||
lastIngested=int(datetime.now(timezone.utc).timestamp()), | ||
jobId="job1", | ||
dataPlatformInstance="urn:li:dataPlatformInstance:1", | ||
total_runs=10, | ||
) | ||
mock_fetch_dpis.return_value = [] | ||
self.cleanup.delete_dpi_from_datajobs(job) | ||
self.assertEqual(0, self.report.num_aspects_removed) | ||
|
||
@patch( | ||
"datahub.ingestion.source.gc.dataprocess_cleanup.DataProcessCleanup.fetch_dpis" | ||
) | ||
def test_delete_dpi_from_datajobs_without_dpi_created_time(self, mock_fetch_dpis): | ||
job = DataJobEntity( | ||
urn="urn:li:dataJob:1", | ||
flow_urn="urn:li:dataFlow:1", | ||
lastIngested=int(datetime.now(timezone.utc).timestamp()), | ||
jobId="job1", | ||
dataPlatformInstance="urn:li:dataPlatformInstance:1", | ||
total_runs=10, | ||
) | ||
mock_fetch_dpis.return_value = [ | ||
{"urn": f"urn:li:dataprocessInstance:{i}"} for i in range(10) | ||
] + [ | ||
{ | ||
"urn": "urn:li:dataprocessInstance:11", | ||
"created": {"time": int(datetime.now(timezone.utc).timestamp() * 1000)}, | ||
} | ||
] | ||
self.cleanup.delete_dpi_from_datajobs(job) | ||
self.assertEqual(10, self.report.num_aspects_removed) | ||
|
||
def test_fetch_dpis(self): | ||
assert self.cleanup.ctx.graph | ||
self.cleanup.ctx.graph = MagicMock() | ||
self.cleanup.ctx.graph.execute_graphql.return_value = { | ||
"dataJob": { | ||
"runs": { | ||
"runs": [ | ||
{ | ||
"urn": "urn:li:dataprocessInstance:1", | ||
"created": { | ||
"time": int(datetime.now(timezone.utc).timestamp()) | ||
}, | ||
} | ||
] | ||
} | ||
} | ||
} | ||
dpis = self.cleanup.fetch_dpis("urn:li:dataJob:1", 10) | ||
self.assertEqual(len(dpis), 1) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |