@isTest
private class ConsumAssignCancelControllerTest {
    static ConsumTestDataFactory factory;
    @testSetup static void setupTestData(){
        factory = new ConsumTestDataFactory();
        factory.setupTestData();
        factory.selectDetails();
    }
    // 草案中取消
    //static testMethod void testSaveBeforeApprove() {
    static void testSaveBeforeApprove() {
        Consum_Apply__c ca = [SELECT Id FROM Consum_Apply__c LIMIT 1];
        List<Consum_Apply_Equipment_Set_Detail__c> caesdList = null;
        caesdList = [SELECT Id
                          , Cancel_Mem__c
                          , Cancel_Reason__c
                          , Cancel_Time__c
                          , Loaner_cancel_Remarks__c
                    FROM  Consum_Apply_Equipment_Set_Detail__c
                    WHERE Consum_Apply__c =: ca.Id
                ];
        System.assertEquals(null, caesdList[0].Cancel_Mem__c);
        System.assertEquals(null, caesdList[0].Cancel_Reason__c);
        System.assertEquals(null, caesdList[0].Cancel_Time__c);
        System.assertEquals(null, caesdList[0].Loaner_cancel_Remarks__c);

        PageReference ref = new PageReference('/apex/ConsumAssignCancel?pt_recid=' + ca.Id );
        Test.setCurrentPage(ref);
        ConsumAssignCancelController controller = new ConsumAssignCancelController();
        CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
        cmp.pageController = controller;
        controller.myComponentController.init();
        controller.init();
        controller.searchOpp();
        // 一条明细
        System.assertEquals(1, controller.viewlist.size());

        Test.startTest();
        controller.viewList[0].check=true;
        Consum_Apply_Equipment_Set__c caes = (Consum_Apply_Equipment_Set__c) controller.viewList[0].sobj;
        caes.VF_Cancel_Detail_Count__c = 1;
        caes.Cancel_Reason__c = '主动取消';
        controller.save();
        Test.stopTest();

        // 䓍案中取消，明细被删除
        caesdList =  [SELECT Cancel_Select__c FROM Consum_Apply_Equipment_Set_Detail__c WHERE Consum_Apply__c=:ca.Id];
        System.assertEquals(0, caesdList.size());
    }
    // 分配后取消
    static testMethod void testSaveAfterApprove() {
        ConsumTestDataFactory factory = new ConsumTestDataFactory();
        factory.approve();
        factory.defaultAssign();
        Consum_Apply__c ca = [SELECT Id
                                    , Request_approval_time__c
                                    , Status__c
                                 FROM Consum_Apply__c LIMIT 1];
        System.assertNotEquals(null, ca.Request_approval_time__c);
        System.assertEquals('已批准', ca.Status__c);
        List<Consum_Apply_Equipment_Set_Detail__c> caesdList = null;
        caesdList = [SELECT Id
                          , Cancel_Mem__c
                          , Cancel_Reason__c
                          , Cancel_Time__c
                          , Loaner_cancel_Remarks__c
                          , Cancel_Select__c
                          , RAESD_Status__c
                    FROM  Consum_Apply_Equipment_Set_Detail__c
                    WHERE Consum_Apply__c =: ca.Id
                ];
        System.assertEquals(null, caesdList[0].Cancel_Mem__c);
        System.assertEquals(null, caesdList[0].Cancel_Reason__c);
        System.assertEquals(null, caesdList[0].Cancel_Time__c);
        System.assertEquals(null, caesdList[0].Loaner_cancel_Remarks__c);
        System.assertEquals(false, caesdList[0].Cancel_Select__c);
        System.assertEquals('已分配', caesdList[0].RAESD_Status__c);

        PageReference ref = new PageReference('/apex/ConsumAssignCancel?pt_recid=' + ca.Id );
        Test.setCurrentPage(ref);
        ConsumAssignCancelController controller = new ConsumAssignCancelController();
        CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
        cmp.pageController = controller;
        controller.myComponentController.init();
        controller.init();
        controller.searchOpp();
        // 一个型号
        System.assertEquals(1, controller.viewlist.size());

        Test.startTest();
        controller.viewList[0].check=true;
        Consum_Apply_Equipment_Set__c caes = (Consum_Apply_Equipment_Set__c) controller.viewList[0].sobj;
        caes.VF_Cancel_Detail_Count__c = 1;
        caes.Cancel_Reason__c = '主动取消';
        caes.Loaner_cancel_Remarks__c = '取消理由';
        controller.save();
        Test.stopTest();

        caesdList = [SELECT Id
                          , Cancel_Mem__c
                          , Cancel_Reason__c
                          , Cancel_Time__c
                          , Loaner_cancel_Remarks__c
                          , Cancel_Select__c
                          , RAESD_Status__c
                    FROM  Consum_Apply_Equipment_Set_Detail__c
                    WHERE Consum_Apply__c =: ca.Id
                ];
        System.assertNotEquals(null, caesdList[0].Cancel_Mem__c);
        System.assertEquals('主动取消', caesdList[0].Cancel_Reason__c);
        System.assertNotEquals(null, caesdList[0].Cancel_Time__c);
        System.assertEquals('取消理由', caesdList[0].Loaner_cancel_Remarks__c);
        System.assertEquals(true, caesdList[0].Cancel_Select__c);
        System.assertEquals('取消', caesdList[0].RAESD_Status__c);
    }
}