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

Can assert work packages schedule through visual representation

Christophe Bliard022723f6c056
diff --git a/spec/support/schedule_helpers.rb b/spec/support/schedule_helpers.rb
new file mode 100644
index 00000000000..159dac286f4
--- /dev/null
+++ b/spec/support/schedule_helpers.rb
@@ -0,0 +1,364 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module ScheduleHelpers
+ # Contains work packages and relations information from a chart
+ # representation.
+ #
+ # The work package information are:
+ # * subject
+ # * start_date
+ # * due_date
+ #
+ # The relations information are limited to follows relations and are retrieved
+ # with +#predecessors_by_follower+
+ #
+ # The chart uses different symbols in the timeline to represent a work package
+ # start and due dates:
+ # * +X+: a day of the work package duration. The first +X+ is the start date,
+ # the last +X+ is the due date.
+ # * +[+: the work package start date. Can be used instead of +X+ when the work
+ # package has no due date.
+ # * +]+: the work package due date. Can be used instead of +X+ when the work
+ # package has no start date.
+ # * +_+: ignored but useful as a placeholder to highlight particular days, for
+ # instance to highlight the previous dates of a work package.
+ class Chart
+ attr_accessor :first_day
+
+ def self.for(representation)
+ builder = ChartBuilder.new
+ builder.parse(representation)
+ end
+
+ def work_packages
+ @work_packages ||= {}
+ end
+
+ def predecessors_by_follower(follower)
+ @predecessors_by_follower ||= Hash.new { |h, k| h[k] = [] }
+ @predecessors_by_follower[follower]
+ end
+
+ def add_work_package(attributes)
+ key = attributes[:subject].to_sym
+ work_packages[key] = attributes
+ end
+
+ def add_follows_relation(predecessor:, follower:)
+ predecessors_by_follower(follower) << predecessor
+ end
+
+ # Returns the date for the Monday represented on the chart.
+ def monday
+ unless defined?(@monday)
+ @monday = first_day
+ @monday += 1 until @monday.wday == 1
+ end
+ @monday
+ end
+
+ # Returns the date for the Tuesday represented on the chart.
+ def tuesday
+ monday + 1.day
+ end
+
+ # Returns the date for the Wednesday represented on the chart.
+ def wednesday
+ monday + 2.days
+ end
+
+ # Returns the date for the Thursday represented on the chart.
+ def thursday
+ monday + 3.days
+ end
+
+ # Returns the date for the Friday represented on the chart.
+ def friday
+ monday + 4.days
+ end
+
+ # Returns the date for the Saturday represented on the chart.
+ def saturday
+ monday + 5.days
+ end
+
+ # Returns the date for the Sunday represented on the chart.
+ def sunday
+ monday + 6.days
+ end
+ end
+
+ # Builds a +Chart+ instance from a visual chart representation.
+ #
+ # Example:
+ #
+ # ChartBuilder.new.parse(<<~CHART)
+ # days | MTWTFss |
+ # main | XX |
+ # follower | XXX | after main
+ # start_only | [ |
+ # due_only | ] |
+ # no_dates | |
+ # CHART
+ class ChartBuilder
+ attr_reader :chart
+
+ def initialize
+ @chart = Chart.new
+ end
+
+ def parse(representation)
+ lines = representation.split("\n")
+ header = lines.shift
+ parse_header(header)
+ lines.each do |line|
+ parse_line(line)
+ end
+ chart
+ end
+
+ private
+
+ def parse_header(header)
+ _, week_days = header.split(' | ', 2)
+ unless week_days.match?(/mtwtfss/i)
+ raise ArgumentError, "First header line of schedule chart must contain MTWTFSS to indicate day names"
+ end
+
+ nb_days_from_monday = week_days.index(/m/i)
+ chart.first_day = next_monday - nb_days_from_monday.days
+ end
+
+ def next_monday
+ date = Time.zone.today
+ while date.wday != 1
+ date += 1.day
+ end
+ date
+ end
+
+ def parse_line(line)
+ case line
+ when ''
+ # noop
+ when / \| /
+ parse_work_package_line(line)
+ else
+ raise "unable to parse line #{line.inspect}"
+ end
+ end
+
+ def parse_work_package_line(line)
+ name, timespan, modifiers = line.split(' | ', 3)
+ name.strip!
+ attributes = { subject: name }
+ attributes.update(parse_timespan(timespan))
+ chart.add_work_package(attributes)
+
+ modifiers.to_s.split(',').map(&:strip).each do |modifier|
+ parse_modifier(name, modifier)
+ end
+ end
+
+ def parse_modifier(name, modifier)
+ case modifier
+ when /^after (\w+)/
+ predecessor = $1.to_sym
+ if !chart.work_packages.has_key?(predecessor)
+ raise "unable to find work package #{predecessor.inspect} in modifier #{modifier.inspect} for line #{name.inspect}"
+ end
+
+ chart.add_follows_relation(predecessor: $1.to_sym, follower: name.to_sym)
+ else
+ raise "unable to parse modifier #{modifier.inspect} for line #{name.inspect}"
+ end
+ end
+
+ def parse_timespan(timespan)
+ start_pos = timespan.index('[') || timespan.index('X')
+ due_pos = timespan.rindex(']') || timespan.rindex('X')
+ {
+ start_date: start_pos && (chart.first_day + start_pos),
+ due_date: due_pos && (chart.first_day + due_pos)
+ }
+ end
+
+ def parse_follows_relation_line(line)
+ to, from = line.split(' -> ', 2).map(&:strip).map(&:to_sym)
+ [to, from].each do |name|
+ next if chart.work_packages.has_key?(name)
+
+ raise "unable to find work package #{name.inspect} in relation #{line.inspect}"
+ end
+
+ { from:, to:, relation_type: Relation::TYPE_FOLLOWS }
+ end
+ end
+
+ module LetSchedule
+ # Declare work packages and relations from a visual chart representation.
+ #
+ # For instance:
+ #
+ # let_schedule(<<~CHART)
+ # days | MTWTFss |
+ # main | XX |
+ # follower | XXX | after main
+ # start_only | [ |
+ # due_only | ] |
+ # CHART
+ #
+ # is equivalent to:
+ #
+ # let!(:main) do
+ # create(:work_package, subject: 'main', start_date: next_monday, due_date: next_monday + 1.day)
+ # end
+ # let!(:follower) do
+ # create(:work_package, subject: 'follower', start_date: next_monday + 2.days, due_date: next_monday + 4.days) }
+ # end
+ # let!(:relation_follower_follows_main) do
+ # create(:follows_relation, from: follower, to: main) }
+ # end
+ # let!(:start_only) do
+ # create(:work_package, subject: 'start_only', start_date: next_monday + 1.day) }
+ # end
+ # let!(:due_only) do
+ # create(:work_package, subject: 'due_only', due_date: next_monday + 3.days) }
+ # end
+ def let_schedule(chart, **extra_attributes)
+ chart = Chart.for(chart)
+ let(:schedule_chart) { chart }
+ chart.work_packages.each do |name, attributes|
+ let!(name) do
+ create(:work_package, **attributes.reverse_merge(extra_attributes))
+ end
+ chart.predecessors_by_follower(name).each do |predecessor|
+ relation_alias = "relation_#{name}_follows_#{predecessor}"
+ let!(relation_alias) { create(:follows_relation, from: send(name), to: send(predecessor)) }
+ end
+ end
+ end
+ end
+
+ module ExampleMethods
+ # Update the given work packages according to the given chart representation.
+ # Work packages are changed without being saved.
+ #
+ # For instance:
+ #
+ # before do
+ # change_schedule([main], <<~CHART)
+ # days | MTWTFss |
+ # main | XX |
+ # CHART
+ # end
+ #
+ # is equivalent to:
+ #
+ # before do
+ # main.start_date = monday
+ # main.due_date = tuesday
+ # end
+ def update_schedule(work_packages, chart)
+ change_schedule(work_packages, chart)
+ work_packages.each(&:save!)
+ end
+
+ # Change the given work packages according to the given chart representation.
+ # Work packages are changed without being saved.
+ #
+ # For instance:
+ #
+ # before do
+ # change_schedule([main], <<~CHART)
+ # days | MTWTFss |
+ # main | XX |
+ # CHART
+ # end
+ #
+ # is equivalent to:
+ #
+ # before do
+ # main.start_date = monday
+ # main.due_date = tuesday
+ # end
+ def change_schedule(work_packages, chart)
+ by_id = work_packages.index_by(&:subject)
+ chart = Chart.for(chart)
+ chart.work_packages.each do |name, attributes|
+ raise ArgumentError, "unable to find WorkPackage :#{name}" unless respond_to?(name)
+
+ work_package = send(name)
+ work_package = by_id[work_package.id] if by_id.has_key?(work_package.id)
+ attributes.slice(:start_date, :due_date).each do |attribute, value|
+ work_package.send(:"#{attribute}=", value)
+ end
+ end
+ end
+
+ # Expect the given work packages to match a visual chart representation.
+ #
+ # For instance:
+ #
+ # it 'is scheduled' do
+ # expect_schedule(work_packages, <<~CHART)
+ # days | MTWTFss |
+ # main | XX |
+ # follower | XXX |
+ # CHART
+ # end
+ #
+ # is equivalent to:
+ #
+ # it 'is scheduled' do
+ # main = work_packages.find { _1.id == main.id } || main
+ # expect(main.start_date).to eq(next_monday)
+ # expect(main.due_date).to eq(next_monday + 1.day)
+ # follower = work_packages.find { _1.id == follower.id } || follower
+ # expect(follower.start_date).to eq(next_monday + 2.days)
+ # expect(follower.due_date).to eq(next_monday + 4.days)
+ # end
+ def expect_schedule(work_packages, chart)
+ by_id = work_packages.index_by(&:id)
+ chart = Chart.for(chart)
+ chart.work_packages.each do |name, attributes|
+ raise ArgumentError, "unable to find WorkPackage :#{name}" unless respond_to?(name)
+
+ work_package = send(name)
+ work_package = by_id[work_package.id] if by_id.has_key?(work_package.id)
+ expect(work_package).to have_attributes(attributes.slice(:subject, :start_date, :due_date))
+ end
+ end
+ end
+end
+
+RSpec.configure do |config|
+ config.extend ScheduleHelpers::LetSchedule
+ config.include ScheduleHelpers::ExampleMethods
+end
diff --git a/spec/support_spec/schedule_helpers_spec.rb b/spec/support_spec/schedule_helpers_spec.rb
new file mode 100644
index 00000000000..38e3d079527
--- /dev/null
+++ b/spec/support_spec/schedule_helpers_spec.rb
@@ -0,0 +1,237 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe ScheduleHelpers do
+ include ActiveSupport::Testing::TimeHelpers
+
+ create_shared_association_defaults_for_work_package_factory
+
+ let(:today) { Date.new(2022, 6, 16) } # Thursday 16 June 2022
+ let(:monday) { Date.new(2022, 6, 20) } # Monday 20 June
+ let(:tuesday) { Date.new(2022, 6, 21) }
+ let(:wednesday) { Date.new(2022, 6, 22) }
+ let(:thursday) { Date.new(2022, 6, 23) }
+ let(:friday) { Date.new(2022, 6, 24) }
+ let(:saturday) { Date.new(2022, 6, 25) }
+ let(:sunday) { Date.new(2022, 6, 26) }
+
+ describe ScheduleHelpers::Chart do
+ let(:chart) { described_class.new }
+
+ before do
+ chart.first_day = today
+ end
+
+ describe '#first_day' do
+ it 'returns the first day represented on the graph' do
+ expect(chart.first_day).to eq(today)
+ end
+ end
+
+ %i[monday tuesday wednesday thursday friday saturday sunday].each do |day_name|
+ describe "##{day_name}" do
+ it "returns the #{day_name} of the week represented on the chart" do
+ expected_date = send(day_name)
+ expect(chart.send(day_name)).to eq(expected_date)
+ end
+ end
+ end
+ end
+
+ describe ScheduleHelpers::ChartBuilder do
+ let(:builder) { described_class.new }
+
+ describe 'happy path' do
+ let(:next_tuesday) { tuesday + 7.days }
+
+ before do
+ travel_to(today)
+ end
+
+ it 'reads a chart and convert it into objects with attributes' do
+ chart = builder.parse(<<~CHART)
+ days | MTWTFss |
+ main | XX |
+ other | XX..XX |
+ follower | XXX | after main
+ start_only | [ |
+ due_only | ] |
+ no_dates | |
+ CHART
+ expect(chart.work_packages).to eq(
+ {
+ main: { subject: 'main', start_date: monday, due_date: tuesday },
+ other: { subject: 'other', start_date: thursday, due_date: next_tuesday },
+ follower: { subject: 'follower', start_date: wednesday, due_date: friday },
+ start_only: { subject: 'start_only', start_date: tuesday, due_date: nil },
+ due_only: { subject: 'due_only', start_date: nil, due_date: friday },
+ no_dates: { subject: 'no_dates', start_date: nil, due_date: nil }
+ }
+ )
+ expect(chart.predecessors_by_follower(:main)).to eq([])
+ expect(chart.predecessors_by_follower(:other)).to eq([])
+ expect(chart.predecessors_by_follower(:follower)).to eq([:main])
+ end
+ end
+
+ describe 'error handling' do
+ it 'raises an error if the relation references a non-existing work package predecessor' do
+ expect do
+ builder.parse(<<~CHART)
+ | MTWTFss |
+ follower | XX | after main
+ CHART
+ end.to raise_error(RuntimeError, /unable to find work package :main in modifier "after main" for line "follower"/)
+ end
+ end
+ end
+
+ describe 'let_schedule helper' do
+ let_schedule(<<~CHART)
+ days | MTWTFss |
+ main | XX |
+ follower | XXX | after main
+ CHART
+
+ it 'creates let! call for :schedule_chart which returns the chart' do
+ next_monday = (Time.zone.today..(Time.zone.today + 7.days)).find { |d| d.wday == 1 }
+ expect(schedule_chart.first_day).to eq(next_monday)
+ end
+
+ it 'creates let! calls for each work package' do
+ expect([main, follower]).to all(be_an_instance_of(WorkPackage))
+ expect([main, follower]).to all(be_persisted)
+ expect(main).to have_attributes(
+ subject: 'main',
+ start_date: schedule_chart.monday,
+ due_date: schedule_chart.tuesday
+ )
+ expect(follower).to have_attributes(
+ subject: 'follower',
+ start_date: schedule_chart.wednesday,
+ due_date: schedule_chart.friday
+ )
+ end
+
+ it 'creates let! calls for follows relations between work packages' do
+ expect(follower.follows_relations.count).to eq(1)
+ expect(relation_follower_follows_main).to be_an_instance_of(Relation)
+ end
+
+ context 'with additional attributes' do
+ let_schedule(<<~CHART, done_ratio: 50, schedule_manually: true)
+ days | MTWTFss |
+ main | XX |
+ follower | XXX | after main
+ CHART
+
+ it 'applies additional attributes to all created work packages' do
+ expect([main, follower]).to all(have_attributes(done_ratio: 50, schedule_manually: true))
+ end
+ end
+ end
+
+ describe 'expect_schedule helper' do
+ let_schedule(<<~CHART)
+ | MTWTFss |
+ main | XX |
+ other | XXX |
+ CHART
+
+ it 'checks the work packages properties according to the given work packages and chart representation' do
+ expect do
+ expect_schedule([main, other], <<~CHART)
+ | MTWTFss |
+ main | XX |
+ other | XXX |
+ CHART
+ end.not_to raise_error
+ end
+
+ it 'raises an error if start_date is wrong' do
+ expect do
+ expect_schedule([main], <<~CHART)
+ | MTWTFss |
+ main | X |
+ CHART
+ end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
+ end
+
+ it 'raises an error if due_date is wrong' do
+ expect do
+ expect_schedule([main], <<~CHART)
+ | MTWTFss |
+ main | XXXXX |
+ CHART
+ end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
+ end
+
+ it 'raises an error if no work package exists for a given name' do
+ expect do
+ expect_schedule([main], <<~CHART)
+ | MTWTFss |
+ unknown | XX |
+ CHART
+ end.to raise_error(ArgumentError, "unable to find WorkPackage :unknown")
+ end
+
+ it 'checks against the given work packages rather than the ones from the let! definitions' do
+ expect do
+ expect_schedule([], <<~CHART)
+ | MTWTFss |
+ main | XX |
+ CHART
+ end.not_to raise_error
+ end
+
+ it 'uses the work package from the let! definitions if it is not given as parameter' do
+ a_modified_instance_of_main = WorkPackage.find(main.id)
+ a_modified_instance_of_main.due_date += 2.days
+ expect do
+ expect_schedule([main], <<~CHART)
+ | MTWTFss |
+ main | XX |
+ CHART
+ end.not_to raise_error
+ expect do
+ expect_schedule([a_modified_instance_of_main], <<~CHART)
+ | MTWTFss |
+ main | XXXX |
+ CHART
+ end.not_to raise_error
+ expect do
+ expect_schedule([], <<~CHART)
+ | MTWTFss |
+ main | XX |
+ CHART
+ end.not_to raise_error
+ end
+ end
+end