@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);
	}
}