Commit 4ab0eafe022eb9852851f602b660593556019033

Authored by Heitor
1 parent 2e3381f8

Fixed evaluation of metric result ranges with infinite bounds

Changed the comparison method to use Ruby's Range class, and created
corresponding tests.

Signed-off-by: Daniel Miranda <danielkza2@gmail.com>
app/helpers/processings_helper.rb
@@ -11,7 +11,11 @@ module ProcessingsHelper @@ -11,7 +11,11 @@ module ProcessingsHelper
11 range_snapshots = metric_result.metric_configuration.kalibro_ranges 11 range_snapshots = metric_result.metric_configuration.kalibro_ranges
12 12
13 range_snapshots.each do |range_snapshot| 13 range_snapshots.each do |range_snapshot|
14 - return range_snapshot if ((range_snapshot.beginning.to_f <= metric_result.value || range_snapshot.beginning == '-INF') && (range_snapshot.end.to_f >= metric_result.value || range_snapshot.beginning == 'INF')) 14 + range = Range.new(
  15 + range_snapshot.beginning == '-INF' ? -Float::INFINITY : range_snapshot.beginning.to_f,
  16 + range_snapshot.end == 'INF' ? Float::INFINITY : range_snapshot.end.to_f
  17 + )
  18 + return range_snapshot if range === metric_result.value
15 end 19 end
16 20
17 return nil 21 return nil
spec/helpers/processings_helper_spec.rb
1 require 'rails_helper' 1 require 'rails_helper'
2 2
  3 +def make_range(b, e)
  4 + FactoryGirl.build(:range_snapshot, beginning: b, end: e)
  5 +end
  6 +
3 describe ProcessingsHelper, :type => :helper do 7 describe ProcessingsHelper, :type => :helper do
4 describe 'humanize_eplased_time' do 8 describe 'humanize_eplased_time' do
5 it 'should convert it to readable words' do 9 it 'should convert it to readable words' do
@@ -16,20 +20,34 @@ describe ProcessingsHelper, :type =&gt; :helper do @@ -16,20 +20,34 @@ describe ProcessingsHelper, :type =&gt; :helper do
16 describe 'find_range_snapshot' do 20 describe 'find_range_snapshot' do
17 let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id)} 21 let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id)}
18 let(:metric_result) { FactoryGirl.build(:metric_result, {value: 6.0, metric_configuration: metric_configuration})} 22 let(:metric_result) { FactoryGirl.build(:metric_result, {value: 6.0, metric_configuration: metric_configuration})}
19 - let(:range_snapshot_1_to_5) { FactoryGirl.build(:range_snapshot, {beginning: 1.0, end: 5.0}) }  
20 - let(:range_snapshot_5dot1_to_10) { FactoryGirl.build(:range_snapshot, {beginning: 5.1, end: 10.0}) }  
21 - let(:range_snapshot_10dot1_to_15) { FactoryGirl.build(:range_snapshot, {beginning: 10.1, end: 15.0}) }  
22 23
23 before :each do 24 before :each do
24 metric_result.expects(:metric_configuration).returns(metric_configuration) 25 metric_result.expects(:metric_configuration).returns(metric_configuration)
25 - metric_configuration.expects(:kalibro_ranges).  
26 - returns([range_snapshot_1_to_5,  
27 - range_snapshot_5dot1_to_10,  
28 - range_snapshot_10dot1_to_15]) 26 + metric_configuration.expects(:kalibro_ranges).returns(range_snapshots)
  27 + end
  28 +
  29 + context 'with two finite boundaries' do
  30 + let!(:range_snapshots) { [make_range(1.0, 5.0), make_range(5.1, 10.0), make_range(10.1, 15.0)] }
  31 +
  32 + it 'should return the range snapshot which contains the value' do
  33 + expect(helper.find_range_snapshot(metric_result)).to eq(range_snapshots[1])
  34 + end
29 end 35 end
30 36
31 - it 'should return the range snapshot in which the value was in between' do  
32 - expect(helper.find_range_snapshot(metric_result)).to eq(range_snapshot_5dot1_to_10) 37 + context 'with unbounded ranges' do
  38 + let!(:range_snapshots) { [make_range('-INF', 0.0), make_range(0, 'INF')] }
  39 +
  40 + it 'should return the range snapshot which contains the value' do
  41 + expect(helper.find_range_snapshot(metric_result)).to eq(range_snapshots[1])
  42 + end
  43 + end
  44 +
  45 + context 'with an universal range' do
  46 + let!(:range_snapshots) { [make_range('-INF', 'INF')] }
  47 +
  48 + it 'should return the range snapshot which contains the value' do
  49 + expect(helper.find_range_snapshot(metric_result)).to eq(range_snapshots[0])
  50 + end
33 end 51 end
34 end 52 end
35 53