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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
baseline api: fixing scenario in which only a historic timestamp is requested at which the work package does not exist
Sebastian Fiedlschuster9afa92814093
diff --git a/app/models/journable/with_historic_attributes.rb b/app/models/journable/with_historic_attributes.rbindex 50bc7948adf..a63515eb4e5 100644--- a/app/models/journable/with_historic_attributes.rb+++ b/app/models/journable/with_historic_attributes.rb@@ -107,7 +107,7 @@ class Journable::WithHistoricAttributes < SimpleDelegatortimestamps.each do |timestamp|journable.assign_historic_attributes(timestamp:,- historic_journable: journable.at_timestamp(timestamp),+ historic_journable: journable.try(:at_timestamp, timestamp),matching_journable: (query_work_packages(query:, timestamp:).find_by(id: journable.id) if query))end@@ -115,9 +115,10 @@ class Journable::WithHistoricAttributes < SimpleDelegatorend# rubocop:disable Metrics/AbcSize+ # rubocop:disable Metrics/PerceivedComplexitydef self.wrap_multiple(journables, timestamps: nil, query: nil, include_only_changed_attributes: false)timestamps ||= query.try(:timestamps) || []- journables = journables.first.class.at_timestamp(timestamps.last).where(id: journables) if timestamps.last.try(:historic?)+ journables = journables.map { |j| j.at_timestamp(timestamps.last) } if timestamps.last.try(:historic?)journables = journables.map { |j| new(j, timestamps:, query:, include_only_changed_attributes:) }timestamps.each do |timestamp|assign_historic_attributes_to(@@ -131,6 +132,7 @@ class Journable::WithHistoricAttributes < SimpleDelegatorjournablesend# rubocop:enable Metrics/AbcSize+ # rubocop:enable Metrics/PerceivedComplexitydef assign_historic_attributes(timestamp:, historic_journable:, matching_journable:)attributes_by_timestamp[timestamp.to_s] = extract_historic_attributes_from(historic_journable:) if historic_journable@@ -140,9 +142,11 @@ class Journable::WithHistoricAttributes < SimpleDelegatordef self.assign_historic_attributes_to(journables, timestamp:, historic_journables:, matching_journables:, query:)journables.each do |journable|- historic_journable = historic_journables.find_by(id: journable.id)- matching_journable = matching_journables.find_by(id: journable.id) if query- journable.assign_historic_attributes(timestamp:, historic_journable:, matching_journable:)+ if journable+ historic_journable = historic_journables.find_by(id: journable.id)+ matching_journable = matching_journables.find_by(id: journable.id) if query+ journable.assign_historic_attributes(timestamp:, historic_journable:, matching_journable:)+ endendend@@ -213,6 +217,14 @@ class Journable::WithHistoricAttributes < SimpleDelegatorquery.results.work_packagesend+ def id+ __getobj__.try(:id)+ end++ def attributes+ __getobj__.try(:attributes)+ end+def to_ary__getobj__.send(:to_ary)enddiff --git a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb b/lib/api/v3/work_packages/eager_loading/historic_attributes.rbindex 2878bbe8f83..45e31ce232a 100644--- a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb+++ b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb@@ -32,8 +32,10 @@ module API::V3::WorkPackages::EagerLoadingattr_accessor :timestamps, :querydef apply(work_package)- work_package_with_historic_attributes = work_packages_with_historic_attributes.detect { |wp| wp.id == work_package.id }- work_package.attributes = work_package_with_historic_attributes.attributes.except('timestamp')+ # TODO Add spec with multiple work packages to make sure the order is preserved+ work_package_array_index = work_packages.map(&:id).find_index(work_package.id)+ work_package_with_historic_attributes = work_packages_with_historic_attributes[work_package_array_index]+ work_package.attributes = work_package_with_historic_attributes.attributes.try(:except, 'timestamp')work_package.baseline_attributes = work_package_with_historic_attributes.baseline_attributeswork_package.attributes_by_timestamp = work_package_with_historic_attributes.attributes_by_timestampwork_package.timestamps = work_package_with_historic_attributes.timestampsdiff --git a/spec/models/journable/with_historic_attributes_spec.rb b/spec/models/journable/with_historic_attributes_spec.rbindex 23809d40cd3..e0294deed9c 100644--- a/spec/models/journable/with_historic_attributes_spec.rb+++ b/spec/models/journable/with_historic_attributes_spec.rb@@ -171,7 +171,7 @@ describe Journable::WithHistoricAttributes doendend- describe "when the work package did not exist yet at the basline date" do+ describe "when the work package did not exist yet at the baseline date" dolet(:timestamps) { [Timestamp.parse("2021-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }it "provides access to the work-package attributes" do@@ -191,6 +191,26 @@ describe Journable::WithHistoricAttributes doexpect(subject.exists_at_timestamps).to include Timestamp.parse("PT0S")endend++ describe "when the work package did not exist at the only requested date" do+ let(:timestamps) { [Timestamp.parse("2021-01-01T00:00:00Z")] }++ it "has no attributes" do+ expect(subject.attributes).to be_nil+ end++ it "has no attributes at the baseline date, which is the only given date" do+ expect(subject.attributes_by_timestamp["2021-01-01T00:00:00Z"]).to be_nil+ end++ it "has no baseline attributes" do+ expect(subject.baseline_attributes).to be_nil+ end++ it "does not include the timestamp in the exists_at_timestamps array" do+ expect(subject.exists_at_timestamps).not_to include Timestamp.parse("2021-01-01T00:00:00Z")+ end+ endenddescribe ".wrap_multiple" do@@ -260,6 +280,31 @@ describe Journable::WithHistoricAttributes doexpect(subject.first.historic?).to be trueendend++ describe "when the work package did not exist at the only requested date" do+ let(:timestamps) { [Timestamp.parse("2021-01-01T00:00:00Z")] }++ specify "the given work package does exist (at present time)" do+ expect(work_package).to be_present+ expect(work_packages.count).to eq 1+ end++ it "has no attributes" do+ expect(subject.first.attributes).to be_nil+ end++ it "has no attributes at the baseline date, which is the only given date" do+ expect(subject.first.attributes_by_timestamp["2021-01-01T00:00:00Z"]).to be_nil+ end++ it "has no baseline attributes" do+ expect(subject.first.baseline_attributes).to be_nil+ end++ it "does not include the timestamp in the exists_at_timestamps array" do+ expect(subject.first.exists_at_timestamps).not_to include Timestamp.parse("2021-01-01T00:00:00Z")+ end+ endendenddiff --git a/spec/requests/api/v3/work_packages/show_resource_spec.rb b/spec/requests/api/v3/work_packages/show_resource_spec.rbindex 1f7d8f56878..86aa16e16ea 100644--- a/spec/requests/api/v3/work_packages/show_resource_spec.rb+++ b/spec/requests/api/v3/work_packages/show_resource_spec.rb@@ -220,14 +220,15 @@ describe 'API v3 Work package resource',context 'when providing timestamps' dolet(:timestamps) { [Timestamp.parse('2015-01-01T00:00:00Z'), Timestamp.now] }let(:baseline_time) { timestamps.first.to_time }+ let(:created_at) { baseline_time - 1.day }let(:work_package) donew_work_package = create(:work_package, subject: "The current work package", project:)- new_work_package.update_columns created_at: baseline_time - 1.day+ new_work_package.update_columns(created_at:)new_work_packageendlet(:original_journal) do- create_journal(journable: work_package, timestamp: baseline_time - 1.day,+ create_journal(journable: work_package, timestamp: created_at,version: 1,attributes: { subject: "The original work package" })end@@ -292,6 +293,62 @@ describe 'API v3 Work package resource',.at_path('_links/self/href')endend++ describe "when the work package has not been present at the baseline time" do+ let(:timestamps) { [Timestamp.parse('2015-01-01T00:00:00Z'), Timestamp.now] }+ let(:created_at) { 10.days.ago }++ describe "attributesByTimestamp" do+ describe "exists" do+ it "marks the work package as not existing at the baseline time" do+ expect(subject)+ .to be_json_eql(false.to_json)+ .at_path("_embedded/attributesByTimestamp/0/_meta/exists")+ end++ it "marks the work package as existing at the current time" do+ expect(subject)+ .to be_json_eql(true.to_json)+ .at_path("_embedded/attributesByTimestamp/1/_meta/exists")+ end+ end+ end++ describe "_meta" do+ describe "exits" do+ it "is true because the work package does exist at the last given timestamp" do+ expect(subject)+ .to be_json_eql(true.to_json)+ .at_path("_meta/exists")+ end+ end+ end+ end++ describe "when the work package does not exist at the only requested timestamp" do+ let(:timestamps) { [Timestamp.parse('2015-01-01T00:00:00Z')] }+ let(:created_at) { 10.days.ago }++ describe "attributesByTimestamp" do+ describe "exists" do+ it "marks the work package as not existing at the requested time" do+ expect(subject)+ .to be_json_eql(false.to_json)+ .at_path("_embedded/attributesByTimestamp/0/_meta/exists")+ end+ end+ end++ describe "_meta" do+ describe "exits" do+ it "is false because the work package does not exist at the requested timestamp" do+ expect(subject)+ .to be_json_eql(false.to_json)+ .at_path("_meta/exists")+ end+ end+ end+ endendendend