liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
 * 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 OppMonthlyRatingBatchTest {
 
    static testMethod void myUnitTest() {
        // recode type を取得
        //病院
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'] ;
        if (rectCo.size() == 0) {
            return;
        }
        
        //戦略科室分類
        RecordType rectSct1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 普外科'];
        RecordType rectSct2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        
        //科室分類
        RecordType rectDpt1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 普外科'];
        RecordType rectDpt2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科'];
        
        //商談のレコードタイプ
        RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity' ];
        
         Address_Level__c al1 = new Address_Level__c(
            Name = '四川省'
        );
        insert al1;
        Address_Level2__c al2 = new Address_Level2__c(
            Name = '成都市',
            Address_Level__c = al1.Id
        );
        insert al2;
        
 
        // insert
        Account company1 = new Account();
        Account company2 = new Account();
        
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = 'HPテスト1';
        company1.State_Master__c = al1.Id;
        company1.City_Master__c = al2.Id;
        
        company2.RecordTypeId = rectCo[0].Id;
        company2.Name = 'HPテスト2';
        company2.State_Master__c = al1.Id;
        company2.City_Master__c = al2.Id;
        List<Account> hps = new Account[] {company1, company2};
        insert hps;
  
        Account dc1= [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id and RecordTypeId =: rectSct1.Id] ;
        Account dc2= [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id and RecordTypeId =: rectSct2.Id] ;
        
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt1.Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1.Id;
        depart1.Department_Class__c = dc1.Id;
        depart1.Hospital__c         = company1.Id;
        
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt2.Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc2.Id;
        depart2.Department_Class__c = dc2.Id;
        depart2.Hospital__c         = company2.Id;
 
        insert new Account[] {depart1, depart2};
        
        Date prevDate = Date.today().addMonths(-1);
        Date prevDate2 = Date.today().addMonths(-2);
        
        Opportunity opp1 = new Opportunity(
            Name='testOpp1',
            StageName='引合',
            CloseDate=Date.today(),
            AccountId=depart1.Id,
            Competitor__c ='A',
            Click_Close_Date__c = null,
            RecordType = rectOpp
        );
        Opportunity opp2 = new Opportunity(
            Name='testOpp2',
            StageName='引合',
            CloseDate=Date.today(),
            AccountId=depart2.Id,
            Competitor__c ='B',
            Click_Close_Date__c = null,
            RecordType = rectOpp
        );
        Opportunity opp3 = new Opportunity(
            Name='testOpp3',
            StageName='引合',
            CloseDate=Date.today(),
            AccountId=depart1.Id,
            Competitor__c ='C',
            Click_Close_Date__c = Date.today().addMonths(-5),
            RecordType = rectOpp
        );
        Opportunity opp4 = new Opportunity(
            Name='testOpp4',
            StageName='引合',
            CloseDate=Date.today(),
            AccountId=depart1.Id,
            Competitor__c ='C',
            RecordType = rectOpp
        );
        
        
        insert new Opportunity[] {opp1, opp2, opp3, opp4};
        
        opp4.StageName = '完了';
        update opp4;
        
        
        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new OppMonthlyRatingBatch(), 4);
        System.Test.StopTest();
        
        
        Date prevYMD = Date.newInstance(prevDate.year(), prevDate.month(), 1);
        List<Opportunity> oppList = [select Id from Opportunity where RecordType.DeveloperName in :ControllerUtil.oppRecordTypeDevNames
                                         and (Click_Close_Date__c = null or Click_Close_Date__c >= :prevYMD)
                                         and (id = :opp1.Id) ];
                                         
        system.assertEquals(1, oppList.size());
        Integer thisMonth = Date.today().month();
        opp1 = [select Rating__c, Rating01__c,Rating02__c,Rating03__c,Rating04__c,Rating05__c,Rating06__c,Rating07__c,Rating08__c,Rating09__c,Rating10__c,Rating11__c,Rating12__c
                from Opportunity where id = :opp1.Id];
        if (thisMonth == 1) {
            system.assertEquals(opp1.Rating__c, opp1.Rating12__c);
            system.assertEquals(null, opp1.Rating01__c);
        } else if (thisMonth == 2) {
            system.assertEquals(opp1.Rating__c, opp1.Rating01__c);
            system.assertEquals(null, opp1.Rating02__c);
        } else if (thisMonth == 3) {
            system.assertEquals(opp1.Rating__c, opp1.Rating02__c);
            system.assertEquals(null, opp1.Rating03__c);
        } else if (thisMonth == 4) {
            system.assertEquals(opp1.Rating__c, opp1.Rating03__c);
            system.assertEquals(null, opp1.Rating04__c);
        } else if (thisMonth == 5) {
            system.assertEquals(opp1.Rating__c, opp1.Rating04__c);
            system.assertEquals(null, opp1.Rating05__c);
        } else if (thisMonth == 6) {
            system.assertEquals(opp1.Rating__c, opp1.Rating05__c);
            system.assertEquals(null, opp1.Rating06__c);
        } else if (thisMonth == 7) {
            system.assertEquals(opp1.Rating__c, opp1.Rating06__c);
            system.assertEquals(null, opp1.Rating07__c);
        } else if (thisMonth == 8) {
            system.assertEquals(opp1.Rating__c, opp1.Rating07__c);
            system.assertEquals(null, opp1.Rating08__c);
        } else if (thisMonth == 9) {
            system.assertEquals(opp1.Rating__c, opp1.Rating08__c);
            system.assertEquals(null, opp1.Rating09__c);
        } else if (thisMonth == 10) {
            system.assertEquals(opp1.Rating__c, opp1.Rating09__c);
            system.assertEquals(null, opp1.Rating10__c);
        } else if (thisMonth == 11) {
            system.assertEquals(opp1.Rating__c, opp1.Rating10__c);
            system.assertEquals(null, opp1.Rating11__c);
        } else if (thisMonth == 12) {
            system.assertEquals(opp1.Rating__c, opp1.Rating11__c);
            system.assertEquals(null, opp1.Rating12__c);
        } else {}
        
        opp2 = [select Rating__c, Rating01__c,Rating02__c,Rating03__c,Rating04__c,Rating05__c,Rating06__c,Rating07__c,Rating08__c,Rating09__c,Rating10__c,Rating11__c,Rating12__c
                from Opportunity where id = :opp2.Id];
        if (thisMonth == 1) {
            system.assertEquals(opp2.Rating__c, opp2.Rating12__c);
            system.assertEquals(null, opp2.Rating01__c);
        } else if (thisMonth == 2) {
            system.assertEquals(opp2.Rating__c, opp2.Rating01__c);
            system.assertEquals(null, opp2.Rating02__c);
        } else if (thisMonth == 3) {
            system.assertEquals(opp2.Rating__c, opp2.Rating02__c);
            system.assertEquals(null, opp2.Rating03__c);
        } else if (thisMonth == 4) {
            system.assertEquals(opp2.Rating__c, opp2.Rating03__c);
            system.assertEquals(null, opp2.Rating04__c);
        } else if (thisMonth == 5) {
            system.assertEquals(opp2.Rating__c, opp2.Rating04__c);
            system.assertEquals(null, opp2.Rating05__c);
        } else if (thisMonth == 6) {
            system.assertEquals(opp2.Rating__c, opp2.Rating05__c);
            system.assertEquals(null, opp2.Rating06__c);
        } else if (thisMonth == 7) {
            system.assertEquals(opp2.Rating__c, opp2.Rating06__c);
            system.assertEquals(null, opp2.Rating07__c);
        } else if (thisMonth == 8) {
            system.assertEquals(opp2.Rating__c, opp2.Rating07__c);
            system.assertEquals(null, opp2.Rating08__c);
        } else if (thisMonth == 9) {
            system.assertEquals(opp2.Rating__c, opp2.Rating08__c);
            system.assertEquals(null, opp2.Rating09__c);
        } else if (thisMonth == 10) {
            system.assertEquals(opp2.Rating__c, opp2.Rating09__c);
            system.assertEquals(null, opp2.Rating10__c);
        } else if (thisMonth == 11) {
            system.assertEquals(opp2.Rating__c, opp2.Rating10__c);
            system.assertEquals(null, opp2.Rating11__c);
        } else if (thisMonth == 12) {
            system.assertEquals(opp2.Rating__c, opp2.Rating11__c);
            system.assertEquals(null, opp2.Rating12__c);
        } else {}
        
        opp3 = [select Rating__c, Rating01__c,Rating02__c,Rating03__c,Rating04__c,Rating05__c,Rating06__c,Rating07__c,Rating08__c,Rating09__c,Rating10__c,Rating11__c,Rating12__c
                from Opportunity where id = :opp3.Id];
        system.assertEquals(null, opp3.Rating01__c);
        system.assertEquals(null, opp3.Rating02__c);
        system.assertEquals(null, opp3.Rating03__c);
        system.assertEquals(null, opp3.Rating04__c);
        system.assertEquals(null, opp3.Rating05__c);
        system.assertEquals(null, opp3.Rating06__c);
        system.assertEquals(null, opp3.Rating07__c);
        system.assertEquals(null, opp3.Rating08__c);
        system.assertEquals(null, opp3.Rating09__c);
        system.assertEquals(null, opp3.Rating10__c);
        system.assertEquals(null, opp3.Rating11__c);
        system.assertEquals(null, opp3.Rating12__c);
        
        opp4 = [select Rating__c, Rating01__c,Rating02__c,Rating03__c,Rating04__c,Rating05__c,Rating06__c,Rating07__c,Rating08__c,Rating09__c,Rating10__c,Rating11__c,Rating12__c,StageName,Click_Close_Date__c
                from Opportunity where id = :opp4.Id];
                
        system.assertEquals('完了', opp4.StageName);        
        system.assertEquals(Date.today(), opp4.Click_Close_Date__c);
        if (thisMonth == 1) {
            system.assertEquals(opp4.Rating__c, opp4.Rating12__c);
            system.assertEquals(null, opp4.Rating01__c);
        } else if (thisMonth == 2) {
            system.assertEquals(opp4.Rating__c, opp4.Rating01__c);
            system.assertEquals(null, opp4.Rating02__c);
        } else if (thisMonth == 3) {
            system.assertEquals(opp4.Rating__c, opp4.Rating02__c);
            system.assertEquals(null, opp4.Rating03__c);
        } else if (thisMonth == 4) {
            system.assertEquals(opp4.Rating__c, opp4.Rating03__c);
            system.assertEquals(null, opp4.Rating04__c);
        } else if (thisMonth == 5) {
            system.assertEquals(opp4.Rating__c, opp4.Rating04__c);
            system.assertEquals(null, opp4.Rating05__c);
        } else if (thisMonth == 6) {
            system.assertEquals(opp4.Rating__c, opp4.Rating05__c);
            system.assertEquals(null, opp4.Rating06__c);
        } else if (thisMonth == 7) {
            system.assertEquals(opp4.Rating__c, opp4.Rating06__c);
            system.assertEquals(null, opp4.Rating07__c);
        } else if (thisMonth == 8) {
            system.assertEquals(opp4.Rating__c, opp4.Rating07__c);
            system.assertEquals(null, opp4.Rating08__c);
        } else if (thisMonth == 9) {
            system.assertEquals(opp4.Rating__c, opp4.Rating08__c);
            system.assertEquals(null, opp4.Rating09__c);
        } else if (thisMonth == 10) {
            system.assertEquals(opp4.Rating__c, opp4.Rating09__c);
            system.assertEquals(null, opp4.Rating10__c);
        } else if (thisMonth == 11) {
            system.assertEquals(opp4.Rating__c, opp4.Rating10__c);
            system.assertEquals(null, opp4.Rating11__c);
        } else if (thisMonth == 12) {
            system.assertEquals(opp4.Rating__c, opp4.Rating11__c);
            system.assertEquals(null, opp4.Rating12__c);
        } else {}
    }
 
}