@isTest
private class RentalApplyHpDeptUpdTriggerTest {

    static testMethod void testUpdate() {
        // recode type を取得
        List<RecordType> recthp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (recthp.size() == 0) {
            return;
        }
        List<RecordType> rectdc = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectdc.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 hp1 = new Account();
        hp1.RecordTypeId = recthp[0].Id;
        hp1.Name = '病院テスト1';
        
        Account hp2 = new Account();
        hp2.RecordTypeId = recthp[0].Id;
        hp2.Name = '病院テスト2';
        
        insert new Account[] {hp1, hp2};
  
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp1.Id order by Department_Class_Label__c];
        List<Account> dc2s = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp2.Id order by Department_Class_Label__c];

        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = hp1.Id;
        
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[1].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc2s[1].Id;
        depart2.Department_Class__c = dc2s[1].Id;
        depart2.Hospital__c         = hp2.Id;

        insert new Account[] {depart1, depart2};

        Rental_Apply__c ra1 = new Rental_Apply__c(Account__c = depart1.Id
                , Hospital__c = hp1.Id
                , Strategic_dept__c =dc1s[0].Id
                , applyUser__c = UserInfo.getUserId()
                , direct_send__c = '直送'
                , direct_shippment_address__c = 'test'
                , Request_return_day__c = Date.today() + 30
        );

        Rental_Apply__c ra2 = new Rental_Apply__c(Account__c = depart2.Id
                , Hospital__c = hp2.Id
                , Strategic_dept__c =dc2s[1].Id
                , applyUser__c = UserInfo.getUserId()
                , direct_send__c = '直送'
                , direct_shippment_address__c = 'test'
                , Request_return_day__c = Date.today() + 30
        );

        insert new Rental_Apply__c[] {ra1, ra2};

        //まずひとつ追加
        //まずひとつ
        Rental_Apply__c raChanged = [Select Id, Account__c, Strategic_dept__c, Hospital__c from Rental_Apply__c where Id = :ra1.Id ];
        System.assertEquals(raChanged.Strategic_dept__c, dc1s[0].Id);
        System.assertEquals(raChanged.Hospital__c , hp1.Id);

        Rental_Apply__c raChanged2 = [Select Id, Account__c, Strategic_dept__c, Hospital__c from Rental_Apply__c where Id = :ra2.Id];        
        System.assertEquals(raChanged2.Strategic_dept__c, dc2s[1].Id);
        System.assertEquals(raChanged2.Hospital__c , hp2.Id);
        
        ra1.Account__c = depart2.Id;
        ra2.Account__c = depart1.Id;
        update new Rental_Apply__c[] {ra1, ra2};
        
        //まずひとつ
        raChanged = [Select Id, Account__c, Strategic_dept__c, Hospital__c from Rental_Apply__c where Id = :ra1.Id ];
        System.assertEquals(raChanged.Strategic_dept__c, dc2s[1].Id);
        System.assertEquals(raChanged.Hospital__c , hp2.Id);

        raChanged2 = [Select Id, Account__c, Strategic_dept__c, Hospital__c from Rental_Apply__c where Id = :ra2.Id];       
        System.assertEquals(raChanged2.Strategic_dept__c, dc1s[0].Id);
        System.assertEquals(raChanged2.Hospital__c , hp1.Id);

    }
}