@isTest
private class PowerBIUpdateBatchTest {
    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;
    }

    private static void setuserset(){
        PowerBISyncDefine__c oppset = new PowerBISyncDefine__c();
        oppset.Name = 'Opportunity';
        oppset.To_Table__c = 'BI_Opportunity__c';
        insert(oppset);

        PowerBISyncDefine__c oliset = new PowerBISyncDefine__c();
        oliset.Name = 'OpportunityLineItem';
        oliset.To_Table__c = 'BI_OpportunityLineItem__c';
        insert(oliset);
    }

	static testMethod void myUnitTest() {
        setuserset();

		Opportunity opp1 = new Opportunity(Name='aiueo1', StageName='引合', CurrencyIsoCode='CNY', CloseDate=Date.today());
        insert opp1;

        PricebookEntry entryCNY = buildPB('CNY');

        OpportunityLineItem oli1 = new OpportunityLineItem(OpportunityId=opp1.Id, Name__c='oli1', Quantity=21, UnitPrice=100, PricebookEntryId=entryCNY.Id);
        insert oli1;

        BI_OpportunityLineItem__c bi_oli1 = [select id, ShareWithPowerBI__c, ShareWithPowerBIFormula__c, ShareWithPowerBI_NeedUpd__c from BI_OpportunityLineItem__c where OpportunityId__c = :opp1.Id];
        System.assertEquals(true, bi_oli1.ShareWithPowerBI__c);
        System.assertEquals(true, bi_oli1.ShareWithPowerBIFormula__c);
        System.assertEquals(false, bi_oli1.ShareWithPowerBI_NeedUpd__c);

        opp1.StageName = '出荷';
        update opp1;

        BI_OpportunityLineItem__c bi_oli2 = [select id, ShareWithPowerBI__c, ShareWithPowerBIFormula__c, ShareWithPowerBI_NeedUpd__c from BI_OpportunityLineItem__c where OpportunityId__c = :opp1.Id];
        System.assertEquals(true, bi_oli2.ShareWithPowerBI__c);
        System.assertEquals(false, bi_oli2.ShareWithPowerBIFormula__c);
        System.assertEquals(true, bi_oli2.ShareWithPowerBI_NeedUpd__c);

        Id execBTId = Database.executeBatch(new PowerBIUpdateBatch(opp1.Id), 100);
	}
}