高章伟
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
@isTest
private class SSOpportunityBatch2Test {
 
    private static Product2 product = null;
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
 
    static {
        product = new Product2( Name='テスト商品');
        insert product;
    }
 
    private static PricebookEntry buildPB(String input) {
        PricebookEntry entry = new PricebookEntry(Pricebook2Id=pricebookId, Product2Id=product.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = input;
        insert entry;
        return entry;
    }
 
    @isTest(SeeAllData=true)
    static void makeSqlTest() {
        // データを作って、データ移行の項目をテスト
        // テストデータ insertSSTest と同じするする
        Opportunity opp1 = new Opportunity(Name='aiueo1', StageName='出荷', CurrencyIsoCode='USD', CloseDate=Date.today());
        Opportunity opp2 = new Opportunity(Name='aiueo2', StageName='出荷', CurrencyIsoCode='CNY', CloseDate=Date.today());
        insert new Opportunity[] {opp1, opp2};
//        PricebookEntry entryUSD = buildPB('USD');       // opp1用
        PricebookEntry entryCNY = buildPB('CNY');       // opp2用
        OpportunityLineItem oli21 = new OpportunityLineItem(OpportunityId=opp2.Id, Name__c='oli21', Quantity=21, UnitPrice=100, PricebookEntryId=entryCNY.Id);
        OpportunityLineItem oli22 = new OpportunityLineItem(OpportunityId=opp2.Id, Name__c='oli22', Quantity=22, UnitPrice=200, PricebookEntryId=entryCNY.Id);
        insert new OpportunityLineItem[] {oli21, oli22};
        Id execBTId = Database.executeBatch(new SSOpportunityBatch2(new Id[] {opp1.Id}), 1);
        // ExceptionがなければOK
 
        // 念のため、再度 select sql の確認
        List<SS_Batch_Column_Mapping__c> settingList = SS_Batch_Column_Mapping__c.getall().values();
        for (SS_Batch_Column_Mapping__c setting : settingList) {
            String soql = SSOpportunityBatch2.makeSql(setting, new Set<String>());
            soql += ' limit 1';
            Database.query(soql);
            // ExceptionがなければOK
        }
    }
 
    @isTest
    static void updateOppTest() {
        // カスタム設定を定義
        SS_Batch_Column_Mapping__c oppMapping = new SS_Batch_Column_Mapping__c(Name = 'Opportunity', SS_TableName__c='SS_Prospect_5days_list__c');
        oppMapping.From_Column_001__c = 'Id';
        oppMapping.SS_Column_001__c = 'Opportunity.Name';
        oppMapping.From_Column_002__c = 'Id';                           // 重複してもエラーにはならない
        oppMapping.SS_Column_002__c = 'Opportunity.Name';
        oppMapping.From_Column_003__c = 'Forecast_last_month__c';
        oppMapping.SS_Column_003__c = 'Forecast_this_month__c';
        oppMapping.From_Column_004__c = 'Last_Shipping_Date_backup__c';
        oppMapping.SS_Column_004__c = 'Last_Shipping_Date__c';
        SS_Batch_Column_Mapping__c oliMapping = new SS_Batch_Column_Mapping__c(Name = 'OpportunityLineItem', SS_TableName__c='SS_Prospect_detail_product__c');
        insert new SS_Batch_Column_Mapping__c[] {oppMapping, oliMapping};
        // テストデータ
        Opportunity opp1 = new Opportunity(Name='aiueo1', StageName='引合', CloseDate=Date.today());
        Opportunity opp2 = new Opportunity(Name='aiueo2', StageName='引合', CloseDate=Date.today());
        insert new Opportunity[] {opp1, opp2};
 
        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new SSOpportunityBatch2(null), 5);
        System.Test.StopTest();
    }
 
    @isTest
    static void insertSSTest() {
        // カスタム設定を定義
        SS_Batch_Column_Mapping__c oppMapping = new SS_Batch_Column_Mapping__c(Name = 'Opportunity', SS_TableName__c='SS_Prospect_5days_list__c');
        oppMapping.From_Column_001__c = 'Name';
        oppMapping.SS_Column_001__c = 'Opportunity_Name__c';
        oppMapping.From_Column_002__c = 'Owner.Alias';
        oppMapping.SS_Column_002__c = 'Account_Name__c';
        oppMapping.From_Column_003__c = 'Forecast_last_month__c';
        oppMapping.SS_Column_003__c = 'Forecast_this_month__c';
        oppMapping.From_Column_004__c = 'Last_Shipping_Date_backup__c';
        oppMapping.SS_Column_004__c = 'Last_Shipping_Date__c';
        SS_Batch_Column_Mapping__c oliMapping = new SS_Batch_Column_Mapping__c(Name = 'OpportunityLineItem', SS_TableName__c='SS_Prospect_detail_product__c');
        oliMapping.From_Column_001__c = 'Name__c';
        oliMapping.SS_Column_001__c = 'Product_name__c';
        oliMapping.From_Column_002__c = 'Quantity';
        oliMapping.SS_Column_002__c = 'Number__c';
        insert new SS_Batch_Column_Mapping__c[] {oppMapping, oliMapping};
 
        // テストデータ
        Opportunity opp1 = new Opportunity(Name='aiueo1', StageName='引合', CurrencyIsoCode='USD', CloseDate=Date.today());
        Opportunity opp2 = new Opportunity(Name='aiueo2', StageName='引合', CurrencyIsoCode='CNY', CloseDate=Date.today());
        insert new Opportunity[] {opp1, opp2};
//        PricebookEntry entryUSD = buildPB('USD');       // opp1用
        PricebookEntry entryCNY = buildPB('CNY');       // opp2用
        OpportunityLineItem oli21 = new OpportunityLineItem(OpportunityId=opp2.Id, Name__c='oli21', Quantity=21, UnitPrice=100, PricebookEntryId=entryCNY.Id);
        OpportunityLineItem oli22 = new OpportunityLineItem(OpportunityId=opp2.Id, Name__c='oli22', Quantity=22, UnitPrice=200, PricebookEntryId=entryCNY.Id);
        insert new OpportunityLineItem[] {oli21, oli22};
 
        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new SSOpportunityBatch2(null), 5);
        System.Test.StopTest();
    }
}