|
8 | 8 |
|
9 | 9 | import pytest |
10 | 10 |
|
| 11 | +from fabric_cicd import constants |
11 | 12 | from fabric_cicd._common._item import Item |
12 | 13 | from fabric_cicd._items._lakehouse import ShortcutPublisher |
13 | 14 | from fabric_cicd.fabric_workspace import FabricWorkspace |
@@ -56,6 +57,39 @@ def create_shortcut_file(shortcuts_data): |
56 | 57 | return file_obj |
57 | 58 |
|
58 | 59 |
|
| 60 | +def set_deployed_shortcuts(mock_fabric_workspace, deployed_shortcuts): |
| 61 | + """Configure endpoint behavior to return specific deployed shortcuts.""" |
| 62 | + |
| 63 | + def mock_invoke(method, url, **_kwargs): |
| 64 | + if method == "GET" and "shortcuts" in url: |
| 65 | + return {"body": {"value": deployed_shortcuts}, "header": {}} |
| 66 | + if method == "POST" and "shortcuts" in url: |
| 67 | + return {"body": {"id": "mock-shortcut-id"}} |
| 68 | + if method == "DELETE" and "shortcuts" in url: |
| 69 | + return {"body": {}} |
| 70 | + return {"body": {}} |
| 71 | + |
| 72 | + mock_fabric_workspace.endpoint.invoke.side_effect = mock_invoke |
| 73 | + |
| 74 | + |
| 75 | +def get_shortcut_post_calls(mock_fabric_workspace): |
| 76 | + """Return all shortcut create/overwrite API calls.""" |
| 77 | + return [ |
| 78 | + call |
| 79 | + for call in mock_fabric_workspace.endpoint.invoke.call_args_list |
| 80 | + if call[1].get("method") == "POST" and "shortcuts" in call[1].get("url", "") |
| 81 | + ] |
| 82 | + |
| 83 | + |
| 84 | +@pytest.fixture |
| 85 | +def restore_feature_flags(): |
| 86 | + """Ensure feature flags are restored after test.""" |
| 87 | + original_flags = constants.FEATURE_FLAG.copy() |
| 88 | + yield |
| 89 | + constants.FEATURE_FLAG.clear() |
| 90 | + constants.FEATURE_FLAG.update(original_flags) |
| 91 | + |
| 92 | + |
59 | 93 | def test_process_shortcuts_with_exclude_regex_filters_shortcuts(mock_fabric_workspace, mock_item): |
60 | 94 | """Test that shortcut_exclude_regex correctly filters shortcuts from deployment.""" |
61 | 95 |
|
@@ -304,3 +338,140 @@ def test_process_shortcuts_with_complex_regex_pattern(mock_fabric_workspace, moc |
304 | 338 | # Verify the published shortcut is the prod one |
305 | 339 | published_shortcut = post_calls[0][1]["body"] |
306 | 340 | assert published_shortcut["name"] == "prod_shortcut" |
| 341 | + |
| 342 | + |
| 343 | +def test_shortcut_smart_diff_skips_unchanged_shortcuts(mock_fabric_workspace, mock_item): |
| 344 | + """Smart diff should skip publish for unchanged shortcuts.""" |
| 345 | + shortcuts_data = [ |
| 346 | + { |
| 347 | + "name": "shortcut1", |
| 348 | + "path": "/Tables", |
| 349 | + "target": {"type": "OneLake", "oneLake": {"path": "Tables/s1", "itemId": "item-1"}}, |
| 350 | + }, |
| 351 | + { |
| 352 | + "name": "shortcut2", |
| 353 | + "path": "/Files", |
| 354 | + "target": {"type": "OneLake", "oneLake": {"path": "Files/s2", "itemId": "item-2"}}, |
| 355 | + }, |
| 356 | + ] |
| 357 | + deployed_shortcuts = [ |
| 358 | + { |
| 359 | + "name": "shortcut1", |
| 360 | + "path": "/Tables", |
| 361 | + "target": {"type": "OneLake", "oneLake": {"path": "Tables/s1", "itemId": "item-1"}}, |
| 362 | + "serverManaged": {"createdBy": "system"}, |
| 363 | + }, |
| 364 | + { |
| 365 | + "name": "shortcut2", |
| 366 | + "path": "/Files", |
| 367 | + "target": {"type": "OneLake", "oneLake": {"path": "Files/s2", "itemId": "item-2"}}, |
| 368 | + "serverManaged": {"createdBy": "system"}, |
| 369 | + }, |
| 370 | + ] |
| 371 | + |
| 372 | + mock_item.item_files = [create_shortcut_file(shortcuts_data)] |
| 373 | + set_deployed_shortcuts(mock_fabric_workspace, deployed_shortcuts) |
| 374 | + constants.FEATURE_FLAG.add("enable_shortcut_smart_diff") |
| 375 | + try: |
| 376 | + ShortcutPublisher(mock_fabric_workspace, mock_item).publish_all() |
| 377 | + post_calls = get_shortcut_post_calls(mock_fabric_workspace) |
| 378 | + assert len(post_calls) == 0 |
| 379 | + finally: |
| 380 | + constants.FEATURE_FLAG.discard("enable_shortcut_smart_diff") |
| 381 | + |
| 382 | + |
| 383 | +def test_shortcut_smart_diff_publishes_only_changed_shortcut(mock_fabric_workspace, mock_item): |
| 384 | + """Smart diff should publish only shortcuts with changed fields.""" |
| 385 | + shortcuts_data = [ |
| 386 | + { |
| 387 | + "name": "shortcut1", |
| 388 | + "path": "/Tables", |
| 389 | + "target": {"type": "OneLake", "oneLake": {"path": "Tables/s1", "itemId": "item-1"}}, |
| 390 | + }, |
| 391 | + { |
| 392 | + "name": "shortcut2", |
| 393 | + "path": "/Files", |
| 394 | + "target": {"type": "OneLake", "oneLake": {"path": "Files/s2-new", "itemId": "item-2"}}, |
| 395 | + }, |
| 396 | + ] |
| 397 | + deployed_shortcuts = [ |
| 398 | + { |
| 399 | + "name": "shortcut1", |
| 400 | + "path": "/Tables", |
| 401 | + "target": {"type": "OneLake", "oneLake": {"path": "Tables/s1", "itemId": "item-1"}}, |
| 402 | + }, |
| 403 | + { |
| 404 | + "name": "shortcut2", |
| 405 | + "path": "/Files", |
| 406 | + "target": {"type": "OneLake", "oneLake": {"path": "Files/s2-old", "itemId": "item-2"}}, |
| 407 | + }, |
| 408 | + ] |
| 409 | + |
| 410 | + mock_item.item_files = [create_shortcut_file(shortcuts_data)] |
| 411 | + set_deployed_shortcuts(mock_fabric_workspace, deployed_shortcuts) |
| 412 | + constants.FEATURE_FLAG.add("enable_shortcut_smart_diff") |
| 413 | + try: |
| 414 | + ShortcutPublisher(mock_fabric_workspace, mock_item).publish_all() |
| 415 | + |
| 416 | + post_calls = get_shortcut_post_calls(mock_fabric_workspace) |
| 417 | + assert len(post_calls) == 1 |
| 418 | + assert post_calls[0][1]["body"]["name"] == "shortcut2" |
| 419 | + finally: |
| 420 | + constants.FEATURE_FLAG.discard("enable_shortcut_smart_diff") |
| 421 | + |
| 422 | + |
| 423 | +def test_shortcut_smart_diff_publishes_client_side_new_field(mock_fabric_workspace, mock_item): |
| 424 | + """Smart diff should publish when repo contains a new field missing from deployed shortcut.""" |
| 425 | + shortcuts_data = [ |
| 426 | + { |
| 427 | + "name": "shortcut1", |
| 428 | + "path": "/Tables", |
| 429 | + "target": {"type": "OneLake", "oneLake": {"path": "Tables/s1", "itemId": "item-1"}}, |
| 430 | + "clientField": {"owner": "team-a"}, |
| 431 | + } |
| 432 | + ] |
| 433 | + deployed_shortcuts = [ |
| 434 | + { |
| 435 | + "name": "shortcut1", |
| 436 | + "path": "/Tables", |
| 437 | + "target": {"type": "OneLake", "oneLake": {"path": "Tables/s1", "itemId": "item-1"}}, |
| 438 | + } |
| 439 | + ] |
| 440 | + |
| 441 | + mock_item.item_files = [create_shortcut_file(shortcuts_data)] |
| 442 | + set_deployed_shortcuts(mock_fabric_workspace, deployed_shortcuts) |
| 443 | + constants.FEATURE_FLAG.add("enable_shortcut_smart_diff") |
| 444 | + try: |
| 445 | + ShortcutPublisher(mock_fabric_workspace, mock_item).publish_all() |
| 446 | + |
| 447 | + post_calls = get_shortcut_post_calls(mock_fabric_workspace) |
| 448 | + assert len(post_calls) == 1 |
| 449 | + assert post_calls[0][1]["body"]["name"] == "shortcut1" |
| 450 | + finally: |
| 451 | + constants.FEATURE_FLAG.discard("enable_shortcut_smart_diff") |
| 452 | + |
| 453 | + |
| 454 | +def test_shortcut_smart_diff_disabled_keeps_publish_all_behavior(mock_fabric_workspace, mock_item): |
| 455 | + """When smart diff is disabled, unchanged shortcuts are still published.""" |
| 456 | + shortcuts_data = [ |
| 457 | + { |
| 458 | + "name": "shortcut1", |
| 459 | + "path": "/Tables", |
| 460 | + "target": {"type": "OneLake", "oneLake": {"path": "Tables/s1", "itemId": "item-1"}}, |
| 461 | + } |
| 462 | + ] |
| 463 | + deployed_shortcuts = [ |
| 464 | + { |
| 465 | + "name": "shortcut1", |
| 466 | + "path": "/Tables", |
| 467 | + "target": {"type": "OneLake", "oneLake": {"path": "Tables/s1", "itemId": "item-1"}}, |
| 468 | + } |
| 469 | + ] |
| 470 | + |
| 471 | + mock_item.item_files = [create_shortcut_file(shortcuts_data)] |
| 472 | + set_deployed_shortcuts(mock_fabric_workspace, deployed_shortcuts) |
| 473 | + |
| 474 | + ShortcutPublisher(mock_fabric_workspace, mock_item).publish_all() |
| 475 | + |
| 476 | + post_calls = get_shortcut_post_calls(mock_fabric_workspace) |
| 477 | + assert len(post_calls) == 1 |
0 commit comments