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: adding missing specs for wrapping multiple work packages

Sebastian Fiedlschuster12a73fdc48a0
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 45e31ce232a..6a069e0f981 100644
--- a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
+++ b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
@@ -32,7 +32,6 @@ module API::V3::WorkPackages::EagerLoading
attr_accessor :timestamps, :query
def apply(work_package)
- # 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')
diff --git a/spec/models/journable/with_historic_attributes_spec.rb b/spec/models/journable/with_historic_attributes_spec.rb
index e0294deed9c..8879939bd47 100644
--- a/spec/models/journable/with_historic_attributes_spec.rb
+++ b/spec/models/journable/with_historic_attributes_spec.rb
@@ -306,6 +306,143 @@ describe Journable::WithHistoricAttributes do
end
end
end
+
+ context "with multiple work packages" do
+ let!(:work_package2) do
+ new_work_package = create(:work_package, subject: "Other work package", project: project1)
+ new_work_package.update_columns(created_at:)
+ new_work_package.journals.update_all(created_at:)
+ new_work_package
+ end
+ let(:work_packages) { [work_package, work_package2] }
+
+ it "returns an array of Journable::WithHistoricAttributes instances" do
+ expect(subject).to all be_a described_class
+ end
+
+ it "provides access to the work-package attributes" do
+ expect(subject.first.subject).to eq "The current work package"
+ expect(subject.second.subject).to eq "Other work package"
+ end
+
+ it "provides access to the work-package attributes at timestamps" do
+ expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package"
+ expect(subject.first.attributes_by_timestamp["PT0S"].subject).to eq "The current work package"
+ expect(subject.second.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "Other work package"
+ expect(subject.second.attributes_by_timestamp["PT0S"].subject).to eq "Other work package"
+ end
+
+ describe "when providing a query" do
+ let(:query) do
+ login_as(user1)
+ build(:query, user: nil, project: nil).tap do |query|
+ query.filters.clear
+ query.add_filter 'subject', '~', search_term
+ end
+ end
+ let(:search_term) { "original" }
+
+ it "determines for each timestamp whether the journables matches the query at that timestamp" do
+ expect(subject.first.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
+ expect(subject.first.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
+ expect(subject.second.matches_query_filters_at_timestamps).to be_empty
+ end
+ end
+
+ describe "when requesting only historic data" do
+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
+
+ it "provides access to the historic work-package attributes" do
+ expect(subject.first.subject).to eq "The original work package"
+ expect(subject.second.subject).to eq "Other work package"
+ end
+
+ it "provides access to the historic work-package attributes at timestamps" do
+ expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package"
+ expect(subject.second.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "Other work package"
+ end
+
+ it "determines whether the journable attributes are historic" do
+ expect(subject.first.historic?).to be true
+ expect(subject.second.historic?).to be true
+ end
+ end
+
+ describe "when both work packages did not exist at the only requested date" do
+ let(:timestamps) { [Timestamp.parse("2021-01-01T00:00:00Z")] }
+
+ specify "the given work packages do exist (at present time)" do
+ expect(work_package).to be_present
+ expect(work_package2).to be_present
+ expect(work_packages.count).to eq 2
+ end
+
+ it "has no attributes" do
+ expect(subject.first.attributes).to be_nil
+ expect(subject.second.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
+ expect(subject.second.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
+ expect(subject.second.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")
+ expect(subject.second.exists_at_timestamps).not_to include Timestamp.parse("2021-01-01T00:00:00Z")
+ end
+ end
+
+ describe "when only one work package did exist at the only requested date" do
+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
+ let!(:work_package2) do
+ new_work_package = create(:work_package, subject: "Other work package", project: project1)
+ new_work_package.update_columns(created_at: "2022-05-01")
+ new_work_package.journals.update_all(created_at: "2022-05-01")
+ new_work_package
+ end
+
+ specify "the given work packages do exist (at present time)" do
+ expect(work_package).to be_present
+ expect(work_package2).to be_present
+ expect(work_packages.count).to eq 2
+ end
+
+ specify "only one work package exists at the requested date" do
+ expect(work_package.at_timestamp(timestamps.first)).to be_present
+ expect(work_package2.at_timestamp(timestamps.first)).not_to be_present
+ end
+
+ it "provides two wrapper objects" do
+ expect(subject.count).to eq 2
+ end
+
+ it "marks only one work package as existing at the requested date" do
+ expect(subject.first.exists_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
+ expect(subject.second.exists_at_timestamps).not_to include Timestamp.parse("2022-01-01T00:00:00Z")
+ end
+
+ it "provides access to the work-package attributes at the requested date" do
+ expect(subject.first.subject).to eq "The original work package"
+ expect(subject.second).not_to respond_to :subject
+ end
+
+ it "provides access to the work-package attributes at timestamps" do
+ expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package"
+ expect(subject.second.attributes_by_timestamp["2022-01-01T00:00:00Z"]).to be_nil
+ end
+
+ it "determines whether the journable attributes are historic" do
+ expect(subject.first.historic?).to be true
+ expect(subject.second).not_to respond_to :historic?
+ end
+ end
+ end
end
describe "#baseline_timestamp" do
diff --git a/spec/requests/api/v3/work_packages/index_resource_spec.rb b/spec/requests/api/v3/work_packages/index_resource_spec.rb
index 762a34dfde2..e228cfedc02 100644
--- a/spec/requests/api/v3/work_packages/index_resource_spec.rb
+++ b/spec/requests/api/v3/work_packages/index_resource_spec.rb
@@ -675,6 +675,43 @@ describe 'API v3 Work package resource',
.not_to have_json_path('_embedded/elements/0/_embedded/attributesByTimestamp')
end
end
+
+ describe "for multiple work packages" do
+ let!(:work_package2) do
+ new_work_package = create(:work_package, subject: "Other work package", project:)
+ new_work_package.update_columns(created_at:)
+ new_work_package.journals.update_all(created_at:)
+ new_work_package
+ end
+
+ it "succeeds" do
+ expect(subject.status).to eq(200)
+ end
+
+ it "has the current attributes of both work packages" do
+ expect(subject.body)
+ .to be_json_eql(work_package.subject.to_json)
+ .at_path('_embedded/elements/0/subject')
+ expect(subject.body)
+ .to be_json_eql(work_package2.subject.to_json)
+ .at_path('_embedded/elements/1/subject')
+ end
+
+ it "embeds the attributesByTimestamp for both work packages" do
+ expect(subject.body)
+ .to have_json_path('_embedded/elements/0/_embedded/attributesByTimestamp')
+ expect(subject.body)
+ .to have_json_path('_embedded/elements/1/_embedded/attributesByTimestamp')
+ end
+
+ it "has the attributes that are different from the current attributes in the embedded objects" do
+ expect(subject.body)
+ .to be_json_eql("The original work package".to_json)
+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/subject')
+ expect(subject.body)
+ .not_to have_json_path('_embedded/elements/1/_embedded/attributesByTimestamp/0/subject')
+ end
+ end
end
end
end