From 878bb3096a546afcf0de543731b57388b129010d Mon Sep 17 00:00:00 2001 From: Alessandro Palmeira + Paulo Meirelles Date: Tue, 6 Nov 2012 16:15:27 -0200 Subject: [PATCH] [Mezuro] Refactored range model. --- plugins/mezuro/lib/kalibro/range.rb | 16 +++++++++++++++- plugins/mezuro/test/fixtures/range_fixtures.rb | 18 ++++++------------ plugins/mezuro/test/unit/kalibro/range_test.rb | 39 +++++++++++++++++++++++++++++---------- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/plugins/mezuro/lib/kalibro/range.rb b/plugins/mezuro/lib/kalibro/range.rb index 006c4ea..e7ab565 100644 --- a/plugins/mezuro/lib/kalibro/range.rb +++ b/plugins/mezuro/lib/kalibro/range.rb @@ -1,6 +1,6 @@ class Kalibro::Range < Kalibro::Model - attr_accessor :beginning, :end, :label, :grade, :color, :comments + attr_accessor :id, :beginning, :end, :reading_id, :comments def beginning=(value) @beginning = value.to_f @@ -37,5 +37,19 @@ class Kalibro::Range < Kalibro::Model def mezuro_color @color.nil? ? "e4ca2d" : @color.gsub(/^ff/, "") end + + def self.ranges_of( metric_configuration_id ) + request("Range", :ranges_of, {:metric_configuration_id => metric_configuration_id} )[:range].to_a.map { |range| new range } + end + + def save( metric_configuration_id ) + begin + self.id = self.class.request("Range", :save_range, {:range => self.to_hash, :metric_configuration_id => metric_configuration_id})[:range_id] + true + rescue Exception => exception + add_error exception + false + end + end end diff --git a/plugins/mezuro/test/fixtures/range_fixtures.rb b/plugins/mezuro/test/fixtures/range_fixtures.rb index 78b0c3e..836e5ab 100644 --- a/plugins/mezuro/test/fixtures/range_fixtures.rb +++ b/plugins/mezuro/test/fixtures/range_fixtures.rb @@ -1,21 +1,15 @@ class RangeFixtures - Infinity = 1.0/0.0 - - def self.range_excellent - Kalibro::Range.new range_excellent_hash - end - - def self.range_bad - Kalibro::Range.new range_bad_hash + def self.range + Kalibro::Range.new range_hash end - def self.range_excellent_hash - {:beginning => 0.0, :end => 7.0, :label => 'Excellent', :grade => 10.0, :color => 'ff00ff00'} + def self.created_range + Kalibro::Range.new :beginning => 19.5, :end => "INF", :reading_id => 1, :comments => "Test range 1" end - def self.range_bad_hash - {:beginning => 19.5, :end => "INF", :label => 'Bad',:grade => 0.0, :color => 'ffff0000'} + def self.range_hash + {:id => 1, :beginning => 19.5, :end => "INF", :reading_id => 1, :comments => "Test range 1"} end end diff --git a/plugins/mezuro/test/unit/kalibro/range_test.rb b/plugins/mezuro/test/unit/kalibro/range_test.rb index 84fce0f..58d360f 100644 --- a/plugins/mezuro/test/unit/kalibro/range_test.rb +++ b/plugins/mezuro/test/unit/kalibro/range_test.rb @@ -5,24 +5,43 @@ require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/range_fixtures" class RangeTest < ActiveSupport::TestCase def setup - @hash = RangeFixtures.range_bad_hash - @range = RangeFixtures.range_bad + @hash = RangeFixtures.range_hash + @range = RangeFixtures.range + @created_range = RangeFixtures.created_range end should 'create range from hash' do - assert_equal @hash[:label], Kalibro::Range.new(@hash).label + assert_equal @hash[:comments], Kalibro::Range.new(@hash).comments end should 'convert range to hash' do assert_equal @hash, @range.to_hash end - should 'create a default color for new range' do - assert_equal "e4ca2d", Kalibro::Range.new.mezuro_color - end - - should "convert color from 'ff' to '#'" do - assert_equal "ff0000", @range.mezuro_color - end + should 'get ranges of a metric configuration' do + metric_configuration_id = 31 + Kalibro::Range.expects(:request).with("Range", :ranges_of, {:metric_configuration_id => metric_configuration_id}).returns({:range => [@hash]}) + assert_equal @hash[:comments], Kalibro::Range.ranges_of(metric_configuration_id).first.comments + end + + should 'return true when range is saved successfully' do + id_from_kalibro = 1 + metric_configuration_id = 2 + Kalibro::Range.expects(:request).with("Range", :save_range, {:range => @created_range.to_hash, :metric_configuration_id => metric_configuration_id}).returns(:range_id => id_from_kalibro) + assert @created_range.save(metric_configuration_id) + assert_equal id_from_kalibro, @created_range.id + end + + should 'return false when range is not saved successfully' do + metric_configuration_id = 2 + Kalibro::Range.expects(:request).with("Range", :save_range, {:range => @created_range.to_hash, :metric_configuration_id => metric_configuration_id}).raises(Exception.new) + assert !(@created_range.save(metric_configuration_id)) + assert_nil @created_range.id + end + + should 'destroy range by id' do + Kalibro::Range.expects(:request).with("Range", :delete_range, {:range_id => @range.id}) + @range.destroy + end end -- libgit2 0.21.2