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

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.rb
index 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 < SimpleDelegator
timestamps.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 < SimpleDelegator
end
# rubocop:disable Metrics/AbcSize
+ # rubocop:disable Metrics/PerceivedComplexity
def 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 < SimpleDelegator
journables
end
# rubocop:enable Metrics/AbcSize
+ # rubocop:enable Metrics/PerceivedComplexity
def 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 < SimpleDelegator
def 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:)
+ end
end
end
@@ -213,6 +217,14 @@ class Journable::WithHistoricAttributes < SimpleDelegator
query.results.work_packages
end
+ def id
+ __getobj__.try(:id)
+ end
+
+ def attributes
+ __getobj__.try(:attributes)
+ end
+
def to_ary
__getobj__.send(:to_ary)
end
diff --git a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
index 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::EagerLoading
attr_accessor :timestamps, :query
def 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_attributes
work_package.attributes_by_timestamp = work_package_with_historic_attributes.attributes_by_timestamp
work_package.timestamps = work_package_with_historic_attributes.timestamps
diff --git a/spec/models/journable/with_historic_attributes_spec.rb b/spec/models/journable/with_historic_attributes_spec.rb
index 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 do
end
end
- 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" do
let(: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 do
expect(subject.exists_at_timestamps).to include Timestamp.parse("PT0S")
end
end
+
+ 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
+ end
end
describe ".wrap_multiple" do
@@ -260,6 +280,31 @@ describe Journable::WithHistoricAttributes do
expect(subject.first.historic?).to be true
end
end
+
+ 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
+ end
end
end
diff --git a/spec/requests/api/v3/work_packages/show_resource_spec.rb b/spec/requests/api/v3/work_packages/show_resource_spec.rb
index 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' do
let(: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) do
new_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_package
end
let(: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')
end
end
+
+ 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
+ end
end
end
end