/**
 * 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 EventOpportunityPileUpTest {

    static testMethod void myUnitTest() {
        Opportunity opp = new Opportunity(Name='aiueo', StageName='contact', CloseDate=Date.today());
        insert opp;
        
        Date t = Date.today();
        
        Event e1 = new Event(
            StartDateTime = t.addDays(1),
            EndDateTime = t.addDays(2),
            Related_Opportunity1_ID__c = opp.Id
        );
        Event e2 = new Event(
            StartDateTime = t.addDays(2),
            EndDateTime = t.addDays(3),
            Related_Opportunity1_ID__c = opp.Id
        );
        insert new Event[] {e1, e2};
        
        opp = [select Last_Visit_Scheduled_Date__c from Opportunity where Id = :opp.Id];
        System.assertEquals(t.addDays(2), opp.Last_Visit_Scheduled_Date__c);
        
        e2.StartDateTime = t;
        update e2;
        
        opp = [select Last_Visit_Scheduled_Date__c from Opportunity where Id = :opp.Id];
        System.assertEquals(t.addDays(1), opp.Last_Visit_Scheduled_Date__c);
        
        ControllerUtil.delEventIdSet.add(e1.Id);
        delete e1;
        opp = [select Last_Visit_Scheduled_Date__c from Opportunity where Id = :opp.Id];
        System.assertEquals(t, opp.Last_Visit_Scheduled_Date__c);
        List<SFDelete__c> delList = [select Id, delSfId__c, tableName__c
                                 from SFDelete__c
                                where tableName__c = 'Event'];
        System.assertEquals(1, delList.size());
        System.assertEquals(e1.Id, delList[0].delSfId__c);
    }

    static testMethod void testDelete() {
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
        
        // insert事前データ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name = 'Katsu テスト';
        insert company;
        // 戦略課室に、担当者とチームを設定
        List<Account> sectionList = [select Id, Department_Class_Label__c from Account where ParentId = :company.Id order by Department_Class_Label__c];
        Account depart0 = new Account();
        depart0.RecordTypeId = rectDpt[0].Id;
        depart0.Name         = '*';
        depart0.Department_Name__c  = 'EventTestDepart0';
        depart0.ParentId            = sectionList[0].Id;
        depart0.Department_Class__c = sectionList[0].Id;
        depart0.Hospital__c         = company.Id;
        insert new Account[] {depart0};

        // 日報データ作成
        Date today1 = Date.today();
        Daily_Report__c dr4 = new Daily_Report__c();
        //Date repDate4 = Date.newinstance(today1.year(), 4, 10);
        dr4.Reported_Date__c = Date.today();
        dr4.Status__c = '作成中';
        dr4.Reporter__c = Userinfo.getUserId();
        insert new Daily_Report__c[] {dr4};
        
        // Eventデータ作成
        Datetime dt4 = DateTime.now();
        Event__c e40 = new Event__c(
            Subject__c = 'test40',
            StartDateTime__c = dt4,
            ActivityDate__c = dt4.date(),
            DurationInMinutes__c = 60,
            EndDateTime__c = dt4.addMinutes(60),
            Daily_Report__c = dr4.Id,
            Account_ID__c = depart0.Id
        );
        insert new Event__c[] {e40};
        Event e = [Select Id from Event where EventC_ID__c = :e40.Id];

        // 削除
        System.Test.startTest();
        delete e40;
        System.Test.stopTest();
        List<SFDelete__c> delList = [select Id, delSfId__c, tableName__c
                                 from SFDelete__c
                                where tableName__c = 'Event'];
        System.assertEquals(1, delList.size());
        System.assertEquals(e.Id, delList[0].delSfId__c);
    }
}