| Conditions | 1 |
| Total Lines | 276 |
| Code Lines | 209 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | """Module to test the Path class.""" |
||
| 417 | @patch("requests.post") |
||
| 418 | def test_get_disjoint_paths(self, mock_requests_post): |
||
| 419 | """Test get_disjoint_paths method.""" |
||
| 420 | |||
| 421 | controller = MagicMock() |
||
| 422 | controller.get_interface_by_id.side_effect = id_to_interface_mock |
||
| 423 | DynamicPathManager.set_controller(controller) |
||
| 424 | |||
| 425 | evc = MagicMock() |
||
| 426 | |||
| 427 | # Topo0 |
||
| 428 | paths1 = { |
||
| 429 | "paths": [ |
||
| 430 | { |
||
| 431 | "cost": 11, |
||
| 432 | "hops": [ |
||
| 433 | "00:00:00:00:00:00:00:01:1", |
||
| 434 | "00:00:00:00:00:00:00:01", |
||
| 435 | "00:00:00:00:00:00:00:01:2", |
||
| 436 | "00:00:00:00:00:00:00:02:2", |
||
| 437 | "00:00:00:00:00:00:00:02", |
||
| 438 | "00:00:00:00:00:00:00:02:3", |
||
| 439 | "00:00:00:00:00:00:00:04:2", |
||
| 440 | "00:00:00:00:00:00:00:04", |
||
| 441 | "00:00:00:00:00:00:00:04:3", |
||
| 442 | "00:00:00:00:00:00:00:05:2", |
||
| 443 | "00:00:00:00:00:00:00:05", |
||
| 444 | "00:00:00:00:00:00:00:05:1" |
||
| 445 | ] |
||
| 446 | }, |
||
| 447 | { |
||
| 448 | "cost": 11, |
||
| 449 | "hops": [ |
||
| 450 | "00:00:00:00:00:00:00:01:1", |
||
| 451 | "00:00:00:00:00:00:00:01", |
||
| 452 | "00:00:00:00:00:00:00:01:3", |
||
| 453 | "00:00:00:00:00:00:00:03:2", |
||
| 454 | "00:00:00:00:00:00:00:03", |
||
| 455 | "00:00:00:00:00:00:00:03:3", |
||
| 456 | "00:00:00:00:00:00:00:04:4", |
||
| 457 | "00:00:00:00:00:00:00:04", |
||
| 458 | "00:00:00:00:00:00:00:04:3", |
||
| 459 | "00:00:00:00:00:00:00:05:2", |
||
| 460 | "00:00:00:00:00:00:00:05", |
||
| 461 | "00:00:00:00:00:00:00:05:1" |
||
| 462 | ] |
||
| 463 | }, |
||
| 464 | { |
||
| 465 | "cost": 14, |
||
| 466 | "hops": [ |
||
| 467 | "00:00:00:00:00:00:00:01:1", |
||
| 468 | "00:00:00:00:00:00:00:01", |
||
| 469 | "00:00:00:00:00:00:00:01:2", |
||
| 470 | "00:00:00:00:00:00:00:02:2", |
||
| 471 | "00:00:00:00:00:00:00:02", |
||
| 472 | "00:00:00:00:00:00:00:02:3", |
||
| 473 | "00:00:00:00:00:00:00:04:2", |
||
| 474 | "00:00:00:00:00:00:00:04", |
||
| 475 | "00:00:00:00:00:00:00:04:5", |
||
| 476 | "00:00:00:00:00:00:00:06:2", |
||
| 477 | "00:00:00:00:00:00:00:06", |
||
| 478 | "00:00:00:00:00:00:00:06:3", |
||
| 479 | "00:00:00:00:00:00:00:05:3", |
||
| 480 | "00:00:00:00:00:00:00:05", |
||
| 481 | "00:00:00:00:00:00:00:05:1" |
||
| 482 | ] |
||
| 483 | }, |
||
| 484 | { |
||
| 485 | "cost": 14, |
||
| 486 | "hops": [ |
||
| 487 | "00:00:00:00:00:00:00:01:1", |
||
| 488 | "00:00:00:00:00:00:00:01", |
||
| 489 | "00:00:00:00:00:00:00:01:3", |
||
| 490 | "00:00:00:00:00:00:00:03:2", |
||
| 491 | "00:00:00:00:00:00:00:03", |
||
| 492 | "00:00:00:00:00:00:00:03:3", |
||
| 493 | "00:00:00:00:00:00:00:04:4", |
||
| 494 | "00:00:00:00:00:00:00:04", |
||
| 495 | "00:00:00:00:00:00:00:04:5", |
||
| 496 | "00:00:00:00:00:00:00:06:2", |
||
| 497 | "00:00:00:00:00:00:00:06", |
||
| 498 | "00:00:00:00:00:00:00:06:3", |
||
| 499 | "00:00:00:00:00:00:00:05:3", |
||
| 500 | "00:00:00:00:00:00:00:05", |
||
| 501 | "00:00:00:00:00:00:00:05:1" |
||
| 502 | ] |
||
| 503 | }, |
||
| 504 | { |
||
| 505 | "cost": 17, |
||
| 506 | "hops": [ |
||
| 507 | "00:00:00:00:00:00:00:01:1", |
||
| 508 | "00:00:00:00:00:00:00:01", |
||
| 509 | "00:00:00:00:00:00:00:01:3", |
||
| 510 | "00:00:00:00:00:00:00:03:2", |
||
| 511 | "00:00:00:00:00:00:00:03", |
||
| 512 | "00:00:00:00:00:00:00:03:3", |
||
| 513 | "00:00:00:00:00:00:00:04:4", |
||
| 514 | "00:00:00:00:00:00:00:04", |
||
| 515 | "00:00:00:00:00:00:00:04:5", |
||
| 516 | "00:00:00:00:00:00:00:06:2", |
||
| 517 | "00:00:00:00:00:00:00:06", |
||
| 518 | "00:00:00:00:00:00:00:06:4", |
||
| 519 | "00:00:00:00:00:00:00:07:2", |
||
| 520 | "00:00:00:00:00:00:00:07", |
||
| 521 | "00:00:00:00:00:00:00:07:3", |
||
| 522 | "00:00:00:00:00:00:00:05:4", |
||
| 523 | "00:00:00:00:00:00:00:05", |
||
| 524 | "00:00:00:00:00:00:00:05:1" |
||
| 525 | ] |
||
| 526 | }, |
||
| 527 | ] |
||
| 528 | } |
||
| 529 | |||
| 530 | mock_response = MagicMock() |
||
| 531 | mock_response.status_code = 200 |
||
| 532 | mock_response.json.return_value = paths1 |
||
| 533 | |||
| 534 | # when we dont have the current_path |
||
| 535 | mock_requests_post.return_value = mock_response |
||
| 536 | disjoint_paths = list(DynamicPathManager.get_disjoint_paths(evc, [])) |
||
| 537 | self.assertEqual(disjoint_paths, []) |
||
| 538 | |||
| 539 | current_path = [ |
||
| 540 | Link( |
||
| 541 | id_to_interface_mock("00:00:00:00:00:00:00:01:2"), |
||
| 542 | id_to_interface_mock("00:00:00:00:00:00:00:02:2") |
||
| 543 | ), |
||
| 544 | Link( |
||
| 545 | id_to_interface_mock("00:00:00:00:00:00:00:02:3"), |
||
| 546 | id_to_interface_mock("00:00:00:00:00:00:00:04:2") |
||
| 547 | ), |
||
| 548 | Link( |
||
| 549 | id_to_interface_mock("00:00:00:00:00:00:00:04:3"), |
||
| 550 | id_to_interface_mock("00:00:00:00:00:00:00:05:2") |
||
| 551 | ), |
||
| 552 | ] |
||
| 553 | |||
| 554 | # only one path available from pathfinder (precesilly the |
||
| 555 | # current_path), so the maximum disjoint path will be empty |
||
| 556 | mock_response.json.return_value = {"paths": paths1["paths"][0:1]} |
||
| 557 | mock_requests_post.return_value = mock_response |
||
| 558 | paths = list(DynamicPathManager.get_disjoint_paths(evc, current_path)) |
||
| 559 | self.assertEqual(len(paths), 0) |
||
| 560 | |||
| 561 | expected_disjoint_path = [ |
||
| 562 | Link( |
||
| 563 | id_to_interface_mock("00:00:00:00:00:00:00:01:3"), |
||
| 564 | id_to_interface_mock("00:00:00:00:00:00:00:03:2") |
||
| 565 | ), |
||
| 566 | Link( |
||
| 567 | id_to_interface_mock("00:00:00:00:00:00:00:03:3"), |
||
| 568 | id_to_interface_mock("00:00:00:00:00:00:00:04:4") |
||
| 569 | ), |
||
| 570 | Link( |
||
| 571 | id_to_interface_mock("00:00:00:00:00:00:00:04:5"), |
||
| 572 | id_to_interface_mock("00:00:00:00:00:00:00:06:2") |
||
| 573 | ), |
||
| 574 | Link( |
||
| 575 | id_to_interface_mock("00:00:00:00:00:00:00:06:3"), |
||
| 576 | id_to_interface_mock("00:00:00:00:00:00:00:05:3") |
||
| 577 | ), |
||
| 578 | ] |
||
| 579 | |||
| 580 | # there are one alternative path |
||
| 581 | mock_response.json.return_value = paths1 |
||
| 582 | mock_requests_post.return_value = mock_response |
||
| 583 | paths = list(DynamicPathManager.get_disjoint_paths(evc, current_path)) |
||
| 584 | self.assertEqual(len(paths), 4) |
||
| 585 | # for more information on the paths please refer to EP029 |
||
| 586 | self.assertEqual(len(paths[0]), 4) # path S-Z-W-I-D |
||
| 587 | self.assertEqual(len(paths[1]), 5) # path S-Z-W-I-J-D |
||
| 588 | self.assertEqual(len(paths[2]), 3) # path S-Z-W-D |
||
| 589 | self.assertEqual(len(paths[3]), 4) # path S-X-W-I-D |
||
| 590 | self.assertEqual( |
||
| 591 | [link.id for link in paths[0]], |
||
| 592 | [link.id for link in expected_disjoint_path] |
||
| 593 | ) |
||
| 594 | |||
| 595 | # EP029 Topo2 |
||
| 596 | paths2 = { |
||
| 597 | "paths": [ |
||
| 598 | { |
||
| 599 | "cost": 14, |
||
| 600 | "hops": [ |
||
| 601 | "00:00:00:00:00:00:00:01:1", |
||
| 602 | "00:00:00:00:00:00:00:01", |
||
| 603 | "00:00:00:00:00:00:00:01:2", |
||
| 604 | "00:00:00:00:00:00:00:02:1", |
||
| 605 | "00:00:00:00:00:00:00:02", |
||
| 606 | "00:00:00:00:00:00:00:02:2", |
||
| 607 | "00:00:00:00:00:00:00:03:1", |
||
| 608 | "00:00:00:00:00:00:00:03", |
||
| 609 | "00:00:00:00:00:00:00:03:2", |
||
| 610 | "00:00:00:00:00:00:00:04:1", |
||
| 611 | "00:00:00:00:00:00:00:04", |
||
| 612 | "00:00:00:00:00:00:00:04:2", |
||
| 613 | "00:00:00:00:00:00:00:07:2", |
||
| 614 | "00:00:00:00:00:00:00:07", |
||
| 615 | "00:00:00:00:00:00:00:07:1" |
||
| 616 | ] |
||
| 617 | }, |
||
| 618 | { |
||
| 619 | "cost": 17, |
||
| 620 | "hops": [ |
||
| 621 | "00:00:00:00:00:00:00:01:1", |
||
| 622 | "00:00:00:00:00:00:00:01", |
||
| 623 | "00:00:00:00:00:00:00:01:2", |
||
| 624 | "00:00:00:00:00:00:00:02:1", |
||
| 625 | "00:00:00:00:00:00:00:02", |
||
| 626 | "00:00:00:00:00:00:00:02:3", |
||
| 627 | "00:00:00:00:00:00:00:05:1", |
||
| 628 | "00:00:00:00:00:00:00:05", |
||
| 629 | "00:00:00:00:00:00:00:05:2", |
||
| 630 | "00:00:00:00:00:00:00:06:1", |
||
| 631 | "00:00:00:00:00:00:00:06", |
||
| 632 | "00:00:00:00:00:00:00:06:2", |
||
| 633 | "00:00:00:00:00:00:00:04:3", |
||
| 634 | "00:00:00:00:00:00:00:04", |
||
| 635 | "00:00:00:00:00:00:00:04:2", |
||
| 636 | "00:00:00:00:00:00:00:07:2", |
||
| 637 | "00:00:00:00:00:00:00:07", |
||
| 638 | "00:00:00:00:00:00:00:07:1" |
||
| 639 | ] |
||
| 640 | } |
||
| 641 | ] |
||
| 642 | } |
||
| 643 | |||
| 644 | current_path = [ |
||
| 645 | Link( |
||
| 646 | id_to_interface_mock("00:00:00:00:00:00:00:01:2"), |
||
| 647 | id_to_interface_mock("00:00:00:00:00:00:00:02:1") |
||
| 648 | ), |
||
| 649 | Link( |
||
| 650 | id_to_interface_mock("00:00:00:00:00:00:00:02:2"), |
||
| 651 | id_to_interface_mock("00:00:00:00:00:00:00:03:1") |
||
| 652 | ), |
||
| 653 | Link( |
||
| 654 | id_to_interface_mock("00:00:00:00:00:00:00:03:2"), |
||
| 655 | id_to_interface_mock("00:00:00:00:00:00:00:04:1") |
||
| 656 | ), |
||
| 657 | Link( |
||
| 658 | id_to_interface_mock("00:00:00:00:00:00:00:04:2"), |
||
| 659 | id_to_interface_mock("00:00:00:00:00:00:00:07:2") |
||
| 660 | ), |
||
| 661 | ] |
||
| 662 | |||
| 663 | expected_disjoint_path = [ |
||
| 664 | Link( |
||
| 665 | id_to_interface_mock("00:00:00:00:00:00:00:01:2"), |
||
| 666 | id_to_interface_mock("00:00:00:00:00:00:00:02:1") |
||
| 667 | ), |
||
| 668 | Link( |
||
| 669 | id_to_interface_mock("00:00:00:00:00:00:00:02:3"), |
||
| 670 | id_to_interface_mock("00:00:00:00:00:00:00:05:1") |
||
| 671 | ), |
||
| 672 | Link( |
||
| 673 | id_to_interface_mock("00:00:00:00:00:00:00:05:2"), |
||
| 674 | id_to_interface_mock("00:00:00:00:00:00:00:06:1") |
||
| 675 | ), |
||
| 676 | Link( |
||
| 677 | id_to_interface_mock("00:00:00:00:00:00:00:06:2"), |
||
| 678 | id_to_interface_mock("00:00:00:00:00:00:00:04:3") |
||
| 679 | ), |
||
| 680 | Link( |
||
| 681 | id_to_interface_mock("00:00:00:00:00:00:00:04:2"), |
||
| 682 | id_to_interface_mock("00:00:00:00:00:00:00:07:2") |
||
| 683 | ), |
||
| 684 | ] |
||
| 685 | |||
| 686 | mock_response.json.return_value = {"paths": paths2["paths"]} |
||
| 687 | mock_requests_post.return_value = mock_response |
||
| 688 | paths = list(DynamicPathManager.get_disjoint_paths(evc, current_path)) |
||
| 689 | self.assertEqual(len(paths), 1) |
||
| 690 | self.assertEqual( |
||
| 691 | [link.id for link in paths[0]], |
||
| 692 | [link.id for link in expected_disjoint_path] |
||
| 693 | ) |
||
| 694 |