Code Duplication    Length = 102-103 lines in 2 locations

tests/unit/test_main.py 2 locations

@@ 675-777 (lines=103) @@
672
        # verify add circuit in sched
673
        sched_add_mock.assert_called_once()
674
675
    @patch("napps.kytos.mef_eline.main.Main._check_no_tag_duplication")
676
    @patch("napps.kytos.mef_eline.models.evc.EVC._tag_lists_equal")
677
    @patch("napps.kytos.mef_eline.main.Main._use_uni_tags")
678
    @patch("napps.kytos.mef_eline.models.evc.EVC.deploy")
679
    @patch("napps.kytos.mef_eline.scheduler.Scheduler.add")
680
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
681
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
682
    @patch("napps.kytos.mef_eline.main.EVC.as_dict")
683
    @patch("napps.kytos.mef_eline.models.evc.EVC._validate")
684
    async def test_create_a_circuit_case_6(
685
        self,
686
        validate_mock,
687
        evc_as_dict_mock,
688
        mongo_controller_upsert_mock,
689
        uni_from_dict_mock,
690
        sched_add_mock,
691
        evc_deploy_mock,
692
        mock_use_uni_tags,
693
        mock_tags_equal,
694
        mock_check_duplicate
695
    ):
696
        """Test create a new intra circuit with a disabled interface"""
697
        # pylint: disable=too-many-locals
698
        self.napp.controller.loop = asyncio.get_running_loop()
699
        validate_mock.return_value = True
700
        mongo_controller_upsert_mock.return_value = True
701
        evc_deploy_mock.return_value = True
702
        mock_use_uni_tags.return_value = True
703
        mock_tags_equal.return_value = True
704
        mock_check_duplicate.return_value = True
705
        uni1 = create_autospec(UNI)
706
        uni2 = create_autospec(UNI)
707
        uni1.interface = create_autospec(Interface)
708
        uni2.interface = create_autospec(Interface)
709
        switch = MagicMock()
710
        switch.status = EntityStatus.UP
711
        switch.return_value = "00:00:00:00:00:00:00:01"
712
        uni1.interface.switch = switch
713
        uni1.interface.status = EntityStatus.DISABLED
714
        uni2.interface.switch = switch
715
        uni_from_dict_mock.side_effect = [uni1, uni2]
716
        evc_as_dict_mock.return_value = {}
717
        sched_add_mock.return_value = True
718
        self.napp.mongo_controller.get_circuits.return_value = {}
719
720
        url = f"{self.base_endpoint}/v2/evc/"
721
        payload = {
722
            "name": "my evc1",
723
            "frequency": "* * * * *",
724
            "uni_a": {
725
                "interface_id": "00:00:00:00:00:00:00:01:1",
726
                "tag": {"tag_type": 'vlan', "value": 80},
727
            },
728
            "uni_z": {
729
                "interface_id": "00:00:00:00:00:00:00:01:2",
730
                "tag": {"tag_type": 'vlan', "value": 1},
731
            },
732
            "dynamic_backup_path": True,
733
            "primary_constraints": {
734
                "spf_max_path_cost": 8,
735
                "mandatory_metrics": {
736
                    "ownership": "red"
737
                }
738
            },
739
            "secondary_constraints": {
740
                "spf_attribute": "priority",
741
                "mandatory_metrics": {
742
                    "ownership": "blue"
743
                }
744
            }
745
        }
746
747
        response = await self.api_client.post(url, json=payload)
748
        current_data = response.json()
749
750
        # verify expected result from request
751
        assert 201 == response.status_code
752
        assert "circuit_id" in current_data
753
754
        # verify uni called
755
        uni_from_dict_mock.called_twice()
756
        uni_from_dict_mock.assert_any_call(payload["uni_z"])
757
        uni_from_dict_mock.assert_any_call(payload["uni_a"])
758
759
        # verify validation called
760
        validate_mock.assert_called_once()
761
        validate_mock.assert_called_with(
762
            table_group={'evpl': 0, 'epl': 0},
763
            frequency="* * * * *",
764
            name="my evc1",
765
            uni_a=uni1,
766
            uni_z=uni2,
767
            dynamic_backup_path=True,
768
            primary_constraints=payload["primary_constraints"],
769
            secondary_constraints=payload["secondary_constraints"],
770
        )
771
        # verify save method is called
772
        mongo_controller_upsert_mock.assert_called_once()
773
774
        # verify evc as dict is called to save in the box
775
        evc_as_dict_mock.assert_called()
776
        # verify add circuit in sched
777
        sched_add_mock.assert_called_once()
778
779
    async def test_create_a_circuit_invalid_queue_id(self):
780
        """Test create a new circuit with invalid queue_id."""
@@ 572-673 (lines=102) @@
569
        response = await self.api_client.post(url, json=payload)
570
        assert 400 == response.status_code, response.data
571
572
    @patch("napps.kytos.mef_eline.main.Main._check_no_tag_duplication")
573
    @patch("napps.kytos.mef_eline.models.evc.EVC._tag_lists_equal")
574
    @patch("napps.kytos.mef_eline.main.Main._use_uni_tags")
575
    @patch("napps.kytos.mef_eline.models.evc.EVC.deploy")
576
    @patch("napps.kytos.mef_eline.scheduler.Scheduler.add")
577
    @patch("napps.kytos.mef_eline.main.Main._uni_from_dict")
578
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
579
    @patch("napps.kytos.mef_eline.main.EVC.as_dict")
580
    @patch("napps.kytos.mef_eline.models.evc.EVC._validate")
581
    async def test_create_a_circuit_case_5(
582
        self,
583
        validate_mock,
584
        evc_as_dict_mock,
585
        mongo_controller_upsert_mock,
586
        uni_from_dict_mock,
587
        sched_add_mock,
588
        evc_deploy_mock,
589
        mock_use_uni_tags,
590
        mock_tags_equal,
591
        mock_check_duplicate
592
    ):
593
        """Test create a new intra circuit with a disabled switch"""
594
        # pylint: disable=too-many-locals
595
        self.napp.controller.loop = asyncio.get_running_loop()
596
        validate_mock.return_value = True
597
        mongo_controller_upsert_mock.return_value = True
598
        evc_deploy_mock.return_value = True
599
        mock_use_uni_tags.return_value = True
600
        mock_tags_equal.return_value = True
601
        mock_check_duplicate.return_value = True
602
        uni1 = create_autospec(UNI)
603
        uni2 = create_autospec(UNI)
604
        uni1.interface = create_autospec(Interface)
605
        uni2.interface = create_autospec(Interface)
606
        switch = MagicMock()
607
        switch.status = EntityStatus.DISABLED
608
        switch.return_value = "00:00:00:00:00:00:00:01"
609
        uni1.interface.switch = switch
610
        uni2.interface.switch = switch
611
        uni_from_dict_mock.side_effect = [uni1, uni2]
612
        evc_as_dict_mock.return_value = {}
613
        sched_add_mock.return_value = True
614
        self.napp.mongo_controller.get_circuits.return_value = {}
615
616
        url = f"{self.base_endpoint}/v2/evc/"
617
        payload = {
618
            "name": "my evc1",
619
            "frequency": "* * * * *",
620
            "uni_a": {
621
                "interface_id": "00:00:00:00:00:00:00:01:1",
622
                "tag": {"tag_type": 'vlan', "value": 80},
623
            },
624
            "uni_z": {
625
                "interface_id": "00:00:00:00:00:00:00:01:2",
626
                "tag": {"tag_type": 'vlan', "value": 1},
627
            },
628
            "dynamic_backup_path": True,
629
            "primary_constraints": {
630
                "spf_max_path_cost": 8,
631
                "mandatory_metrics": {
632
                    "ownership": "red"
633
                }
634
            },
635
            "secondary_constraints": {
636
                "spf_attribute": "priority",
637
                "mandatory_metrics": {
638
                    "ownership": "blue"
639
                }
640
            }
641
        }
642
643
        response = await self.api_client.post(url, json=payload)
644
        current_data = response.json()
645
646
        # verify expected result from request
647
        assert 201 == response.status_code
648
        assert "circuit_id" in current_data
649
650
        # verify uni called
651
        uni_from_dict_mock.called_twice()
652
        uni_from_dict_mock.assert_any_call(payload["uni_z"])
653
        uni_from_dict_mock.assert_any_call(payload["uni_a"])
654
655
        # verify validation called
656
        validate_mock.assert_called_once()
657
        validate_mock.assert_called_with(
658
            table_group={'evpl': 0, 'epl': 0},
659
            frequency="* * * * *",
660
            name="my evc1",
661
            uni_a=uni1,
662
            uni_z=uni2,
663
            dynamic_backup_path=True,
664
            primary_constraints=payload["primary_constraints"],
665
            secondary_constraints=payload["secondary_constraints"],
666
        )
667
        # verify save method is called
668
        mongo_controller_upsert_mock.assert_called_once()
669
670
        # verify evc as dict is called to save in the box
671
        evc_as_dict_mock.assert_called()
672
        # verify add circuit in sched
673
        sched_add_mock.assert_called_once()
674
675
    @patch("napps.kytos.mef_eline.main.Main._check_no_tag_duplication")
676
    @patch("napps.kytos.mef_eline.models.evc.EVC._tag_lists_equal")