0
0

More than 1 year has passed since last update.

Batch process which periodically archive SObjects to Big Objects

Last updated at Posted at 2022-01-19

It may have been solved.

I could not use the Apex stub API to mock big object insert (given that I would have to stub the calls in the Database namespace, which is not possible). The only solution avaliable to me is to isolate the database insert/delete operations in a separate interface. Have an implementation for production and a mock implementation for tests that persists the object in-memory instead. Very cumbersome and annoying.

Apex スタブ API を使用して大きなオブジェクトの挿入をモックすることはできませんでした (データベース名前空間でコールをスタブ化する必要があるため、これは不可能です)。
私が利用できる唯一の解決策は、データベースの挿入/削除操作を別のインターフェイスに分離することです。
実稼働用の実装と、代わりにオブジェクトをメモリ内に保持するテスト用のモック実装を用意してください。非常に面倒で迷惑です。

Creating BigObject Record in Test Class Using Test.loadData(..)

public class bigObject implements database.Batchable<Sobject> {
    
    public database.QueryLocator start(database.BatchableContext bc){
        string query = 'select id,FirstName,LastName,Phone from Contact';
        return database.getQueryLocator(query);
    }
    public void execute(database.BatchableContext bc , list<Contact> scope ){
        list<PhoneBook__b> pbList = new list<PhoneBook__b>();
        for(Contact c : scope){
            PhoneBook__b pb = new PhoneBook__b();
            pb.FirstName__c = c.FirstName;
            pb.LastName__c = c.LastName;
            //pb.Address__c = '1 Market St';
            pb.PhoneNumber__c = c.Phone;
            
            pbList.add(pb);
            
        }
        if (test.isRunningTest()){            
        } else {
            database.insertImmediate(pbList);
        }
        
    }
    public void finish(database.BatchableContext bc){
        
    }
}
@isTest
public class bigObject_test {
    
    @TestSetup
    private static void createTestData() {
        
        Profile p;
		User u;
 		// プロファイルを取得する
		p = [select Id from Profile where Name = 'Standard Platform User'];
         
        // ユーザを作成する
		u = new User();
        u.FirstName = 'keiji';
		u.LastName = 'Otsubo';
		u.Alias = 'Otsubo';
		u.Email = 'K_otsubo@test.com';
		u.UserName = u.Email;
		u.EmailEncodingKey = 'ISO-2022-JP';
		u.TimeZoneSidKey = 'Asia/Tokyo';
		u.LocaleSidKey = 'ja_JP';
		u.LanguageLocaleKey = 'ja';
		u.profileId = p.Id;
        
		insert u;
        
        System.runAs(u){
            Account myAcc = new Account();
            myAcc.Name ='Test';
            insert myAcc;
            
            Contact myCon = New Contact();
            myCon.AccountId = myAcc.Id;
            myCon.FirstName = 'Fname';
            myCon.LastName = 'Lname';
            insert myCon;
        }
    }
    
    static testMethod void test1() {
       
        Test.startTest();
        
        bigObject bat  = new bigObject();
        ID jobId = Database.executeBatch(bat);
        
        Test.stopTest();
        
    }
}

The test class doesn't work.

FATAL_ERROR System.UnexpectedException: A callout was unsuccessful because of pending uncommitted work related to a process, flow, or Apex operation. Commit or roll back the work, and then try again.

public class bigObject implements database.Batchable<Sobject> {
    
    public database.QueryLocator start(database.BatchableContext bc){
        string query = 'select id,FirstName,LastName,Phone from Contact';
        return database.getQueryLocator(query);
    }
    public void execute(database.BatchableContext bc , list<Contact> scope ){
        list<PhoneBook__b> pbList = new list<PhoneBook__b>();
        for(Contact c : scope){
            PhoneBook__b pb = new PhoneBook__b();
            pb.FirstName__c = c.FirstName;
            pb.LastName__c = c.LastName;
            //pb.Address__c = '1 Market St';
            pb.PhoneNumber__c = c.Phone;
            
            pbList.add(pb);
            
        }
        database.insertImmediate(pbList);
    }
    public void finish(database.BatchableContext bc){
        
    }
}
@isTest
public class bigObject_test {
    
    private static void createTestData() {
        Account myAcc = new Account();
        myAcc.Name ='Test';
        insert myAcc;
        
        Contact myCon = New Contact();
        myCon.AccountId = myAcc.Id;
        myCon.FirstName = 'Fname';
        myCon.LastName = 'Lname';
        insert myCon;
    }
    
    static testMethod void test1() {
        createTestData();
        
        Test.startTest();
        
        bigObject bat  = new bigObject();
        ID jobId = Database.executeBatch(bat);
        
        Test.stopTest();
        
    }
}

SeeAllData = True does not work

@isTest(SeeAllData=True)
public class bigObject_test {
    
    
    
    static testMethod void test1() {
       
        Test.startTest();
        
        bigObject bat  = new bigObject();
        ID jobId = Database.executeBatch(bat);
        
        Test.stopTest();
        
    }
}

@TestSetup does not work

@isTest
public class bigObject_test {
    
    @TestSetup
    private static void createTestData() {
        
        Profile p;
		User u;
 		// プロファイルを取得する
		p = [select Id from Profile where Name = 'Standard Platform User'];
         
        // ユーザを作成する
		u = new User();
        u.FirstName = 'keiji';
		u.LastName = 'Otsubo';
		u.Alias = 'Otsubo';
		u.Email = 'K_otsubo@test.com';
		u.UserName = u.Email;
		u.EmailEncodingKey = 'ISO-2022-JP';
		u.TimeZoneSidKey = 'Asia/Tokyo';
		u.LocaleSidKey = 'ja_JP';
		u.LanguageLocaleKey = 'ja';
		u.profileId = p.Id;
        
		insert u;
        
        System.runAs(u){
            Account myAcc = new Account();
            myAcc.Name ='Test';
            insert myAcc;
            
            Contact myCon = New Contact();
            myCon.AccountId = myAcc.Id;
            myCon.FirstName = 'Fname';
            myCon.LastName = 'Lname';
            insert myCon;
        }
    }
    
    static testMethod void test1() {
       
        Test.startTest();
        
        bigObject bat  = new bigObject();
        ID jobId = Database.executeBatch(bat);
        
        Test.stopTest();
        
    }
}

未解決

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0