Skip to content

Commit 63b3111

Browse files
committed
coverage
1 parent b38a5d1 commit 63b3111

File tree

3 files changed

+80
-39
lines changed

3 files changed

+80
-39
lines changed

metadata-ingestion/tests/unit/cli/dataset/test_dataset_cmd.py

+80-13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@ def test_yaml_file():
2727
temp_file.unlink()
2828

2929

30+
@pytest.fixture
31+
def invalid_value_yaml_file():
32+
"""Creates a temporary yaml file - correctly formatted but bad datatype for testing."""
33+
invalid_content = """
34+
## This file is intentionally malformed
35+
- id: user.badformat
36+
platform: hive
37+
schema:
38+
fields:
39+
- id: ip
40+
type: bad_type
41+
description: The IP address
42+
"""
43+
44+
# Create a temporary file
45+
temp_file = TEST_RESOURCES_DIR / "invalid_dataset.yaml.tmp"
46+
with open(temp_file, "w") as f:
47+
f.write(invalid_content)
48+
49+
yield temp_file
50+
51+
# Clean up
52+
if temp_file.exists():
53+
temp_file.unlink()
54+
3055
@pytest.fixture
3156
def malformed_yaml_file():
3257
"""Creates a temporary malformed yaml file for testing."""
@@ -219,30 +244,29 @@ def test_multiple_datasets_in_file(self, mock_dataset, test_yaml_file):
219244
mock_dataset2.to_yaml.assert_called_once()
220245

221246
@patch("datahub.cli.specific.dataset_cli.get_default_graph")
222-
def test_dry_run_sync(self, mock_get_default_graph):
247+
def test_dry_run_sync(self, mock_get_default_graph, test_yaml_file):
223248
mock_graph = MagicMock()
249+
mock_graph.exists.return_value = True
224250
mock_get_default_graph.return_value.__enter__.return_value = mock_graph
225251

226-
test_file = TEST_RESOURCES_DIR / "dataset_dry_run.yaml"
227-
228252
runner = CliRunner()
229253
result = runner.invoke(
230-
dataset, ["sync", "--dry-run", "--to-datahub", "-f", str(test_file)]
254+
dataset, ["sync", "--dry-run", "--to-datahub", "-f", str(test_yaml_file)]
231255
)
232256

233257
# Verify
234258
assert result.exit_code == 0
235259
assert not mock_get_default_graph.emit.called
236260

237261
@patch("datahub.cli.specific.dataset_cli.get_default_graph")
238-
def test_dry_run_sync_fail(self, mock_get_default_graph):
262+
def test_dry_run_sync_fail_bad_type(self, mock_get_default_graph, invalid_value_yaml_file):
239263
mock_graph = MagicMock()
264+
mock_graph.exists.return_value = True
240265
mock_get_default_graph.return_value.__enter__.return_value = mock_graph
241266

242-
test_file = TEST_RESOURCES_DIR / "dataset_dry_run_bad_type.yaml"
243267
runner = CliRunner()
244268
result = runner.invoke(
245-
dataset, ["sync", "--dry-run", "--to-datahub", "-f", str(test_file)]
269+
dataset, ["sync", "--dry-run", "--to-datahub", "-f", str(invalid_value_yaml_file)]
246270
)
247271

248272
# Verify
@@ -251,29 +275,72 @@ def test_dry_run_sync_fail(self, mock_get_default_graph):
251275
assert "Type bad_type is not a valid primitive type" in result.output
252276

253277
@patch("datahub.cli.specific.dataset_cli.get_default_graph")
254-
def test_run_sync(self, mock_get_default_graph):
278+
def test_dry_run_sync_fail_missing_ref(self, mock_get_default_graph, test_yaml_file):
255279
mock_graph = MagicMock()
280+
mock_graph.exists.return_value = False
256281
mock_get_default_graph.return_value.__enter__.return_value = mock_graph
257282

258-
test_file = TEST_RESOURCES_DIR / "dataset_dry_run.yaml"
283+
runner = CliRunner()
284+
result = runner.invoke(
285+
dataset, ["sync", "--dry-run", "--to-datahub", "-f", str(test_yaml_file)]
286+
)
287+
288+
# Verify
289+
assert result.exit_code != 0
290+
assert not mock_get_default_graph.emit.called
291+
assert "missing entity reference" in result.output
292+
293+
@patch("datahub.cli.specific.dataset_cli.get_default_graph")
294+
def test_run_sync(self, mock_get_default_graph, test_yaml_file):
295+
mock_graph = MagicMock()
296+
mock_graph.exists.return_value = True
297+
mock_get_default_graph.return_value.__enter__.return_value = mock_graph
259298

260299
runner = CliRunner()
261-
result = runner.invoke(dataset, ["sync", "--to-datahub", "-f", str(test_file)])
300+
result = runner.invoke(dataset, ["sync", "--to-datahub", "-f", str(test_yaml_file)])
262301

263302
# Verify
264303
assert result.exit_code == 0
265304
assert mock_graph.emit.called
266305

267306
@patch("datahub.cli.specific.dataset_cli.get_default_graph")
268-
def test_run_sync_fail(self, mock_get_default_graph):
307+
def test_run_sync_fail(self, mock_get_default_graph, invalid_value_yaml_file):
308+
mock_graph = MagicMock()
309+
mock_graph.exists.return_value = True
310+
mock_get_default_graph.return_value.__enter__.return_value = mock_graph
311+
312+
runner = CliRunner()
313+
result = runner.invoke(dataset, ["sync", "--to-datahub", "-f", str(invalid_value_yaml_file)])
314+
315+
# Verify
316+
assert result.exit_code != 0
317+
assert not mock_get_default_graph.emit.called
318+
assert "is not a valid primitive type" in result.output
319+
320+
@patch("datahub.cli.specific.dataset_cli.get_default_graph")
321+
def test_run_upsert_fail(self, mock_get_default_graph, invalid_value_yaml_file):
269322
mock_graph = MagicMock()
323+
mock_graph.exists.return_value = True
270324
mock_get_default_graph.return_value.__enter__.return_value = mock_graph
271325

272-
test_file = TEST_RESOURCES_DIR / "dataset_dry_run_bad_type.yaml"
273326
runner = CliRunner()
274-
result = runner.invoke(dataset, ["sync", "--to-datahub", "-f", str(test_file)])
327+
result = runner.invoke(dataset, ["upsert", "-f", str(invalid_value_yaml_file)])
275328

276329
# Verify
277330
assert result.exit_code != 0
278331
assert not mock_get_default_graph.emit.called
279332
assert "is not a valid primitive type" in result.output
333+
334+
@patch("datahub.cli.specific.dataset_cli.get_default_graph")
335+
def test_sync_from_datahub_fail(self, mock_get_default_graph, test_yaml_file):
336+
mock_graph = MagicMock()
337+
mock_graph.exists.return_value = False
338+
mock_get_default_graph.return_value.__enter__.return_value = mock_graph
339+
340+
runner = CliRunner()
341+
result = runner.invoke(dataset, ["sync", "--dry-run", "--from-datahub", "-f", str(test_yaml_file)])
342+
343+
# Verify
344+
assert result.exit_code != 0
345+
assert "does not exist" in result.output
346+

metadata-ingestion/tests/unit/cli/dataset/test_resources/dataset_dry_run.yaml

-13
This file was deleted.

metadata-ingestion/tests/unit/cli/dataset/test_resources/dataset_dry_run_bad_type.yaml

-13
This file was deleted.

0 commit comments

Comments
 (0)