/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class BidAnnounceTriggerTest {

    static testMethod void test1_fail() {
        Opportunity opp = new Opportunity(
            Name='Opp',
            StageName='引合',
            CloseDate=Date.today(),
            CurrencyIsoCode = 'CNY'
        );
        insert opp;
        
        Quote q = new Quote(
            Name = 'quote',
            OpportunityId = opp.Id
        );
        insert q;

        Bid_Announcement__c ba = new Bid_Announcement__c(
            Opportunity_name__c = opp.Id,
            Status__c = '草案中'
        );
        insert ba;
        
        try {
            ba.Status__c = '处理完毕';
            update ba;
        } catch (Exception e) {
            // 一回目はエラーになります。
            ba = [select Status__c from Bid_Announcement__c where Id = :ba.Id];
            System.assertEquals('草案中', ba.Status__c);
        }
    }
    
    static testMethod void test1_success() {
        Opportunity opp = new Opportunity(
            Name='Opp',
            StageName='引合',
            CloseDate=Date.today(),
            CurrencyIsoCode = 'CNY'
        );
        insert opp;
        
        Quote q = new Quote(
            Name = 'quote',
            OpportunityId = opp.Id
        );
        insert q;
        
        opp.Estimation_Id__c = q.Id;
        update opp;

        Bid_Announcement__c ba = new Bid_Announcement__c(
            Opportunity_name__c = opp.Id,
            Status__c = '草案中'
        );
        insert ba;
        
        ba.Status__c = '处理完毕';
        update ba;
        
        // 今回は成功
        ba = [select Status__c, Quote__c from Bid_Announcement__c where Id = :ba.Id];
        System.assertEquals('处理完毕', ba.Status__c);
        System.assertEquals(q.Id, ba.Quote__c);
        
        q = [select BidAnnounce__c from Quote where Id = :q.id];
        System.assertEquals(ba.Id, q.BidAnnounce__c);
    }
    
    static testMethod void test2() {
        Opportunity opp = new Opportunity(
            Name='Opp',
            StageName='引合',
            CloseDate=Date.today(),
            CurrencyIsoCode = 'CNY'
        );
        insert opp;
        
        Quote q = new Quote(
            Name = 'quote',
            OpportunityId = opp.Id
        );
        insert q;
        
        opp.Estimation_Id__c = q.Id;
        update opp;

        Bid_Announcement__c ba = new Bid_Announcement__c(
            Opportunity_name__c = opp.Id,
            Status__c = '处理完毕'
        );
        insert ba;
        
        // 取消中标
        ba.Status__c = '草案中';
        update ba;
        
        ba = [select Status__c, Quote__c from Bid_Announcement__c where Id = :ba.Id];
        System.assertEquals('草案中', ba.Status__c);
        System.assertEquals(null, ba.Quote__c);
        
        q = [select BidAnnounce__c from Quote where Id = :q.id];
        System.assertEquals(null, q.BidAnnounce__c);
    }
}