Commit 1f5622047cf2bb92fde7ec675bb6165a9c8f0951
Committed by
João M. M. da Silva
1 parent
014f43ee
Exists in
staging
and in
42 other branches
[Mezuro] Refactored range model.
Showing
3 changed files
with
50 additions
and
23 deletions
Show diff stats
plugins/mezuro/lib/kalibro/range.rb
| 1 | class Kalibro::Range < Kalibro::Model | 1 | class Kalibro::Range < Kalibro::Model |
| 2 | 2 | ||
| 3 | - attr_accessor :beginning, :end, :label, :grade, :color, :comments | 3 | + attr_accessor :id, :beginning, :end, :reading_id, :comments |
| 4 | 4 | ||
| 5 | def beginning=(value) | 5 | def beginning=(value) |
| 6 | @beginning = value.to_f | 6 | @beginning = value.to_f |
| @@ -37,5 +37,19 @@ class Kalibro::Range < Kalibro::Model | @@ -37,5 +37,19 @@ class Kalibro::Range < Kalibro::Model | ||
| 37 | def mezuro_color | 37 | def mezuro_color |
| 38 | @color.nil? ? "e4ca2d" : @color.gsub(/^ff/, "") | 38 | @color.nil? ? "e4ca2d" : @color.gsub(/^ff/, "") |
| 39 | end | 39 | end |
| 40 | + | ||
| 41 | + def self.ranges_of( metric_configuration_id ) | ||
| 42 | + request("Range", :ranges_of, {:metric_configuration_id => metric_configuration_id} )[:range].to_a.map { |range| new range } | ||
| 43 | + end | ||
| 44 | + | ||
| 45 | + def save( metric_configuration_id ) | ||
| 46 | + begin | ||
| 47 | + self.id = self.class.request("Range", :save_range, {:range => self.to_hash, :metric_configuration_id => metric_configuration_id})[:range_id] | ||
| 48 | + true | ||
| 49 | + rescue Exception => exception | ||
| 50 | + add_error exception | ||
| 51 | + false | ||
| 52 | + end | ||
| 53 | + end | ||
| 40 | 54 | ||
| 41 | end | 55 | end |
plugins/mezuro/test/fixtures/range_fixtures.rb
| 1 | class RangeFixtures | 1 | class RangeFixtures |
| 2 | 2 | ||
| 3 | - Infinity = 1.0/0.0 | ||
| 4 | - | ||
| 5 | - def self.range_excellent | ||
| 6 | - Kalibro::Range.new range_excellent_hash | ||
| 7 | - end | ||
| 8 | - | ||
| 9 | - def self.range_bad | ||
| 10 | - Kalibro::Range.new range_bad_hash | 3 | + def self.range |
| 4 | + Kalibro::Range.new range_hash | ||
| 11 | end | 5 | end |
| 12 | 6 | ||
| 13 | - def self.range_excellent_hash | ||
| 14 | - {:beginning => 0.0, :end => 7.0, :label => 'Excellent', :grade => 10.0, :color => 'ff00ff00'} | 7 | + def self.created_range |
| 8 | + Kalibro::Range.new :beginning => 19.5, :end => "INF", :reading_id => 1, :comments => "Test range 1" | ||
| 15 | end | 9 | end |
| 16 | 10 | ||
| 17 | - def self.range_bad_hash | ||
| 18 | - {:beginning => 19.5, :end => "INF", :label => 'Bad',:grade => 0.0, :color => 'ffff0000'} | 11 | + def self.range_hash |
| 12 | + {:id => 1, :beginning => 19.5, :end => "INF", :reading_id => 1, :comments => "Test range 1"} | ||
| 19 | end | 13 | end |
| 20 | 14 | ||
| 21 | end | 15 | end |
plugins/mezuro/test/unit/kalibro/range_test.rb
| @@ -5,24 +5,43 @@ require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/range_fixtures" | @@ -5,24 +5,43 @@ require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/range_fixtures" | ||
| 5 | class RangeTest < ActiveSupport::TestCase | 5 | class RangeTest < ActiveSupport::TestCase |
| 6 | 6 | ||
| 7 | def setup | 7 | def setup |
| 8 | - @hash = RangeFixtures.range_bad_hash | ||
| 9 | - @range = RangeFixtures.range_bad | 8 | + @hash = RangeFixtures.range_hash |
| 9 | + @range = RangeFixtures.range | ||
| 10 | + @created_range = RangeFixtures.created_range | ||
| 10 | end | 11 | end |
| 11 | 12 | ||
| 12 | should 'create range from hash' do | 13 | should 'create range from hash' do |
| 13 | - assert_equal @hash[:label], Kalibro::Range.new(@hash).label | 14 | + assert_equal @hash[:comments], Kalibro::Range.new(@hash).comments |
| 14 | end | 15 | end |
| 15 | 16 | ||
| 16 | should 'convert range to hash' do | 17 | should 'convert range to hash' do |
| 17 | assert_equal @hash, @range.to_hash | 18 | assert_equal @hash, @range.to_hash |
| 18 | end | 19 | end |
| 19 | 20 | ||
| 20 | - should 'create a default color for new range' do | ||
| 21 | - assert_equal "e4ca2d", Kalibro::Range.new.mezuro_color | ||
| 22 | - end | ||
| 23 | - | ||
| 24 | - should "convert color from 'ff' to '#'" do | ||
| 25 | - assert_equal "ff0000", @range.mezuro_color | ||
| 26 | - end | 21 | + should 'get ranges of a metric configuration' do |
| 22 | + metric_configuration_id = 31 | ||
| 23 | + Kalibro::Range.expects(:request).with("Range", :ranges_of, {:metric_configuration_id => metric_configuration_id}).returns({:range => [@hash]}) | ||
| 24 | + assert_equal @hash[:comments], Kalibro::Range.ranges_of(metric_configuration_id).first.comments | ||
| 25 | + end | ||
| 26 | + | ||
| 27 | + should 'return true when range is saved successfully' do | ||
| 28 | + id_from_kalibro = 1 | ||
| 29 | + metric_configuration_id = 2 | ||
| 30 | + 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) | ||
| 31 | + assert @created_range.save(metric_configuration_id) | ||
| 32 | + assert_equal id_from_kalibro, @created_range.id | ||
| 33 | + end | ||
| 34 | + | ||
| 35 | + should 'return false when range is not saved successfully' do | ||
| 36 | + metric_configuration_id = 2 | ||
| 37 | + Kalibro::Range.expects(:request).with("Range", :save_range, {:range => @created_range.to_hash, :metric_configuration_id => metric_configuration_id}).raises(Exception.new) | ||
| 38 | + assert !(@created_range.save(metric_configuration_id)) | ||
| 39 | + assert_nil @created_range.id | ||
| 40 | + end | ||
| 41 | + | ||
| 42 | + should 'destroy range by id' do | ||
| 43 | + Kalibro::Range.expects(:request).with("Range", :delete_range, {:range_id => @range.id}) | ||
| 44 | + @range.destroy | ||
| 45 | + end | ||
| 27 | 46 | ||
| 28 | end | 47 | end |