From 4f7e02c7c1a86df0977249eb2bb1884add798b71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Luka=C5=A1=C3=ADk?= Date: Wed, 23 Nov 2016 10:25:50 +0100 Subject: [PATCH] Split test cases for hourly rate --- spec/models/chargeback_rate_detail_spec.rb | 58 ++++++++++++---------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/spec/models/chargeback_rate_detail_spec.rb b/spec/models/chargeback_rate_detail_spec.rb index 808ba1353bd..303882613d6 100644 --- a/spec/models/chargeback_rate_detail_spec.rb +++ b/spec/models/chargeback_rate_detail_spec.rb @@ -55,34 +55,42 @@ expect(cbd.hourly_cost(cvalue, consumption)).to eq(0.0) end - it "#hourly" do - [ - 0, - 0.0, - 0.00 - ].each do |rate| - cbd = FactoryGirl.build(:chargeback_rate_detail, :per_time => 'hourly') + describe '#hourly (rate)' do + let(:rate) { 8.26 } + it 'returns 0 when the rate was 0' do + [ + 0, + 0.0, + 0.00 + ].each do |zero| + cbd = FactoryGirl.build(:chargeback_rate_detail, :per_time => 'hourly') + expect(cbd.hourly(zero, consumption)).to eq(0.0) + end + end - expect(cbd.hourly(rate, consumption)).to eq(0.0) + it 'calculates hourly rate for given rate' do + cbdm = FactoryGirl.create(:chargeback_rate_detail_measure) + [ + 'hourly', 'megabytes', rate, + 'daily', 'megabytes', rate / 24, + 'weekly', 'megabytes', rate / 24 / 7, + 'monthly', 'megabytes', rate / 24 / 30, + 'yearly', 'megabytes', rate / 24 / 365 + ].each_slice(3) do |per_time, per_unit, hourly_rate| + cbd = FactoryGirl.build(:chargeback_rate_detail, + :per_time => per_time, + :per_unit => per_unit, + :metric => 'derived_memory_available', + :chargeback_rate_detail_measure_id => cbdm.id) + expect(cbd.hourly(rate, consumption)).to eq(hourly_rate) + end end - cbdm = FactoryGirl.create(:chargeback_rate_detail_measure) - rate = 8.26 - [ - 'hourly', 'megabytes', rate, - 'daily', 'megabytes', rate / 24, - 'weekly', 'megabytes', rate / 24 / 7, - 'monthly', 'megabytes', rate / 24 / 30, - 'yearly', 'megabytes', rate / 24 / 365 - ].each_slice(3) do |per_time, per_unit, hourly_rate| - cbd = FactoryGirl.build(:chargeback_rate_detail, - :per_time => per_time, - :per_unit => per_unit, - :metric => 'derived_memory_available', - :chargeback_rate_detail_measure_id => cbdm.id) - expect(cbd.hourly(rate, consumption)).to eq(hourly_rate) + + let(:annual_rate) { FactoryGirl.build(:chargeback_rate_detail, :per_time => 'annually') } + it 'cannot calculate for unknown time interval' do + expect { annual_rate.hourly(rate, consumption) }.to raise_error(RuntimeError, + "rate time unit of 'annually' not supported") end - cbd = FactoryGirl.build(:chargeback_rate_detail, :per_time => 'annually') - expect { cbd.hourly(rate, consumption) }.to raise_error(RuntimeError, "rate time unit of 'annually' not supported") end it "#rate_adjustment" do