From d4f2df646bd0ab1fe7b42780692429c44428e0ca Mon Sep 17 00:00:00 2001 From: João M. M. da Silva + Diego Araújo Date: Tue, 6 Nov 2012 17:42:13 -0200 Subject: [PATCH] [Mezuro] Completed project model, fixtures and tests refactoring. --- plugins/mezuro/lib/kalibro/project.rb | 19 ++++++++++--------- plugins/mezuro/test/fixtures/project_fixtures.rb | 19 ++++++------------- plugins/mezuro/test/unit/kalibro/project_test.rb | 76 ++++++++++++++++++++++++++++++++++++++++++---------------------------------- 3 files changed, 58 insertions(+), 56 deletions(-) diff --git a/plugins/mezuro/lib/kalibro/project.rb b/plugins/mezuro/lib/kalibro/project.rb index 1de0b02..a071241 100644 --- a/plugins/mezuro/lib/kalibro/project.rb +++ b/plugins/mezuro/lib/kalibro/project.rb @@ -1,21 +1,21 @@ class Kalibro::Project < Kalibro::Model - attr_accessor :name, :license, :description, :repository, :configuration_name, :state, :kalibro_error + attr_accessor :id, :name, :description - def self.all_names - response = request("Project", :get_project_names)[:project_name] + def self.all + response = request("Project", :all_projects)[:project].to_a response = [] if response.nil? - response + response.map {|project| new project} end - def self.find_by_name(project_name) - new request("Project", :get_project, :project_name => project_name)[:project] + def self.find(project_id) + new request("Project", :get_project, :project_id => project_id)[:project] end - def repository=(value) - @repository = Kalibro::Repository.to_object value + def self.project_of(repository_id) + new request("Project", :project_of, :repository_id => repository_id)[:project] end - +=begin def error=(value) @kalibro_error = Kalibro::Error.to_object value end @@ -47,5 +47,6 @@ class Kalibro::Project < Kalibro::Model add_error exception end end +=end end diff --git a/plugins/mezuro/test/fixtures/project_fixtures.rb b/plugins/mezuro/test/fixtures/project_fixtures.rb index 4c18d24..39c9880 100644 --- a/plugins/mezuro/test/fixtures/project_fixtures.rb +++ b/plugins/mezuro/test/fixtures/project_fixtures.rb @@ -6,22 +6,15 @@ class ProjectFixtures Kalibro::Project.new project_hash end + def self.created_project + Kalibro::Project.new :name => 'Qt-Calculator', :description => 'Calculator for Qt' + end + def self.project_hash { + :id => 42, :name => 'Qt-Calculator', - :license => 'GPL', - :description => 'Calculator for Qt', - :repository => RepositoryFixtures.repository_hash, - :configuration_name => 'Kalibro for Java', - :state => 'READY', - :attributes! => - { - :repository=> - { - "xsi:type"=>"kalibro:repositoryXml", - "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance" - } - } + :description => 'Calculator for Qt' } end diff --git a/plugins/mezuro/test/unit/kalibro/project_test.rb b/plugins/mezuro/test/unit/kalibro/project_test.rb index 39e80bf..9d6accf 100644 --- a/plugins/mezuro/test/unit/kalibro/project_test.rb +++ b/plugins/mezuro/test/unit/kalibro/project_test.rb @@ -7,64 +7,71 @@ class ProjectTest < ActiveSupport::TestCase def setup @hash = ProjectFixtures.project_hash @project = ProjectFixtures.project + @created_project = ProjectFixtures.created_project @project_content = ProjectFixtures.project_content end + + should 'initialize new project from hash' do + project = Kalibro::Project.new @hash + assert_equal @hash[:name], project.name + end - should 'get all project names' do - response_hash = {:project_name => [@project.name]} - Kalibro::Project.expects(:request).with("Project", :get_project_names).returns(response_hash) - assert_equal response_hash[:project_name], Kalibro::Project.all_names + should 'convert project to hash' do + hash = @project.to_hash + assert_equal @project.name, hash[:name] end - should 'return empty when there are no projects' do - response_hash = {:project_name => nil} - Kalibro::Project.expects(:request).with("Project", :get_project_names).returns(response_hash) - assert_equal [], Kalibro::Project.all_names + should 'answer if project exists in kalibro' do + Kalibro::Project.expects(:request).with("Project", :project_exists, {:project_id => @project.id}).returns({:exists => true}) + assert Kalibro::Project.exists?(@project.id) end - should 'find project by name' do - request_body = {:project_name => @project.name} - response_hash = {:project => @hash} - Kalibro::Project.expects(:new).with(@hash).returns(@project) - Kalibro::Project.expects(:request).with("Project", :get_project, request_body).returns(response_hash) - assert_equal @project, Kalibro::Project.find_by_name(@project.name) + should 'find project' do + Kalibro::Project.expects(:request).with("Project", :get_project, {:project_id => @project.id}).returns(:project => @hash) + assert_equal @hash[:name], Kalibro::Project.find(@project.id).name end should 'raise error when project doesnt exist' do - request_body = {:project_name => @project.name} - Kalibro::Project.expects(:request).with("Project", :get_project, request_body).raises(Exception.new("(S:Server) There is no project named " + @project.name)) - assert_raise Exception do Kalibro::Project.find_by_name(@project.name) end + request_body = {:project_id => @project.id} + Kalibro::Project.expects(:request).with("Project", :get_project, request_body).raises(Exception.new("(S:Server) There is no project with id #{@project.id}")) + assert_raise Exception do Kalibro::Project.find(@project.id) end + end + + should 'get project of a repository' do + repository_id = 31 + Kalibro::Project.expects(:request).with("Project", :project_of, {:repository_id => repository_id}).returns({:project => @hash}) + assert_equal @hash[:name], Kalibro::Project.project_of(repository_id).name + end + + should 'get all project' do + Kalibro::Project.expects(:request).with("Project", :all_projects).returns({:project => [@hash]}) + assert_equal @hash[:name], Kalibro::Project.all.first.name + end + + should 'return empty when there are no projects' do + Kalibro::Project.expects(:request).with("Project", :all_projects).returns({:project => nil}) + assert_equal [], Kalibro::Project.all end should 'return true when project is saved successfully' do - Kalibro::Project.expects(:request).with("Project", :save_project, {:project => @project.to_hash}) - assert @project.save + id_from_kalibro = 1 + Kalibro::Project.expects(:request).with("Project", :save_project, {:project => @created_project.to_hash}).returns(id_from_kalibro) + assert @created_project.save + assert_equal id_from_kalibro, @created_project.id end should 'return false when project is not saved successfully' do Kalibro::Project.expects(:request).with("Project", :save_project, {:project => @project.to_hash}).raises(Exception.new) assert !(@project.save) + assert_nil @created_project.id end should 'remove existent project from service' do - Kalibro::Project.expects(:request).with("Project", :remove_project, {:project_name => @project.name}) + Kalibro::Project.expects(:request).with("Project", :delete_project, {:project_id => @project.id}) @project.destroy end - - should 'initialize new project from hash' do - project = Kalibro::Project.new @hash - assert_equal @project.name, project.name - assert_equal @project.repository.type, project.repository.type - end - should 'convert project to hash' do - hash = @project.to_hash - assert_equal @hash[:name], hash[:name] - assert_equal @hash[:configuration_name], hash[:configuration_name] - assert_equal @hash[:repository], hash[:repository] - assert_equal @hash[:state], hash[:state] - end - +=begin should 'process project without days' do Kalibro::Project.expects(:request).with('Kalibro', :process_project, {:project_name => @project.name}) @project.process_project @@ -84,5 +91,6 @@ class ProjectTest < ActiveSupport::TestCase Kalibro::Project.expects(:request).with("Kalibro", :cancel_periodic_process, {:project_name => @project.name}) @project.cancel_periodic_process end +=end end -- libgit2 0.21.2