Commit 5031856d4961e61744a9daa8df74470f286ad065

Authored by Rafael Manzo
2 parents 2e3381f8 fa1fa3a4

Merge pull request #211 from mezuro/metric_result_ranges_fix

Metric result ranges fix
app/helpers/processings_helper.rb
... ... @@ -11,7 +11,12 @@ module ProcessingsHelper
11 11 range_snapshots = metric_result.metric_configuration.kalibro_ranges
12 12  
13 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 + exclude_end: true
  18 + )
  19 + return range_snapshot if range === metric_result.value
15 20 end
16 21  
17 22 return nil
... ...
spec/helpers/processings_helper_spec.rb
1 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 7 describe ProcessingsHelper, :type => :helper do
4 8 describe 'humanize_eplased_time' do
5 9 it 'should convert it to readable words' do
... ... @@ -16,21 +20,44 @@ describe ProcessingsHelper, :type =&gt; :helper do
16 20 describe 'find_range_snapshot' do
17 21 let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id)}
18 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 24 before :each do
24 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)
29 27 end
30 28  
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)
  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
33 35 end
  36 +
  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
  51 + end
  52 +
  53 + context 'with incomplete ranges' do
  54 + let!(:range_snapshots) { [make_range('-INF', 6.0), make_range(6.1, 'INF')] }
  55 +
  56 + it 'should return nil' do
  57 + expect(helper.find_range_snapshot(metric_result)).to be_nil
  58 + end
  59 + end
  60 +
34 61 end
35 62  
36 63 describe 'format_module_name' do
... ...