diff --git a/lib/redmine_contracts/budget_plugin_migration.rb b/lib/redmine_contracts/budget_plugin_migration.rb index 95b9595..9970b2a 100644 --- a/lib/redmine_contracts/budget_plugin_migration.rb +++ b/lib/redmine_contracts/budget_plugin_migration.rb @@ -9,6 +9,36 @@ module RedmineContracts def self.migrate(old_data) @@data = old_data + + ActiveRecord::Base.transaction do + @@data.each do |old_deliverable| + + deliverable = Deliverable.new( + :title => old_deliverable['subject'], + :end_date => old_deliverable['due'], + :notes => old_deliverable['description'] + ) + deliverable.type = old_deliverable['type'] + + project = Project.find(old_deliverable['project_id']) + contract = Contract.find_by_project_id(project.id) + if contract.nil? + contract = Contract.new(:name => 'Converted Contract', + :start_date => old_deliverable['due'], + :end_date => old_deliverable['due']) + + + contract.project = project + contract.account_executive = project.users.first + contract.save! + end + + deliverable.contract = contract + deliverable.manager = project.users.first + + deliverable.save! + end + end end def self.data diff --git a/test/integration/budget_plugin_migration_test.rb b/test/integration/budget_plugin_migration_test.rb index d1004bf..604385b 100644 --- a/test/integration/budget_plugin_migration_test.rb +++ b/test/integration/budget_plugin_migration_test.rb @@ -12,6 +12,18 @@ class BudgetPluginMigrationTest < ActionController::IntegrationTest context "migrate" do setup do @data = load_test_fixture + @project_one = Project.generate! + @project_two = Project.generate! + # Stub out the Project finders because we need to match the ids but can't + # be sure what ids object_daddy will give us + Project.stubs(:find_by_id).with(1).returns(@project_one) + Project.stubs(:find_by_id).with(2).returns(@project_one) + + @manager = User.generate! + @role = Role.generate! + User.add_to_project(@manager, @project_one, @role) + User.add_to_project(@manager, @project_two, @role) + end should "load a YAML dump of the old budget data" do @@ -20,9 +32,32 @@ class BudgetPluginMigrationTest < ActionController::IntegrationTest assert_equal @data, RedmineContracts::BudgetPluginMigration.data end - should "create a new Deliverable for each old Deliverable" + should "create a new Deliverable for each old Deliverable" do + + assert_difference("Deliverable.count", 3) do + assert_difference("HourlyDeliverable.count", 2) do + assert_difference("FixedDeliverable.count", 1) do + RedmineContracts::BudgetPluginMigration.migrate(@data) + end + end + end + + end - should "create a new Contract for each project that had an old deliverable" + should "create a new Contract for each project that had an old deliverable" do + assert_difference("Contract.count", 2) do + RedmineContracts::BudgetPluginMigration.migrate(@data) + end + + assert_equal 1, @project_one.reload.contracts.first.deliverables.count + assert_equal 2, @project_two.reload.contracts.first.deliverables.count + end + + should "pick the first project member for the deliverable manager" do + RedmineContracts::BudgetPluginMigration.migrate(@data) + + assert_equal [@manager, @manager, @manager], Deliverable.all.collect(&:manager) + end end end