Commit e1ee85a8edefa1dbb9506bc453c39790f8dc4094

Authored by Alessandro Palmeira
Committed by Diego Camarinha
1 parent 25c16982

Fixed range numericality unit tests and added beginning greater than end validation.

Signed off by: Rafael Manzo <rr.manzo@gmail.com>
app/models/mezuro_range.rb
1 1 require "validators/beginning_uniqueness_validator.rb"
  2 +require "validators/greater_than_beginning_validator.rb"
2 3  
3 4 class MezuroRange < KalibroGatekeeperClient::Entities::Range
4 5 include KalibroRecord
... ... @@ -9,6 +10,7 @@ class MezuroRange &lt; KalibroGatekeeperClient::Entities::Range
9 10 validates :beginning, numericality: true, if: :non_infinite_beginning?
10 11 validates :end, presence: true
11 12 validates :end, numericality: true, if: :non_infinite_end?
  13 + validates :end, greater_than_beginning: true
12 14 validates :reading_id, presence: true
13 15  
14 16 private
... ...
app/models/validators/greater_than_beginning_validator.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +class GreaterThanBeginningValidator < ActiveModel::EachValidator
  2 + def validate_each(record, attribute, value)
  3 + if record.beginning.is_a?(String) || value.is_a?(String) #TOOO This will be useless when we start representing INF as ruby Infinity with the new Kalibro configuration application.
  4 + if record.beginning=="INF" || value=="-INF" || record.beginning == value
  5 + add_error(record,attribute)
  6 + end
  7 + elsif record.beginning >= value
  8 + add_error(record,attribute)
  9 + end
  10 + end
  11 +
  12 + private
  13 +
  14 + def add_error(record, attribute)
  15 + record.errors[attribute] << "The end value should be greater than the beginning value."
  16 + end
  17 +
  18 +end
... ...
spec/factories/mezuro_ranges.rb
... ... @@ -5,7 +5,7 @@
5 5 # it under the terms of the GNU General Public License as published by
6 6 # the Free Software Foundation, either version 3 of the License, or
7 7 # (at your option) any later version.
8   -#
  8 +#
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
... ... @@ -16,8 +16,8 @@
16 16  
17 17 FactoryGirl.define do
18 18 factory :mezuro_range do
19   - beginning 1.1
20   - self.end 5.1
  19 + beginning 1.1
  20 + self.end 5.1
21 21 reading_id 3
22 22 comments "Comment"
23 23 metric_configuration_id 32
... ...
spec/models/mezuro_range_spec.rb
... ... @@ -5,7 +5,8 @@ describe MezuroRange, :type =&gt; :model do
5 5 describe 'validations' do
6 6 context 'active model validations' do
7 7 before :each do
8   - MezuroRange.expects(:ranges_of).with(subject.metric_configuration_id).at_least_once.returns([])
  8 + BeginningUniquenessValidator.any_instance.stubs(:validate_each)
  9 + GreaterThanBeginningValidator.any_instance.stubs(:validate_each)
9 10 end
10 11  
11 12 it { is_expected.to validate_presence_of(:beginning) }
... ... @@ -13,8 +14,8 @@ describe MezuroRange, :type =&gt; :model do
13 14 it { is_expected.to validate_presence_of(:reading_id) }
14 15  
15 16 context 'beginning and end numericality' do
16   - it { is_expected.to validate_presence_of(:beginning) }
17   - it { is_expected.to validate_presence_of(:end) }
  17 + it { is_expected.to validate_numericality_of(:beginning) }
  18 + it { is_expected.to validate_numericality_of(:end) }
18 19  
19 20 it 'should allow -INF and INF to beginning' do
20 21 subject.beginning = '-INF'
... ... @@ -45,7 +46,7 @@ describe MezuroRange, :type =&gt; :model do
45 46  
46 47 context 'beginning validations' do
47 48 before :each do
48   - MezuroRange.expects(:request).returns(2)
  49 + GreaterThanBeginningValidator.any_instance.stubs(:validate_each)
49 50 end
50 51  
51 52 it 'should validate uniqueness' do
... ... @@ -53,4 +54,15 @@ describe MezuroRange, :type =&gt; :model do
53 54 subject.save
54 55 end
55 56 end
56   -end
  57 +
  58 + context 'end validations' do
  59 + before :each do
  60 + BeginningUniquenessValidator.any_instance.stubs(:validate_each)
  61 + end
  62 +
  63 + it 'should validate that end is greater than beginning' do
  64 + GreaterThanBeginningValidator.any_instance.expects(:validate_each).with(subject, :end, subject.end)
  65 + subject.save
  66 + end
  67 + end
  68 +end
57 69 \ No newline at end of file
... ...
spec/models/validators/greater_than_beginning_validator_spec.rb 0 → 100644
... ... @@ -0,0 +1,39 @@
  1 +require 'rails_helper'
  2 +
  3 +describe GreaterThanBeginningValidator, :type => :model do
  4 + describe 'methods' do
  5 + describe 'validate_each' do
  6 + before :each do
  7 + BeginningUniquenessValidator.any_instance.stubs(:validate_each)
  8 + end
  9 + context 'when beginning is INF or end is -INF' do
  10 + subject { FactoryGirl.build(:mezuro_range, end: "-INF") }
  11 + it 'is expected to return an error' do
  12 + subject.save
  13 + expect(subject.errors[:end]).to eq(["The end value should be greater than the beginning value."])
  14 + end
  15 + end
  16 + context 'when beginning is -INF or end is INF' do
  17 + subject { FactoryGirl.build(:mezuro_range, end: "INF") }
  18 + it 'is expected to not return an error' do
  19 + subject.save
  20 + expect(subject.errors[:end]).to be_empty
  21 + end
  22 + end
  23 + context 'when beginning is greater than end' do
  24 + subject { FactoryGirl.build(:mezuro_range, beginning: 1.0, end: 0.0) }
  25 + it 'is expected to return an error' do
  26 + subject.save
  27 + expect(subject.errors[:end]).to eq(["The end value should be greater than the beginning value."])
  28 + end
  29 + end
  30 + context 'when beginning is smaller than end' do
  31 + subject { FactoryGirl.build(:mezuro_range) }
  32 + it 'is expected to not return an error' do
  33 + subject.save
  34 + expect(subject.errors[:end]).to be_empty
  35 + end
  36 + end
  37 + end
  38 + end
  39 +end
... ...