require 'spec_helper'

describe AffiliateDistribution do
  let(:affiliate_partner){ FactoryGirl.create(:affiliate_partner) }
  let(:affiliate_distribution1){ FactoryGirl.create(:affiliate_distribution, created_at: DateTime.new(2015, 1, 31), product_type: 'Subscription', product_code: 'jamsubsilver', affiliate_referral: affiliate_partner) }
  let(:affiliate_distribution2){ FactoryGirl.create(:affiliate_distribution, created_at: DateTime.new(2015, 2, 1), product_type: 'Subscription', product_code: 'jamsubsilver', affiliate_referral: affiliate_partner) }
  let(:affiliate_distribution3){ FactoryGirl.create(:affiliate_distribution, created_at: DateTime.new(2015, 2, 2), product_type: 'Subscription', product_code: 'jamsubgold', affiliate_referral: affiliate_partner) }
  let(:affiliate_distribution4){ FactoryGirl.create(:affiliate_distribution, created_at: DateTime.new(2015, 2, 3), product_type: 'Subscription', product_code: 'jamsubgold', affiliate_referral: affiliate_partner) }
  let(:affiliate_distribution5){ FactoryGirl.create(:affiliate_distribution, created_at: DateTime.new(2015, 2, 7), product_type: 'Subscription', product_code: 'jamsubplatinum', affiliate_referral: affiliate_partner) }
  let(:affiliate_distribution6){ FactoryGirl.create(:affiliate_distribution, created_at: DateTime.new(2015, 3, 1), product_type: 'Subscription', product_code: 'jamsubsilver', affiliate_referral: affiliate_partner) }
  
  it "gives subscription plans counts for a partner between start and end dates" do
   
    affiliate_distribution1.reload
    affiliate_distribution2.reload
    affiliate_distribution3.reload
    affiliate_distribution4.reload
    affiliate_distribution5.reload
    affiliate_distribution6.reload

    start_date = Date.new(2015, 2, 1)
    end_date = Date.new(2015, 2, 7)

    expect(AffiliateDistribution.count).to eq(6)
    expect(AffiliateDistribution.subscription_plans_count(affiliate_partner.id, start_date, end_date)).to eq(
      [
        { plan: 'jamsubsilver', count: 1 },
        { plan: 'jamsubgold', count: 2 },
        { plan: 'jamsubplatinum', count: 1 }
      ]
    )
    
  end
end