/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class QISMBCTriggerTest {

    static testMethod void myUnitTest() {
        QIS_Report__c qr = new QIS_Report__c();
        qr.RC__c = UserInfo.getUserId();
        qr.Damage_For_Doc_Or_Pat__c = '有';
        qr.Relation_With_The_Problem__c = '有可能';
        qr.Report_For_Goz__c = '不知道';
        insert qr;
        
        QIS_M_BC__c qm = new QIS_M_BC__c(
            QIS_request_name__c = qr.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        insert qm;
        
        qm = [select Name, OwnerAlias__c, QIS_submit_date_for_MBC__c, QIS_M_BC_reception__c,
                QIS_M_BC_answer_day__c, Received_feiqi_date__c from QIS_M_BC__c where Id = :qm.Id];
        
        qr = [select Wether_publish_QIS_not__c, QIS_no_for_m_BC__c, QIS_submit_date_for_MBC__c, QIS_M_BC_submit_person__c,
                QIS_M_BC_reception__c, QIS_M_BC_reply_date__c, Received_feiqi_date__c, QIS_M_BC_answer__c
                from QIS_Report__c where Id = :qr.Id];
        //System.assertEquals('发行', qr.Wether_publish_QIS_not__c);
        System.assertEquals(qm.Name, qr.QIS_no_for_m_BC__c);
        System.assertEquals(qm.QIS_submit_date_for_MBC__c, qr.QIS_submit_date_for_MBC__c);
        System.assertEquals(qm.OwnerAlias__c, qr.QIS_M_BC_submit_person__c);
        System.assertEquals(qm.QIS_M_BC_reception__c, qr.QIS_M_BC_reception__c);
        System.assertEquals(qm.QIS_M_BC_answer_day__c, qr.QIS_M_BC_reply_date__c);
        System.assertEquals(qm.Received_feiqi_date__c, qr.Received_feiqi_date__c);
        System.assertEquals('test', qr.QIS_M_BC_answer__c);
    }
    
    static testMethod void insertQISMBCTest() {
      QIS_Report__c qr = new QIS_Report__c();
        qr.RC__c = UserInfo.getUserId();
        qr.Damage_For_Doc_Or_Pat__c = '有';
        qr.Relation_With_The_Problem__c = '有可能';
        qr.Report_For_Goz__c = '不知道';
        insert qr;
        QIS_Report__c qr2 = new QIS_Report__c();
        qr2.RC__c = UserInfo.getUserId();
        qr2.Damage_For_Doc_Or_Pat__c = '有';
        qr2.Relation_With_The_Problem__c = '有可能';
        qr2.Report_For_Goz__c = '不知道';
        insert qr2;
        
        QIS_M_BC__c qm = new QIS_M_BC__c(
            QIS_request_name__c = qr.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        QIS_M_BC__c qm2 = new QIS_M_BC__c(
            QIS_request_name__c = qr2.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        insert new List<QIS_M_BC__c> {qm, qm2};
        
        qr = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr.Id];
        qm = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm.Id];
        qr2 = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr2.Id];
        qm2 = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm2.Id];
        
        System.assertEquals(qm.Name, qr.QIS_no_for_m_BC__c);
        System.assertEquals(qm.QIS_request_name__c, qr.Id);
        System.assertEquals(qm2.Name, qr2.QIS_no_for_m_BC__c);
        System.assertEquals(qm2.QIS_request_name__c, qr2.Id);
    }
    
    static testMethod void updateQISMBCTest() {
        System.Test.StartTest();
      QIS_Report__c qr = new QIS_Report__c();
        qr.RC__c = UserInfo.getUserId();
        qr.Damage_For_Doc_Or_Pat__c = '有';
        qr.Relation_With_The_Problem__c = '有可能';
        qr.Report_For_Goz__c = '不知道';
        insert qr;
        QIS_Report__c qr2 = new QIS_Report__c();
        qr2.RC__c = UserInfo.getUserId();
        qr2.Damage_For_Doc_Or_Pat__c = '有';
        qr2.Relation_With_The_Problem__c = '有可能';
        qr2.Report_For_Goz__c = '不知道';
        insert qr2;
        QIS_Report__c qr3 = new QIS_Report__c();
        qr3.RC__c = UserInfo.getUserId();
        qr3.Damage_For_Doc_Or_Pat__c = '有';
        qr3.Relation_With_The_Problem__c = '有可能';
        qr3.Report_For_Goz__c = '不知道';
        insert qr3;
        
        QIS_M_BC__c qm = new QIS_M_BC__c(
            QIS_request_name__c = qr.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        QIS_M_BC__c qm2 = new QIS_M_BC__c(
            QIS_request_name__c = qr2.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        insert new List<QIS_M_BC__c> {qm, qm2};
        System.Test.StopTest();
        qr = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr.Id];
        qm = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm.Id];
        qr2 = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr2.Id];
        qm2 = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm2.Id];
        
        System.assertEquals(qm.Name, qr.QIS_no_for_m_BC__c);
        System.assertEquals(qm.QIS_request_name__c, qr.Id);
        System.assertEquals(qm2.Name, qr2.QIS_no_for_m_BC__c);
        System.assertEquals(qm2.QIS_request_name__c, qr2.Id);
        
        qm.QIS_request_name__c = qr2.id;
        qm2.QIS_request_name__c = qr3.id;
        update new List<QIS_M_BC__c> {qm, qm2};
        
        //qr = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr.Id];
        //qr2 = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr2.Id];
        //qr3 = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr3.Id];
        //qm = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm.Id];
        //qm2 = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm2.Id];
        
        //System.assertEquals(null, qr.QIS_no_for_m_BC__c);
        //System.assertEquals(qm.Name, qr2.QIS_no_for_m_BC__c);
        //System.assertEquals(qm.QIS_request_name__c, qr2.Id);
        //System.assertEquals(qm2.Name, qr3.QIS_no_for_m_BC__c);
        //System.assertEquals(qm2.QIS_request_name__c, qr3.Id);
    }
    // static testMethod void QISMBCTest_2() {
    //     QIS_Report__c qr = new QIS_Report__c();
    //     qr.RC__c = UserInfo.getUserId();
    //     qr.Damage_For_Doc_Or_Pat__c = '有';
    //     qr.Relation_With_The_Problem__c = '有可能';
    //     qr.Report_For_Goz__c = '不知道';
    //     insert qr;
    //     QIS_Report__c qr2 = new QIS_Report__c();
    //     qr2.RC__c = UserInfo.getUserId();
    //     qr2.Damage_For_Doc_Or_Pat__c = '有';
    //     qr2.Relation_With_The_Problem__c = '有可能';
    //     qr2.Report_For_Goz__c = '不知道';
    //     insert qr2;
    //     QIS_Report__c qr3 = new QIS_Report__c();
    //     qr3.RC__c = UserInfo.getUserId();
    //     qr3.Damage_For_Doc_Or_Pat__c = '有';
    //     qr3.Relation_With_The_Problem__c = '有可能';
    //     qr3.Report_For_Goz__c = '不知道';
    //     insert qr3;
        
    //     QIS_M_BC__c qm = new QIS_M_BC__c(
    //         QIS_request_name__c = qr.Id,
    //         QIS_submit_date_for_MBC__c = Date.today(),
    //         QIS_M_BC_reception__c = Date.today().addDays(1),
    //         QIS_M_BC_answer_day__c = Date.today().addDays(1),
    //         Received_feiqi_date__c = Date.today().addDays(1),
    //         QIS_M_BC_answer_detail__c = 'test',
    //         accsessary_ship__c = 'test'
    //     );
    //     QIS_M_BC__c qm2 = new QIS_M_BC__c(
    //         QIS_request_name__c = qr2.Id,
    //         QIS_submit_date_for_MBC__c = Date.today(),
    //         QIS_M_BC_reception__c = Date.today().addDays(1),
    //         QIS_M_BC_answer_day__c = Date.today().addDays(1),
    //         Received_feiqi_date__c = Date.today().addDays(1),
    //         QIS_M_BC_answer_detail__c = 'test',
    //         accsessary_ship__c = 'test'
    //     );
    //     insert new List<QIS_M_BC__c> {qm, qm2};
    //     qm.QIS_request_name__c = qr2.id;
    //     qm2.QIS_request_name__c = qr3.id;
    //     update new List<QIS_M_BC__c> {qm, qm2};
        
    //     qr = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr.Id];
    //     qr2 = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr2.Id];
    //     qr3 = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr3.Id];
    //     qm = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm.Id];
    //     //qm2 = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm2.Id];
        
    //     System.assertEquals(null, qr.QIS_no_for_m_BC__c);
    //     System.assertEquals(qm.Name, qr2.QIS_no_for_m_BC__c);
    //     System.assertEquals(qm.QIS_request_name__c, qr2.Id);
    //     //System.assertEquals('OSH--149PA-100489', qr3.QIS_no_for_m_BC__c);
    //    // System.assertEquals(qm2.QIS_request_name__c, qr3.Id);
    // }
    static testMethod void deleteQISMBCTest() {
      QIS_Report__c qr = new QIS_Report__c();
        qr.RC__c = UserInfo.getUserId();
        qr.Damage_For_Doc_Or_Pat__c = '有';
        qr.Relation_With_The_Problem__c = '有可能';
        qr.Report_For_Goz__c = '不知道';
        insert qr;
        
        QIS_M_BC__c qm = new QIS_M_BC__c(
            QIS_request_name__c = qr.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        insert qm;
        
        qr = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr.Id];
        qm = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm.Id];
        
        System.assertEquals(qm.Name, qr.QIS_no_for_m_BC__c);
        System.assertEquals(qm.QIS_request_name__c, qr.Id);
        
        delete qm;
        
        qr = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr.Id];
        System.assertEquals(null, qr.QIS_no_for_m_BC__c);
    }
    
    static testMethod void insertErrorTest() {
      QIS_Report__c qr = new QIS_Report__c();
        qr.RC__c = UserInfo.getUserId();
        qr.Damage_For_Doc_Or_Pat__c = '有';
        qr.Relation_With_The_Problem__c = '有可能';
        qr.Report_For_Goz__c = '不知道';
        insert qr;
        
        QIS_M_BC__c qm = new QIS_M_BC__c(
            QIS_request_name__c = qr.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        QIS_M_BC__c qm2 = new QIS_M_BC__c(
            QIS_request_name__c = qr.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        
        try {
          insert new List<QIS_M_BC__c> {qm, qm2};
          System.assertEquals('没有执行', qm.Id);
        } catch (DmlException e) {
          System.assert(e.getMessage().contains('一个QIS只能创建一个QISMBC'), e.getMessage());
        }
    }
    
    static testMethod void updateErrorTest() {
      QIS_Report__c qr = new QIS_Report__c();
        qr.RC__c = UserInfo.getUserId();
        qr.Damage_For_Doc_Or_Pat__c = '有';
        qr.Relation_With_The_Problem__c = '有可能';
        qr.Report_For_Goz__c = '不知道';
        insert qr;
        QIS_Report__c qr2 = new QIS_Report__c();
        qr2.RC__c = UserInfo.getUserId();
        qr2.Damage_For_Doc_Or_Pat__c = '有';
        qr2.Relation_With_The_Problem__c = '有可能';
        qr2.Report_For_Goz__c = '不知道';
        insert qr2;
        
        QIS_M_BC__c qm = new QIS_M_BC__c(
            QIS_request_name__c = qr.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        QIS_M_BC__c qm2 = new QIS_M_BC__c(
            QIS_request_name__c = qr2.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        
        insert new List<QIS_M_BC__c> {qm, qm2};
        
        qm2.QIS_request_name__c = qr.Id;
        
        try {
          update qm2;
          System.assertEquals('没有执行', qm.Id);
        } catch (DmlException e) {
          System.assert(e.getMessage().contains('一个QIS只能创建一个QISMBC'), e.getMessage());
        }
    }
    
    // static testMethod void setMailsTest() {
    //   QIS_Report__c qr = new QIS_Report__c(
    //         RC__c = UserInfo.getUserId(),
    //     OCM_Manager_Mail__c = 'qr_ocm_manager_mail@test.com',
    //     OCM_Member_Mail__c = 'qr_ocm_member_mail@test.com',
    //     OCM_Repair_Mail__c = 'qr_ocm_repair_mail@test.com',
    //     OCM_Repair_Mail1__c = 'qr_ocm_repair_mail1@test.com',
    //         Damage_For_Doc_Or_Pat__c = '有',
    //         Relation_With_The_Problem__c = '有可能',
    //         Report_For_Goz__c = '不知道'
    //   );
    //     insert qr;
    //     QIS_Report__c qr2 = new QIS_Report__c(
    //         RC__c = UserInfo.getUserId(),
    //     OCM_Manager_Mail__c = 'qr2_ocm_manager_mail@test.com',
    //     OCM_Member_Mail__c = 'qr2_ocm_member_mail@test.com',
    //     OCM_Repair_Mail__c = 'qr2_ocm_repair_mail@test.com',
    //     OCM_Repair_Mail1__c = 'qr2_ocm_repair_mail1@test.com',
    //         Damage_For_Doc_Or_Pat__c = '有',
    //         Relation_With_The_Problem__c = '有可能',
    //         Report_For_Goz__c = '不知道'
    //   );
    //     insert qr2;
        
    //     QIS_M_BC__c qm = new QIS_M_BC__c(
    //         QIS_request_name__c = qr.Id,
    //         QIS_submit_date_for_MBC__c = Date.today(),
    //         QIS_M_BC_reception__c = Date.today().addDays(1),
    //         QIS_M_BC_answer_day__c = Date.today().addDays(1),
    //         Received_feiqi_date__c = Date.today().addDays(1),
    //         QIS_M_BC_answer_detail__c = 'test',
    //         accsessary_ship__c = 'test'
    //     );
        
    //     insert qm;
        
    //     qm = [select Id, Name, QIS_request_name__c, OCM_Manager_Mail__c, OCM_Member_Mail__c, OCM_Repair_Mail__c, OCM_Repair_Mail1__c from QIS_M_BC__c where Id = :qm.Id];
        
    //     System.assertEquals('qr_ocm_manager_mail@test.com', qm.OCM_Manager_Mail__c);
    //     System.assertEquals('qr_ocm_member_mail@test.com', qm.OCM_Member_Mail__c);
    //     System.assertEquals('qr_ocm_repair_mail@test.com', qm.OCM_Repair_Mail__c);
    //     System.assertEquals('qr_ocm_repair_mail1@test.com', qm.OCM_Repair_Mail1__c);
        
    //     qm.QIS_request_name__c = qr2.Id;
    //     update qm;
        
    //     qm = [select Id, Name, QIS_request_name__c, OCM_Manager_Mail__c, OCM_Member_Mail__c, OCM_Repair_Mail__c, OCM_Repair_Mail1__c from QIS_M_BC__c where Id = :qm.Id];
        
    //     System.assertEquals('qr2_ocm_manager_mail@test.com', qm.OCM_Manager_Mail__c);
    //     System.assertEquals('qr2_ocm_member_mail@test.com', qm.OCM_Member_Mail__c);
    //     System.assertEquals('qr2_ocm_repair_mail@test.com', qm.OCM_Repair_Mail__c);
    //     System.assertEquals('qr2_ocm_repair_mail1@test.com', qm.OCM_Repair_Mail1__c);
    // }
}