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

    static testMethod void testDelete() {
        // データ作成
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }
        // 病院、診療科
        Account hospital = new Account();
        hospital.RecordTypeId = rectCo[0].Id;
        hospital.Name         = 'TestHospital';
        upsert hospital;
        Account section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Parent.Id = :hospital.Id and RecordTypeId = :rectSct[0].Id limit 1];
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = hospital.Id;
        upsert depart;
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[0].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = 'TestDepart2';
        depart2.ParentId            = section.Id;
        depart2.Department_Class__c = section.Id;
        depart2.Hospital__c         = hospital.Id;
        upsert depart2;

        // 病院、診療科の再取得
        List<String> accIds = new String[] {hospital.Id, depart.Id, depart2.Id};
        List<Account> accList = [select Management_Code__c, Id, Service_contract_active_number__c, Service_contract_active_number_HP__c from Account where Id IN :accIds order by Management_Code__c];
        System.assertEquals(0, accList[2].Service_contract_active_number__c);
        System.assertEquals(0, accList[1].Service_contract_active_number__c);
        System.assertEquals(0, accList[0].Service_contract_active_number_HP__c);

        // 削除
        System.Test.startTest();
        delete new Account[] {depart, depart2};
        System.Test.stopTest();
        List<SFDelete__c> delList = [select Id, delSfId__c, tableName__c
                                 from SFDelete__c
                                where tableName__c = 'Account'];
        System.assertEquals(2, delList.size());
        if (depart.Id == delList[0].delSfId__c) {
            System.assertEquals(depart2.Id, delList[1].delSfId__c);
        } else {
            System.assertEquals(depart.Id, delList[0].delSfId__c);
        }
    }
}