@@ -27,6 +27,31 @@ def test_yaml_file():
27
27
temp_file .unlink ()
28
28
29
29
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
+
30
55
@pytest .fixture
31
56
def malformed_yaml_file ():
32
57
"""Creates a temporary malformed yaml file for testing."""
@@ -219,30 +244,29 @@ def test_multiple_datasets_in_file(self, mock_dataset, test_yaml_file):
219
244
mock_dataset2 .to_yaml .assert_called_once ()
220
245
221
246
@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 ):
223
248
mock_graph = MagicMock ()
249
+ mock_graph .exists .return_value = True
224
250
mock_get_default_graph .return_value .__enter__ .return_value = mock_graph
225
251
226
- test_file = TEST_RESOURCES_DIR / "dataset_dry_run.yaml"
227
-
228
252
runner = CliRunner ()
229
253
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 )]
231
255
)
232
256
233
257
# Verify
234
258
assert result .exit_code == 0
235
259
assert not mock_get_default_graph .emit .called
236
260
237
261
@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 ):
239
263
mock_graph = MagicMock ()
264
+ mock_graph .exists .return_value = True
240
265
mock_get_default_graph .return_value .__enter__ .return_value = mock_graph
241
266
242
- test_file = TEST_RESOURCES_DIR / "dataset_dry_run_bad_type.yaml"
243
267
runner = CliRunner ()
244
268
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 )]
246
270
)
247
271
248
272
# Verify
@@ -251,29 +275,72 @@ def test_dry_run_sync_fail(self, mock_get_default_graph):
251
275
assert "Type bad_type is not a valid primitive type" in result .output
252
276
253
277
@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 ):
255
279
mock_graph = MagicMock ()
280
+ mock_graph .exists .return_value = False
256
281
mock_get_default_graph .return_value .__enter__ .return_value = mock_graph
257
282
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
259
298
260
299
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 )])
262
301
263
302
# Verify
264
303
assert result .exit_code == 0
265
304
assert mock_graph .emit .called
266
305
267
306
@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 ):
269
322
mock_graph = MagicMock ()
323
+ mock_graph .exists .return_value = True
270
324
mock_get_default_graph .return_value .__enter__ .return_value = mock_graph
271
325
272
- test_file = TEST_RESOURCES_DIR / "dataset_dry_run_bad_type.yaml"
273
326
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 )])
275
328
276
329
# Verify
277
330
assert result .exit_code != 0
278
331
assert not mock_get_default_graph .emit .called
279
332
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
+
0 commit comments