<commit_before>describe ChargebackRateDetail do
  describe "#chargeback_rate" do
    it "is invalid without a valid chargeback_rate" do
      invalid_chargeback_rate_id = (ChargebackRate.maximum(:id) || -1) + 1
      chargeback_rate_detail = FactoryGirl.build(:chargeback_rate_detail,
                                                 :chargeback_rate_id => invalid_chargeback_rate_id)
      expect(chargeback_rate_detail).to be_invalid
      expect(chargeback_rate_detail.errors.messages).to include(:chargeback_rate => [/can't be blank/])
    end
  end

  describe "#find_rate" do
    let(:cvalue) { {"val1" => 0.0, "val2" => 10.0, "val3" => 20.0, "val4" => 50.0} }
    let(:cbt1) { FactoryGirl.build(:chargeback_tier, :start => 0, :finish => 10, :fixed_rate => 3.0, :variable_rate => 0.3) }
    let(:cbt2) { FactoryGirl.build(:chargeback_tier, :start => 10, :finish => 50, :fixed_rate => 2.0, :variable_rate => 0.2) }
    let(:cbt3) { FactoryGirl.build(:chargeback_tier, :start => 50, :finish => Float::INFINITY, :fixed_rate => 1.0, :variable_rate => 0.1) }
    let(:cbd) { FactoryGirl.build(:chargeback_rate_detail, :chargeback_tiers => [cbt3, cbt2, cbt1]) }

    it "finds proper rate according the value" do
      expect(cbd.find_rate(cvalue["val1"])).to eq([cbt1.fixed_rate, cbt1.variable_rate])
      expect(cbd.find_rate(cvalue["val2"])).to eq([cbt1.fixed_rate, cbt1.variable_rate])
      expect(cbd.find_rate(cvalue["val3"])).to eq([cbt2.fixed_rate, cbt2.variable_rate])
      expect(cbd.find_rate(cvalue["val4"])).to eq([cbt2.fixed_rate, cbt2.variable_rate])
    end
  end

  let(:consumption) { instance_double('Consumption', :hours_in_month => (1.month / 1.hour)) }

  it '#hourly_cost' do
    cvalue   = 42.0<commit_msg>Specs for find_rate with rate adjustment<commit_after>describe ChargebackRateDetail do
  describe "#chargeback_rate" do
    it "is invalid without a valid chargeback_rate" do
      invalid_chargeback_rate_id = (ChargebackRate.maximum(:id) || -1) + 1
      chargeback_rate_detail = FactoryGirl.build(:chargeback_rate_detail,
                                                 :chargeback_rate_id => invalid_chargeback_rate_id)
      expect(chargeback_rate_detail).to be_invalid
      expect(chargeback_rate_detail.errors.messages).to include(:chargeback_rate => [/can't be blank/])
    end
  end

  describe "#find_rate" do
    let(:cvalue) { {"val1" => 0.0, "val2" => 10.0, "val3" => 20.0, "val4" => 50.0} }
    let(:cbt1) { FactoryGirl.build(:chargeback_tier, :start => 0, :finish => 10, :fixed_rate => 3.0, :variable_rate => 0.3) }
    let(:cbt2) { FactoryGirl.build(:chargeback_tier, :start => 10, :finish => 50, :fixed_rate => 2.0, :variable_rate => 0.2) }
    let(:cbt3) { FactoryGirl.build(:chargeback_tier, :start => 50, :finish => Float::INFINITY, :fixed_rate => 1.0, :variable_rate => 0.1) }
    let(:cbd) { FactoryGirl.build(:chargeback_rate_detail, :chargeback_tiers => [cbt3, cbt2, cbt1]) }

    it "finds proper rate according the value" do
      expect(cbd.find_rate(cvalue["val1"])).to eq([cbt1.fixed_rate, cbt1.variable_rate])
      expect(cbd.find_rate(cvalue["val2"])).to eq([cbt1.fixed_rate, cbt1.variable_rate])
      expect(cbd.find_rate(cvalue["val3"])).to eq([cbt2.fixed_rate, cbt2.variable_rate])
      expect(cbd.find_rate(cvalue["val4"])).to eq([cbt2.fixed_rate, cbt2.variable_rate])
    end

    context 'with rate adjustment' do
      let(:measure) do
        FactoryGirl.build(:chargeback_rate_detail_measure,
                          :units_display => %w(B KB MB GB TB),
                          :units         => %w(bytes kilobytes megabytes gigabytes terabytes))
      end
      let(:cbd) do
        # This charges per gigabyte, tiers are per gigabytes
        FactoryGirl.build(:chargeback_rate_detail,
                          :chargeback_tiers => [cbt1, cbt2, cbt3],
                          :detail_measure   => measure,
                          :per_unit         => 'gigabytes',
                          :metric           => 'derived_vm_allocated_disk_storage')
      end
      it 'finds proper tier for the value' do
        expect(cbd.find_rate(0.0)).to                   eq([cbt1.fixed_rate, cbt1.variable_rate])
        expect(cbd.find_rate(10.gigabytes)).to          eq([cbt1.fixed_rate, cbt1.variable_rate])
        expect(cbd.find_rate(10.gigabytes + 1.byte)).to eq([cbt2.fixed_rate, cbt2.variable_rate])
        expect(cbd.find_rate(50.gigabytes)).to          eq([cbt2.fixed_rate, cbt2.variable_rate])
        expect(cbd.find_rate(50.gigabytes + 1.byte)).to eq([cbt3.fixed_rate, cbt3.variable_rate])
      end
    end
  end

  let(:consumption) { instance_double('Consumption', :hours_in_month => (1.month / 1.hour)) }

  it '#hourly_cost' do
    cvalue   = 42.0
{'fraction_non_alphanumeric': 0.14249578414839797, 'fraction_numerical': 0.030986509274873526, 'mean_word_length': 3.1806167400881056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '965800', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1642, 'n_words': 296}