Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tier selection when using different units. #13593

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/chargeback_rate_detail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def fixed?
def find_rate(value)
fixed_rate = 0.0
variable_rate = 0.0
tier_found = chargeback_tiers.detect { |tier| tier.includes?(value / rate_adjustment) }
tier_found = chargeback_tiers.detect { |tier| tier.includes?(value * rate_adjustment) }
unless tier_found.nil?
fixed_rate = tier_found.fixed_rate
variable_rate = tier_found.variable_rate
Expand Down
23 changes: 23 additions & 0 deletions spec/models/chargeback_rate_detail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@
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)) }
Expand Down