Commit 4b852f680e277a693c3fd9f4325b11a6bc4d3ee7
Committed by
João M. M. da Silva
1 parent
d63ab05c
Exists in
staging
and in
42 other branches
[Mezuro] Completed metric result model, fixtures and test.
Showing
4 changed files
with
81 additions
and
53 deletions
Show diff stats
plugins/mezuro/lib/kalibro/metric_result.rb
| 1 | class Kalibro::MetricResult < Kalibro::Model | 1 | class Kalibro::MetricResult < Kalibro::Model |
| 2 | 2 | ||
| 3 | - attr_accessor :metric, :value, :range, :descendent_result, :weight | ||
| 4 | - | ||
| 5 | - def metric=(value) | ||
| 6 | - if value.kind_of?(Hash) | ||
| 7 | - @metric = native?(value) ? Kalibro::NativeMetric.to_object(value) : Kalibro::CompoundMetric.to_object(value) | ||
| 8 | - else | ||
| 9 | - @metric = value | ||
| 10 | - end | ||
| 11 | - end | 3 | + attr_accessor :id, :configuration, :value, :error |
| 12 | 4 | ||
| 13 | def value=(value) | 5 | def value=(value) |
| 14 | @value = value.to_f | 6 | @value = value.to_f |
| 15 | end | 7 | end |
| 16 | 8 | ||
| 17 | - def range=(value) | ||
| 18 | - @range = Kalibro::Range.to_object value | 9 | + def configuration=(value) |
| 10 | + @configuration = Kalibro::MetricConfigurationSnapshot.to_object value | ||
| 19 | end | 11 | end |
| 20 | 12 | ||
| 21 | - def descendent_result=(value) | ||
| 22 | - array = value.kind_of?(Array) ? value : [value] | ||
| 23 | - @descendent_result = array.collect {|element| element.to_f} | 13 | + def error=(value) |
| 14 | + @error = Kalibro::Throwable.to_object value | ||
| 24 | end | 15 | end |
| 25 | - | ||
| 26 | - def descendent_results | ||
| 27 | - @descendent_result | 16 | + |
| 17 | + def descendant_results | ||
| 18 | + self.class.request("MetricResult", :descendant_results_of, {:metric_result_id => self.id})[:descendant_result].to_a | ||
| 28 | end | 19 | end |
| 29 | 20 | ||
| 30 | - def descendent_results=(descendent_results) | ||
| 31 | - @descendent_result = descendent_results | 21 | + def self.metric_results_of(module_result_id) |
| 22 | + request("MetricResult", :metric_results_of, {:module_result_id => module_result_id})[:metric_result].to_a.map {|metric_result| new metric_result} | ||
| 32 | end | 23 | end |
| 33 | - | ||
| 34 | - private | ||
| 35 | - | ||
| 36 | - def native?(value) | ||
| 37 | - value.has_key?(:origin) ? true : false | 24 | + |
| 25 | + def history_of(module_id) | ||
| 26 | + self.class.request("MetricResult", :history_of, {:metric_name => self.configuration.metric.name, :module_result_id => module_id})[:date_metric_result].to_a.map {|date_metric_result| Kalibro::DateMetricResult.new date_metric_result} | ||
| 38 | end | 27 | end |
| 39 | 28 | ||
| 40 | end | 29 | end |
plugins/mezuro/test/fixtures/metric_configuration_snapshot_fixtures.rb
| @@ -23,4 +23,24 @@ class MetricConfigurationSnapshotFixtures | @@ -23,4 +23,24 @@ class MetricConfigurationSnapshotFixtures | ||
| 23 | } | 23 | } |
| 24 | end | 24 | end |
| 25 | 25 | ||
| 26 | + def self.compound_metric_configuration_snapshot | ||
| 27 | + Kalibro::MetricConfigurationSnapshot.new compound_metric_configuration_snapshot_hash | ||
| 28 | + end | ||
| 29 | + | ||
| 30 | + def self.compound_metric_configuration_snapshot_hash | ||
| 31 | + { | ||
| 32 | + :code => "code", | ||
| 33 | + :weight => 1, | ||
| 34 | + :aggregation_form => 'AVERAGE', | ||
| 35 | + :metric => MetricFixtures.compound_metric, | ||
| 36 | + :base_tool_name => "Analizo", | ||
| 37 | + :range => [RangeSnapshotFixtures.range_snapshot_hash], | ||
| 38 | + :attributes! => { | ||
| 39 | + :metric => { | ||
| 40 | + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', | ||
| 41 | + 'xsi:type' => 'kalibro:metricXml' } | ||
| 42 | + } | ||
| 43 | + } | ||
| 44 | + end | ||
| 45 | + | ||
| 26 | end | 46 | end |
plugins/mezuro/test/fixtures/metric_result_fixtures.rb
| 1 | -require File.dirname(__FILE__) + '/compound_metric_fixtures' | ||
| 2 | -require File.dirname(__FILE__) + '/native_metric_fixtures' | ||
| 3 | -require File.dirname(__FILE__) + '/range_fixtures' | 1 | +require File.dirname(__FILE__) + '/metric_configuration_snapshot_fixtures' |
| 2 | +require File.dirname(__FILE__) + '/throwable_fixtures' | ||
| 4 | 3 | ||
| 5 | class MetricResultFixtures | 4 | class MetricResultFixtures |
| 6 | 5 | ||
| @@ -12,32 +11,42 @@ class MetricResultFixtures | @@ -12,32 +11,42 @@ class MetricResultFixtures | ||
| 12 | Kalibro::MetricResult.new compound_metric_result_hash | 11 | Kalibro::MetricResult.new compound_metric_result_hash |
| 13 | end | 12 | end |
| 14 | 13 | ||
| 14 | + def self.metric_result_with_error_hash | ||
| 15 | + { | ||
| 16 | + :id => 41, | ||
| 17 | + :configuration => MetricConfigurationSnapshotFixtures.metric_configuration_snapshot_hash, | ||
| 18 | + :error => ThrowableFixtures.throwable_hash | ||
| 19 | + } | ||
| 20 | + end | ||
| 21 | + | ||
| 15 | def self.native_metric_result_hash | 22 | def self.native_metric_result_hash |
| 16 | { | 23 | { |
| 17 | - :metric => NativeMetricFixtures.amloc_hash, | 24 | + :id => 42, |
| 25 | + :configuration => MetricConfigurationSnapshotFixtures.metric_configuration_snapshot_hash, | ||
| 18 | :value => 0.0, | 26 | :value => 0.0, |
| 19 | - :descendent_result => [40.0, 42.0], | ||
| 20 | - :range => RangeFixtures.range_excellent_hash, | ||
| 21 | - :attributes! => { | ||
| 22 | - :metric => { | ||
| 23 | - 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', | ||
| 24 | - 'xsi:type' => 'kalibro:nativeMetricXml' }, | ||
| 25 | - :range => { | ||
| 26 | - 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', | ||
| 27 | - 'xsi:type' => 'kalibro:rangeXml' } | 27 | + :attributes! => |
| 28 | + { | ||
| 29 | + :configuration => | ||
| 30 | + { | ||
| 31 | + "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance", | ||
| 32 | + "xsi:type"=>"kalibro:metricConfigurationSnapshotXml" | ||
| 33 | + } | ||
| 28 | } | 34 | } |
| 29 | } | 35 | } |
| 30 | end | 36 | end |
| 31 | 37 | ||
| 32 | def self.compound_metric_result_hash | 38 | def self.compound_metric_result_hash |
| 33 | { | 39 | { |
| 34 | - :metric => CompoundMetricFixtures.compound_metric_hash, | 40 | + :id => 43, |
| 41 | + :configuration => MetricConfigurationSnapshotFixtures.compound_metric_configuration_snapshot_hash, | ||
| 35 | :value => 1.0, | 42 | :value => 1.0, |
| 36 | - :descendent_result => [2.0, 42.0], | ||
| 37 | - :attributes! => { | ||
| 38 | - :metric => { | ||
| 39 | - 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', | ||
| 40 | - 'xsi:type' => 'kalibro:compoundMetricXml' } | 43 | + :attributes! => |
| 44 | + { | ||
| 45 | + :configuration => | ||
| 46 | + { | ||
| 47 | + "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance", | ||
| 48 | + "xsi:type"=>"kalibro:metricConfigurationSnapshotXml" | ||
| 49 | + } | ||
| 41 | } | 50 | } |
| 42 | } | 51 | } |
| 43 | end | 52 | end |
plugins/mezuro/test/unit/kalibro/metric_result_test.rb
| 1 | require "test_helper" | 1 | require "test_helper" |
| 2 | 2 | ||
| 3 | require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/metric_result_fixtures" | 3 | require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/metric_result_fixtures" |
| 4 | +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/date_metric_result_fixtures" | ||
| 4 | 5 | ||
| 5 | class MetricResultTest < ActiveSupport::TestCase | 6 | class MetricResultTest < ActiveSupport::TestCase |
| 6 | 7 | ||
| 7 | def setup | 8 | def setup |
| 8 | - @hash = MetricResultFixtures.native_metric_result_hash | 9 | + @native_hash = MetricResultFixtures.native_metric_result_hash |
| 10 | + @compound_hash = MetricResultFixtures.compound_metric_result_hash | ||
| 9 | @result = MetricResultFixtures.native_metric_result | 11 | @result = MetricResultFixtures.native_metric_result |
| 10 | end | 12 | end |
| 11 | 13 | ||
| 12 | should 'create metric result from hash' do | 14 | should 'create metric result from hash' do |
| 13 | - assert_equal @hash[:metric][:name], Kalibro::MetricResult.new(@hash).metric.name | 15 | + assert_equal @native_hash[:configuration][:code], Kalibro::MetricResult.new(@native_hash).configuration.code |
| 14 | end | 16 | end |
| 15 | 17 | ||
| 16 | should 'convert metric result to hash' do | 18 | should 'convert metric result to hash' do |
| 17 | - assert_equal @hash, @result.to_hash | 19 | + assert_equal @native_hash, @result.to_hash |
| 18 | end | 20 | end |
| 19 | 21 | ||
| 20 | - should 'create appropriate metric type' do | ||
| 21 | - assert MetricResultFixtures.native_metric_result.metric.instance_of?(Kalibro::NativeMetric) | ||
| 22 | - assert MetricResultFixtures.compound_metric_result.metric.instance_of?(Kalibro::CompoundMetric) | 22 | + should 'return descendant results of a metric result' do |
| 23 | + descendant = [31, 13] | ||
| 24 | + Kalibro::MetricResult.expects(:request).with("MetricResult", :descendant_results_of, {:metric_result_id => @result.id}).returns({:descendant_result => descendant}) | ||
| 25 | + assert_equal descendant, @result.descendant_results | ||
| 26 | + end | ||
| 27 | + | ||
| 28 | + should 'return metric results of a module result' do | ||
| 29 | + id = 31 | ||
| 30 | + Kalibro::MetricResult.expects(:request).with("MetricResult", :metric_results_of, {:module_result_id => id}).returns(:metric_result => [@native_hash, @compound_hash]) | ||
| 31 | + assert_equal @native_hash[:id], Kalibro::MetricResult.metric_results_of(id).first.id | ||
| 23 | end | 32 | end |
| 24 | 33 | ||
| 25 | - should 'convert single descendent result to array' do | ||
| 26 | - @result.descendent_result = 1 | ||
| 27 | - assert_equal [1], @result.descendent_results | 34 | + should 'return history of a metric with a module result id' do |
| 35 | + module_id = 31 | ||
| 36 | + Kalibro::MetricResult.expects(:request).with("MetricResult", :history_of, {:metric_name => @result.configuration.metric.name, :module_result_id => module_id}).returns({:date_metric_result => [DateMetricResultFixtures.date_metric_result_hash]}) | ||
| 37 | + assert_equal DateMetricResultFixtures.date_metric_result_hash[:metric_result][:id], @result.history_of(module_id).first.metric_result.id | ||
| 28 | end | 38 | end |
| 29 | - | 39 | + |
| 30 | end | 40 | end |