高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
@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);
 
    }
}