From 4b852f680e277a693c3fd9f4325b11a6bc4d3ee7 Mon Sep 17 00:00:00 2001 From: João M. M. da Silva + Diego Araújo Date: Wed, 7 Nov 2012 16:36:50 -0200 Subject: [PATCH] [Mezuro] Completed metric result model, fixtures and test. --- plugins/mezuro/lib/kalibro/metric_result.rb | 37 +++++++++++++------------------------ plugins/mezuro/test/fixtures/metric_configuration_snapshot_fixtures.rb | 20 ++++++++++++++++++++ plugins/mezuro/test/fixtures/metric_result_fixtures.rb | 47 ++++++++++++++++++++++++++++------------------- plugins/mezuro/test/unit/kalibro/metric_result_test.rb | 30 ++++++++++++++++++++---------- 4 files changed, 81 insertions(+), 53 deletions(-) diff --git a/plugins/mezuro/lib/kalibro/metric_result.rb b/plugins/mezuro/lib/kalibro/metric_result.rb index 1ef5c09..62484b6 100644 --- a/plugins/mezuro/lib/kalibro/metric_result.rb +++ b/plugins/mezuro/lib/kalibro/metric_result.rb @@ -1,40 +1,29 @@ class Kalibro::MetricResult < Kalibro::Model - attr_accessor :metric, :value, :range, :descendent_result, :weight - - def metric=(value) - if value.kind_of?(Hash) - @metric = native?(value) ? Kalibro::NativeMetric.to_object(value) : Kalibro::CompoundMetric.to_object(value) - else - @metric = value - end - end + attr_accessor :id, :configuration, :value, :error def value=(value) @value = value.to_f end - def range=(value) - @range = Kalibro::Range.to_object value + def configuration=(value) + @configuration = Kalibro::MetricConfigurationSnapshot.to_object value end - def descendent_result=(value) - array = value.kind_of?(Array) ? value : [value] - @descendent_result = array.collect {|element| element.to_f} + def error=(value) + @error = Kalibro::Throwable.to_object value end - - def descendent_results - @descendent_result + + def descendant_results + self.class.request("MetricResult", :descendant_results_of, {:metric_result_id => self.id})[:descendant_result].to_a end - def descendent_results=(descendent_results) - @descendent_result = descendent_results + def self.metric_results_of(module_result_id) + request("MetricResult", :metric_results_of, {:module_result_id => module_result_id})[:metric_result].to_a.map {|metric_result| new metric_result} end - - private - - def native?(value) - value.has_key?(:origin) ? true : false + + def history_of(module_id) + 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} end end diff --git a/plugins/mezuro/test/fixtures/metric_configuration_snapshot_fixtures.rb b/plugins/mezuro/test/fixtures/metric_configuration_snapshot_fixtures.rb index 541b4f2..832643e 100644 --- a/plugins/mezuro/test/fixtures/metric_configuration_snapshot_fixtures.rb +++ b/plugins/mezuro/test/fixtures/metric_configuration_snapshot_fixtures.rb @@ -23,4 +23,24 @@ class MetricConfigurationSnapshotFixtures } end + def self.compound_metric_configuration_snapshot + Kalibro::MetricConfigurationSnapshot.new compound_metric_configuration_snapshot_hash + end + + def self.compound_metric_configuration_snapshot_hash + { + :code => "code", + :weight => 1, + :aggregation_form => 'AVERAGE', + :metric => MetricFixtures.compound_metric, + :base_tool_name => "Analizo", + :range => [RangeSnapshotFixtures.range_snapshot_hash], + :attributes! => { + :metric => { + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', + 'xsi:type' => 'kalibro:metricXml' } + } + } + end + end diff --git a/plugins/mezuro/test/fixtures/metric_result_fixtures.rb b/plugins/mezuro/test/fixtures/metric_result_fixtures.rb index 6baff5c..cca63b8 100644 --- a/plugins/mezuro/test/fixtures/metric_result_fixtures.rb +++ b/plugins/mezuro/test/fixtures/metric_result_fixtures.rb @@ -1,6 +1,5 @@ -require File.dirname(__FILE__) + '/compound_metric_fixtures' -require File.dirname(__FILE__) + '/native_metric_fixtures' -require File.dirname(__FILE__) + '/range_fixtures' +require File.dirname(__FILE__) + '/metric_configuration_snapshot_fixtures' +require File.dirname(__FILE__) + '/throwable_fixtures' class MetricResultFixtures @@ -12,32 +11,42 @@ class MetricResultFixtures Kalibro::MetricResult.new compound_metric_result_hash end + def self.metric_result_with_error_hash + { + :id => 41, + :configuration => MetricConfigurationSnapshotFixtures.metric_configuration_snapshot_hash, + :error => ThrowableFixtures.throwable_hash + } + end + def self.native_metric_result_hash { - :metric => NativeMetricFixtures.amloc_hash, + :id => 42, + :configuration => MetricConfigurationSnapshotFixtures.metric_configuration_snapshot_hash, :value => 0.0, - :descendent_result => [40.0, 42.0], - :range => RangeFixtures.range_excellent_hash, - :attributes! => { - :metric => { - 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', - 'xsi:type' => 'kalibro:nativeMetricXml' }, - :range => { - 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', - 'xsi:type' => 'kalibro:rangeXml' } + :attributes! => + { + :configuration => + { + "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance", + "xsi:type"=>"kalibro:metricConfigurationSnapshotXml" + } } } end def self.compound_metric_result_hash { - :metric => CompoundMetricFixtures.compound_metric_hash, + :id => 43, + :configuration => MetricConfigurationSnapshotFixtures.compound_metric_configuration_snapshot_hash, :value => 1.0, - :descendent_result => [2.0, 42.0], - :attributes! => { - :metric => { - 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', - 'xsi:type' => 'kalibro:compoundMetricXml' } + :attributes! => + { + :configuration => + { + "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance", + "xsi:type"=>"kalibro:metricConfigurationSnapshotXml" + } } } end diff --git a/plugins/mezuro/test/unit/kalibro/metric_result_test.rb b/plugins/mezuro/test/unit/kalibro/metric_result_test.rb index 41deaac..683e75a 100644 --- a/plugins/mezuro/test/unit/kalibro/metric_result_test.rb +++ b/plugins/mezuro/test/unit/kalibro/metric_result_test.rb @@ -1,30 +1,40 @@ require "test_helper" require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/metric_result_fixtures" +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/date_metric_result_fixtures" class MetricResultTest < ActiveSupport::TestCase def setup - @hash = MetricResultFixtures.native_metric_result_hash + @native_hash = MetricResultFixtures.native_metric_result_hash + @compound_hash = MetricResultFixtures.compound_metric_result_hash @result = MetricResultFixtures.native_metric_result end should 'create metric result from hash' do - assert_equal @hash[:metric][:name], Kalibro::MetricResult.new(@hash).metric.name + assert_equal @native_hash[:configuration][:code], Kalibro::MetricResult.new(@native_hash).configuration.code end should 'convert metric result to hash' do - assert_equal @hash, @result.to_hash + assert_equal @native_hash, @result.to_hash end - should 'create appropriate metric type' do - assert MetricResultFixtures.native_metric_result.metric.instance_of?(Kalibro::NativeMetric) - assert MetricResultFixtures.compound_metric_result.metric.instance_of?(Kalibro::CompoundMetric) + should 'return descendant results of a metric result' do + descendant = [31, 13] + Kalibro::MetricResult.expects(:request).with("MetricResult", :descendant_results_of, {:metric_result_id => @result.id}).returns({:descendant_result => descendant}) + assert_equal descendant, @result.descendant_results + end + + should 'return metric results of a module result' do + id = 31 + Kalibro::MetricResult.expects(:request).with("MetricResult", :metric_results_of, {:module_result_id => id}).returns(:metric_result => [@native_hash, @compound_hash]) + assert_equal @native_hash[:id], Kalibro::MetricResult.metric_results_of(id).first.id end - should 'convert single descendent result to array' do - @result.descendent_result = 1 - assert_equal [1], @result.descendent_results + should 'return history of a metric with a module result id' do + module_id = 31 + 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]}) + assert_equal DateMetricResultFixtures.date_metric_result_hash[:metric_result][:id], @result.history_of(module_id).first.metric_result.id end - + end -- libgit2 0.21.2