Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Fix failing tests by deriving dates in factory

Christophe Bliardcfa7826a6cc4
diff --git a/spec/factories/work_package_factory.rb b/spec/factories/work_package_factory.rb
index b892e9afb58..c4ba6c3bfae 100644
--- a/spec/factories/work_package_factory.rb
+++ b/spec/factories/work_package_factory.rb
@@ -30,6 +30,7 @@ FactoryBot.define do
factory :work_package do
transient do
custom_values { nil }
+ days { WorkPackages::Shared::Days.for(self) }
end
priority
@@ -40,6 +41,21 @@ FactoryBot.define do
author factory: :user
created_at { Time.zone.now }
updated_at { Time.zone.now }
+ start_date do
+ # derive start date if due date and duration were provided
+ next unless OpenProject::FeatureDecisions.work_packages_duration_field_active?
+ next unless %i[due_date duration].all? { |field| __override_names__.include?(field) }
+
+ days.start_date(due_date&.to_date, duration)
+ end
+ due_date do
+ # derive due date if start date and duration were provided
+ next unless OpenProject::FeatureDecisions.work_packages_duration_field_active?
+ next unless %i[start_date duration].all? { |field| __override_names__.include?(field) }
+
+ days.due_date(start_date&.to_date, duration)
+ end
+ duration { days.duration(start_date&.to_date, due_date&.to_date) }
trait :is_milestone do
type factory: :type_milestone
@@ -48,16 +64,6 @@ FactoryBot.define do
callback(:after_build) do |work_package, evaluator|
work_package.type = work_package.project.types.first unless work_package.type
- # set duration / start date / due date according to two others
- days = WorkPackages::Shared::Days.for(work_package)
- if work_package.duration.nil? && work_package.start_date && work_package.due_date
- work_package.duration = days.duration(work_package.start_date.to_date, work_package.due_date.to_date)
- elsif work_package.start_date.nil? && work_package.due_date && work_package.duration
- work_package.start_date = days.start_date(work_package.due_date.to_date, work_package.duration)
- elsif work_package.due_date.nil? && work_package.start_date && work_package.duration
- work_package.due_date = days.due_date(work_package.start_date.to_date, work_package.duration)
- end
-
custom_values = evaluator.custom_values || {}
if custom_values.is_a? Hash
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 228c4ac4f0e..72d862911ae 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -1153,7 +1153,7 @@ describe WorkPackages::SetAttributesService,
let(:call_attributes) { { ignore_non_working_days: false } }
it_behaves_like 'service call' do
- it "updates the start date to be on next working day, and due date to accomodate duration" do
+ it "updates the start date to be on next working day, and due date to accommodate duration" do
expect { subject }
.to change { work_package.slice(:start_date, :due_date, :duration) }
.from(start_date: monday - 1.day, due_date: friday, duration: 6)
diff --git a/spec/services/work_packages/set_schedule_service_spec.rb b/spec/services/work_packages/set_schedule_service_spec.rb
index bdb2256e505..4f69d3dfc8a 100644
--- a/spec/services/work_packages/set_schedule_service_spec.rb
+++ b/spec/services/work_packages/set_schedule_service_spec.rb
@@ -138,16 +138,19 @@ describe WorkPackages::SetScheduleService do
expect(result.start_date)
.to eql(start_date),
- "Expected work package ##{wp.id} '#{wp.subject}' to have start date #{start_date}, got #{result.start_date}"
+ "Expected work package ##{wp.id} '#{wp.subject}' " \
+ "to have start date #{start_date.inspect}, got #{result.start_date.inspect}"
expect(result.due_date)
.to eql(due_date),
- "Expected work package ##{wp.id} '#{wp.subject}' to have due date #{due_date}, got #{result.due_date}"
+ "Expected work package ##{wp.id} '#{wp.subject}' " \
+ "to have due date #{due_date.inspect}, got #{result.due_date.inspect}"
duration = WorkPackages::Shared::AllDays.new.duration(start_date, due_date)
expect(result.duration)
.to eql(duration),
- "Expected work package ##{wp.id} '#{wp.subject}' to have duration #{duration}, got #{result.duration}"
+ "Expected work package ##{wp.id} '#{wp.subject}' " \
+ "to have duration #{duration.inspect}, got #{result.duration.inspect}"
end
end