高章伟
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
@isTest
private class SmarmSetUserCountTriggerTest {
    
    static testMethod void testTrigger() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User user = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id, Last_Smarm_Date__c = null, Smarm_Count__c = 0);
        insert user;
        
        SMARM__c sm1 = new SMARM__c();
        sm1.OwnerId = user.Id;
        sm1.CurrencyIsoCode = 'CNY';
        sm1.Report_Date__c = Date.today().addDays(-1);
        sm1.Review_Target__c = user.Id;
        insert sm1;
        
        User[] users1 = [select Id,Last_Smarm_Date__c,Smarm_Count__c from User where Id = :user.Id];
        System.assertEquals(Date.today().addDays(-1), users1[0].Last_Smarm_Date__c);
        System.assertEquals(1, users1[0].Smarm_Count__c);
        
        SMARM__c sm2 = new SMARM__c();
        sm2.OwnerId = user.Id;
        sm2.CurrencyIsoCode = 'CNY';
        sm2.Report_Date__c = Date.today();
        sm2.Review_Target__c = user.Id;
        insert sm2;
        
        User[] users2 = [select Id,Last_Smarm_Date__c,Smarm_Count__c from User where Id = :user.Id];
        System.assertEquals(Date.today(), users2[0].Last_Smarm_Date__c);
        System.assertEquals(2, users2[0].Smarm_Count__c);
        
        SMARM__c sm3 = new SMARM__c();
        sm3.OwnerId = user.Id;
        sm3.CurrencyIsoCode = 'CNY';
        sm3.Report_Date__c = Date.today().addDays(1);
        sm3.Review_Target__c = user.Id;
        insert sm3;
        
        User[] users3 = [select Id,Last_Smarm_Date__c,Smarm_Count__c from User where Id = :user.Id];
        System.assertEquals(Date.today().addDays(1), users3[0].Last_Smarm_Date__c);
        System.assertEquals(3, users3[0].Smarm_Count__c);
        
        delete new SMARM__c[] {sm2,sm3};
        
        User[] users4 = [select Id,Last_Smarm_Date__c,Smarm_Count__c from User where Id = :user.Id];
        System.assertEquals(Date.today().addDays(-1), users4[0].Last_Smarm_Date__c);
        System.assertEquals(1, users4[0].Smarm_Count__c);
        
        delete sm1;
        
        User[] users5 = [select Id,Last_Smarm_Date__c,Smarm_Count__c from User where Id = :user.Id];
        System.assertEquals(null, users5[0].Last_Smarm_Date__c);
        System.assertEquals(0, users5[0].Smarm_Count__c);
    }
}