From a964974ab8ba43fc899f06543ba54cd63e9ab49d Mon Sep 17 00:00:00 2001 From: Alessandro Palmeira + Eduardo Morais Date: Tue, 13 Nov 2012 17:54:34 -0200 Subject: [PATCH] [Mezuro] Refactoring project_content, some methods and tests missing. --- plugins/mezuro/lib/kalibro/metric_result.rb | 4 ++-- plugins/mezuro/lib/mezuro_plugin/project_content.rb | 28 +++++++++++++++++----------- plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------- 3 files changed, 88 insertions(+), 66 deletions(-) diff --git a/plugins/mezuro/lib/kalibro/metric_result.rb b/plugins/mezuro/lib/kalibro/metric_result.rb index d797af0..55e41d8 100644 --- a/plugins/mezuro/lib/kalibro/metric_result.rb +++ b/plugins/mezuro/lib/kalibro/metric_result.rb @@ -22,8 +22,8 @@ class Kalibro::MetricResult < Kalibro::Model request(:metric_results_of, {:module_result_id => module_result_id})[:metric_result].to_a.map {|metric_result| new metric_result} end - def history_of(module_id) - self.class.request(: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} + def history_of(module_result_id) + self.class.request(:history_of, {:metric_name => self.configuration.metric.name, :module_result_id => module_result_id})[:date_metric_result].to_a.map {|date_metric_result| Kalibro::DateMetricResult.new date_metric_result} end end diff --git a/plugins/mezuro/lib/mezuro_plugin/project_content.rb b/plugins/mezuro/lib/mezuro_plugin/project_content.rb index 6e72af8..da89ed7 100644 --- a/plugins/mezuro/lib/mezuro_plugin/project_content.rb +++ b/plugins/mezuro/lib/mezuro_plugin/project_content.rb @@ -42,6 +42,8 @@ class MezuroPlugin::ProjectContent < Article begin if Kalibro::Processing.has_ready_processing(repository_id) @processing ||= Kalibro::Processing.last_ready_processing_of(repository_id) + else + @processing = Kalibro::Processing.last_processing_of(repository_id) end rescue Exception => error errors.add_to_base(error.message) @@ -51,10 +53,10 @@ class MezuroPlugin::ProjectContent < Article def processing_with_date(repository_id, date) begin - if Kalibro::Processing.has_processing_before(repository_id, date) + if Kalibro::Processing.has_processing_after(repository_id, date) + @processing ||= Kalibro::Processing.first_processing_after(repository_id, date) + elsif Kalibro::Processing.has_processing_before(repository_id, date) @processing ||= Kalibro::Processing.last_processing_before(repository_id, date) - elsif Kalibro::Processing.has_processing_after(repository_id, date) - @processing ||= Kalibro::Processing.last_processing_after(repository_id, date) end rescue Exception => error errors.add_to_base(error.message) @@ -65,16 +67,16 @@ class MezuroPlugin::ProjectContent < Article def module_result(repository_id, date = nil) @processing ||= date.nil? ? processing(repository_id) : processing_with_date(repository_id, date) begin - @module_result ||= Kalibro::ModuleResult.find(@procesing.results_root_id) + @module_result ||= Kalibro::ModuleResult.find(@processing.results_root_id) rescue Exception => error errors.add_to_base(error.message) end @module_result end - def result_history(module_id) + def result_history(module_result_id) begin - @result_history ||= Kalibro::MetricResult.history_of(module_id) + @result_history ||= Kalibro::MetricResult.history_of(module_result_id) rescue Exception => error errors.add_to_base(error.message) end @@ -83,20 +85,24 @@ class MezuroPlugin::ProjectContent < Article def description=(value) @description=value end + + def description + @description + end def repositories=(value) @repositories = value.kind_of?(Array) ? value : [value] - @repositories = @repositories.map { |element| to_object(element) } - end - - def self.to_object value - value.kind_of?(Hash) ? Kalibro::Repository.new(value) : value + @repositories = @repositories.map { |element| to_repository(element) } end after_save :send_project_to_service after_destroy :destroy_project_from_service private + + def self.to_repository value + value.kind_of?(Hash) ? Kalibro::Repository.new(value) : value + end def validate_repository_url if(@repositories.nil? || repository_url == "") diff --git a/plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb b/plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb index 5374ad1..a2f05a0 100644 --- a/plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb +++ b/plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb @@ -5,15 +5,19 @@ require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/project_content_fixtures" require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/processing_fixtures" require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/module_fixtures" require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/module_result_fixtures" +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/date_metric_result_fixtures" class ProjectContentTest < ActiveSupport::TestCase def setup - @project = ProjectFixtures.project @project_content = ProjectContentFixtures.project_content + @project = ProjectFixtures.project + @repository = RepositoryFixtures.repository @processing = ProcessingFixtures.processing + @date = @processing.date @module = ModuleFixtures.module @module_result = ModuleResultFixtures.module_result + @date_metric_result = DateMetricResultFixtures.date_metric_result end should 'provide proper short description' do @@ -33,64 +37,87 @@ class ProjectContentTest < ActiveSupport::TestCase assert_equal @project, @project_content.project end - should 'add error when the project does not exist' do + should 'add error to base when the project does not exist' do Kalibro::Project.expects(:find).with(@project.id).raises(Kalibro::Errors::RecordNotFound) + assert_nil @project_content.errors[:base] @project_content.project - - assert_not_nil @project_content.errors + assert_not_nil @project_content.errors[:base] end -=begin should 'get repositories of the project from service' do - + Kalibro::Repository.expects(:repositories_of).with(@project.id).returns([@repository]) + assert_equal [@repository], @project_content.repositories + end + + should 'add error to base when getting the repositories of a project that does not exist' do + Kalibro::Repository.expects(:repositories_of).with(@project.id).raises(Kalibro::Errors::RecordNotFound) + assert_nil @project_content.errors[:base] + @project_content.repositories + assert_not_nil @project_content.errors[:base] + end + + should 'get processing of a repository' do + Kalibro::Processing.expects(:has_ready_processing).with(@repository.id).returns(true) + Kalibro::Processing.expects(:last_ready_processing_of).with(@repository.id).returns(@processing) + assert_equal @processing, @project_content.processing(@repository.id) end + + should 'get not ready processing of a repository' do + Kalibro::Processing.expects(:has_ready_processing).with(@repository.id).returns(false) + Kalibro::Processing.expects(:last_processing_of).with(@repository.id).returns(@processing) + assert_equal @processing, @project_content.processing(@repository.id) + end + + should 'get processing of a repository after date' do + Kalibro::Processing.expects(:has_processing_after).with(@repository.id, @date).returns(true) + Kalibro::Processing.expects(:first_processing_after).with(@repository.id, @date).returns(@processing) + assert_equal @processing, @project_content.processing_with_date(@repository.id, @date) + end + + should 'get processing of a repository before date' do + Kalibro::Processing.expects(:has_processing_after).with(@repository.id, @date).returns(false) + Kalibro::Processing.expects(:has_processing_before).with(@repository.id, @date).returns(true) + Kalibro::Processing.expects(:last_processing_before).with(@repository.id, @date).returns(@processing) + assert_equal @processing, @project_content.processing_with_date(@repository.id, @date) + end + + should 'get module result' do + @project_content.expects(:processing).with(@repository.id).returns(@processing) + Kalibro::ModuleResult.expects(:find).with(@processing.results_root_id).returns(@module_result) + assert_equal @module_result, @project_content.module_result(@repository.id) - should 'get module result from service without date' do - date_with_milliseconds = Kalibro::ProjectResult.date_with_milliseconds(@project_result.date) - Kalibro::ProjectResult.expects(:request).with('ProjectResult', :get_last_result_of, {:project_name => @project.name}).returns({:project_result => @project_result.to_hash}) - Kalibro::ModuleResult.expects(:request).with( - 'ModuleResult', - :get_module_result, - { - :project_name => @project.name, - :module_name => @module.name, - :date => date_with_milliseconds - }).returns({:module_result => @module_result.to_hash}) - assert_equal @module_result.grade, @project_content.module_result({:module_name => @module.name}).grade - end - - should 'get module result from service with date' do - date_with_milliseconds = Kalibro::ProjectResult.date_with_milliseconds(@project_result.date) - request_body = {:project_name => @project.name, :date => @project_result.date} - Kalibro::ProjectResult.expects(:request).with("ProjectResult", :has_results_before, request_body).returns({:has_results => false}) - Kalibro::ProjectResult.expects(:request).with("ProjectResult", :get_first_result_after, request_body).returns({:project_result => @project_result.to_hash}) - Kalibro::ModuleResult.expects(:request).with( - 'ModuleResult', - :get_module_result, - { - :project_name => @project.name, - :module_name => @module.name, - :date => date_with_milliseconds - }).returns({:module_result => @module_result.to_hash}) - assert_equal @module_result.grade, @project_content.module_result({:module_name => @module.name, :date => @project_result.date}).grade + end + + should 'get module result with date' do + @project_content.expects(:processing_with_date).with(@repository.id,@date.to_s).returns(@processing) + Kalibro::ModuleResult.expects(:find).with(@processing.results_root_id).returns(@module_result) + assert_equal @module_result, @project_content.module_result(@repository.id, @date.to_s) end should 'get result history' do - Kalibro::ModuleResult.expects(:request).with( - 'ModuleResult', - :get_result_history, - { - :project_name => @project.name, - :module_name => @module.name - }).returns({:module_result => @module_result.to_hash}) - @project_content.result_history(@module.name) + Kalibro::MetricResult.expects(:history_of).with(@module_result.id).returns([@date_metric_result]) + assert_equal [@date_metric_result], @project_content.result_history(@module_result.id) + end + + should 'add error to base when the module_result does not exist' do + Kalibro::MetricResult.expects(:history_of).with(@module_result.id).raises(Kalibro::Errors::RecordNotFound) + assert_nil @project_content.errors[:base] + @project_content.result_history(@module_result.id) + assert_not_nil @project_content.errors[:base] end +=begin should 'send project to service after saving' do @project_content.expects :send_project_to_service @project_content.run_callbacks :after_save end + should 'destroy project from service' do + Kalibro::Project.expects(:request).with("Project", :get_project, :project_name => @project.name).returns({:project => @project.to_hash}) + Kalibro::Project.expects(:request).with("Project", :remove_project, {:project_name => @project.name}) + @project_content.send :destroy_project_from_service + end + should 'send correct project to service' do hash = ProjectFixtures.project_hash hash.delete(:attributes!) @@ -100,16 +127,5 @@ class ProjectContentTest < ActiveSupport::TestCase @project_content.send :send_project_to_service end - should 'destroy project from service' do - Kalibro::Project.expects(:request).with("Project", :get_project, :project_name => @project.name).returns({:project => @project.to_hash}) - Kalibro::Project.expects(:request).with("Project", :remove_project, {:project_name => @project.name}) - @project_content.send :destroy_project_from_service - end - - should 'not save a project with an existing project name in kalibro' do - Kalibro::Project.expects(:all_names).returns([@project_content.name]) - @project_content.send :validate_kalibro_project_name - assert_equal "Project name already exists in Kalibro", @project_content.errors.on_base - end =end end -- libgit2 0.21.2