高章伟
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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
 * 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 QISMBCTriggerTest {
 
    static testMethod void myUnitTest() {
        QIS_Report__c qr = new QIS_Report__c();
        qr.RC__c = UserInfo.getUserId();
        qr.Damage_For_Doc_Or_Pat__c = '有';
        qr.Relation_With_The_Problem__c = '有可能';
        qr.Report_For_Goz__c = '不知道';
        insert qr;
        
        QIS_M_BC__c qm = new QIS_M_BC__c(
            QIS_request_name__c = qr.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        insert qm;
        
        qm = [select Name, OwnerAlias__c, QIS_submit_date_for_MBC__c, QIS_M_BC_reception__c,
                QIS_M_BC_answer_day__c, Received_feiqi_date__c from QIS_M_BC__c where Id = :qm.Id];
        
        qr = [select Wether_publish_QIS_not__c, QIS_no_for_m_BC__c, QIS_submit_date_for_MBC__c, QIS_M_BC_submit_person__c,
                QIS_M_BC_reception__c, QIS_M_BC_reply_date__c, Received_feiqi_date__c, QIS_M_BC_answer__c
                from QIS_Report__c where Id = :qr.Id];
        //System.assertEquals('发行', qr.Wether_publish_QIS_not__c);
        System.assertEquals(qm.Name, qr.QIS_no_for_m_BC__c);
        System.assertEquals(qm.QIS_submit_date_for_MBC__c, qr.QIS_submit_date_for_MBC__c);
        System.assertEquals(qm.OwnerAlias__c, qr.QIS_M_BC_submit_person__c);
        System.assertEquals(qm.QIS_M_BC_reception__c, qr.QIS_M_BC_reception__c);
        System.assertEquals(qm.QIS_M_BC_answer_day__c, qr.QIS_M_BC_reply_date__c);
        System.assertEquals(qm.Received_feiqi_date__c, qr.Received_feiqi_date__c);
        System.assertEquals('test', qr.QIS_M_BC_answer__c);
    }
    
    static testMethod void insertQISMBCTest() {
      QIS_Report__c qr = new QIS_Report__c();
        qr.RC__c = UserInfo.getUserId();
        qr.Damage_For_Doc_Or_Pat__c = '有';
        qr.Relation_With_The_Problem__c = '有可能';
        qr.Report_For_Goz__c = '不知道';
        insert qr;
        QIS_Report__c qr2 = new QIS_Report__c();
        qr2.RC__c = UserInfo.getUserId();
        qr2.Damage_For_Doc_Or_Pat__c = '有';
        qr2.Relation_With_The_Problem__c = '有可能';
        qr2.Report_For_Goz__c = '不知道';
        insert qr2;
        
        QIS_M_BC__c qm = new QIS_M_BC__c(
            QIS_request_name__c = qr.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        QIS_M_BC__c qm2 = new QIS_M_BC__c(
            QIS_request_name__c = qr2.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        insert new List<QIS_M_BC__c> {qm, qm2};
        
        qr = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr.Id];
        qm = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm.Id];
        qr2 = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr2.Id];
        qm2 = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm2.Id];
        
        System.assertEquals(qm.Name, qr.QIS_no_for_m_BC__c);
        System.assertEquals(qm.QIS_request_name__c, qr.Id);
        System.assertEquals(qm2.Name, qr2.QIS_no_for_m_BC__c);
        System.assertEquals(qm2.QIS_request_name__c, qr2.Id);
    }
    
    static testMethod void updateQISMBCTest() {
        System.Test.StartTest();
      QIS_Report__c qr = new QIS_Report__c();
        qr.RC__c = UserInfo.getUserId();
        qr.Damage_For_Doc_Or_Pat__c = '有';
        qr.Relation_With_The_Problem__c = '有可能';
        qr.Report_For_Goz__c = '不知道';
        insert qr;
        QIS_Report__c qr2 = new QIS_Report__c();
        qr2.RC__c = UserInfo.getUserId();
        qr2.Damage_For_Doc_Or_Pat__c = '有';
        qr2.Relation_With_The_Problem__c = '有可能';
        qr2.Report_For_Goz__c = '不知道';
        insert qr2;
        QIS_Report__c qr3 = new QIS_Report__c();
        qr3.RC__c = UserInfo.getUserId();
        qr3.Damage_For_Doc_Or_Pat__c = '有';
        qr3.Relation_With_The_Problem__c = '有可能';
        qr3.Report_For_Goz__c = '不知道';
        insert qr3;
        
        QIS_M_BC__c qm = new QIS_M_BC__c(
            QIS_request_name__c = qr.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        QIS_M_BC__c qm2 = new QIS_M_BC__c(
            QIS_request_name__c = qr2.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        insert new List<QIS_M_BC__c> {qm, qm2};
        System.Test.StopTest();
        qr = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr.Id];
        qm = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm.Id];
        qr2 = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr2.Id];
        qm2 = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm2.Id];
        
        System.assertEquals(qm.Name, qr.QIS_no_for_m_BC__c);
        System.assertEquals(qm.QIS_request_name__c, qr.Id);
        System.assertEquals(qm2.Name, qr2.QIS_no_for_m_BC__c);
        System.assertEquals(qm2.QIS_request_name__c, qr2.Id);
        
        qm.QIS_request_name__c = qr2.id;
        qm2.QIS_request_name__c = qr3.id;
        update new List<QIS_M_BC__c> {qm, qm2};
        
        //qr = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr.Id];
        //qr2 = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr2.Id];
        //qr3 = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr3.Id];
        //qm = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm.Id];
        //qm2 = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm2.Id];
        
        //System.assertEquals(null, qr.QIS_no_for_m_BC__c);
        //System.assertEquals(qm.Name, qr2.QIS_no_for_m_BC__c);
        //System.assertEquals(qm.QIS_request_name__c, qr2.Id);
        //System.assertEquals(qm2.Name, qr3.QIS_no_for_m_BC__c);
        //System.assertEquals(qm2.QIS_request_name__c, qr3.Id);
    }
    // static testMethod void QISMBCTest_2() {
    //     QIS_Report__c qr = new QIS_Report__c();
    //     qr.RC__c = UserInfo.getUserId();
    //     qr.Damage_For_Doc_Or_Pat__c = '有';
    //     qr.Relation_With_The_Problem__c = '有可能';
    //     qr.Report_For_Goz__c = '不知道';
    //     insert qr;
    //     QIS_Report__c qr2 = new QIS_Report__c();
    //     qr2.RC__c = UserInfo.getUserId();
    //     qr2.Damage_For_Doc_Or_Pat__c = '有';
    //     qr2.Relation_With_The_Problem__c = '有可能';
    //     qr2.Report_For_Goz__c = '不知道';
    //     insert qr2;
    //     QIS_Report__c qr3 = new QIS_Report__c();
    //     qr3.RC__c = UserInfo.getUserId();
    //     qr3.Damage_For_Doc_Or_Pat__c = '有';
    //     qr3.Relation_With_The_Problem__c = '有可能';
    //     qr3.Report_For_Goz__c = '不知道';
    //     insert qr3;
        
    //     QIS_M_BC__c qm = new QIS_M_BC__c(
    //         QIS_request_name__c = qr.Id,
    //         QIS_submit_date_for_MBC__c = Date.today(),
    //         QIS_M_BC_reception__c = Date.today().addDays(1),
    //         QIS_M_BC_answer_day__c = Date.today().addDays(1),
    //         Received_feiqi_date__c = Date.today().addDays(1),
    //         QIS_M_BC_answer_detail__c = 'test',
    //         accsessary_ship__c = 'test'
    //     );
    //     QIS_M_BC__c qm2 = new QIS_M_BC__c(
    //         QIS_request_name__c = qr2.Id,
    //         QIS_submit_date_for_MBC__c = Date.today(),
    //         QIS_M_BC_reception__c = Date.today().addDays(1),
    //         QIS_M_BC_answer_day__c = Date.today().addDays(1),
    //         Received_feiqi_date__c = Date.today().addDays(1),
    //         QIS_M_BC_answer_detail__c = 'test',
    //         accsessary_ship__c = 'test'
    //     );
    //     insert new List<QIS_M_BC__c> {qm, qm2};
    //     qm.QIS_request_name__c = qr2.id;
    //     qm2.QIS_request_name__c = qr3.id;
    //     update new List<QIS_M_BC__c> {qm, qm2};
        
    //     qr = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr.Id];
    //     qr2 = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr2.Id];
    //     qr3 = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr3.Id];
    //     qm = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm.Id];
    //     //qm2 = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm2.Id];
        
    //     System.assertEquals(null, qr.QIS_no_for_m_BC__c);
    //     System.assertEquals(qm.Name, qr2.QIS_no_for_m_BC__c);
    //     System.assertEquals(qm.QIS_request_name__c, qr2.Id);
    //     //System.assertEquals('OSH--149PA-100489', qr3.QIS_no_for_m_BC__c);
    //    // System.assertEquals(qm2.QIS_request_name__c, qr3.Id);
    // }
    static testMethod void deleteQISMBCTest() {
      QIS_Report__c qr = new QIS_Report__c();
        qr.RC__c = UserInfo.getUserId();
        qr.Damage_For_Doc_Or_Pat__c = '有';
        qr.Relation_With_The_Problem__c = '有可能';
        qr.Report_For_Goz__c = '不知道';
        insert qr;
        
        QIS_M_BC__c qm = new QIS_M_BC__c(
            QIS_request_name__c = qr.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        insert qm;
        
        qr = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr.Id];
        qm = [select Id, Name, QIS_request_name__c from QIS_M_BC__c where Id = :qm.Id];
        
        System.assertEquals(qm.Name, qr.QIS_no_for_m_BC__c);
        System.assertEquals(qm.QIS_request_name__c, qr.Id);
        
        delete qm;
        
        qr = [select Id, QIS_no_for_m_BC__c from QIS_Report__c where Id = :qr.Id];
        System.assertEquals(null, qr.QIS_no_for_m_BC__c);
    }
    
    static testMethod void insertErrorTest() {
      QIS_Report__c qr = new QIS_Report__c();
        qr.RC__c = UserInfo.getUserId();
        qr.Damage_For_Doc_Or_Pat__c = '有';
        qr.Relation_With_The_Problem__c = '有可能';
        qr.Report_For_Goz__c = '不知道';
        insert qr;
        
        QIS_M_BC__c qm = new QIS_M_BC__c(
            QIS_request_name__c = qr.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        QIS_M_BC__c qm2 = new QIS_M_BC__c(
            QIS_request_name__c = qr.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        
        try {
          insert new List<QIS_M_BC__c> {qm, qm2};
          System.assertEquals('没有执行', qm.Id);
        } catch (DmlException e) {
          System.assert(e.getMessage().contains('一个QIS只能创建一个QISMBC'), e.getMessage());
        }
    }
    
    static testMethod void updateErrorTest() {
      QIS_Report__c qr = new QIS_Report__c();
        qr.RC__c = UserInfo.getUserId();
        qr.Damage_For_Doc_Or_Pat__c = '有';
        qr.Relation_With_The_Problem__c = '有可能';
        qr.Report_For_Goz__c = '不知道';
        insert qr;
        QIS_Report__c qr2 = new QIS_Report__c();
        qr2.RC__c = UserInfo.getUserId();
        qr2.Damage_For_Doc_Or_Pat__c = '有';
        qr2.Relation_With_The_Problem__c = '有可能';
        qr2.Report_For_Goz__c = '不知道';
        insert qr2;
        
        QIS_M_BC__c qm = new QIS_M_BC__c(
            QIS_request_name__c = qr.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        QIS_M_BC__c qm2 = new QIS_M_BC__c(
            QIS_request_name__c = qr2.Id,
            QIS_submit_date_for_MBC__c = Date.today(),
            QIS_M_BC_reception__c = Date.today().addDays(1),
            QIS_M_BC_answer_day__c = Date.today().addDays(1),
            Received_feiqi_date__c = Date.today().addDays(1),
            QIS_M_BC_answer_detail__c = 'test',
            accsessary_ship__c = 'test'
        );
        
        insert new List<QIS_M_BC__c> {qm, qm2};
        
        qm2.QIS_request_name__c = qr.Id;
        
        try {
          update qm2;
          System.assertEquals('没有执行', qm.Id);
        } catch (DmlException e) {
          System.assert(e.getMessage().contains('一个QIS只能创建一个QISMBC'), e.getMessage());
        }
    }
    
    // static testMethod void setMailsTest() {
    //   QIS_Report__c qr = new QIS_Report__c(
    //         RC__c = UserInfo.getUserId(),
    //     OCM_Manager_Mail__c = 'qr_ocm_manager_mail@test.com',
    //     OCM_Member_Mail__c = 'qr_ocm_member_mail@test.com',
    //     OCM_Repair_Mail__c = 'qr_ocm_repair_mail@test.com',
    //     OCM_Repair_Mail1__c = 'qr_ocm_repair_mail1@test.com',
    //         Damage_For_Doc_Or_Pat__c = '有',
    //         Relation_With_The_Problem__c = '有可能',
    //         Report_For_Goz__c = '不知道'
    //   );
    //     insert qr;
    //     QIS_Report__c qr2 = new QIS_Report__c(
    //         RC__c = UserInfo.getUserId(),
    //     OCM_Manager_Mail__c = 'qr2_ocm_manager_mail@test.com',
    //     OCM_Member_Mail__c = 'qr2_ocm_member_mail@test.com',
    //     OCM_Repair_Mail__c = 'qr2_ocm_repair_mail@test.com',
    //     OCM_Repair_Mail1__c = 'qr2_ocm_repair_mail1@test.com',
    //         Damage_For_Doc_Or_Pat__c = '有',
    //         Relation_With_The_Problem__c = '有可能',
    //         Report_For_Goz__c = '不知道'
    //   );
    //     insert qr2;
        
    //     QIS_M_BC__c qm = new QIS_M_BC__c(
    //         QIS_request_name__c = qr.Id,
    //         QIS_submit_date_for_MBC__c = Date.today(),
    //         QIS_M_BC_reception__c = Date.today().addDays(1),
    //         QIS_M_BC_answer_day__c = Date.today().addDays(1),
    //         Received_feiqi_date__c = Date.today().addDays(1),
    //         QIS_M_BC_answer_detail__c = 'test',
    //         accsessary_ship__c = 'test'
    //     );
        
    //     insert qm;
        
    //     qm = [select Id, Name, QIS_request_name__c, OCM_Manager_Mail__c, OCM_Member_Mail__c, OCM_Repair_Mail__c, OCM_Repair_Mail1__c from QIS_M_BC__c where Id = :qm.Id];
        
    //     System.assertEquals('qr_ocm_manager_mail@test.com', qm.OCM_Manager_Mail__c);
    //     System.assertEquals('qr_ocm_member_mail@test.com', qm.OCM_Member_Mail__c);
    //     System.assertEquals('qr_ocm_repair_mail@test.com', qm.OCM_Repair_Mail__c);
    //     System.assertEquals('qr_ocm_repair_mail1@test.com', qm.OCM_Repair_Mail1__c);
        
    //     qm.QIS_request_name__c = qr2.Id;
    //     update qm;
        
    //     qm = [select Id, Name, QIS_request_name__c, OCM_Manager_Mail__c, OCM_Member_Mail__c, OCM_Repair_Mail__c, OCM_Repair_Mail1__c from QIS_M_BC__c where Id = :qm.Id];
        
    //     System.assertEquals('qr2_ocm_manager_mail@test.com', qm.OCM_Manager_Mail__c);
    //     System.assertEquals('qr2_ocm_member_mail@test.com', qm.OCM_Member_Mail__c);
    //     System.assertEquals('qr2_ocm_repair_mail@test.com', qm.OCM_Repair_Mail__c);
    //     System.assertEquals('qr2_ocm_repair_mail1@test.com', qm.OCM_Repair_Mail1__c);
    // }
}